import API from '../../api/default'
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "../../../SearchModal";

const items = [
    { type: 'input', name: 'dcId', label: '仓配中心ID', hidden: true },
    { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
    {
      type: 'search',
      name: 'warehouseName',
      label: '仓库',
      destroyOnClose:true,
      required: true,
      targetClick: true,
      span: 6,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseName',
            label: '仓库名称',
            span: 8,
            value: ''
          },
          {
            type: 'input',
            name: 'warehouseCode',
            label: '仓库编码',
            span: 8,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'warehouseName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
          { dataIndex: 'warehouseCode', title: '仓库编码', key: 'warehouseCode' },
          { dataIndex: 'warehouseId', title: '仓库ID', key: 'warehouseId' }
        ]
      },
      remoteUrl: API.warehouse,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseId: rowData[0].warehouseId,
            dcId:rowData[0].dcId
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              warehouseId: '',
            })
          }
        }
      }
    },
    ...genCargoownerAuthSearchModal(1, true,false,null,null,{span: 6}),
    {
      type: 'search',
      name: 'dcName',
      label: '部门编号',
      title: '部门编号',
      destroyOnClose:true,
      targetClick: true,
      span: 6,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'dcName',
            label: '部门名称',
            span: 8,
            value: ''
          },
          {
            type: 'input',
            name: 'code1',
            label: '部门编号',
            span: 8,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'dcName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [{ dataIndex: 'dcName', title: '部门名称', key: 'dcName' },{ dataIndex: 'code1', title: '部门编号', key: 'code1' }]
      },
      value: '',
      remoteUrl: API.wmsdcPage,
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, 'rowDatarowData')
        }
      }
  },
    {
      type: 'input',
      name: 'code2',
      disabled:true,
      label: '部门名称',
  },
  { type: 'input', name: 'warehouseWorkerId', label: '操作员ID', hidden: true },
  {
      type: 'search',
      name: 'warehouseWorkerName',
      label: '操作员',
      destroyOnClose:true,
      targetClick: true,
      span: 6,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseWorkerName',
            label: '操作员',
            span: 8,
            value: ''
          },
          {
            type: 'input',
            name: 'code1',
            label: '操作员Id',
            span: 8,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'dcName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [{ dataIndex: 'dcName', title: '操作员', key: 'dcName' },{ dataIndex: 'code1', title: '部门编号', key: 'code1' }]
      },
      value: '',
      remoteUrl: API.wmsdcPage,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseWorkerId: rowData[0].id
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              warehouseWorkerId: '',
            })
          }
        }
      }
  },
    {
      type: 'input',
      name: 'code4',
      disabled: true,
      label: '单据日期'
    },
    {
      type: 'input',
      name: 'code5',
      label: '单据编号'
    }
]
let itemId = ''
let batchId = ''
const columns = function (self) {
  return [
    {
      dataIndex: 'itemCode',
      width: 140,
      title: '商品编码',
      type: 'search',
      targetValueKey: 'itemId',
      destroyOnClose:true,
      targetClick: true,
      tableConfig: {
        rowKey: 'itemId',
        remoteUrl: (res) => {
          return {
            ...API.batchNum
          }
        },
        remoteBefore: (res, remoteParams) => {
          let refMain = self.queryRefFromName('main')
          let values = refMain.form.getFieldsValue()
          remoteParams.params = {
            ...remoteParams.params,...values,itemId:'580488529298198528'
          } 
          return remoteParams
        },
        columns: [
          {
            dataIndex: 'itemName',
            title: '商品名称',
            key:'itemName'
          },
          {
            dataIndex: 'itemId',
            title: '商品id',
            key:'itemId'
          },
          {
            dataIndex: 'batchProductionDate',
            title: '生产日期',
            key:'batchProductionDate'
          },
        ]
      },
      on: {
        ok(event, value, rowData, name, index, tableRowData) {
          itemId = rowData[0].itemId
          batchId=rowData[0].batchId,
          tableRowData.itemId = rowData[0].itemId
          tableRowData.batchNum = rowData[0].batchNum
          tableRowData.batchId = rowData[0].batchId
          tableRowData.itemName = rowData[0].itemName
          tableRowData.pieceBarcode = rowData[0].pieceBarcode
          tableRowData.pieceUom = rowData[0].pieceUom
          tableRowData.uom = rowData[0].pieceUom
          tableRowData.pieceConversionRate = rowData[0].pieceConversionRate
          tableRowData.conversionRate = rowData[0].pieceConversionRate
          tableRowData.manufacturerName = rowData[0].manufacturerName
          tableRowData.caseConversionRate =  rowData[0].caseConversionRate
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              itemId: '',
              batchId:''
            })
          }
        }
      },
      required: true
    },
    { dataIndex: 'itemId', title: '商品Id', key: 'itemId', hidden: true },
    { dataIndex: 'moveDate', title: '移位时间', key: 'moveDate', type: 'txt' },
    // { dataIndex: 'batchId', title: '批次Id', key: 'batchId', hidden: true  },
    { dataIndex: 'batchNum', title: '批次号', key: 'batchNum', hidden: true  },
    // { dataIndex: 'pieceBarcode', title: '商品条码', key: 'pieceBarcode' },
    { dataIndex: 'itemName', title: '商品名称', key: 'itemName' },
    { dataIndex: 'pieceUom', title: '包装单位', key: 'pieceUom' },
    { dataIndex: 'uom', title: '包装单位', key: 'uom', hidden: true },
    { dataIndex: 'pieceConversionRate', title: '包装数', key: 'pieceConversionRate' },
    { dataIndex: 'conversionRate', title: '包装数', key: 'conversionRate',hidden: true },
    { dataIndex: 'caseConversionRate', title: '最大单位（箱单位）数量', key: 'caseConversionRate',hidden: true },
    { dataIndex: 'outLocatorId', title: '移出货位ID', key: 'outLocatorId', hidden: true },
    {
      dataIndex: 'locatorCode',
      width: 140,
      title: '移出货位编码',
      type: 'search',
      remoteUrl: API.getstock,
      targetValueKey: 'locatorCode',
      destroyOnClose:true,
      autoLoad:false,
      targetClick: true,
      required: true,
      tableConfig: {
        columns: [
          {
            dataIndex: 'locatorCode',
            title: '货位编码',
            key: 'locatorCode'
          },
          {
            dataIndex: 'locatorId',
            title: '货位ID',
            key: 'locatorId'
          },
        ],
        remoteBefore: (res, remoteParams) => {
          console.log(remoteParams,'remoteParamsremoteParamsremoteParamsremoteParams')
          let refMain = self.queryRefFromName('main')
          let formsVal = refMain.form.getFieldsValue()
          remoteParams.params = {...remoteParams.params,...formsVal,itemId:itemId,batchId:batchId}
          return remoteParams
        }
      },
      on: {
        search(val, val1, val2, val3,name,index,tableRowData) {
          console.log('searchsearchsearchsearch', arguments)
          console.log(val, val1, val2,val3,'val, val1, val2val, val1, val2val, val1, val2')
          
        },
        ok(event, value, rowData, name, index, tableRowData) {
          // itemId = rowData[0].itemId
          // batchId=rowData[0].batchId,
          tableRowData.outLocatorId = rowData[0].locatorId
          tableRowData.outwarehouseAreaCategory = rowData[0].warehouseAreaCategory
          // tableRowData.itemName = rowData[0].itemName
          // tableRowData.pieceBarcode = rowData[0].pieceBarcode
          // tableRowData.pieceUom = rowData[0].pieceUom
          // tableRowData.pieceConversionRate = rowData[0].pieceConversionRate
        },
        change(value, label, options, form) {
            if (!value) {
              form.setFieldsValue({ code4: '' })
            }
          }
      }
    },
    { dataIndex: 'outwarehouseAreaCategory', title: '移出库别', key: 'outwarehouseAreaCategory' },
    {
      dataIndex: 'warehouseLocatorCode',
      width: 140,
      title: '移入货位编码',
      type: 'search',
      remoteUrl: API.locatorWHPage,
      targetClick: true,
      destroyOnClose:true,
      targetValueKey: 'warehouseLocatorCode',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseLocatorName',
            label: '货位名称',
            span: 6,
            value: ''
          },
          {
            type: 'input',
            name: 'warehouseLocatorCode',
            label: '货位编码',
            span: 6,
            value: ''
          },
          {
            type: 'input',
            name: 'warehouseName',
            label: '仓库名称',
            span: 6,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'warehouseLocatorId',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseLocatorName', title: '货位名称', key: 'warehouseLocatorName' },
          { dataIndex: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
          { dataIndex: 'warehouseLocatorId', title: '货位ID', key: 'warehouseLocatorId' }
        ]
      },
      on: {
        ok(event, value, rowData, name, index, tableRowData) {
          tableRowData.inwarehouseAreaCategory = rowData[0].warehouseAreaCategory
          tableRowData.inLocatorId = rowData[0].warehouseLocatorId
        },
      },
      required: true
    },
    { dataIndex: 'inwarehouseAreaCategory', title: '移入库别', key: 'inwarehouseAreaCategory' },
    { dataIndex: 'inLocatorId', title: '移入货位ID', key: 'inLocatorId', hidden: true },
    { dataIndex: 'numData', title: '数量', key: 'numData', },
    {
      dataIndex: 'quantity', title: '件数', key: 'quantity', type: 'edit', required: true,
      on: {
        change(event, value, rowData, name, index, tableRowData) {
          console.log(tableRowData,'tableRowDatatableRowData')
          tableRowData.numData = rowData[0].pieceConversionRate * value + Number(rowData[0].oddPieceQuantity)
        },
      },
    },
    { dataIndex: 'oddPieceQuantity', title: '零散数', key: 'oddPieceQuantity', type: 'edit', required: true,on: {
      change(event, value, rowData, name, index, tableRowData) {
        console.log(tableRowData,'tableRowDatatableRowData')
          tableRowData.numData = rowData[0].pieceConversionRate * value + Number(rowData[0].oddPieceQuantity)
        },
      }, },
    {
      dataIndex: 'reason',
      type: 'select',
    title: '移位原因',
      options: [
        { label: '原因1', value: '原因1' },
        { label: '原因2', value: '原因2' },
      ]
    },
    { dataIndex: 'manufacturerName', title: '生产厂家', key: 'manufacturerName'},
  ]
}

export function detailConfig(self) {
  return [
    {
      key: 'one', checkedKey: 'main',
      btns: [
        { label: '重置', click() { 
          let refMain = self.queryRefFromName('main')
          let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
          refMain.form.enableItem(['warehouseName', 'cargoOwnerCode'])
          ref.delItems() // 删除附表信息
        } },
        {
          label: '保存', click() {
            let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
            self.saveData(ref.getListData(true))
            
         } },
      ],
      list: [
        {
          key: 'main',
          tab: '主要信息',
          btns: [
            // { label: '辅助功能1', click() { } },
          ],
          item: { type: 'p-grid-form', config: items }
        },
      ]
    },
    {
      key: 'two', checkedKey: 'baseMtlItemUomConversionsList',
      list: [
        {
          key: 'baseMtlItemUomConversionsList',
          tab: '移货位明细',
          btns: [
            {
              label: '新增', click() {
                let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
                let refMain = self.queryRefFromName('main')
                // console.log(self.getData())
                let formsVal = refMain.form.getFieldsValue()
                if (!formsVal.cargoOwnerId) return self.$message.warn('请选择货主')
                ref.addItem() // 新增
                // self.formsVal = formsVal  disabled
                refMain.form.disableItem(['warehouseName', 'cargoOwnerCode'])
                // refMain.form.disableItem() // form禁用
            } },
            {
              label: '删除', click() {
                self.$confirm({
                  content: self.$lang('确定删除?'),
                  onOk: () => {
                    let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
                    ref.delSelectedItems()
                  }
                })
            } },
          ],
          item: {
            type: 'p-table-edit',
            config: columns(self),
            rowKey: '_row_count',
            rowSelectType: 2,
            colChange({ record, name, value }) {
              record.numData = record.pieceConversionRate * record.quantity + Number(record.oddPieceQuantity)
            }
          }
        },
      ]
    },
  ];
}
