import request from "@/utils/request";
import moment from "moment";
import { genDelegationPartySearchModal, genManufacturerLotNumSearchModal, genProxiesCodeSearchModal, genContainerSearchModal } from "../../../../../SearchModal";
import { upcloud_base_item, upcloud_base_wh, upcloud_base_strategy, upcloud_base_common, upcloud_base_wh_inbound } from "@/config/serviceSettings";
function getContainers(parameter) {
  return request({
    method: "post",
    url: upcloud_base_wh + "/container/getPageList",
    data: parameter,
  });
}
let cacheWhAreaCategory = {};
let stockStatusOptions = [
  { label: "收货", value: "0" },
  { label: "拒收", value: "3" },
];
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" },
  { label: "AGV食品库", value: "AGVSP" },
  { label: "AGV器械库", value: "AGVQX" },
  { label: "AGV中药", value: "AGVZY" },
];
// 原订单数量
export const packingColumns = function (self) {
  let pieceQtyConfig = {};
  let stockStatusConfig = {
    type: "select",
    width: 80,
    title: "收货结论",
    dataIndex: "stockStatus",
    key: "stockStatus",
    options: [...stockStatusOptions],
    on: {
      dropdownVisibleChange({ record, index, columnItem }) {
        setTimeout(() => {
          console.log(self.stockStatusOptions);
          const options = self.stockStatusOptions;
          columnItem.options.splice(0, columnItem.options.length, ...options);
        }, 20);
      },
    },
    // relatedClean: ['whAreaCategoryCode'],
    required: true,
  };
  //   if (self.businessData == "int") {
  //     pieceQtyConfig = {
  //       type: "select",
  //       remoteUrl: {
  //         url: upcloud_base_item + "/item",
  //         method: "get",
  //       },
  //       autoLoad: true,
  //       replaceFields: {
  //         value: "conversionRate",
  //         label: "conversionRate",
  //       },
  //       doneBefore(data) {
  //         data.data.list = data.data.baseMtlItemUomConversionsList;
  //       },
  //       relatedQuery: [
  //         { key: "itemId", target: "itemId" },
  //         { key: "uomLevel", target: "uomLevel", default: "3" },
  //       ],
  //       // 取值
  //       multipleKey: [
  //         { key: "uom", target: "uom" },
  //         { key: "itemUomId", target: "itemUomId" },
  //       ],
  //       // 校验
  //       rules: [
  //         {
  //           required: true,
  //           message: "请选择包装数",
  //         },
  //       ],
  //     };
  //   }

  //   if (self.businessData == "kzy") {
  //     console.log(self.businessData, "self.businessDataself.businessData");
  //     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: "isDoubleReceive", dataIndex: "isDoubleReceive", key: "isDoubleReceive" },
    { 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" },
    { hidden: true, title: "tempAsnLineId", dataIndex: "tempAsnLineId", key: "tempAsnLineId" },
    //货位
    // { hidden: true, title: 'locatorId', dataIndex: 'locatorId', key: 'locatorId', },
    // { hidden: true, title: 'locatorCode', dataIndex: 'locatorCode', key: 'locatorCode', },
    { hidden: true, title: "locatorName", dataIndex: "locatorName", key: "locatorName" },
    { hidden: true, title: "是否转换代管", dataIndex: "inventoryOpType", key: "inventoryOpType" },
    { hidden: true, title: "是否异常驳回", dataIndex: "errorResult", key: "errorResult" },

    {
      width: 80,
      title: "是否追溯",
      dataIndex: "isRetrospect",
      key: "isRetrospect",
      customRender(text) {
        return text == "1" ? "是" : "否";
      },
    },
    {
      title: "是否双人验收",
      dataIndex: "itemHealthIsDouble",
      key: "itemHealthIsDouble",
      width: 120,
      customRender(text) {
        return text == "1" ? "是" : "否";
      },
    },
    {
      hidden: true,
      width: 90,
      title: "托盘码放规则",
      dataIndex: "palletPlacedRule",
      key: "palletPlacedRule",
    },
    {
      width: 90,
      title: "商品编码",
      dataIndex: "itemNum",
      key: "itemNum",
    },
    {
      width: 100,
      title: "商品名称",
      dataIndex: "itemName",
      key: "itemName",
    },

    {
      width: 90,
      title: "商品大类",
      dataIndex: "categoryName",
      key: "categoryName",
      value: "",
    },
    //未收件数oddCaseQty*包装数conversionRate+未收零散数oddPieceQty=数量pieceQty
    {
      type: "number",
      width: 80,
      title: "件数",
      dataIndex: "oddCaseQty",
      key: "oddCaseQty",
      precision: 0,
      min: 0,
      rules: [
        {
          type: "number",
          validator: function (rule, value, callback, source, options) {
            if (!source.locatorCode) return true;
            console.log(rule, value, callback, source, options);
            if ((+source.tempQty || 0) < (+source.pieceQty || 0)) {
              self.$message.warning("当前数量总数超过代管库位库存数量，请检查数量！！");
              source.oddCaseQty = 0;
              // return false;
            }
            return true;
          },
        },
      ],
      on: {
        async change(value) {
          let record = value.record;
          let dataSource = "0";
          let category = "RcvAreaCategoryChoose";
          let { categoryName, warehouseId, mtlOwnerId, enterpriseId, oddPieceQty, oddCaseQty, stockStatus, isAgv } = record;
          console.log(arguments);
          let res = await self.strategyExecuteFun({
            dataSource,
            categoryName,
            warehouseId,
            mtlOwnerId,
            category,
            enterpriseId,
            oddPieceQty,
            oddCaseQty,
            stockStatus,
            isAgv,
          });
          console.log(res);
          let o = self.getwhAreaCategoryCode(res, record);
          console.log(o, o.includes(record.whAreaCategoryCode));
          if (record.whAreaCategoryCode && !o.includes(record.whAreaCategoryCode)) {
            self.$message.warning("当前行数量发生变化,请重新选择库别!");
            record.whAreaCategoryCode = null;
          }
        },
      },
    },
    {
      type: "number",
      width: 80,
      title: "零散数",
      dataIndex: "oddPieceQty",
      key: "oddPieceQty",
      min: 0,
      rules: [
        {
          type: "number",
          validator: function (rule, value, callback, source, options) {
            if (!source.locatorCode) return true;
            console.log(rule, value, callback, source, options);
            if ((+source.tempQty || 0) < (+source.pieceQty || 0)) {
              self.$message.warning("当前数量总数超过代管库位库存数量，请检查数量！！");
              return false;
            }
            return true;
          },
          message: "当前数量总数超过代管库位库存数量，请检查数量！！",
        },
      ],
      on: {
        async change(value) {
          let record = value.record;
          let dataSource = "0";
          let category = "RcvAreaCategoryChoose";
          let { categoryName, warehouseId, mtlOwnerId, enterpriseId, oddPieceQty, oddCaseQty, stockStatus, isAgv } = record;
          console.log(arguments);
          let res = await self.strategyExecuteFun({
            dataSource,
            categoryName,
            warehouseId,
            mtlOwnerId,
            category,
            enterpriseId,
            oddPieceQty,
            oddCaseQty,
            stockStatus,
            isAgv,
          });
          console.log(res);
          let o = self.getwhAreaCategoryCode(res, record);
          console.log(o, o.includes(record.whAreaCategoryCode));
          if (record.whAreaCategoryCode && !o.includes(record.whAreaCategoryCode)) {
            self.$message.warning("当前行数量发生变化,请重新选择库别!");
            record.whAreaCategoryCode = null;
          }
        },
      },
    },
    {
      width: 80,
      title: "数量",
      dataIndex: "pieceQty",
      key: "pieceQty",
    },
    {
      hidden: true,
      width: 80,
      title: "零时存放代管库位数量",
      dataIndex: "tempQty",
      key: "tempQty",
    },
    {
      width: 150,
      title: "包装数",
      dataIndex: "conversionRate",
      key: "conversionRate",
      ...pieceQtyConfig,
    },
    // {
    //   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" },
    //         { key: "stockStatus", value: "2" },
    //       ],
    //     },
    //   ],
    // },
    // { 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" },
            { key: "stockStatus", value: "2" },
          ],
        },
      ],
      disabled: [
        {
          $value: true,
          $match: [{ key: "inventoryOpType", value: 0 }],
        },
        { $value: false },
      ],
    },
    {
      //   hidden: true,
      //   type: "p-date-picker",
      type: "input",
      width: 120,
      title: "显示生产日期",
      dataIndex: "showProductionDate",
      key: "showProductionDate",
      max: 8,
      rules: [
        {
          required: true,
          message: "请输入显示生产日期",
          match: [
            { key: "stockStatus", value: "0" },
            { key: "stockStatus", value: "3" },
            { key: "stockStatus", value: "2" },
          ],
        },
      ],
      disabled: [
        {
          $value: true,
          $match: [{ key: "inventoryOpType", value: 0 }],
        },
        { $value: false },
      ],
    },
    {
      //   hidden: true,
      //   type: "p-date-picker",
      type: "input",
      width: 120,
      title: "显示有效期至",
      dataIndex: "showExpiredDate",
      key: "showExpiredDate",
      max: 8,
      rules: [
        {
          required: true,
          message: "请输入显示生产日期",
          match: [
            { key: "stockStatus", value: "0" },
            { key: "stockStatus", value: "3" },
            { key: "stockStatus", value: "2" },
          ],
        },
      ],
      disabled: [
        {
          $value: true,
          $match: [{ key: "inventoryOpType", value: 0 }],
        },
        { $value: false },
      ],
    },
    { 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" },
            { key: "stockStatus", value: "2" },
          ],
        },
      ],
      disabled: [
        {
          $value: true,
          $match: [{ key: "inventoryOpType", value: 0 }],
        },
        { $value: false },
      ],
    },
    stockStatusConfig,
    // {
    //   type: "input",
    //   width: 80,
    //   title: "拼箱号",
    //   dataIndex: "locNo",
    //   key: "locNo",
    // },
    {
      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: "" },
    {
      hidden: true,
      width: 100,
      title: "包装数ID",
      dataIndex: "itemUomId",
      key: "itemUomId",
    },
    {
      hidden: true,
      width: 100,
      title: "单位包装数量",
      dataIndex: "qty",
      key: "qty",
    },
    {
      width: 80,
      title: "含税单价",
      dataIndex: "piecePriceTi",
      key: "piecePriceTi",
    },
    {
      width: 80,
      title: "不含税单价",
      dataIndex: "piecePriceTe",
      key: "piecePriceTe",
      hidden: true,
    },
    { hidden: true, width: 120, title: "refType", dataIndex: "refType", key: "refType" },
    { 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" },
    {
      ...genContainerSearchModal(2, false, false, null, null),
      remoteUrl: { url: upcloud_base_wh + "/container/getPageList", method: "post" },
      title: "容器号",
      dataIndex: "containerCode",
      key: "containerCode",
      targetValueKey: "containerCode",
    //   required: true,
      rowKey: "containerId",
      width: 150,
      targetClick: false,
      rules: [
        {
          required: true,
          message: "请选择容器号",
          match: [
            { key: "stockStatus", value: "0" },
            // { key: "stockStatus", value: "3" },
            { key: "stockStatus", value: "2" },
          ],
        },
      ],
      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: "itemIsPieceOrCase",
    //   dataIndex: "itemIsPieceOrCase",
    //   transform(record) {
    //     console.log(record);
    //     return 1;
    //   },
    // },
    // {
    //   type: "select",
    //   title: "库别",
    //   required: true,
    //   dataIndex: "whAreaCategory",
    //   key: "whAreaCategory",
    //   destroyOptions: true,
    //   width: 100,
    //   allowClear: true,
    //   //   options: [...whAreaCategoryCodeList],
    //   remoteUrl: { url: upcloud_base_wh_inbound + "/rcvLines/v1.0/byConditionToAreaCategory", method: "post" },
    //   replaceFields: {
    //     value: "value",
    //     label: "label",
    //   },
    //   relatedQuery: [
    //     { key: "warehouseId", target: "warehouseId" },
    //     { key: "itemId", target: "itemId" },
    //     { key: "oddCaseQty", target: "oddCaseQty" },
    //     { key: "oddPieceQty", target: "oddPieceQty" },
    //     { key: "pieceQty", target: "pieceQty" },
    //     { key: "mtlOwnerId", target: "mtlOwnerId" },
    //     { key: "conversionRate", target: "conversionRate" },
    //     { key: "rcvType", target: "rcvType" },
    //     {
    //       key: "isCaseOrPiece",
    //       target: "itemIsPieceOrCase",
    //       transform(value, record) {
    //         console.log(value, record);
    //         let oddCaseQty = record.oddCaseQty;
    //         let oddPieceQty = record.oddPieceQty;
    //         let v;
    //         if (!oddCaseQty && oddPieceQty) v = 1;
    //         if (oddCaseQty && !oddPieceQty) v = 0;
    //         if (oddCaseQty && oddPieceQty) v = 2;
    //         return v;
    //       },
    //     },
    //     { key: "refType", target: "refType", default: "0" },
    //   ],

    //   doneBefore(data) {
    //     console.log(data);
    //     console.log(arguments);
    //     return {
    //       success: true,
    //       message: "查询成功",
    //       code: "200",
    //       data: data.data.areaCategoryList || [],
    //     };
    //   },
    //   // open: false,
    //   showArrow: false,
    //   // dropdownClassName: 'xxx-xxx-xxx',
    //   on: {
    //     dropdownVisibleChange({ record, index, columnItem }) {
    //       // if (open == false) return;
    //       //   setTimeout(() => {
    //       //     let main = self.detailData.main;
    //       //     let caseQty = +record.oddCaseQty || 0;
    //       //     let newOptions = null;
    //       //     newOptions = getOption(record, main, whAreaCategoryCodeList, caseQty, self);
    //       //     columnItem.options.splice(0, columnItem.options.length, ...newOptions);
    //       //   }, 20);
    //     },
    //   },
    // },

    {
      type: "input",
      title: "isAgv",
      dataIndex: "isAgv",
      hidden: true,
    },
    {
      type: "select",
      title: "库别",
      dataIndex: "whAreaCategoryCode",
      key: "whAreaCategoryCode",
      width: 100,
      // required: true,
      rules: [
        {
          required: true,
          message: "请选择库别",
          match: [
            { key: "stockStatus", value: "0" },
          ],
        },
      ],
      allowClear: true,
      destroyOptions: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=wms_storeroom", method: "post" },
      relatedQuery: [
        // categoryName
        // dataSource
        // { key: "dataSource", target: "dataSource", default: "0" },
        { key: "categoryId", target: "categoryId" },
        // { key: "warehouseId", target: "warehouseId" },
        // { key: "mtlOwnerId", target: "cargoOwnerId" },
        // { key: "category", target: "category", default: "RcvAreaCategoryChoose" },
        // { key: "enterpriseId", target: "enterpriseId", transform: () => self.$store.state.user.info.enterpriseId },
        // { key: "oddPieceQty", target: "oddPieceQty" },
        // { key: "oddCaseQty", target: "oddCaseQty" },
        // { key: "stockStatus1", target: "stockStatus1" },
        // { key: "stockStatus", target: "stockStatus" },
        // { key: "isCaseOrPiece", target: "isCaseOrPiece" },
        // { key: "categoryGroup", target: "categoryGroup" },
        // { key: "isAgv", target: "isAgv" },
      ],
      remoteBefore(vNode, params) {
        //   const {dataSource ,categoryName} = params.params;

        // 缓存入参
        cacheWhAreaCategory = params.data;
        console.log(params,'params');
        return params;
      },
      doneBefore(res) {
        // console.log(arguments);
        console.log(res,'res');
        // if (!res.success) {
        //   self.$message.error("件数、零散数、收货结论、质量状态不能为空!");
        //   return { success: true, data: [] };
        // }
        try {
          const params = cacheWhAreaCategory;
          const matchObj = {
            // oddPieceQty: String(params.oddPieceQty),
            // oddCaseQty: String(params.oddCaseQty),
            // stockStatus: String(params.stockStatus),
            categoryId: String(params.categoryId),
            // dataSource: String(params.dataSource),
            // isAgv: String(params.isAgv),
          };
          // console.log(matchObj);
          // let hitRule = res.data.rules.find((rule) => {
          //   const { matchFactors } = rule;
          //   if (matchFactors.length === 0) return false;
          //   return rule.matchFactors.every((matchFactor) => {
          //     const { fieldName, matchType, value } = matchFactor;
          //     if (matchType === "equal") {
          //       return matchObj[fieldName] === value;
          //     }
          //     if (matchType === "in") {
          //       return value.split(",").includes(matchObj[fieldName]);
          //       // if (fieldName == "categoryName") {
          //       //   value.split(",").some((i) => matchObj.categoryName.indexOf(i) > -1);
          //       // } else {
          //       //   return value.split(",").includes(matchObj[fieldName]);
          //       // }
          //     }
          //     if (matchType === "gt") {
          //       return matchObj[fieldName] > value;
          //     }
          //     if (matchType === "lt") {
          //       return matchObj[fieldName] < value;
          //     }
          //   });
          // });
          // console.log("hitRule", hitRule);
          // if (!hitRule) {
          //   console.error("没有命中的规则");
          //   resolve([]);
          // }
          //   let options = hitRule.searchFactors.map((_) => {
          //     const label = whAreaCategoryCodeList.find((item) => item.value === _.value)?.label || _.value;
          //     return { label, value: _.value };
          //   });

          // let value = hitRule.searchFactors.map((_) => _.value)[0].split(",");
          // console.log(value);
          console.log(matchObj,'matchObj');
          let list = res.data.filter(item=>item.isEnable==1)
          if (matchObj.categoryId=='012') {
            list = res.data.filter(item=>item.isEnable==1 && item.dictCode=='WLK')
          }
          let options = list.map((i) => {
            const label = whAreaCategoryCodeList.find((v) => {
              if (v.value == i.dictCode) {
                return v.label;
              } else {
                return null;
              }
            });
            return { label: label.label, value: i.dictCode };
          });

          console.log("options", options);
          return { success: true, data: options };
        } catch (e) {
          return { success: true, data: [] };
        }
      },
    },
    { hidden: true, title: "大包装隐藏字段", dataIndex: "maxSu", key: "maxSu" },
    { hidden: true, title: "中包装隐藏字段", dataIndex: "middleSu", key: "middleSu" },
    { hidden: true, title: "小包装隐藏字段", dataIndex: "minSu", key: "minSu" },
    { hidden: true, title: "拆分标识隐藏字段", dataIndex: "refAsnLineId", key: "refAsnLineId" },
    { hidden: true, title: "批次号ID", dataIndex: "batchId", key: "batchId" },
    { hidden: true, title: "代管库位ID", dataIndex: "hostLocatorId", key: "hostLocatorId" },

    { hidden: true, type: "input", title: "代管库位", dataIndex: "locatorCode", key: "locatorCode", width: 150 },
    {
      hidden: true,
      width: 80,
      title: "库房编号",
      dataIndex: "warehouseRoomCodes",
      key: "warehouseRoomCodes",
      // required: true,
    },
    {
      hidden: true,
      type: "input",
      width: 80,
      title: "货格",
      dataIndex: "goodsCaseCode",
      key: "goodsCaseCode",
      // required: true,
    },

    {
      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" },
            { key: "whAreaCategoryCode", value: "NHK" },
          ],
        },
      ],
    },
    {
      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" },
            { key: "whAreaCategoryCode", value: "NHK" },
          ],
        },
      ],
    },
    {
      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: 200,
      title: "批准文号",
      dataIndex: "approvalNumber",
      key: "approvalNumber",
    },
    {
      width: 100,
      title: "存储条件",
      dataIndex: "storeTemperature",
      key: "storeTemperature",
    },
    {
      title: "存储分类",
      width: 100,
      dataIndex: "itemHealthStoreCategory",
      key: "itemHealthStoreCategory",
    },
    {
      hidden: true,
      title: "库房区域",
      width: 100,
      dataIndex: "warehouseAreaCategorys",
      key: "warehouseAreaCategorys",
    },
    // {
    //   title: "库别隐藏字段",
    //   width: 100,
    //   dataIndex: "whAreaCategoryCode",
    //   key: "whAreaCategoryCode",
    //   hidden: true,
    // },

    {
      hidden: true,
      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" },
    {
      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" ? "整散分开" : "整散合一";
      },
    },
    {
      hidden: true,
      ...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" }],
    },
  ];
};

function getOption(record, main, whAreaCategoryCodeList, caseQty, self) {
  console.log(self, "self.businessData");
  console.log(record, main, whAreaCategoryCodeList, caseQty, self, "3333333");
  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") {
    //代管类型只能入代管库
    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 (self.businessData == "int" && t.value == "LTK" && caseQty > 0 && oddPieceQty == 0) {
        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;
}
