import { genCloumnContainer } from "../../../../../config/genColumnContainer";
import {upcloud_base_common,upcloud_base_wh_inbound,upcloud_base_wh} from "@/config/serviceSettings";
import { genContainerSearchModal } from "../../../../../../SearchModal";
import request from '@/utils/request'

let whAreaCategoryCodeList = [
    { label: "立体库", value: "LTK" },
    { label: "整件库", value: "ZJK" },
    { label: "零货库", value: "LHK" },
    { label: "直调库", value: "ZDK" },
    { label: "整散合一库", value: "HYK" },
    { label: "待处理库", value: "CLK" },
    { label: "代管库", value: "DGK" },
    { label: "越库区", value: "YKQ" },
    { label: "退货库", value: "THK" },
    { label: "冷库", value: "LK" },
    { label: "AGV库", value: "AGVK" },
    { label: "AGV托盘库", value: "AGVZ" },
    { label: "AGV货架库", value: "AGVL" },
    { label: "密集库", value: "MJK" },
    { label: "备货库", value: "BHK" },
    { label: "不合格库", value: "NHK" },
    { label: "盘点库", value: "PDK" },
    { label: "整散合一库", value: "HYK" },
    { label: "调整库", value: "ADJ" },
    { label: "直通库", value: "ZTK" },
    { label: "高架库", value: "GJK" },
    { label: "中药库", value: "ZYK" },
    { label: "物料库", value: "WLK" },
  ];
let parameter = {}

function getContainers(parameter) {
  return request({
    method: 'post',
    url: upcloud_base_wh + '/container/getPageList',
    data: parameter
  })
}
export const packingColumns = function (self) { 
  let stockStatusConfig ={
    type: "select", 
    title: "验收结论", 
    dataIndex: "qcResult1", 
    width: 140, 
    options: [
      // 合格0/不合格1/待处理2/拒绝3 
      { value: '0', label: '合格' },
      // { value: '1', label: '不合格' },
      // { value: '2', label: '待处理' },
      { value: '3', label: '拒收' }
    ],
  }
  if (self.businessData == 'kzy') {
    console.log(self.businessData,'self.businessDataself.businessData');
    stockStatusConfig = {
      type: 'select',
      width: 80,
      title: '收货结论',
      autoLoad: true, 
      destroyOptions:true,
      dataIndex: 'qcResult1',
      key: 'qcResult1', 
      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' },
        { key: 'stockStatus', target: 'stockStatus' },
      ],
      // 取值
      multipleKey: [
        { key: 'code', target: 'code' },
        { key: 'name', target: 'name' },
      ],
      required: true,
    }
  } 

return [
  { hidden: true, title: 'taskRqcId', dataIndex: 'taskRqcId', key: 'taskRqcId' },
  { hidden: true, title: 'itemId', dataIndex: 'itemId', key: 'itemId' },
  { hidden: true, title: 'batchId', dataIndex: 'batchId', key: 'batchId' },
  { hidden: true, title: 'mtlOwnerId', dataIndex: 'mtlOwnerId', key: 'mtlOwnerId' },
  { hidden: true, title: 'mtlOwnerName', dataIndex: 'mtlOwnerName', key: 'mtlOwnerName' },
  { hidden: true, title: 'warehouseId', dataIndex: 'warehouseId', key: 'warehouseId' },
  { hidden: true, title: 'rcvHeaderId', dataIndex: 'rcvHeaderId', key: 'rcvHeaderId' },
  { hidden: true, title: 'asnHeaderId', dataIndex: 'asnHeaderId', key: 'asnHeaderId' },
  { hidden: true, title: 'srcAutoBillNum', dataIndex: 'srcAutoBillNum', key: 'srcAutoBillNum' },
  { hidden: true, title: "srcLineId", dataIndex: "srcLineId", key: "srcLineId" },
  { hidden: true, title: 'srcBillTyp', dataIndex: 'srcBillTyp', key: 'srcBillTyp' },
  { hidden: true, title: 'srcHeaderId', dataIndex: 'srcHeaderId', key: 'srcHeaderId' },
  { hidden: true, title: "asnLineId", dataIndex: "asnLineId", key: "asnLineId" },
  { hidden: true, title: "双人业务类型(0:正常 1：双人验收，默认正常)", dataIndex: "itemHealthIsDouble", key: "itemHealthIsDouble" },
  { hidden: true, title: 'rcvLineId', dataIndex: 'rcvLineId', key: 'rcvLineId' },
  { hidden: true, title: "categoryId", dataIndex: "categoryId", key: "categoryId" },
  { hidden: true, title: "piecePriceTe", dataIndex: "piecePriceTe", key: "piecePriceTe" },
  { hidden: true, title: "piecePriceTi", dataIndex: "piecePriceTi", key: "piecePriceTi" },
  { hidden: true, title: '商品助记码', dataIndex: 'itemZjm', key: 'itemZjm', },
  { hidden: true, title: '单位助记码', dataIndex: 'shipperZjm', key: 'shipperZjm', },
  { hidden: true, title: "质检次数", dataIndex: "times", key: "times" },
  { hidden: true, title: "rcvType", dataIndex: "rcvType", key: "rcvType" },

  {
    title: "是否追溯", dataIndex: "isSupervise", key: "isSupervise", width: 120, 
    customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  // (0:正常 1：双人验收，默认正常)
  {
    title: "是否双人验收", dataIndex: "itemHealthIsDouble", key: "itemHealthIsDouble", width: 120, 
    customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  { title: '存储分类', dataIndex: 'itemHealthStoreCategory', key: 'itemHealthStoreCategory', width: 100,  },
  { title: "存储条件", dataIndex: "storeTemperature", key: "storeTemperature", width: 120,  },
  { title: "验收温度", dataIndex: "rqcTemperature", key: "rqcTemperature", width: 120, type: "input" },
  { title: "库房编号", dataIndex: "warehouseRoomCodes", key: "warehouseRoomCodes", width: 120,  },
  { title: "商品编号", dataIndex: "itemNum", key: "itemNum", width: 120,  },
  { hidden: true, title: "商品条码", dataIndex: "itemBarCode", key: "itemBarCode", width: 100,  },
  { title: "商品名称", dataIndex: "itemName", key: "itemName", width: 160,  },
  { title: "商品规格", dataIndex: "specification", key: "specification", width: 120,  },
  { title: "商品大类", dataIndex: "categoryName", key: "categoryName", width: 120,  },
  { title: "生产厂家", dataIndex: "manufacturerName", key: "manufacturerName", width: 120,  },
  { title: "产地", dataIndex: "productionPlace", key: "productionPlace", width: 60,  },
  {
    title: "是否受托生产", dataIndex: "isDelegate", key: "isDelegate", width: 120, 
    customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  { title: "受托生产企业", dataIndex: "delegationPartyName", key: "delegationPartyName", width: 120,  },
  { title: "批准文号", dataIndex: "approvalNumber", key: "approvalNumber", width: 120,  },
  { title: "上市许可持有人", dataIndex: "itemHealthListedLicensor", key: "itemHealthListedLicensor", width: 120,  },
  { title: "注册证号", dataIndex: "itemHealthRegisterNbr", key: "itemHealthRegisterNbr", width: 120,  },
  { title: "生产企业许可证号", dataIndex: "itemHealthProduceLicense", key: "itemHealthProduceLicense", width: 120,  },

  { title: "包装单位", dataIndex: "uom", key: "uom", width: 120,  },
  // {
  //   title: "包装单位", dataIndex: "uom", key: "uom", width: 80, 
  //   remoteUrl: { url: upcloud_base_common+'/dict/listbytype?dicttypeCodes=pub_itempack_packcode', method: 'post' },
  //   replaceFields: {
  //     value: 'dictCode',
  //     label: 'dictName'
  //   }
  // },
  { title: "包装数", dataIndex: "conversionRate", key: "conversionRate", width: 120,  },
  { title: "件数", dataIndex: "caseQty", key: "caseQty", width: 80,  },
  { title: "零散数", dataIndex: "oddPieceQty", key: "oddPieceQty", width: 80,  },
  { title: "数量", dataIndex: "pieceQty", key: "pieceQty", width: 120,  },
  { hidden: true, title: "建筑物", dataIndex: "bldgId", key: "bldgId", width: 120,  },
  { hidden: true, title: "建筑物", dataIndex: "bldgCode", key: "bldgCode", width: 120,  },
  { title: "建筑物", dataIndex: "bldgName", key: "bldgName", width: 120,  },
  { title: "批号", dataIndex: "manufacturerLotNum", key: "manufacturerLotNum", width: 120,  },
  { title: "显示生产日期", dataIndex: "showProductionDate", key: "showProductionDate", width: 120,  },
  { title: "显示有效期至", dataIndex: "showExpiredDate", key: "showExpiredDate", width: 120,  },
  { title: "生产日期", dataIndex: "batchProductionDate", key: "batchProductionDate", width: 120,  },
  { title: '有效期至', dataIndex: 'batchExpiredDate', key: 'batchExpiredDate', width: 120,  },
  { hidden: true, title: '判断报告单', dataIndex: 'rqcReportNoNew', key: 'rqcReportNoNew', width: 120,  },
  {
    type: 'input', width: 120, title: '质检报告单号', dataIndex: 'rqcReportNo', key: 'rqcReportNo',
    disabled: [
      {
        $value: true,
        $match: [
          { key: 'rqcReportNoNew', value: '${*}' },
        ]
      },
      { $value: false }
    ],
  },
  // 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;
          }
        });
      }
    }
  },
  { title: "货格", dataIndex: "goodsCaseCode", key: "goodsCaseCode", width: 60, type: "input" },
  { title: "区域编号", dataIndex: "warehouseAreaCodes", key: "warehouseAreaCodes", width: 120,  },
  stockStatusConfig,
  {
    type: "select", title: "处理原因", dataIndex: "handlingReason", width: 140, allowClear: true,
    // scopedSlots: { customRender: 'handlingReason' }
    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: 'qcResult1', value: '2' },
          { key: 'qcResult1', value: '3' }
        ]
      }
    ]
  },
  {
    type: "select", title: "不足原因", dataIndex: "defReason", width: 140, allowClear: true,
    // scopedSlots: { customRender: 'defReason' }
    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: 'qcResult1', value: '2' },
          { key: 'qcResult1', value: '3' }
        ]
      }
    ]
  },
  {
    type: "select", title: "处理措施", dataIndex: "handlingMeasure", width: 140, allowClear: true,
    // scopedSlots: { customRender: 'handlingMeasure' }
    autoLoad: true, remoteUrl: { url: upcloud_base_common+'/dict/listbytype?dicttypeCodes=rcv_handle_measure', method: 'post' },
    replaceFields: {
      // value: 'dictCode',
      value: 'dictName',
      label: 'dictName'
    },
    rules: [
      {
        required: true, message: '结论为拒收或待处理，请输入处理措施！', match: [
          { key: 'qcResult1', value: '2' },
          { key: 'qcResult1', value: '3' }
        ]
      }
    ]
  },
  {
    type: "select", title: "退货类型", dataIndex: "returnType", width: 140, allowClear: true,
    // scopedSlots: { customRender: 'returnType' }
    autoLoad: true, remoteUrl: { url: upcloud_base_common+'/dict/listbytype?dicttypeCodes=rcv_return_type', method: 'post' },
    replaceFields: {
      // value: 'dictCode',
      value: 'dictName',
      label: 'dictName'
    },
  },
  { title: "退货说明", dataIndex: "returnExplain", key: "returnExplain", width: 120, type: "input" },
  {  width: 80, title: '含税单价', dataIndex: 'piecePriceTi', key: 'piecePriceTi', },
  { hidden: true,  width: 80, title: '不含税单价', dataIndex: 'piecePriceTe', key: 'piecePriceTe', },
  {  title: "处理意见", dataIndex: "handlingSuggestion", key: "handlingSuggestion", width: 120, },
  {
    title: "是否已复检", dataIndex: "isRechecked", key: "isRechecked", width: 120,  customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  { title: "复检结果", dataIndex: "reCheckResult", key: "reCheckResult", width: 120,  },
  {
    title: "是否进口", dataIndex: "itemIsImported", key: "itemIsImported", width: 120, 
    customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  {
     width: 80, title: '是否标准器械', dataIndex: 'isInstrument', key: 'isInstrument', customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  
  {
    type: "select", title: "库别", dataIndex: "whAreaCategory", key: "whAreaCategory", width: 100, allowClear: true,
    options: [...whAreaCategoryCodeList],
    showArrow: false,
    on: {
      dropdownVisibleChange({ record, index, columnItem }) {
        setTimeout(() => {
          let main = self.detailData.main;
          let caseQty = +record.oddCaseQty || 0;
          let newOptions = null;
          newOptions = getOption(record, main, whAreaCategoryCodeList, caseQty)
          columnItem.options.splice(0, columnItem.options.length, ...newOptions)
        }, 20);
      }
    }
  },
  { title: "来源数据", dataIndex: "sourceType", key: "sourceType", width: 120,  },
  { hidden: true, title: "要货部门", dataIndex: "needDept", key: "needDept", width: 120,  },
  { hidden: true, title: "收货员", dataIndex: "rcvUser", key: "rcvUser", width: 60,  },
  { hidden: true, title: '有效期天数', dataIndex: 'shelfLifeDays', key: 'shelfLifeDays', width: 80,  },
  { hidden: true, title: "asn备注", dataIndex: "asnLineRemark", key: "asnLineRemark", width: 100,  },
  { hidden: true, title: "备注", dataIndex: "lineRemark", key: "lineRemark", width: 100, type: "input" },
  { hidden: true, title: "单位包装数量", dataIndex: "qty", key: "qty", width: 80,  },
  { hidden: true, title: "质检件数", dataIndex: "qcOddCaseActQty", key: "qcOddCaseActQty", width: 100,  },
  { hidden: true, title: "质检零散数", dataIndex: "qcOddPieceActQty", key: "qcOddPieceActQty", width: 100,  },
  { hidden: true, title: "质检人1", dataIndex: "qcOpcode1", key: "qcOpcode1", width: 120,  },
  { hidden: true, title: "质检人2", dataIndex: "qcOpcode2", key: "qcOpcode2", width: 120,  },
  { hidden: true, title: "批次号", dataIndex: "batchNum", key: "batchNum", width: 120,  },
  { hidden: true, title: "供应商批号", dataIndex: "vendorBatchNum", key: "vendorBatchNum", width: 150,  },

]
}


function getOption(record, main, whAreaCategoryCodeList, caseQty) {
  let newOptions = null;
  var tempData = null
  //销售退回有效期至超过当前日期，库别默认退货库
  tempData = new Date(record.batchExpiredDate)
  if (tempData < new Date()) {
    if (main.rcvType == '2') {
      newOptions = whAreaCategoryCodeList.filter(t => {
        if (t.value == 'THK') return true;
        else return false;
      });
    }
  }
  //代管类型只能入代管库
  else if (main.rcvType == '8' || main.rcvType == '9') {
    console.log(main.rcvType, 'main.rcvType');
    newOptions = whAreaCategoryCodeList.filter(t => {
      if (t.value == 'DGK') return true;
      else return false;
    });
  }
  // 10.整散合一库 只能入整散合一的商品，即整散合一的商品默认并且只能入整散合一库
  else if (record.itemHealthStoreCategory.indexOf('合一') >= 0) {
    newOptions = whAreaCategoryCodeList.filter(t => {
      //不再限制越库区
      if (main.rcvType == '7' && (t.value == 'ZDK' || t.value == 'YKQ')) { return true; }
      if (main.rcvType == '6' && t.value == 'YKQ') { return true; }
      if (t.value == 'HYK' || t.value == 'YKQ') return true;
      else return false;

    });
  }
  // 其它组合
  else {
    // 
    //整件caseQty;
    let oddPieceQty = +record.oddPieceQty || 0;//零散
    newOptions = whAreaCategoryCodeList.filter(t => {
      //特殊单据直调、越库 需单独判断是否整散分开
      // 1.整件库 只能入整散分开的整件
      //不再限制越库区
      if (main.rcvType == '7' && caseQty > 0 && oddPieceQty == 0 && (t.value == 'ZJK' || t.value == 'ZDK' || t.value == 'YKQ')) { return true; }
      if (main.rcvType == '6' && caseQty > 0 && oddPieceQty == 0 && (t.value == 'ZJK' || t.value == 'YKQ')) { return true; }
      // 2.零货库 只能入整散分开的
      if (main.rcvType == '7' && caseQty == 0 && oddPieceQty > 0 && (t.value == 'LHK' || t.value == 'ZDK' || t.value == 'YKQ')) { return true; }
      if (main.rcvType == '6' && caseQty == 0 && oddPieceQty > 0 && (t.value == 'LHK' || t.value == 'YKQ')) { return true; }

      // 14.其他订单判断是否整散，再选择库别
      if (caseQty > 0 && oddPieceQty == 0 && (t.value == 'ZJK' || t.value == 'YKQ')) { return true; }
      if (caseQty == 0 && oddPieceQty > 0 && (t.value == 'LHK' || t.value == 'YKQ')) { return true; }
      // if ((main.rcvType == '6' || main.rcvType == '1' || main.rcvType == '2') && t.value == 'YKQ') { return true; }
      // 11.待处理库 收货结论为不合格的才可以入待处理库
      if (record.qcResult1 == '1' && t.value == 'CLK') { return true; }
      if (t.value == 'YKQ') return true;
      return false;
    });
  }
  return newOptions

}