import moment from "moment";
import request from '@/utils/request'

import {
  genDelegationPartySearchModal,
  genManufacturerLotNumSearchModal, genContainerSearchModal
} from "../../../../../../SearchModal";
import { genCloumnContainer } from "../../../../../config/genColumnContainer";
import { upcloud_base_common, upcloud_base_wh,upcloud_base_wh_inbound } from "@/config/serviceSettings";

function getContainers(parameter) {
  return request({
    method: 'post',
    url: upcloud_base_wh + '/container/getPageList',
    data: parameter
  })
}

// 缓存
let paramData = {};
let clickRecord = null;
// 原订单数量
export const packingColumns = function (self) {
  let stockStatusConfig =
  {
    type: 'select',
    width: 80,
    title: '收货结论',
    dataIndex: 'stockStatus',
    key: 'stockStatus',
    options: [
      // 合格0/不合格1/待处理2/拒绝3
      { value: '0', label: '收货' },
      // { value: '2', label: '待处理', disabled: true },
      { value: '3', label: '拒收', }
    ],
    required: true,
  }
  if (self.businessData == 'kzy') {
    stockStatusConfig = {
      type: 'select',
      width: 80,
      title: '收货结论',
      autoLoad: true, 
      destroyOptions:true,
      dataIndex: 'stockStatus',
      key: 'stockStatus', 
      replaceFields: {
        value: 'code',
        label: 'name'
      },
      remoteUrl: {
        url: upcloud_base_wh_inbound + '/rcvLines/v1.0/getConclusionSingle',
        method: 'post'
      },
      //传参
      relatedQuery: [
        { key: 'asnLineId', target: 'refId' },
        { key: 'mtlOwnerId', target: 'mtlOwnerId' },
        { key: 'warehouseId', target: 'warehouseId' },
        { key: 'rcvType', target: 'rcvType' },
        { key: 'batchExpiredDate', target: 'batchExpiredDate' },
      ],
      // 取值
      multipleKey: [
        { key: 'code', target: 'code' },
        { key: 'name', target: 'name' },
      ],
      required: true,
    }
  } 

  return [
    { hidden: true, title: 'itemId', dataIndex: 'itemId', key: 'itemId' },
    { hidden: true, title: 'mtlOwnerId', dataIndex: 'mtlOwnerId', key: 'mtlOwnerId' },
    { hidden: true, title: 'itemHealthIsDouble', dataIndex: 'itemHealthIsDouble', key: 'itemHealthIsDouble' },
    { hidden: true, title: 'qcMode', dataIndex: 'qcMode', key: 'qcMode' },
    { hidden: true, title: 'managementExpiredDate', dataIndex: 'managementExpiredDate', key: 'managementExpiredDate' },
    { hidden: true, title: 'itemHealthTwovoteflag', dataIndex: 'itemHealthTwovoteflag', key: 'itemHealthTwovoteflag' },
    { hidden: true, title: 'industry', dataIndex: 'industry', key: 'industry' },
    { hidden: true, title: 'commonName', dataIndex: 'commonName', key: 'commonName' },
    { hidden: true, title: 'countryOriginId', dataIndex: 'countryOriginId', key: 'countryOriginId' },
    { hidden: true, title: 'caseConversionRate', dataIndex: 'caseConversionRate', key: 'caseConversionRate' },
    { hidden: true, title: 'caseBarCode', dataIndex: 'caseBarCode', key: 'caseBarCode' },
    { hidden: true, title: 'pieceBarCode', dataIndex: 'pieceBarCode', key: 'pieceBarCode' },
    { hidden: true, title: 'itemAssistantCode', dataIndex: 'itemAssistantCode', key: 'itemAssistantCode' },
    { hidden: true, title: 'itemVolume', dataIndex: 'itemVolume', key: 'itemVolume' },
    { hidden: true, title: 'itemWeight', dataIndex: 'itemWeight', key: 'itemWeight' },
    { hidden: true, title: 'freeQty', dataIndex: 'freeQty', key: 'freeQty' },
    { hidden: true, title: 'warehouseId', dataIndex: 'warehouseId', key: 'warehouseId' },
    { hidden: true, title: 'asnHeaderId', dataIndex: 'asnHeaderId', key: 'asnHeaderId' },
    { hidden: true, title: 'asnAutoBillNum', dataIndex: 'asnAutoBillNum', key: 'asnAutoBillNum' },
    { hidden: true, title: 'asnLineId', dataIndex: 'asnLineId', key: 'asnLineId' },
    { hidden: true, title: 'categoryId', dataIndex: 'categoryId', key: 'categoryId', },
    { hidden: true, title: 'shelfLifeUnit', dataIndex: 'shelfLifeUnit', key: 'shelfLifeUnit', },
    { hidden: true, title: 'shelfLifeNum', dataIndex: 'shelfLifeNum', key: 'shelfLifeNum', },
    { hidden: true, title: 'stackStockCount', dataIndex: 'stackStockCount', key: 'stackStockCount', },
    { hidden: true, title: 'remainStackStockCount', dataIndex: 'remainStackStockCount', key: 'remainStackStockCount', },
    { hidden: true, title: 'shelfStockCount', dataIndex: 'shelfStockCount', key: 'shelfStockCount', },
    { hidden: true, title: 'remainShelfStockCount', dataIndex: 'remainShelfStockCount', key: 'remainShelfStockCount', },
    { hidden: true, title: 'rcvLinesPicDTOList', dataIndex: 'rcvLinesPicDTOList', key: 'rcvLinesPicDTOList', },
    { hidden: true, title: 'shipperid', dataIndex: 'shipperid', key: 'shipperid', },
    { hidden: true, title: 'tempControlScope', dataIndex: 'tempControlScope', key: 'tempControlScope', },
    { hidden: true, title: '商品助记码', dataIndex: 'itemZjm', key: 'itemZjm', },
    { hidden: true, title: 'isDelegate', dataIndex: 'isDelegate', key: 'isDelegate', },
    { hidden: true, title: '入库类型', dataIndex: 'rcvType', key: 'rcvType', },


    {
      width: 80,
      title: '是否追溯',
      dataIndex: 'isRetrospect',
      key: 'isRetrospect',
      customRender(text) {
        return text == '1' ? '是' : '否'
      }
    },
    {
      width: 90,
      title: '商品编码',
      dataIndex: 'itemNum',
      key: 'itemNum'
    },
    {
      width: 100,
      title: '商品名称',
      dataIndex: 'itemName',
      key: 'itemName',
    },

    {
      width: 90,
      title: '商品大类',
      dataIndex: 'categoryName',
      key: 'categoryName',
      value: ''
    },
    {
      width: 80,
      title: '生产厂家',
      dataIndex: 'manufacturerName',
      key: 'manufacturerName'
    },
    {
      width: 80,
      title: '产地',
      dataIndex: 'productionPlace',
      key: 'productionPlace'
    },
    {
      width: 140,
      title: '商品规格',
      dataIndex: 'specification',
      key: 'specification',
    },

    { title: "包装单位", dataIndex: "uom", width: 100, value: '' },
    {
      width: 90,
      title: '包装数',
      dataIndex: 'conversionRate',
      key: 'conversionRate',
      value: '',
    },
    {
      hidden: true,
      width: 100,
      title: '单位包装数量',
      dataIndex: 'qty',
      key: 'qty',
    },
    //件数oddCaseQty*包装数conversionRate+零散数oddPieceQty=数量pieceQty
    {
      type: 'number',
      width: 80,
      title: '件数',
      dataIndex: 'oddCaseQty',
      key: 'oddCaseQty',
      precision: 0,
      min: 0,
    },
    {
      type: 'number',
      width: 80,
      title: '零散数',
      dataIndex: 'oddPieceQty',
      key: 'oddPieceQty',
      min: 0,
    },
    {
      width: 80,
      title: '数量',
      dataIndex: 'pieceQty',
      key: 'pieceQty',
    },
    {
      width: 80,
      title: '含税单价',
      dataIndex: 'piecePriceTi',
      key: 'piecePriceTi',
    },
    {
      width: 80,
      title: '不含税单价',
      dataIndex: 'piecePriceTe',
      key: 'piecePriceTe',
      hidden: true,
    },

    // {
    //   type: 'select',
    //   width: 150,
    //   title: '建筑物',
    //   dataIndex: 'bldgId',
    //   key: 'bldgId',
    //   value: '',
    //   defaultValueIndex: 0,
    //   remoteUrl: {
    //     url: upcloud_base_wh + '/wmsWhBldg/v1/page',
    //     method: 'get'
    //   },
    //   autoLoad: true,
    //   replaceFields: {
    //     value: 'bldgId',
    //     label: 'bldgName'
    //   },
    //   done(data) {
    //     return data.list
    //   },
    //   //传参
    //   relatedQuery: [
    //     { key: 'warehouseId', target: 'warehouseId' },
    //     { key: 'pageNum', target: 'pageNum', default: 1 },
    //     { key: 'pageSize', target: 'pageSize', default: 50 },
    //   ],
    //   // 取值
    //   multipleKey: [
    //     { key: 'bldgName', target: 'bldgName' },
    //     { key: 'bldgCode', target: 'bldgCode' },
    //   ],
    //   // 校验
    //   rules: [
    //     {
    //       required: true, message: '请选择建筑物', match: [
    //         { key: 'stockStatus', value: '0' },
    //       ]
    //     }
    //   ]
    // },
    // { hidden: true, title: '建筑物', dataIndex: 'bldgName', key: 'bldgName' },
    // { hidden: true, title: '建筑物', dataIndex: 'bldgCode', key: 'bldgCode' },

    {
      ...genManufacturerLotNumSearchModal(2, false, false, null, null, null, {
        enableFuzzyQuery: ['itemId', 'warehouseId', 'mtlOwnerId'],
      }),
      width: 150,
      relatedQuery: [
        { key: 'itemId', target: 'itemId' },
        { key: 'warehouseId', target: 'warehouseId' },
        { key: 'mtlOwnerId', target: 'mtlOwnerId' },
      ],
      multipleKey: [
        { key: 'batchProductionDate', target: 'showProductionDate', transform: function (val) { return val.substring(0, 10).replace(/-/g, '') } },
        { key: 'batchExpiredDate', target: 'showExpiredDate', transform: function (val) { return val.substring(0, 10).replace(/-/g, '') } },
        { key: 'batchProductionDate', target: 'showProductionDateOld', transform: function (val) { return val.substring(0, 10).replace(/-/g, '') } },
        { key: 'batchExpiredDate', target: 'showExpiredDateOld', transform: function (val) { return val.substring(0, 10).replace(/-/g, '') } },
        { key: 'batchProductionDate', target: 'batchProductionDate', transform: function (val) { return val.substring(0, 10) } },
        { key: 'batchExpiredDate', target: 'batchExpiredDate', transform: function (val) { return val.substring(0, 10) } },
        { key: 'batchProductionDate', target: 'batchProductionDateOld', transform: function (val) { return val.substring(0, 10) } },
        { key: 'batchExpiredDate', target: 'batchExpiredDateOld' },
        { key: 'manufacturerLotNum', target: 'manufacturerLotNumOld' },
        { key: 'batchId', target: 'batchId' },
        { key: 'batchNum', target: 'batchNum' },
        { key: 'rqcReportNo', target: 'rqcReportNo' },
      ],
      relatedClean: ['manufacturerLotNumOld', 'batchProductionDateOld', 'batchExpiredDateOld', 'showProductionDateOld', 'showExpiredDateOld', 'batchNum', 'batchId', 'rqcReportNo'],
      rules: [
        {
          required: true, message: '请选择批号', match: [
            { key: 'stockStatus', value: '0' },
          ]
        }
      ],
    },
    {
      type: 'input', width: 120, title: '显示生产日期', dataIndex: 'showProductionDate', key: 'showProductionDate', max: 8,
      rules: [
        {
          required: true, message: '请输入显示生产日期', match: [
            { key: 'stockStatus', value: '0' },
          ]
        }
      ],
    },
    {
      type: 'input', width: 120, title: '显示有效期至', dataIndex: 'showExpiredDate', key: 'showExpiredDate', max: 8,
      rules: [
        {
          required: true, message: '请输入显示有效期至', match: [
            { key: 'stockStatus', value: '0' },
          ]
        }
      ],
    },
    { width: 120, title: '生产日期', dataIndex: 'batchProductionDate', key: 'batchProductionDate', },
    {
      type: 'input', width: 120, title: '有效期至', dataIndex: 'batchExpiredDate', key: 'batchExpiredDate',
      rules: [
        {
          required: true, message: '请输入有效期至', match: [
            { key: 'stockStatus', value: '0' }
          ]
        }
      ],
    }, { width: 120, title: '质检报告单号', dataIndex: 'rqcReportNo', key: 'rqcReportNo' },
    { hidden: true, title: '批号old', dataIndex: 'manufacturerLotNumOld', key: 'manufacturerLotNumOld' },
    { hidden: true, title: '显示生产日期old', dataIndex: 'showProductionDateOld', key: 'showProductionDateOld' },
    { hidden: true, title: '显示有效期至old', dataIndex: 'showExpiredDateOld', key: 'showExpiredDateOld' },
    { hidden: true, title: '生产日期old', dataIndex: 'batchProductionDateOld', key: 'batchProductionDateOld' },
    { hidden: true, title: '有效期至old', dataIndex: 'batchExpiredDateOld', key: 'batchExpiredDateOld' },
    // genCloumnContainer(self),
    {
      ...genContainerSearchModal(2, false, false, null, null),
      remoteUrl: { url: upcloud_base_wh + '/container/getPageList', method: 'post' },
      title: '容器编码',
      dataIndex: 'containerCode',
      key: 'containerCode',
      targetValueKey: 'containerCode',
      required: false,
      rowKey: 'containerId',
      width: 150,
      targetClick: false,
      relatedQuery: [
        { key: 'warehouseId', target: 'warehouseId' },
        { key: 'containerStatus', target: 'containerStatus', default: '10' },
      ],
      on: {
        blur({ value, index, record }) {
          let code = value || '';
          code = code.trim();
          if (!code) return;
          getContainers({ pageSize: 1000, containerStatus: '10', containerCode: code, warehouseId: self.mainlist[0].warehouseId }).then(res => {
            if (!res.success) {
              this.$exception(res)
              record.containerCode = '';
              return;
            }
            if (!Array.isArray(res.data.list) || res.data.list.length === 0) {
              record.containerCode = '';
              self.$message.warn('容器不存在或已占有');
              return;
            }
            //根据输入的值匹配接口返回值，如果没有匹配到一模一样的值就取返回的第一个值赋给当前容器号
            if (res.data.list.every(tt => tt.containerCode != code)) {
              let item = res.data.list[0];
              record.containerCode = item.containerCode;
            }
          });
        }
      }
    },
    {

      width: 80,
      title: '库房编号',
      dataIndex: 'warehouseRoomCodes',
      key: 'warehouseRoomCodes',
      // required: true,
    },
    {
      type: 'input',
      width: 80,
      title: '货格',
      dataIndex: 'goodsCaseCode',
      key: 'goodsCaseCode',
      // required: true,
    },
    stockStatusConfig,
    {
      type: "select", title: "处理原因", dataIndex: "rcvReason1", width: 140, allowClear: true,
      // scopedSlots: { customRender: 'rcvReason1' }
      autoLoad: true, remoteUrl: { url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=rcv_billd_rcvreason1', method: 'post' },
      replaceFields: {
        // value: 'dictCode',
        value: 'dictName',
        label: 'dictName'
      },
      rules: [
        {
          required: true, message: '结论为拒收，请输入处理原因！', match: [
            { key: 'stockStatus', value: '3' }
          ]
        }
      ]
    },
    {
      type: "select", title: "不足原因", dataIndex: "rcvReason2", width: 140, allowClear: true,
      // scopedSlots: { customRender: 'rcvReason2' }
      autoLoad: true, remoteUrl: { url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=rcv_billd_rcvreason2', method: 'post' },
      replaceFields: {
        // value: 'dictCode',
        value: 'dictName',
        label: 'dictName'
      },
      rules: [
        {
          required: true, message: '结论为拒收，请输入处理原因！', match: [
            { key: 'stockStatus', value: '3' }
          ]
        }
      ]
    },
    {
      type: 'input',
      width: 80,
      title: '不足原因补充',
      dataIndex: 'rcvrEason3',
      key: 'rcvrEason3',
      // required: true,
    },
    {
      width: 180,
      title: 'asn备注',
      dataIndex: 'asnLineRemark',
      key: 'asnLineRemark',
      // required: true,
    },
    {
      type: 'input',
      width: 180,
      title: '备注',
      dataIndex: 'comments',
      key: 'comments',
      // required: true,
    },
    {
      width: 80,
      title: '批准文号',
      dataIndex: 'approvalNumber',
      key: 'approvalNumber'
    },
    {
      width: 100,
      title: '存储条件',
      dataIndex: 'storeTemperature',
      key: 'storeTemperature'
    },
    {
      title: '存储分类',
      width: 100,
      dataIndex: 'itemHealthStoreCategory',
      key: 'itemHealthStoreCategory'
    },
    {
      title: '库房区域',
      width: 100,
      dataIndex: 'warehouseAreaCategorys',
      key: 'warehouseAreaCategorys'
    },

    {
      title: '区域编号',
      width: 100,
      dataIndex: 'warehouseAreaCodes',
      key: 'warehouseAreaCodes'
    },

    {
      title: '上市许可证持有人',
      width: 120,
      dataIndex: 'itemHealthListedLicensor',
      key: 'itemHealthListedLicensor'
    },
    {
      width: 100,
      title: '注册证号',
      dataIndex: 'itemHealthRegisterNbr',
      key: 'itemHealthRegisterNbr'
    },
    {
      width: 120,
      title: '生产企业许可证号',
      dataIndex: 'itemHealthProduceLicense',
      key: 'itemHealthProduceLicense'
    },
    { width: 80, title: '批次号', dataIndex: 'batchNum', key: 'batchNum' },
    { hidden: true, title: '批次号ID', dataIndex: 'batchId', key: 'batchId' },
    {
      width: 80,
      title: '最小包装数',
      dataIndex: 'pieceConversionRate',
      key: 'pieceConversionRate',
    },
    {
      width: 80,
      title: '是否进口商品',
      dataIndex: 'itemIsImported',
      key: 'itemIsImported',
      customRender(text) {
        return text == '1' ? '是' : '否'
      }
    },
    {
      width: 80,
      title: '是否标准器械',
      dataIndex: 'isInstrument',
      key: 'isInstrument',
      customRender(text) {
        return text == '1' ? '是' : '否'
      }
    },
    {
      title: '是否整散分离', dataIndex: 'isCaseOrPiece', key: 'isCaseOrPiece', width: 90, hidden: true,
      customRender(text) {
        return text == '1' ? '整散分开' : '整散合一'
      }
    },

    {
      ...genDelegationPartySearchModal(2, false, false, null, null, null),
      title: '受托生产企业名称',
      width: 140,
      dataIndex: 'delegationPartyName', key: 'delegationPartyName',
      disabled: [
        {
          $value: true,
          $match: [
            { key: 'isDelegate', value: 0 },
          ]
        },
        { $value: false }
      ],
      rules: [
        {
          required: true, message: '请填写受托生产企业名称', match: [
            { key: 'stockStatus', value: '0' ,group:1},
            { key: 'isDelegate', value: 1,group:1 }
          ]
        }
      ],
      relatedQuery: [
        { key: 'num', target: 'data.num', default: 'rcv_oem_choose' },
        { key: 'mtlOwnerId', target: 'data.params.mtlOwnerId' },
        { origin: true, key: 'oemCondition', target: 'data.params.oemCondition', },
      ],
      multipleKey: [
        { key: 'oemName', target: 'delegationPartyName' },
      ]
    },

    // {
    //   ...genDelegationPartySearchModal(2, false, false, function (res, p) {
    //     let tt = { ...p }
    //     tt.data = {
    //       id: 'rcv_oem_choose',
    //       pageNum: p.data.pageNum,
    //       pageSize: p.data.pageSize,
    //       params: p.data
    //     };
    //     delete tt.data.params.pageSize
    //     delete tt.data.params.pageNum
    //     p.data.mtlOwnerId = paramData.mtlOwnerId;
    //     return tt;
    //   }, function (modalRowData, index) {
    //     if (modalRowData) {
    //       let ref = self.queryRefFromName('list')
    //       // let oldItem = ref.data[index];
    //       // let newItem = {
    //       //   ...oldItem,
    //       // }
    //       ref.data[index].delegationPartyName = modalRowData[0].oemName;
    //     }
    //   }, null, {
    //     search(p1, p2, p3, p4, p5) {
    //       // 
    //       paramData = p5
    //     },
    //   }),
    //   title: '受托生产企业名称',
    //   width: 140,
    //   dataIndex: 'delegationPartyName', key: 'delegationPartyName',
    //   customColumnProps({ value, index, record }) {
    //     if (record.isDelegate === 0) {
    //       return { disabled: true }
    //     } else {
    //       return {}
    //     }
    //   },
    //   // hidden: true,
    // },
  ]
}