const mapFields = {
  primaryClassification:'primaryClassification', // 一级分类
  secondaryClassification:'secondaryClassification',  // 二级分类
  threeLevelClassification:'threeLevelClassification', // 三级分类
  price:'price', // 单价
  unit:'unit', // 单位
  nonTaxQuotes:'nonTaxQuotes', // 非税报价
  taxRate:'taxRate', // 税率
  taxIncludedQuote:'taxIncludedQuote', //含税报价
  remark:'remark' //备注
}

export const getTableProperty = (key) => mapTemplateToTable[key]

const mapTemplateToTable = {
  '一级分类': mapFields.primaryClassification,
  '二级分类': mapFields.secondaryClassification,
  '三级分类': mapFields.threeLevelClassification,
  '单价': mapFields.price,
  '单位': mapFields.unit,
  '非税报价': mapFields.nonTaxQuotes,
  '税率': mapFields.taxRate,
  '含税报价': mapFields.taxIncludedQuote,
  '备注':mapFields.remark
}

export const columns = [
  {
      type: 'index',
      label: '序号',
      width: '70px'
  },
  {
      prop: mapFields.primaryClassification,
      label: '一级分类',
      width: '110px',
      required: true
  },
  {
      prop: mapFields.secondaryClassification,
      label: '二级分类',
      width: '120px',
      required: true
  },
  {
      prop: mapFields.threeLevelClassification,
      label: '三级分类',
      width: '156px',
      required: true
  },
  {
      prop: mapFields.price,
      label: '单价',
      width: '180px',
      required: true
  },
  {
      prop: mapFields.nonTaxQuotes,
      label: '非税报价',
      width: '156px',
      required: true
  },
  {
      prop: mapFields.taxRate,
      label: '税率',
      width: '156px',
      required: false
  },
  {
      prop: mapFields.taxIncludedQuote,
      label: '含税报价',
      width: '150px',
      required: false
  },
  {
      prop: mapFields.remark,
      label: '备注',
      minWidth: '150px',
      required: false
  },
  {
    prop: "",
    label: '操作',
    minWidth: '80px',
    required: false
},
]

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

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

export const template = Array(1).fill(1).map(() => Object.keys(mapTemplateToTable).reduce((prev, property) => {
  prev[property] = ''
  return prev
}, {}))


export const requiredFields = []


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