import { getGoodsUrl } from '../api'

export const goodsModalConfig = (self) => {
  return {
    toolBar: {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      items: [
        {
          type: 'input',
          name: 'code',
          label: '商品编码',
          value: '',
        },
        {
          type: 'input',
          name: 'name',
          label: '商品名称',
          value: '',
        },
      ],
    },
    remoteUrl: {
      url: getGoodsUrl,
      method: 'post',
    },
    rowKey: 'itemId',
    title: '商品信息',
    remoteBefore(vnode, config) {
      config.data.cargoOwnerId = self.cargoOwnerId
    },
    tableConfig: {
      dragle: true,
      id: 'goods-columns-model',
      scroll: { x: true },
      rowSelectType: 2,
      getCheckboxProps(record) {
        return {
          props: {
            disabled: self.disableItemId.indexOf(record.itemCode + record.itemName) > -1 ? true : false,
          },
        }
      },
      columns: [
        { dataIndex: 'itemId', title: '商品ID', hidden: true },
        { dataIndex: 'itemCode', title: '商品编码' },
        { dataIndex: 'itemName', title: '商品名称' },
        { dataIndex: 'itemCategoryName', title: '商品品类' },
        { dataIndex: 'packTypeName', title: '包装类型' },
        { dataIndex: 'cargoThermosphereName', title: '货物温层' },
        { dataIndex: 'branchName', title: '默认存放仓库' },
        { dataIndex: 'weight', title: '重量(公斤)' },
        { dataIndex: 'volume', title: '体积(立方)' },
        { dataIndex: 'memo', title: '备注' },
      ],
    },
  }
}

export const setGoogColumns = (self, expData = [], st) => {
  let extendedField = [];
  if (expData.length > 0) {
    extendedField = expData.map(item => {
      return { dataIndex: item.name, title: item.label, type: st === 1 ? 'txt' : item.type === 'input' ? 'edit' : item.type, options: item.type === 'select' ? item.options : null, width: 120 }
    })
  }
  console.log(st, extendedField);

  return [
    { dataIndex: 'itemId', title: 'ID', hidden: true, type: 'txt', width: 200 },
    {
      dataIndex: 'itemCode', title: '商品编码', type: 'txt',
      sorter: (a, b) => {
        return a.itemCode != b.itemCode
      }
    },
    {
      dataIndex: 'itemName', title: '商品名称', type: 'txt',
      sorter: (a, b) => {
        return a.itemName != b.itemName
      },
    },
    {
      dataIndex: 'itemCategoryName', title: '商品品类', type: 'txt',
      sorter: (a, b) => {
        return ~~a.itemCategory - ~~b.itemCategory
      },
    },
    {
      dataIndex: 'packType',
      title: '包装类型Code',
      type: 'txt',
      hidden: true
    },
    {
      dataIndex: 'packTypeName',
      title: '包装类型',
      type: 'select',
      autoLoad: false,
      on: {
        change(data) {
          const { value, record, label } = data

          record.packType = value
          record.packTypeName = label
        }
      },
      get options() {
        return self.options.PACK_TYPE
      },
      sorter: (a, b) => {
        return ~~a.packType - ~~b.packType
      },
    },

    {
      dataIndex: 'cargoThermosphere',
      title: '货物温层Code',
      type: 'txt',
      hidden: true
    },
    {
      dataIndex: 'cargoThermosphereName',
      title: '货物温层',
      type: 'select',
      autoLoad: false,
      on: {
        change(data) {
          const { value, record, label } = data
          record.cargoThermosphere = value
          record.cargoThermosphereName = label
        }
      },
      get options() {
        return self.options.CARGO_THERMOSPHERE
      },
      sorter: (a, b) => {
        return ~~a.cargoThermosphere - ~~b.cargoThermosphere
      },
    },
    {
      dataIndex: 'pcs',
      title: self.subTitle+'件数',
      type: 'number',
      on: {
        change: self.handleChange,
      },
      sorter: (a, b) => {
        return ~~a.pcs - ~~b.pcs
      },
    },
    {
      dataIndex: 'weight',
      title: self.subTitle+'重量(公斤)',
      type: 'number',
      on: {
        change: self.handleChange,
      },
      sorter: (a, b) => {
        return ~~a.weight - ~~b.weight
      },
    },
    {
      dataIndex: 'volume',
      title:self.subTitle+ '体积(立方)',
      type: 'number',
      on: {
        change: self.handleChange,
      },
      sorter: (a, b) => {
        return ~~a.volume - ~~b.volume
      },
    },
    { dataIndex: 'memo', title: '备注', type: 'txt' },
    {
      dataIndex: 'branchName',
      title: '默认存放仓库',
      width: 120,
      type: 'txt',
      sorter: (a, b) => {
        return a.branchName != b.branchName
      },
    },
    {
      dataIndex: 'inventoryPcs',
      title: '库存数量',
      width: 120,
      type: 'txt',
      sorter: (a, b) => {
        return ~~a.inventoryPcs - ~~b.inventoryPcs
      },
    },
    {
      dataIndex: 'stagingLocation',
      title: '暂存位',
      type: 'txt',
      width: 120,
      // sorter: (a, b) => {
      //   return a.memo.length - b.memo.length
      // },
    },
    ...extendedField
  ]
}
