/**
 * 全局formatter
 */
import { VXETable } from 'vxe-table'
import XEUtils from 'xe-utils'

/**
 * 字典转换
 * formatter: ['dictFormat',params]
 * 参数
 * flag: 是否触发更新
 */
VXETable.formats.add('dictFormat',({cellValue, column},dictCode,startWith, flag = false) => {
  const dict=window.localStorage.getItem(dictCode);
  if(dictCode='CLlQ'){
  }

  if (!dict){
    return cellValue;
  }
  let dictItems=JSON.parse(dict)
  if (!dictItems){
    console.log(dictCode+"配置错误")
    return;
  }
  if (startWith){
    dictItems=dictItems.filter(dictItem=>{
      return dictItem.bm.startsWith(startWith)
    });
  }

  for (const item of dictItems) {

    if (item.bm===cellValue){4
      return item.bmsm
    }
  }
  return cellValue;
})

/**
 * 部门码名转换
 */
VXETable.formats.add('deptFormat',({cellValue}, flag = false) => {
  if(cellValue==''){
    return cellValue;
  }
  let deptName=window.localStorage.getItem(cellValue)
  return deptName&&deptName!=='undefined'?deptName:cellValue;
})

/**
 * 需求部门码名转换
 */
VXETable.formats.add('ddeptFormat',({cellValue}, flag = false) => {
  let deptName=window.localStorage.getItem("d_"+cellValue)
  return deptName?deptName:cellValue;
})

/**
 * 检验项目
 */
VXETable.formats.add('byxmFormat',({cellValue}, flag = false) => {
  //console.log(cellValue)
  let deptName=window.localStorage.getItem("byxm_"+cellValue)
  return deptName?deptName:cellValue;
})
VXETable.formats.add('byddFormat',({cellValue}, flag = false) => {
  //console.log(cellValue)
  let deptName=window.localStorage.getItem("bydd_"+cellValue)
  return deptName?deptName:cellValue;
})
/**
 * 仓库码名转换
 */
VXETable.formats.add('institutionFormat',({cellValue, column},dictCode, flag = false) => {
  const dict=window.localStorage.getItem(dictCode);
  if (!dict&&dict!=='undefined'){
    return cellValue;
  }
  let dictItems=JSON.parse(dict)
  if (!dictItems){
    console.log(dictCode+"配置错误")
    return;
  }

  for (const item of dictItems) {
    if (item.institutionCode===cellValue){
      return item.institutionName
    }
  }
  return cellValue;
})
/**
 * 供货商码名转换
 */
VXETable.formats.add('ghsFormat',({cellValue}, flag = false) => {
  let deptName=window.localStorage.getItem("ghs_"+cellValue)
  return deptName?deptName:cellValue;
})

/**
 * 数字转换 ,小数点位数转换
 * 默认转换为int
 * 浮点数保留暂无需求
 * percent 4舍5入 百分比
 */
VXETable.formats.add('numberFormat',({cellValue, column},type='int',digit) => {
  switch (type){
    case "int":
      if (cellValue&&!isNaN(cellValue)){
        return parseInt(cellValue);
      }else {
        return 0;
      }
      break;
    case 'float':
      if (cellValue){
        return parseFloat(cellValue).toFixed(digit);
      }else {
        return 0;
      }

    case 'percent':
      return  XEUtils.toFixed(XEUtils.multiply(cellValue, 100),digit)+'%'

  }
  return cellValue;
})

//测试
//formatter: ['testFormat',this.$refs.myTable,params]
/**
 * 参数
 * table: this.$refs.myTable 无法获取
 */
VXETable.formats.add('testFormat',({cellValue,column},val)=>{
  //获取table自定义参数
  //console.log(table)//无效
  console.log('========================')
  console.log(column.params)//可以获取参数
  console.log(cellValue)//通过这个值转义
})

VXETable.formats.add('dateToStr',({cellValue},val='yyyy/MM/dd')=>{
  return XEUtils.toDateString(cellValue, val)
})
