
import JSONbig from 'json-bigint'
export default {

  toBigInt(id) {
    return JSONbig.parse(`{"v":${id}}`).v;
  },

  trim(str) {
    return str.replace(/^\s+|\s+$/gm, '');
  },


  formatDetailData({ data, rules }) {

    if (data.create_time) {
      data.create_time = new Date(data.create_time * 1000);
    }
    if (data.update_time) {
      data.update_time = new Date(data.update_time * 1000);
    }



    let feachRules = (rules) => {

      rules.forEach(rule => { // 循环规则
        if (rule.children && rule.children.length) {
          feachRules(rule.children);
        }
        if (!rule.dataRule) {
          return;
        }
        if (rule.field in data) { // 如果字段在数据中
          let value = data[rule.field]; // 获取值   
          if (rule.dataRule.type == "timestamp" && value) { // 如果是时间戳

            data[rule.field] = new Date(value * 1000);

          }
        }
      });
    };
    if (rules) {
      feachRules(rules);
    }





    return data;

  },

  //格式化提交的明细类型 字段
  //如果要提交的数据(value) 已经不存在  原有数据 (oldlistdata) ，那么需要把id放到 value.deleteList
  formatPostFieldValue({dataId,oldlistdata,value}){
    if(!dataId){
      return;
    }
    if(!oldlistdata){
      return;
    }
    if(!value){
      return;
    }
    if(!value.insertList){
      return;
    }
    oldlistdata.forEach(item=>{
      if(!item.id){
        return;
      }
      let exist1 = value.insertList.filter(a=>a.id && a.id.toString() == item.id.toString()).length;
      let exist2 = value.updateList.filter(a=>a.id && a.id.toString() == item.id.toString()).length;
      let exist3 = value.deleteList.filter(a=>a == item.id.toString()).length;
      if(!exist1 && !exist2 && !exist3){
        value.deleteList.push(item.id.toString());
      }  
    })

    return value;

  },

  getPostFieldValue({ dataId, list, type, deletedList, fieldName, rowFilter, dataRule }) {

    let d = {};
    console.log(list)
    if (rowFilter) {
      list = list.filter(rowFilter);
    }
    if (dataRule) {
      list.forEach(item => {


        dataRule.forEach(rule => {
          let value = item[rule.field];
          if (rule.type == "integer") {
            item[rule.field] = parseInt(value);
          }
          else if (rule.type == "number") {
            item[rule.field] = parseFloat(value);
          }
          else if (rule.type == "timestamp") {
            if(value){
              if(typeof(value) == 'string'){
                item[rule.field] = parseInt(new Date(value).getTime() / 1000);
              }
            
            }
         
          }
          else if (rule.type == "bigint") {
            if (typeof (item[rule.field]) == 'string') {
              item[rule.field] = JSONbig.parse(`{"v":${item[rule.field]}}`).v;
            }

          }
        })

      })

    }
    if (fieldName) {
      list.forEach(item => {
        item[fieldName] = dataId;
      })
    } 
    if (dataId && type !== 'array') {



      d = {
        insertList: [],
        updateList: [],
        deleteList: []
      };

      list.forEach(item => {
        if (item.id) {
          d.updateList.push(item);
        }
        else {
          d.insertList.push(item);
        }
      });

      deletedList.forEach(item => {
        d.deleteList.push(item);
      })


    } else {
      d = list;
    }
    return d;
  }
}