import API from '../../api/default'
import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genItemSearchModal,
  genContainerSearchModal
} from '@/views/kzyWMS/SearchModal'
import { upcloud_base_wh, upcloud_base_common } from "@/config/serviceSettings";

const items = function(self) {
  let grid = {
    span: 8
  }
  return [
    { type: 'input', name: 'dcId', label: '仓配中心ID', hidden: true },
    { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
    ...genWarehouseAuthSearchModal(
      1,
      true,
      false,
      null,
      (modalRowData, form) => {
        console.log(modalRowData, form, 'modalRowData,formmodalRowData,form')
        if (modalRowData) {
          self.dcId = modalRowData[0].dcId
          self.remoteParams.warehouseId = modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId
        } else {
          self.dcId = ''
          self.remoteParams.warehouseId = ''
        }
      },
      grid
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid),
    {
      type: 'select',
      name: 'moveOutBldg',
      label: '移出建筑',
      required: true,
      defaultValueIndex: 0,
      destroyOptions: true,
      value: '',
      // autoLoad: true,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: 'bldgName',
        value: 'bldgId'
      },
      // remoteParams: self.remoteParams,
      remoteBefore(res, remoteParams) {
        let vals = self.queryRefFromName('main').form.getFieldsValue()
        if (remoteParams.params) {
          remoteParams.params.warehouseId = vals.warehouseId
        } else {
          remoteParams.params = { warehouseId:vals.warehouseId }
        }
        return remoteParams
      },
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, 'rowDatarowData')
        }
      }
    },
    {
      type: 'input',
      name: 'departmentId',
      label: '部门编号',
      disabled: true
    },
    {
      type: 'input',
      name: 'departmentName',
      label: '部门',
      disabled: true
    },
    {
      type: 'select',
      name: 'moveInBldg',
      label: '移入建筑',
      required: true,
      value: '',
      // autoLoad: true,
      destroyOptions: true,
      defaultValueIndex: 0,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: 'bldgName',
        value: 'bldgId'
      },
      // remoteParams: self.remoteParams,
      remoteBefore(res, remoteParams) {
        let vals = self.queryRefFromName('main').form.getFieldsValue()
          if (remoteParams.params) {
            remoteParams.params.warehouseId = vals.warehouseId
          } else {
            remoteParams.params = { warehouseId:vals.warehouseId }
          }
        return remoteParams
      }
    },
    { type: 'input', name: 'warehouseWorkerId', label: '操作员Id', hidden: true },
    { type: 'input', name: 'warehouseWorkerName', label: '操作员',disabled: true},
    {
      type: "date",
      name: "today",
      label: "单据日期",
      disabled:true,
      placeholder: "请输入开始日期",
      value: "",
      format: "YYYY-MM-DD",
    },
  ]
}
const columns = function (self) {
  let warehouseIdData = {
      key: 'warehouseId', target: 'warehouseId', transform(val) {
        let refMain = self.queryRefFromName('main')
        let values = refMain.form.getFieldsValue()
        return values.warehouseId
      }
    }
  let cargoOwnerIdData =  {
      key: 'cargoOwnerId', target: 'cargoOwnerId', transform(val) {
        let refMain = self.queryRefFromName('main')
        let values = refMain.form.getFieldsValue()
        return values.cargoOwnerId
      }
    }

  return [
    {
      dataIndex: 'itemCode',
      title: '商品编码',
      label:'商品编码',
      type: 'search',
      width: 150,
      modalWidth:'70%',
      required: true,
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'itemCode',
            label: '商品编码',
            span: 8,
            value: ''
          },
          {
            type: 'input',
            name: 'itemAssistantCode',
            label: '助记码',
            span: 8,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'itemId',
        remoteUrl: res => {
          return {
            ...API.batchNum
          }
        },
        columns: [
          {
            hidden: true,
            dataIndex: 'itemId',
            title: '商品id',
            key: 'itemId'
          },
          {
            dataIndex: 'itemCode',
            title: '商品编码',
            key: 'itemCode'
          },
          {
            dataIndex: 'itemAssistantCode',
            title: '助记码',
            key: 'itemAssistantCode'
          },
          {
            dataIndex: 'itemName',
            title: '商品名称',
            key: 'itemName'
          },
          {
            dataIndex: 'manufacturerName',
            title: '生产厂家',
            key: 'manufacturerName'
          }
        ]
      },
      relatedQuery: [
        {
          key: 'warehouseId', target: 'warehouseId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.warehouseId
          }
        },{
          key: 'cargoOwnerId', target: 'cargoOwnerId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.cargoOwnerId
          }
        }
      ],
      multipleKey: [
        { key: 'itemName', target: 'itemName' },
        { key: 'pieceBarcode', target: 'pieceBarcode' },
        { key: 'specification', target: 'specification' },
        { key: 'itemId', target: 'itemId' },
        { key: 'batchId', target: 'batchId' },
        { key: 'pieceUom', target: 'uom' },
        // { key: 'caseConversionRate', target: 'caseConversionRate' },
        { key: 'manufacturerName', target: 'manufacturerName' },
        { key: 'countryOriginId', target: 'countryOriginId' },
        // { key: 'conversionRate', target: 'conversionRate' },
      ],
      relatedClean: ['itemName', 'pieceBarcode', 'itemId','locatorCode', 'uom', 'batchNum', 'caseConversionRate', 'manufacturerName', 'conversionRate','warehouseLocatorCode','outLocatorId','batchNum','batchId','manufacturerLotNum','batchProductionDate','batchExpiredDate','batchProductionDateShow','batchExpiredDateShow','availableQuantity'],
    },
    { dataIndex: 'itemId', title: '商品Id', key: 'itemId', hidden: true },
    { dataIndex: 'rowIndex', title: '列表序号', key: 'rowIndex', hidden: true },
    { 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', width:200 },
    { dataIndex: 'specification', title: '规格', key: 'specification', width:100 },
    { dataIndex: 'caseConversionRate', title: '包装数量', key: 'caseConversionRate',  width:80 },
    { dataIndex: 'uom', title: '包装单位', key: 'uom', width:80},
    { dataIndex: 'conversionRate', title: '包装数', key: 'conversionRate', hidden: true,  width:150 },
    { dataIndex: 'warehouseAreaCategory', title: '移出库别', key: 'warehouseAreaCategory', hidden: true },
    { dataIndex: 'quantity', title: '库存数量', key: 'quantity', hidden: true },
    { dataIndex: 'outLocatorId', title: '移出货位id', key: 'outLocatorId', hidden: true,},
    {
      key: 'locatorCode',
      dataIndex: 'locatorCode',
      width: 150,
      title: '移出货位编码',
       label:'移出货位编码',
      type: 'search',
      remoteUrl: API.getstock,
      targetValueKey: 'locatorCode',
      destroyOnClose: true,
      modalWidth:'70%',
      autoLoad: false,
      targetClick: true,
      required: true,
      relatedClean: ['warehouseLocatorCode','outLocatorId','batchNum','batchId','manufacturerLotNum','batchProductionDate','batchExpiredDate','batchProductionDateShow','batchExpiredDateShow','availableQuantity','floor'],
      disabled: [
        {
          $value: false,
          $match: [
            { key: 'itemCode', value: '${*}' }
          ]
        },
        { $value: true }
      ],
      relatedQuery: [
        { key: 'itemId', target: 'itemId' },
        { key: 'batchId', target: 'batchId' },
        {
          key: 'lkFlag', target: 'lkFlag',default:'Y' 
        },
        {
          key: 'warehouseId', target: 'warehouseId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.warehouseId
          }
        },{
          key: 'cargoOwnerId', target: 'cargoOwnerId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.cargoOwnerId
          }
        }
      ],
      multipleKey: [
        { key: 'locatorId', target: 'outLocatorId' },
        { key: 'batchNum', target: 'batchNum' },
        { key: 'batchId', target: 'batchId' },
        { key: 'status', target: 'outStockStatus' },
        { key: 'status', target: 'inStockStatus' },
        { key: 'containerCode', target: 'outContainerCode' },
        { key: 'quantity', target: 'quantity' },
        { key: 'manufacturerLotNum', target: 'manufacturerLotNum' },
        { key: 'batchProductionDate', target: 'batchProductionDate' },
        { key: 'batchExpiredDate', target: 'batchExpiredDate' },
        { key: 'batchProductionDateShow', target: 'batchProductionDateShow' },
        { key: 'batchExpiredDateShow', target: 'batchExpiredDateShow' },
        { key: 'caseConversionRate', target: 'caseConversionRate' },
        { key: 'caseConversionRate', target: 'conversionRate' },
        { key: 'availableQuantity', target: 'availableQuantity' },
        { key: 'warehouseAreaCategory', target: 'warehouseAreaCategory' },
      ],
      tableConfig: {
        columns: [
          {
            dataIndex: 'manufacturerLotNum',
            title: '批号',
            width:250,
            key: 'manufacturerLotNum'
          },
          {
            dataIndex: 'locatorName',
            title: '货位名称',
            key: 'locatorName'
          },
          {
            dataIndex: 'locatorCode',
            title: '货位编码',
            key: 'locatorCode'
          },
          {
            dataIndex: 'availableQuantity',
            title: '可用库存',
            key: 'availableQuantity'
          },
          {
            dataIndex: 'warehouseAreaCategory',
            title: '库别',
            key: 'warehouseAreaCategory'
          },
          {
            dataIndex: 'warehouseAreaName',
            title: '库区',
            key: 'warehouseAreaName'
          }
        ],
      },
    },
    {
      dataIndex: 'warehouseLocatorCode',
      width: 150,
      title: '移入货位编码',
      label:'移入货位编码',
      type: 'search',
      remoteUrl: API.locatorPageLK,
      targetClick: true,
      targetValueKey: 'warehouseLocatorCode',
      relatedQuery: [
        { key: 'itemId', target: 'itemId' },
        { key: 'batchId', target: 'batchId' },
        { key: 'outLocatorId', target: 'locatorId' },
        { key: 'locatorCode', target: 'locatorCode' },
        {
          key: 'warehouseId', target: 'warehouseId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.warehouseId
          }
        },{
          key: 'cargoOwnerId', target: 'cargoOwnerId', transform(val) {
            let refMain = self.queryRefFromName('main')
            let values = refMain.form.getFieldsValue()
            return values.cargoOwnerId
          }
        }
      ],
      multipleKey: [
        { key: 'warehouseLocatorId', target: 'inLocatorId' },
      ],
      disabled: [
        {
          $value: false,
          $match: [
            { key: 'locatorCode', value: '${*}' }
          ]
        },
        { $value: true }
      ],
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseLocatorName',
            label: '货位名称',
            span: 6,
            value: ''
          },
          {
            type: 'input',
            name: 'warehouseLocatorCode',
            label: '货位编码',
            span: 6,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'warehouseLocatorId',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseLocatorName', title: '货位名称', key: 'warehouseLocatorName' },
          { dataIndex: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
        ],
      },
      required: true
    },
    {
      dataIndex: 'caseQuantity',
      title: '件数',
      width: 100,
      required:true,
      key: 'caseQuantity',
      type: 'number',
      disabled: [
        {
          $value: true,
          $match: [
            { key: 'warehouseAreaCategory', value: 'LTK' }
          ]
        },
        { $value: false }
      ],
    },
    { dataIndex: 'numData', title: '移库数量', key: 'numData' ,  width:80,},
    {
      type: 'select',
      dataIndex: 'floor',
      title: '楼层',
      defaultValueIndex: 0,
      destroyOptions:true,
      remoteUrl: API.remoteUrlFun('floor'),
      replaceFields: {
        label: 'dictName',
        value: 'dictCode'
      },
      disabled: [
        {
          $value: false,
          $match: [
            { key: 'warehouseAreaCategory', value: 'LTK' }
          ]
        },
        { $value: true }
      ],
      rules: [
        {
          required: true, message: '请选择楼层', match: [
            { key: 'warehouseAreaCategory', value: 'LTK' },
          ]
        }
      ],
    },
    {
      ...genContainerSearchModal(2, false, false, function (res, p) {
        let vals = self.$refs.detail.$refs.main[0].form.getFieldsValue();
        console.log(vals)
        p.data.warehouseId = vals.warehouseId;
        p.data.containerStatus = '10'; // 空闲状态
        p.data.containerType = '20'; // 容器类型-托盘
        return p;
      }, null),
      remoteUrl: { url: upcloud_base_wh+'/container/getPageList', method: 'post' },
      title: '托盘编号',
      dataIndex: 'outContainerCode',
      key: 'outContainerCode',
      targetValueKey: 'containerCode',
      rowKey: 'containerId',
      width: 150,
      targetClick: true,
      disabled: [
        {
          $value: true,
          $match: [
            { key: 'warehouseAreaCategory', value: 'LTK' }
          ]
        },
        { $value: false }
      ],
    },
    { dataIndex: 'reason', type: 'input',required:true, title: '移库原因', key: 'reason',value:'库内调整', width: 150 },
    { dataIndex: 'manufacturerLotNum', title: '批号', key: 'manufacturerLotNum', width: 80 },
    { dataIndex: 'batchProductionDateShow', title: '显示生产日期', key: 'batchProductionDateShow',  width:150 },
    { dataIndex: 'batchExpiredDateShow', title: '显示有效期至', key: 'batchExpiredDateShow',  width:150},

    { dataIndex: 'inLocatorId', title: '移入货位ID', key: 'inLocatorId', hidden: true ,  width:200},
    { dataIndex: 'countryOriginId', title: '产地', key: 'countryOriginId',  width:200 },
    { dataIndex: 'manufacturerName', title: '生产厂家', key: 'manufacturerName',  width:250 },
  ]
}
const operateFormItems = [
  {
    type: 'input',
    name: 'itemHealthType',
    label: '制单人',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthUsage',
    label: '修改人',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthApprovalNbr',
    label: '制单日期',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthIndustry',
    label: '修改时期',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthCommonName',
    label: '单据状态',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthBuyer',
    label: '上传状态',
    value: '',
    labelCol: { span: 8 },
    span: 8
  },
  {
    type: 'input',
    name: 'itemHealthRegisterNbr',
    label: '作业状态',
    value: '',
    labelCol: { span: 8 },
    span: 8
  }
]

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', 'cargoOwnerName', 'moveOutBldg', 'moveInBldg'])
            // refMain.form.resetFields()
            refMain.form.setFieldsValue({
              dcId: '',
              warehouseId: '',
              warehouseName: '',
              moveOutBldg:'',
              cargoOwnerId: '',
              cargoOwnerName: '',
              moveInBldg: '',
              remark: '',
            })
            ref.resetFields() // 删除附表信息
          }
        },
        {
          label: '保存',
          click() {
            let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
            self.saveData(ref.getFieldsValue(true))
          }
        }
      ],
      list: [
        {
          key: 'main',
          tab: '主要信息',
          btns: [
            // { label: '辅助功能1', click() { } },
          ],
          item: { type: 'p-grid-form', config: items(self) }
        },
        // {
        //   key: 'baseMtlItemHealth',
        //   tab: '操作信息',
        //   item: { type: 'p-grid-form', config: operateFormItems }
        // }
      ]
    },
    {
      key: 'two',
      checkedKey: 'baseMtlItemUomConversionsList',
      list: [
        {
          key: 'baseMtlItemUomConversionsList',
          tab: '移库明细',
          btns: [
            {
              label: '新增',
              click() {
                let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
                let refMain = self.queryRefFromName('main')
                let formsVal = refMain.form.getFieldsValue()
                if (!formsVal.warehouseId) return self.$message.warn('请选择仓库')
                if (!formsVal.cargoOwnerId) return self.$message.warn('请选择货主')
                if (!formsVal.moveOutBldg) return self.$message.warn('请选择移出建筑')
                if (!formsVal.moveInBldg) return self.$message.warn('请选择移入建筑')
                ref.addRow() // 新增
                // self.formsVal = formsVal  disabled
                refMain.form.disableItem(['warehouseName', 'cargoOwnerName', 'moveOutBldg', 'moveInBldg'])
              }
            },
            {
              label: '删除',
              click() {
                self.$confirm({
                  content: self.$lang('确定删除?'),
                  onOk: () => {
                    let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
                    ref.removeSelectedRow()
                  }
                })
              }
            }
          ],
          item: {
            type: 'p-table-sheet',
            config: columns(self),
            rowKey: '_row_count',
            rowSelectType: 2,
            colChange({ record, key, value, index,dataSource }) {
              if (key=='caseConversionRate' || key=='caseQuantity') {
                let num =record.caseConversionRate*record.caseQuantity
                record.numData=Number(num)?Number(num):0
              }
              console.log(record, key,'record, namerecord, name')
              if (key=='locatorCode'&&record.warehouseAreaCategory=='LTK') {
                record.caseQuantity = record.quantity / record.caseConversionRate
                record.numData=record.quantity
              }
              if (key == 'caseQuantity') {
                let max = record.quantity/record.caseConversionRate
                if (record.caseQuantity>max || record.caseQuantity<=0 ) {
                  record.caseQuantity=max
                }
              }
            }
          }
        }
      ]
    }
  ]
}
