
const mapTemplateToTable = {
  '销售订单编号': 'orderCode',
  '导入业务经理名称': 'import_manager'
}

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

export const PROPERTYS = {
  ORDER_CODE: getTableProperty('销售订单编号'),
  IMPORT_MANAGER: getTableProperty('导入业务经理名称')
}

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

export const columns = [
  {
    type: 'selection',
    width: '70px'
  },
  {
    type: 'index',
    label: '序号',
    width: '70px'
  },
  {
    prop: PROPERTYS.ORDER_CODE,
    label: '销售订单编号',
    required: true
  },
  {
    prop: PROPERTYS.IMPORT_MANAGER,
    label: '导入业务经理名称',
    required: true
  },
  {
    prop: "managerid",
    label: '批量设置业务经理ID',
    required: true
  },
  {
    prop:"manager",
    label: '批量设置业务经理名称',
    required: true
  }
]

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 requsetFields = [
  PROPERTYS.ORDER_CODE,
  "managerid",
  "manager"
]
export const requiredFields = [
  PROPERTYS.ORDER_CODE,
  PROPERTYS.IMPORT_MANAGER
]

export const orderCodeFields = [
  PROPERTYS.ORDER_CODE
]

export const helper = {
  fields(sourceObject) {
    return Object.keys(sourceObject)
  },
  values(sourceObject, fields, index = 0) {
    console.log(sourceObject)

    return fields.map(field => {
      return sourceObject[field][index]
    })
  },
  write(sourceObject, fields) {
    return fields.reduce((prev, currentKey) => ({
      ...prev,
      [currentKey]: sourceObject[currentKey]
    }), {})
  }
}
