const mapTemplateToTable = {
  '发票申请编码': 'platform_id',
  '发票类型': 'supplierNo',
  '发票代码': 'companyNo',
  '发票号码': 'customerNo',
  '价税合计': 'cat_id1',
  '发票金额': 'cat_id2',
  '校验码': 'cat_id',
  '开票日期': 'good_unit'
}

export const getTableProperty = (key) => mapTemplateToTable[key]
export const PROPERTYS = {
  COMPANY_NO: getTableProperty('销售方公司编码'),
  CUSTOMER_NO: getTableProperty('购买方公司编码'),
  SUPPLIER_NO: getTableProperty('供应商公司编码'),
  PLATFORM: getTableProperty('所属平台ID'),
  CATEGORY: getTableProperty('三级品类ID'),
  GOOD_NAME: getTableProperty('商品名称'),
  GOOD_UNIT: getTableProperty('商品单位ID'),
  GOOD_NUM: getTableProperty('数量'),
  GOOD_PRICE: getTableProperty('销售单价'),
  TAX: getTableProperty('税率'),
  CGD_PRICE: getTableProperty('采购单价'),
  CGD_TAX: getTableProperty('采购毛利率'),
  SALE_TOTAL: getTableProperty('销售总额'),
  CONTACTOR: getTableProperty('收货人'),
  MOBILE: getTableProperty('联系电话'),
  ADDR: getTableProperty('联系地址'),
  SEND_TIME: getTableProperty('发货时间'),
  PO_CODE: getTableProperty('平台订单号'),
  CGD_TOTAL: getTableProperty('采购总额'),
  WORK_CODE: getTableProperty('单据号')
}

export const template = Array(1).fill(1).map(() => ({
  '所属平台ID': '58',
  '供应商公司编码': 'QS2206011407522117',
  '销售方公司编码': 'GS2302231323386950',
  '购买方公司编码': 'KH2203271822232121',
  '一级品类ID': '871',
  '二级品类ID': '871',
  '三级品类ID': '871',
  '商品单位ID': '7',
  '商品名称': '测试商品名称',
  '销售单价': '32',
  '数量': '100',
  '销售总额': '3200',
  '税率': '13',
  '采购单价': '30.00',
  '采购总额': '3000',
  '采购毛利率': '6',
  '平台订单号': 'PO206020230400240002',
  '收货人': '张三',
  '联系电话': '19955554444',
  '联系地址': 'xx市xx区xx街道',
  '发货时间': '2023-4-9 8:00:00'
}))

export const columns = [
  {
    type: 'index',
    label: '序号',
    width: '80px'
  },
  {
    prop: PROPERTYS.PLATFORM,
    label: '所属平台ID',
    width: '110px',
    required: true
  },
  {
    prop: PROPERTYS.SUPPLIER_NO,
    label: '供应商公司编码',
    width: '120px',
    required: true
  },
  {
    prop: PROPERTYS.COMPANY_NO,
    label: '销售方公司编码',
    width: '156px',
    required: true
  },
  {
    prop: PROPERTYS.CUSTOMER_NO,
    label: '购买方公司编码',
    width: '180px',
    required: true
  },

  {
    prop: getTableProperty('一级品类ID'),
    label: '一级品类ID',
    width: '150px',
    required: true
  },
  {
    prop: getTableProperty('二级品类ID'),
    label: '二级品类ID',
    width: '150px',
    required: true
  },
  {
    prop: getTableProperty('三级品类ID'),
    label: '三级品类ID',
    width: '150px',
    required: true
  },
  {
    prop: PROPERTYS.GOOD_UNIT,
    label: '商品单位ID',
    width: '150px',
    required: true
  },
  {
    prop: PROPERTYS.GOOD_NAME,
    label: '商品名称',
    width: '150px',
    required: true
  },
  {
    prop: PROPERTYS.GOOD_PRICE,
    label: '销售单价',
    'width': '150px',
    required: true
  },
  {
    prop: PROPERTYS.GOOD_NUM,
    label: '数量',
    width: '140px',
    required: true
  },
  {
    prop: PROPERTYS.SALE_TOTAL,
    label: '销售总额',
    'width': '150px'
  },
  {
    prop: PROPERTYS.TAX,
    label: '税率',
    width: '150px',
    required: true
  },
  {
    prop: PROPERTYS.CGD_PRICE,
    label: '采购单价',
    'width': '150px',
    required: true
  },
  {
    prop: PROPERTYS.CGD_TOTAL,
    label: '采购总额',
    'width': '150px',
    required: true
  },
  {
    prop: getTableProperty('采购毛利率'),
    label: '采购毛利率',
    'width': '150px'
  },
  {
    prop: PROPERTYS.PO_CODE,
    label: '平台订单号',
    'width': '150px',
    required: true
  },
  {
    prop: PROPERTYS.CONTACTOR,
    label: '收货人',
    'width': '150px',
    required: true
  },
  // {
  //   prop: PROPERTYS.WORK_CODE,
  //   label: '单据号',
  //   'width': '150px'
  // },
  {
    prop: PROPERTYS.MOBILE,
    label: '联系电话',
    'width': '150px',
    required: true
  },
  {
    prop: PROPERTYS.ADDR,
    label: '联系地址',
    'width': '150px',
    required: true
  },
  {
    prop: PROPERTYS.SEND_TIME,
    label: '发货时间',
    'width': '150px',
    required: true
  }
]

export function createErrorMessage(messages) {
  const rows = Object.keys(messages)
  return [rows[0]].reduce((prev, row, index) => {
    console.log(messages[row])
    const suffix = index === rows.length - 1 ? '</ul>' : ''
    const content = prev + `
      <li>
        <strong style="font-weight:700">第${row}行</strong>,${messages[row][0]}
      </li>
    `
    return content + suffix
  }, '<ul>')
}

export const createFieldVerification = (message) => ({
  notValidRows: [],
  isValid: true,
  message
})

export const requsetFields = [
  PROPERTYS.TAX,
  PROPERTYS.ADDR,
  PROPERTYS.MOBILE,
  PROPERTYS.PO_CODE,
  PROPERTYS.PLATFORM,
  PROPERTYS.CATEGORY,
  PROPERTYS.GOOD_NAME,
  PROPERTYS.GOOD_UNIT,
  PROPERTYS.GOOD_NUM,
  PROPERTYS.CGD_PRICE,
  PROPERTYS.CONTACTOR,
  PROPERTYS.SEND_TIME,
  PROPERTYS.WORK_CODE,
  PROPERTYS.GOOD_PRICE,
  PROPERTYS.COMPANY_NO,
  PROPERTYS.SUPPLIER_NO,
  PROPERTYS.CUSTOMER_NO,
  PROPERTYS.CGD_TAX,
  PROPERTYS.SALE_TOTAL,
  PROPERTYS.CGD_TOTAL
]

// 税率、数量、销售单价、采购单价
export const requiredFields = [
  PROPERTYS.TAX,
  PROPERTYS.ADDR,
  PROPERTYS.MOBILE,
  PROPERTYS.PO_CODE,
  PROPERTYS.PLATFORM,
  PROPERTYS.CATEGORY,
  PROPERTYS.GOOD_NAME,
  PROPERTYS.GOOD_UNIT,
  PROPERTYS.GOOD_NUM,
  PROPERTYS.CONTACTOR,
  PROPERTYS.SEND_TIME,
  PROPERTYS.COMPANY_NO,
  PROPERTYS.SUPPLIER_NO,
  PROPERTYS.CUSTOMER_NO,
  PROPERTYS.CGD_TOTAL,
  PROPERTYS.SALE_TOTAL,
  PROPERTYS.CGD_TAX
]

export const numberFields = [
  PROPERTYS.CGD_TOTAL,
  PROPERTYS.SALE_TOTAL,
  PROPERTYS.GOOD_PRICE,
  PROPERTYS.CGD_PRICE,
  PROPERTYS.CGD_TAX
]

export const intNumberFields = [
  PROPERTYS.CATEGORY,
  PROPERTYS.GOOD_UNIT,
  PROPERTYS.GOOD_NUM,
  PROPERTYS.TAX
]

export const helper = {
  fields: function(sourceObject) {
    return Object.keys(sourceObject)
  },
  values: function(sourceObject, fields, index = 0) {
    return fields.map(field => {
      return sourceObject[field][index]
    })
  },
  write: function(sourceObject, fields) {
    return fields.reduce((prev, currentKey) => ({
      ...prev,
      [currentKey]: sourceObject[currentKey]
    }), {})
  }
}
