import store from '@/store'
import { genWarehouseAuthSearchModal, genBldgSearchModal, genContainerSearchModal } from '@/views/kzyWMS/SearchModal'

// 任务确认右侧搜索
const items = function(_this) {
  return ([
    // 容器
    ...genContainerSearchModal(1, false, false, null,
      function(rowData, form) {
        if (!rowData) {
          form.setFieldsValue({
            containerId: '',
            containerCode: ''
          })
        } else {
          form.setFieldsValue({
            containerId: rowData[0].containerId,
            containerCode: rowData[0].containerCode
            // pickOrderId: _this.pickOrderId,
          })
        }
      },
      { span: 8, labelCol: { span: 5 } }
    ),
    { type: 'input', name: 'itemCode', label: '商品编号', value: '', span: 8, labelCol: { span: 5 } },
    {
      type: 'buttonGroup',
      style: { textAlign: 'right' },
      span: 8,
      labelCol: { span: 5 },
      options: [
        { value: '查询', icon: 'search', btnType: 'primary', style: 'margin-right:16px', htmlType: 'submit' },
        {
          value: '重置', name: 'ccz', icon: 'redo',
          on: {
            click(event, form) {
              form.resetFields()
            }
          }
        }
      ]
    }

  ])
}

// 待索取tab0
const items0 = function(_this) {
  let warehouseId = store.state.user.warehouse.warehouseId
  return (
    [
      // 仓库
      ...genWarehouseAuthSearchModal(1, true, false, null,
        function(rowData, form) {
          if (!rowData) {
            // form.setFieldsValue({
            // 	warehouseId: '',
            // 	warehouseCode:'',
            // 	warehouseName:''
            // })
          } else {
            warehouseId = rowData[0].companyId
            // form.setFieldsValue({
            // 	warehouseId: rowData[0].warehouseId,
            // 	warehouseCode: rowData[0].warehouseCode,
            // 	warehouseName: rowData[0].warehouseName,
            // })
          }
          _this.$emit('emitWarehouseId', warehouseId, '1')
        },
        { span: 8, labelCol: { span: 5 } }
      ),
      // 建筑物
      ...genBldgSearchModal(1, true, false,
        function(vn, p) {
          p.params.warehouseId = warehouseId
          return p
        },
        function(rowData, form) {
          if (!rowData) {
            // form.setFieldsValue({
            // 	warehouseId: '',
            // 	warehouseCode:'',
            // 	warehouseName:''
            // })
          } else {
            _this.$emit('update:bldgInfo', rowData[0])
          }
        }, { span: 8, labelCol: { span: 5 } }
      ),
      { name: 'pickTaskType', label: '任务类型（0-整件/2-整散合一）', value: '3', hidden: true },
      { type: 'input', name: 'warehouseAreaCode', label: '库区编码', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'itemCode', label: '商品编号', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'srcCode', label: '订单编号', value: '', span: 8, labelCol: { span: 5 } },
      {
        type: 'buttonGroup',
        style: { textAlign: 'center' },
        span: 8,
        labelCol: { span: 5 },
        options: [
          { value: '查询', icon: 'search', btnType: 'primary', style: 'margin-right:16px', htmlType: 'submit' },
          {
            value: '重置', name: 'ccz', icon: 'redo', style: 'margin-right:16px',
            on: {
              click(event, form) {
                form.resetFields()
              }
            }
          }
        ]
      }
    ]
  )
}

// 任务索取tab1
const items1 = function(_this) {
  return (
    [
      // 仓库
      ...genWarehouseAuthSearchModal(1, true, false, null,
        function(rowData, form) {
          if (!rowData) {
            // form.setFieldsValue({
            // 	warehouseId: '',
            // 	warehouseCode:'',
            // 	warehouseName:''
            // })
          } else {
            // form.setFieldsValue({
            // 	warehouseId: rowData[0].warehouseId,
            // 	warehouseCode: rowData[0].warehouseCode,
            // 	warehouseName: rowData[0].warehouseName,
            // })
          }
        },
        { span: 8, labelCol: { span: 5 } }
      ),

      { name: 'type', label: '0：任务索取 1：任务确认', value: '0', hidden: true },
      { type: 'input', name: 'pickOrderId', label: '拣选单', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'operatorCode', label: '索取人工号', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'itemCode', label: '商品编号', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'barcode', label: '条码', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'srcCode', label: '订单编号', value: '', span: 8, labelCol: { span: 5 } },
      {
        type: 'buttonGroup',
        style: { textAlign: 'right' },
        span: 23,
        labelCol: { span: 2 },
        options: [
          { value: '查询', icon: 'search', btnType: 'primary', style: 'margin-right:16px', htmlType: 'submit' },
          {
            value: '重置', name: 'ccz', icon: 'redo', style: 'margin-right:16px',
            on: {
              click(event, form) {
                form.resetFields()
              }
            }
          },
          {
            value: '打印拣选标签', name: 'ccz', icon: 'printer',
            on: {
              click: _this.printer
            }
          }
        ]
      }

    ]
  )

}

// 任务确认tab2
const items21 = function(_this) {
  let warehouseId = store.state.user.warehouse.warehouseId
  return (
    [
      // 仓库
      ...genWarehouseAuthSearchModal(1, true, false, null,
        function(rowData, form) {
          if (!rowData) {
            // form.setFieldsValue({
            // 	warehouseId: '',
            // 	warehouseCode:'',
            // 	warehouseName:''
            // })
          } else {
            warehouseId = rowData[0].companyId
            // form.setFieldsValue({
            // 	warehouseId: rowData[0].warehouseId,
            // 	warehouseCode: rowData[0].warehouseCode,
            // 	warehouseName: rowData[0].warehouseName,
            // })
          }
          _this.$emit('emitWarehouseId', warehouseId, '3')
        },
        { span: 8, labelCol: { span: 5 } }
      ),
      // 建筑物
      ...genBldgSearchModal(1, true, false,
        function(vn, p) {
          p.params.warehouseId = warehouseId
          return p
        },
        function(rowData, form) {
          if (!rowData) {
            // form.setFieldsValue({
            // 	warehouseId: '',
            // 	warehouseCode:'',
            // 	warehouseName:''
            // })
          } else {
            _this.$emit('update:bldgInfo', rowData[0])
          }
        }, { span: 8, labelCol: { span: 5 } }
      ),
      { name: 'pickTaskType', label: '任务类型（0-整件/2-整散合一）', value: '3', hidden: true },
      { name: 'type', label: '0：任务索取 1：任务确认', value: '1', hidden: true },
      // { type: 'input', name: 'operatorCode', label: '工号', value: '', disabled: true, span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'pickOrderId', label: '拣选单', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'itemCode', label: '商品编号', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'barcode', label: '条码', value: '', span: 8, labelCol: { span: 5 } },
      { type: 'input', name: 'srcCode', label: '订单编号', value: '', span: 8, labelCol: { span: 5 } },
      {
        type: 'buttonGroup',
        style: { textAlign: 'right' },
        span: 16,
        labelCol: { span: 2 },
        options: [
          { value: '查询', icon: 'search', btnType: 'primary', style: 'margin-right:16px', htmlType: 'submit' },
          {
            value: '重置', name: 'ccz', icon: 'redo', style: 'margin-right:16px',
            on: {
              click(event, form) {
                form.resetFields()
              }
            }
          },
          {
            value: '确认', btnType: 'primary', icon: 'check', style: 'margin-right:16px',
            on: {
              click(event, form) {
                _this.confirmSave()
              }
            }
          },
          {
            value: '解锁数量', btnType: 'primary', icon: 'unlock',
            on: {
              click(event, form) {
                _this.unlock()
              }
            }
          }
        ]
      }

    ]
  )

}

// 功能库房拣货tab0（ 待索取 ）
const columns = [
  // { dataIndex: 'pickOrderId', title: '拣选单', },
  { dataIndex: 'srcCode', title: '订单编号', ellipsis: false, width: 120 },
  { title: '是否监管', dataIndex: 'isSupervise', width: 70, customRender: text => text ? '是' : '否' },
  { title: '批号', dataIndex: 'manufacturerLotNum', ellipsis: false, width: 80 },
  { dataIndex: 'containerCode', title: '容器编号', ellipsis: false, width: 80 },
  // { dataIndex: 'barcode', title: '标签条码' },
  // { dataIndex: 'itemId', title: '商品ID', },
  { dataIndex: 'itemCode', title: '商品编号', ellipsis: false, width: 80 },
  { dataIndex: 'itemName', title: '商品名称', ellipsis: false, width: 160 },
  { dataIndex: 'type', title: '任务类型', width: 80, customRender: text => text == 1 ? '补货' : '拣货' },
  { dataIndex: 'qty', title: '数量', className: 'className', width: 80 },
  { dataIndex: 'saleRedQty', title: '红冲数量' },
  { dataIndex: 'pickQty', title: '已拣数量', width: 80 },
  { dataIndex: 'uom', title: '单位', width: 80 },
  { dataIndex: 'consignorName', title: '客户名称', width: 120 },
  { dataIndex: 'createby', title: '开票员', width: 80 },
  { dataIndex: 'paymentType', title: '账期类型', width: 80 },
  { dataIndex: 'conversionRate', title: '包装数量', width: 80 },
  { dataIndex: 'manufacturer', title: '生产厂家', width: 120 },
  // { dataIndex: 'operatorName', title: '索取人' },
  { dataIndex: 'printStatus', title: '打印状态', scopedSlots: { customRender: 'printStatus' }, width: 80 },
  {
    dataIndex: 'status', title: '任务状态', customRender: (text, rows, index) => {
      if (text === '0') return '待拣选'
      if (text === '1') return '待确认'
      if (text === '2') return '已完成'
      return text
    },
    width: 80
  },
  { dataIndex: 'warehouseAreaCode', title: '库区', width: 80 },
  { dataIndex: 'locatorCode', title: '库位', width: 100 },
  { dataIndex: 'batchNum', title: '批次', width: 80 },
  // { dataIndex: 'pickerName', title: '拣选人' },
  { title: '生产日期', dataIndex: 'productionDateShow', width: 120 },
  { title: '有效期至', dataIndex: 'expiredDateShow', width: 120 },
  { title: '操作', dataIndex: 'operation', fixed: 'right', scopedSlots: { customRender: 'operation' } }
]

//  整件拣货（tab1自动确认+tab2任务确认）
const columns2 = [
  { dataIndex: 'srcCode', title: '订单编号' },
  { dataIndex: 'pickTaskDId', title: '拣选任务明细id' },
  { dataIndex: 'itemCode', title: '商品编号' },
  { dataIndex: 'itemName', title: '商品名称' },
  // { dataIndex: 'barcode', title: '条码' },
  { dataIndex: 'qty', title: '计划数量' },
  { dataIndex: 'saleRedQty', title: '红冲数量' },
  { dataIndex: 'pickQty', slots: { title: 'customTitle' }, customRender: (text, rows, index) => (rows.qty - (+rows.saleRedQty)) }
]

const columns3 = [
  // { dataIndex: 'pickOrderId', title: '拣选单id' },
  // { dataIndex: 'consignorName',  title: '客户名称', width: 120},
  // { dataIndex: 'createby',  title: '开票员', width: 80},
  // { dataIndex: 'paymentType',  title: '账期类型', width: 80},
  { dataIndex: 'srcCode', title: '订单编号', width: 120 },
  { title: '是否监管', dataIndex: 'isSupervise', width: 50, customRender: text => text ? '是' : '否' },
  { title: '批号', dataIndex: 'manufacturerLotNum', width: 120 },
  { dataIndex: 'barcode', title: '标签条码', width: 140 },
  { dataIndex: 'containerCode', title: '容器编号', width: 120 },
  { dataIndex: 'itemCode', title: '商品编号', width: 120 },
  { dataIndex: 'itemName', title: '商品名称', width: 120 },
  { dataIndex: 'itemSpecs', title: '商品规格', width: 120 },
  { dataIndex: 'type', title: '任务类型', width: 80, customRender: text => text == 1 ? '补货' : '拣货' },
  { dataIndex: 'qty', title: '数量', className: 'className', width: 80 },
  { dataIndex: 'saleRedQty', title: '红冲数量', width: 80 },
  { dataIndex: 'pickQty', title: '拣选数量', width: 80 },
  { dataIndex: 'unPickQty', title: '未拣数量', width: 80 },
  { dataIndex: 'oddPieceQty', title: '零散数', width: 80 },
  { dataIndex: 'conversionRate', title: '包装数量', width: 80 },
  { dataIndex: 'manufacturer', title: '生产厂家', width: 120 },
  { dataIndex: 'printStatus', title: '打印状态', customRender: text => text == '1' ? '已打印' : '未打印', width: 80 },
  { dataIndex: 'status', title: '任务状态', scopedSlots: { customRender: 'status' }, width: 80 },
  { dataIndex: 'warehouseAreaCode', title: '库区', width: 120 },
  { dataIndex: 'locatorCode', title: '库位', width: 120 },
  { dataIndex: 'batchNum', title: '批次', width: 120 },
  { dataIndex: 'uom', title: '单位', width: 80 },
  { dataIndex: 'uomQty', title: '件数', width: 80 },
  { dataIndex: 'operatorName', title: '索取人', width: 80 },
  { dataIndex: 'operatorCode', title: '索取人工号', width: 120 },
  { dataIndex: 'pickerName', title: '拣选人', width: 120 },
  { dataIndex: 'pickerNo', title: '拣选人工号', width: 120 },
  { title: '生产日期', dataIndex: 'productionDateShow', width: 120 },
  { title: '有效期至', dataIndex: 'expiredDateShow', width: 120 },
  { title: '操作', dataIndex: 'operation', fixed: 'right', scopedSlots: { customRender: 'operation' } }
]


const modal_columns = [
  // { type: "txt",dataIndex: 'pickOrderId', title: '拣选单' },
  { type: 'txt', dataIndex: 'srcCode', title: '订单编号' },
  { type: 'txt', dataIndex: 'containerCode', title: '容器编号' },
  { type: 'txt', dataIndex: 'itemId', title: '商品ID' },
  { type: 'txt', dataIndex: 'itemCode', title: '商品编号' },
  { type: 'txt', dataIndex: 'itemName', title: '商品名称' },
  { type: 'txt', dataIndex: 'conversionRate', title: '包装数量' },
  { type: 'txt', dataIndex: 'qty', title: '数量' },
  { type: 'txt', dataIndex: 'pickQty', title: '已拣数量' },
  { type: 'number', dataIndex: 'redQty', title: '冲红数量', width: 80 },
  { type: 'edit', dataIndex: 'redReason', title: '冲红原因', required: true, width: 150 },
  {
    type: 'select', dataIndex: 'messageType', title: '拣货错误处理方式', width: 120, required: true,
    options: [
      { value: 0, label: '还货回库' },
      { value: 1, label: '入库盘点' }
      //  { value: 0, label: "入盘点库" },
      //  { value: 1, label: "不转移库存" },
    ]
  },
  { type: 'txt', dataIndex: 'barcode', title: '条码' },
  {
    type: 'txt', dataIndex: 'pickTaskType', title: '整零状态', customRender: (text, record) => {
      // console.log(record)
      if (text == '0') {
        return '整件'
      } else if (text == '1') {
        return '零件'
      } else {
        return ''
      }
    }
  },
  // { type: "txt",dataIndex: 'operatorName', title: '索取人' },
  // { type: "txt",dataIndex: 'printStatus', title: '打印状态',scopedSlots:{customRender:'printStatus'} },
  {
    type: 'txt', dataIndex: 'status', title: '任务状态', customRender: (text, rows, index) => {
      if (text === '0') return '待拣选'
      if (text === '1') return '待确认'
      if (text === '2') return '已完成'
      return text
    }
  },
  { type: 'txt', dataIndex: 'warehouseAreaCode', title: '库区' },
  { type: 'txt', dataIndex: 'locatorCode', title: '库位' },
  { type: 'txt', dataIndex: 'batchNum', title: '批次' },
  { type: 'txt', dataIndex: 'uom', title: '单位' }
  // { type: "txt",dataIndex: 'pickerName', title: '拣选人' },
]


export { items, items0, items1, items21, columns, columns2, columns3, modal_columns }
