import { genCloumnContainer } from "../../../../config/genColumnContainer";
import moment from "moment";
import request from '@/utils/request'
import {
  genDelegationPartySearchModal,
  genManufacturerLotNumSearchModal,
  genProxiesCodeSearchModal,
  genContainerSearchModal
} from "../../../../../SearchModal";
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
  })
}

export const packingColumns = function (self) {
  let stockStatusConfig =  {
    type: 'select', width: 100, title: '收货结论', dataIndex: 'stockStatus', key: 'stockStatus', required: true,
    options: [
      // 合格0/不合格1/待处理2/拒绝3
      {
        value: '0', label: '收货', match: [
          { key: 'batchExpiredDate', value: '${*}', group: 2 }
        ]
      },
      {
        value: '2', label: '待处理', match: [
          //类型为2并且过效期，只能显示待处理
          { key: 'rcvType', value: '2', group: 1 },
          { key: 'batchExpiredDate', value: '${Date_GT_Now()}', group: 1 },
          { key: 'rcvType', value: '${!2}', group: 2 },
          { key: 'batchExpiredDate', value: '${*}', group: 2 }
        ]
      },
      { 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: '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: 'customerId', dataIndex: 'customerId', key: 'customerId' },
    { hidden: true, title: 'customerName', dataIndex: 'customerName', key: 'customerName' },
    { hidden: true, title: 'customerNum', dataIndex: 'customerNum', key: 'customerNum' },
    { hidden: true, title: 'bizUser', dataIndex: 'bizUser', key: 'bizUser' },
    { 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: 'pieceConversionRate', dataIndex: 'pieceConversionRate', key: 'pieceConversionRate' },
    { 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: "piecePriceTe", dataIndex: "piecePriceTe", key: "piecePriceTe" },
    { hidden: true, title: "piecePriceTi", dataIndex: "piecePriceTi", key: "piecePriceTi" },
    { hidden: true, title: "categoryId", dataIndex: "categoryId", key: "categoryId" },
    { hidden: true, title: 'mtlOwnerId', dataIndex: 'mtlOwnerId', key: 'mtlOwnerId' },
    { 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: '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' ? '是' : '否'
      }
    },
    { width: 90, title: '商品编码', dataIndex: 'itemNum', key: 'itemNum' },
    { hidden: true, width: 90, title: '商品条码', dataIndex: 'barCode', key: 'barCode' },
    { width: 100, title: '商品名称', dataIndex: 'itemName', key: 'itemName', },
    { hidden: true, title: "商品助记码", dataIndex: "itemZjm", key: "itemZjm" },
    {
      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' },

    { width: 140, title: '商品规格', dataIndex: 'specification', key: 'specification', },
    { width: 90, title: '商品大类', dataIndex: 'categoryName', key: 'categoryName', value: '' },
    { title: "数量", dataIndex: "pieceQty", key: "pieceQty", width: 120, },
    { width: 120, title: '显示生产日期', dataIndex: 'showProductionDate', key: 'showProductionDate', max: 8, },
    { width: 120, title: '显示有效期至', dataIndex: 'showExpiredDate', key: 'showExpiredDate', max: 8, },
    { width: 120, title: '生产日期', dataIndex: 'batchProductionDate', key: 'batchProductionDate', },
    { width: 120, title: '有效期至', dataIndex: 'batchExpiredDate', key: 'batchExpiredDate' },
    // {
    //   type: 'select', width: 100, title: '收货结论', dataIndex: 'stockStatus', key: 'stockStatus', required: true,
    //   options: [
    //     // 合格0/不合格1/待处理2/拒绝3
    //     {
    //       value: '0', label: '收货', match: [
    //         { key: 'batchExpiredDate', value: '${*}', group: 2 }
    //       ]
    //     },
    //     {
    //       value: '2', label: '待处理', match: [
    //         //类型为2并且过效期，只能显示待处理
    //         { key: 'rcvType', value: '2', group: 1 },
    //         { key: 'batchExpiredDate', value: '${Date_GT_Now()}', group: 1 },
    //         { key: 'rcvType', value: '${!2}', group: 2 },
    //         { key: 'batchExpiredDate', value: '${*}', group: 2 }
    //       ]
    //     },
    //     { value: '3', label: '拒收',}
    //   ],
    // },
    stockStatusConfig,
    { width: 120, title: '单据编号', dataIndex: 'asnAutoBillNum', key: 'asnAutoBillNum' },
    { width: 80, title: '生产厂家', dataIndex: 'manufacturerName', key: 'manufacturerName' },
    { width: 80, title: '产地', dataIndex: 'productionPlace', key: 'productionPlace' },
    { width: 90, title: '包装单位', dataIndex: 'uom', key: 'uom', value: '', },
    { width: 100, title: '包装数量', dataIndex: 'conversionRate', key: 'conversionRate', },
    { title: "上市许可持有人", dataIndex: "itemHealthListedLicensor", key: "itemHealthListedLicensor", width: 120, },
    { width: 120, title: '质检报告单号', dataIndex: 'rqcReportNo', key: 'rqcReportNo' },
    { width: 80, title: '件数', dataIndex: 'oddCaseQty', key: 'oddCaseQty', },
    { width: 80, title: '零散数', dataIndex: 'oddPieceQty', key: 'oddPieceQty', },
    { title: "存储条件", dataIndex: "storeTemperature", key: "storeTemperature" },
    { title: "采购审核意见", dataIndex: "poAuditOpinion", key: "poAuditOpinion", width: 120, },
    { title: "退货原因", dataIndex: "rcvRtReason", key: "rcvRtReason", width: 120, },
    { title: "单价(含税)", dataIndex: "piecePriceTi", key: "piecePriceTi" },
    { hidden: true, width: 100, title: '单位包装数量', dataIndex: 'qty', key: 'qty', },
    { width: 80, title: '存储分类', dataIndex: 'itemHealthStoreCategory', key: 'itemHealthStoreCategory' },
    {
      title: '是否整散分离', dataIndex: 'isCaseOrPiece', key: 'isCaseOrPiece', width: 90, hidden: true,
      customRender(text) { return text == '1' ? '整散分开' : '整散合一' }
    },
    { width: 100, title: '库区列表', dataIndex: 'warehouseAreaCodes', key: 'warehouseAreaCodes', },
    { width: 100, title: '库别列表', dataIndex: 'warehouseAreaCategorys', key: 'warehouseAreaCategorys', },
    { width: 80, title: '批号', dataIndex: 'manufacturerLotNum', key: 'manufacturerLotNum', },
    { hidden: true, width: 80, title: '批次', dataIndex: 'batchId', key: 'batchId', },
    { width: 80, title: '批次号', dataIndex: 'batchNum', key: 'batchNum', },
    { width: 80, title: '有效期天数', dataIndex: 'shelfLifeDays', key: 'shelfLifeDays' },
    // 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;
            }
          });
        }
      }
    },
    { type: 'input', width: 80, title: '货格编码', dataIndex: 'goodsCaseCode', key: 'goodsCaseCode', },
    {
      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: 'stockStatus', value: '2' },
            { key: 'stockStatus', value: '3' }
          ]
        }
      ]
    },


    {
      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: '2' },
            { 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: '2' },
            { key: 'stockStatus', value: '3' }
          ]
        }
      ]
    },
    { type: 'input', width: 80, title: '不足原因补充', dataIndex: 'rcvrEason3', key: 'rcvrEason3', },
    { type: 'input', width: 80, title: '退货说明', dataIndex: 'returnExplain', key: 'returnExplain', },
    {
      hidden: true, type: 'input', cusType: 'autoComplete', title: "退货类型", dataIndex: "returnType", width: 140,
      scopedSlots: { customRender: 'returnType' }
    },
    { hidden: true, type: 'input', width: 180, title: '退货时冷藏状态', dataIndex: 'returnColdQuality', key: 'returnColdQuality', },
    { hidden: true, type: 'input', width: 180, title: '退货时质量状态', dataIndex: 'returnInfo', key: 'returnInfo' },
    { type: 'input', width: 180, title: '备注', dataIndex: 'comments', key: 'comments', },
    { width: 80, title: '批准文号', dataIndex: 'approvalNumber', key: 'approvalNumber' },
    // {  title: '存储条件', width: 80, dataIndex: 'storeTemperature', key: 'storeTemperature' },
    { width: 80, title: '注册证号', dataIndex: 'itemHealthRegisterNbr', key: 'itemHealthRegisterNbr' },
    { width: 120, title: '生产企业许可', dataIndex: 'itemHealthProduceLicense', key: 'itemHealthProduceLicense' },

    {
      width: 80, title: '是否进口商品', dataIndex: 'itemIsImported', key: 'itemIsImported',
      customRender(text) {
        return text == '1' ? '是' : '否'
      }
    }
  ]
}