import {genCustomerSearchModal, genItemRelatedSearchModal} from "../../../SearchModal";
import {upcloud_base_common} from "@/config/serviceSettings";
import {getConsignorItems, getBranchItems, getLOTItems} from './formItems'

const grid = {
  labelCol: {span: 8}, wrapperCol: {span: 16}, span: 6,
}

// 主界面查询表单
export function formItems(self) {
  return [
    // 仓库
    ...getBranchItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({branchcode: companyCode, branchname: companyName, branchid: companyId})
    }),
    // 货主
    ...getConsignorItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({consignorcode: companyCode, consignorname: companyName, consignorid: companyId})
    }),
    // 客户
    ...genCustomerSearchModal(1, false, false, function (vnode, p) {
      let vals = self.$refs.formTarget.form.getFieldsValue();
      p.data.cargoOwnerId = vals.cargoOwnerId;
      return p;
    }, null, grid),
    {
      type: 'select', destroyOptions: true, name: 'department', label: '部门', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=department', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      done(data) {
        const {dictCode = ''} = data[0] || {}
        self.$refs.formTarget.form.setFieldsValue({department: dictCode})
      },
      ...grid
    },
    {
      type: 'select', destroyOptions: true, name: 'billtype', label: '单据类型', value: undefined, autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=BILL_TYPE_VALUE', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {
      type: 'select', destroyOptions: true, name: 'status', label: '单据状态', value: undefined, mode: 'multiple', maxTagCount: 1, autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=do_h_status', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },

    {
      type: 'select', destroyOptions: true, name: 'isEmergency', label: '紧急程度', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_PRIORITY_LEVEL', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {type: 'input', name: 'billno', label: '销售订单编号', value: '', ...grid},
    {
      type: 'select', name: 'shipTerm', label: '运输条件', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_SHIP_TERM', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {
      type: 'select', name: 'arrivalMode', label: '送货方式', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_ARRIVAL_MODE', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      done(data) {
        self.$refs.formTarget.form.setFieldsValue({arrivalMode: '2'})
      },
      ...grid
    },
    {type: 'input', name: 'custPerson', label: '收货人', value: '', ...grid},
    {type: 'input', name: 'custPersonTel', label: '收货人联系电话', value: '', ...grid},
    {type: 'input', name: 'createby', label: '录入员', value: '', ...grid},
    {type: 'input', name: 'salespersonName', label: '销售员', value: '', ...grid},
    {hidden: true, type: 'input', name: 'billDateStart', label: '开始日期', value: '', ...grid},
    {hidden: true, type: 'input', name: 'billDateEnd', label: '结束日期', value: '', ...grid},
    {
      type: 'a-range-picker',
      name: "adata",
      label: "销售订单日期",
      value: [],
      dropdownClassName: 'dropdownClassName1',
      ...grid,
      placeholder: ['起始销售订单日期', '结束销售订单日期'],
      format: "YYYY-MM-DD",
      on: {
        change(date, dateString) {
          self.$refs.formTarget.form.setFieldsValue({
            billDateStart: dateString[0],
            billDateEnd: dateString[1]
          })
        },
      }
    },
    {hidden: true, type: 'input', name: 'attainDateStart', label: '预定到货日期 开始', value: '', ...grid},
    {hidden: true, type: 'input', name: 'attainDateEnd', label: '预定到货日期 结束', value: '', ...grid},
    {
      type: 'a-range-picker',
      name: "bdata",
      label: "预计到货日期",
      placeholder: ['起始预计到货日期', '结束预计到货日期'],
      value: [],
      // showTime: true,
      ...grid,
      format: "YYYY-MM-DD",
      on: {
        change(date, dateString) {
          self.$refs.formTarget.form.setFieldsValue({
            attainDateStart: dateString[0],
            attainDateEnd: dateString[1]
          })
        },
      }
    },
    {
      type: 'buttonGroup',
      ...grid,
      span: 24,
      style: {
        textAlign: 'right'
      },
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-right:10px',
          htmlType: 'submit'
        },
        {
          value: '重置',
          icon: 'redo',
          on: {
            click: () => {
              self.$refs.formTarget.form.resetFields()
            }
          }
        }
      ]
    }
  ]
}

// 主界面汇总列表
export const masterColumns = [
  {title: '销售订单号', dataIndex: 'billno', width: 120},
  {title: '销售订单日期', dataIndex: 'billDate', width: 120},
  {title: '单据类型', dataIndex: 'billtypeName', width: 120},
  {title: '单据状态', dataIndex: 'statusName', width: 120},
  {title: '部门', dataIndex: 'departmentName', width: 120},
  {title: '发货仓库', dataIndex: 'branchname', width: 120},
  {title: '预计到货时间', dataIndex: 'attainDate', width: 120},
  {title: '货主名称', dataIndex: 'consignorname', width: 120},
  {title: '客户名称', dataIndex: 'shippername', width: 120},
  {title: '送货方式', dataIndex: 'arrivalModeName', width: 120},
  {title: '紧急程度', dataIndex: 'isEmergencyName', width: 120},
  {title: '收货人', dataIndex: 'custPerson', width: 120},
  {title: '收货人联系电话', dataIndex: 'custPersonTel', width: 120},
  {title: '总数量', dataIndex: 'totQty', width: 120},
  {title: '运输条件', dataIndex: 'shipTermName', width: 120},
  {title: '录入员', dataIndex: 'createby', width: 120},
  {
    dataIndex: 'operation',
    key: 'operation',
    title: '操作',
    width: 420,
    align: 'center',
    fixed: 'right',
    scopedSlots: {customRender: 'action'}
  }
]
// 主界面明细列表
export const detailsColumns = [
  {title: '收货仓库', dataIndex: 'branchname', width: 120, fixed: 'left'},
  {title: '商品编码', dataIndex: 'itemcode', width: 120, fixed: 'left'},
  {title: '商品名称', dataIndex: 'itemname', width: 120, fixed: 'left'},
  {title: '商品规格', dataIndex: 'itemSpecs', width: 120, fixed: 'left'},
  {title: '生产厂家', dataIndex: 'manufacturer', width: 120},
  {title: '包装数量', dataIndex: 'packTypeNum', width: 120},
  {title: '包装单位', dataIndex: 'packUnits', width: 120},
  {title: '件数', dataIndex: 'itemPcs', width: 120},
  {title: '零散件', dataIndex: 'itemOdd', width: 120},
  {title: '数量', dataIndex: 'itemQty', width: 120},
  {title: '加工类型', dataIndex: 'machType', width: 120},
  {title: '行备注', dataIndex: 'remark', width: 120},
  {title: '最后修改日期', dataIndex: 'lastmodifydate', width: 120},
  {title: '批号', dataIndex: 'lotNo', width: 120},
  {title: '库存状态', dataIndex: 'stockStatus', width: 120},
  {title: '生产日期', dataIndex: 'pdateFrom', width: 120},
  {title: '有效期至', dataIndex: 'validUntil', width: 120}
]

// 编辑页汇总表单
export function editForm(self) {
  return [
    // 仓库
    ...getBranchItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({branchcode: companyCode, branchname: companyName, branchid: companyId})
      self.$refs.table.reset()
      self.tableData = []
    }),
    // 货主
    ...getConsignorItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({consignorcode: companyCode, consignorname: companyName, consignorid: companyId})
      self.$refs.table.reset()
      self.tableData = []
    }),
    // 客户
    ...genCustomerSearchModal(1, true, false, function (vnode, p) {
      let vals = self.$refs.formTarget.form.getFieldsValue();
      p.data.cargoOwnerId = vals.cargoOwnerId;
      return p;
    }, null, grid),
    {
      type: 'select', destroyOptions: true, name: 'department', label: '部门', value: '', required: true, autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=department', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      done(data) {
        const {dictCode = ''} = data[0] || {}
        self.$refs.formTarget.form.setFieldsValue({department: dictCode})
      },
      ...grid
    },
    {
      type: 'select', destroyOptions: true, name: 'billtype', label: '单据类型', value: undefined, required: true, autoLoad: true, maxTagCount: 1,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=BILL_TYPE_VALUE', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {
      type: 'select', name: 'isEmergency', label: '紧急程度', value: '', autoLoad: true, required: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_PRIORITY_LEVEL', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {
      type: 'select', name: 'shipTerm', label: '运输条件', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_SHIP_TERM', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {
      type: 'select', name: 'arrivalMode', label: '送货方式', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=ASN_ARRIVAL_MODE', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      done(data) {
        self.$refs.formTarget.form.setFieldsValue({arrivalMode: '2'})
      },
      ...grid
    },
    {type: 'input', name: 'custPerson', label: '收货人', value: '', ...grid},
    {type: 'input', name: 'custPersonTel', label: '收货人联系电话', value: '', ...grid},
    {type: 'input', name: 'salespersonName', label: '销售员', value: '', ...grid},
    {
      type: 'date',
      name: "billDate",
      label: "销售订单日期",
      required: true,
      showToday: true,
      value: [],
      // showTime: true,
      ...grid,
      format: "YYYY-MM-DD",
      on: {}
    },
    {
      type: 'date',
      name: "attainDate",
      label: "预计到货日期",
      required: true,
      value: [],
      // showTime: true,
      ...grid,
      format: "YYYY-MM-DD",
      on: {}
    },
    {type: 'input', name: 'remark', label: '备注', value: '', ...grid},
  ]
}

// 编辑页明细列-编辑表格
export const columns = function (self) {
  return [
    {hidden: true, title: 'lineid', dataIndex: 'lineid', width: 120},
    {type: 'txt', title: '货主名称', dataIndex: 'consignorname', width: 150, fixed: 'left',},
    {type: 'txt', title: '商品编码', dataIndex: 'itemcode', width: 120, fixed: 'left'},
    {type: 'txt', title: '商品id', dataIndex: 'itemid', width: 120, hidden: true,},
    {
      ...genItemRelatedSearchModal(2, true, false, function (vnode, p) {
        let vals = self.$refs.formTarget.form.getFieldsValue();
        p.data.cargoOwnerId = vals.consignorid;
        p.data.warehouseId = vals.branchid
        return p;
      }, function (rowData, index, value) {
        let itemData = self.tableData[index]
        itemData.itemcode = rowData[0].itemCode
        itemData.itemid = rowData[0].itemId
        itemData.itemname = rowData[0].itemName
        itemData.itemSpecs = rowData[0].specification
        itemData.manufacturer = rowData[0].manufacturerName
        itemData.packTypeNum = rowData[0].caseConversionRate
        itemData.packUnits = rowData[0].caseUom
      }, null, {
        search(p1, p2, p3, index, record) {
          console.log(record, 'record');
        },
      }),
      width: 150, fixed: 'left',
    },
    {type: 'txt', title: '商品规格', dataIndex: 'itemSpecs', fixed: 'left', width: 120},
    {type: 'txt', title: '生产厂家', dataIndex: 'manufacturer', width: 120},
    {type: 'txt', title: '包装数量', dataIndex: 'packTypeNum', width: 120},
    {type: 'txt', title: '包装单位', dataIndex: 'packUnits', width: 120},
    {
      type: 'number', title: '件数', dataIndex: 'itemPcs', width: 120, min: 0,
      on: {
        change({record, value}) {
          //数量 = 件数*包装数 + 零散数
          record.itemQty = value * record.packTypeNum + record.itemOdd
        }
      }
    },
    {type: 'txt', title: '零散数', dataIndex: 'itemOdd', width: 120, min: 0, hidden: true},
    {type: 'txt', title: '数量', dataIndex: 'itemQty', width: 120,},
    {type: 'edit', title: '含税单价', dataIndex: 'price', width: 120},
    // { type: 'txt', title: '总金额', dataIndex: 'amt', width: 120 },genManufacturerLotNumSearchModal
    ...getLOTItems(
      (rowData) => {
        const {modalRowData, index} = rowData
        let itemData = self.tableData[index]
        itemData.stockStatus = modalRowData[0].status
        itemData.pdateFrom = modalRowData[0].batchProductionDate
        itemData.validUntil = modalRowData[0].batchExpiredDate
        itemData.locatorId = modalRowData[0].locatorId
        itemData.containerCode = modalRowData[0].containerCode
        self.tableData = [...self.tableData]
      },
      (res, params, ...args) => {
        const values = self.$refs.formTarget.form.getFieldsValue()
        const record = self.$refs.table.getCurrentRow().obj;
        const {itemid} = record
        params.data.warehouseId = values.branchid;
        params.data.cargoOwnerId = values.consignorid;
        params.data.billtype = values.billtype;
        params.data.itemId = itemid;
        params.data.enterpriseId = self.$store.state.user.info.enterpriseId;

        // params.params.itemId = values.consignorid;
        return params
      },
      () => {
        const record = self.$refs.table.getCurrentRow().obj;
        if (record.itemid) {
          return true
        } else {
          self.$message.warn('请先选择商品')
        }
      }
    ),
    {
      type: 'txt', title: '库存状态', dataIndex: 'stockStatus', width: 120, customRender(text) {
        text = String(text)
        if (text === '1') return '合格'
        if (text === '2') return '不合格'
        if (text === '3') return '待检'
        if (text === '4') return '退货'
        return text
      }
    },
    {type: 'txt', title: '生产日期', dataIndex: 'pdateFrom', width: 120},
    {type: 'txt', title: '有效期至', dataIndex: 'validUntil', width: 120},
    {type: 'edit', title: '行备注', dataIndex: 'lineRemark', width: 120},
    {
      dataIndex: 'operation',
      key: 'operation',
      title: '操作',
      width: 120,
      align: 'center',
      fixed: 'right',
      style: {zIndex: 2},
      scopedSlots: {customRender: 'action'}
    }
  ]
}