// import { rcvTypeOptions } from "../../config/options";
import { message } from "ant-design-vue";
import moment from "moment";
import { genWarehouseAuthSearchModal } from "../../../SearchModal";
import { genManufacturerLotNumSearchModal } from "@/views/kzyWMS/SearchModal";
import { handleShowProductionDate, handleShowExpiredDate } from "../../receive/rcvEditor/script/lineHandleLogic";

import { upcloud_base_common } from "@/config/serviceSettings";

let grid = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 },
  span: 6,
};
// 需要增加单位名称筛选条件，对应销售退回单客户，采购入库对应供应商，可通过单位编码，助记码，中文单位名称进行模糊筛选
// shipperZjm,shipperCode,shipperName单位助记码/单位编码/单位名称
export function setFormItems(self) {
  let _this = self;
  return [
    ...genWarehouseAuthSearchModal(1, true, false, null, null, grid),
    { type: "input", name: "cargoOwnerId", label: "货主ID", ...grid, value: "", hidden: true },
    { type: "input", name: "autoBillNum", label: "收货单号", ...grid, value: "" },
    // { type: 'input', name: 'customBillNum', label: '手工单号', ...grid, value: '', },
    { type: "input", name: "shipperName", label: "单位名称", ...grid, value: "" },
    { type: "input", name: "shipperCode", label: "单位编码", ...grid, value: "" },
    { type: "input", name: "shipperZjm", label: "单位助记码", ...grid, value: "" },

    // { type: 'select', name: 'rcvType', label: '入库类型', ...grid, value: '', options: [...rcvTypeOptions], },
    {
      type: "select",
      name: "rcvType",
      label: "入库类型",
      value: "",
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=oms_asn_bill_type", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    {
      type: "buttonGroup",
      ...grid,
      offset: 6,
      style: { textAlign: "center" },
      options: [
        {
          value: "查询",
          icon: "search",
          btnType: "primary",
          // style: "margin-left:10px",
          htmlType: "submit",
        },
        {
          value: "重置",
          icon: "redo",
          style: "margin-left:10px",
          on: {
            click: () => {
              self.$refs.formTarget.form.resetFields();
            },
          },
        },
      ],
    },
  ];
}

export const tableColumnsConfig = [
  { title: "rcvHeaderId", hidden: true, dataIndex: "rcvHeaderId" },
  // { title: 'warehouseId', hidden: true, dataIndex: 'warehouseId', },
  // { title: 'asnHeaderId', hidden: true, dataIndex: 'asnHeaderId', },
  // { title: '客户', dataIndex: 'customerName', className: 'v_top', },
  { hidden: true, title: "lineNum", dataIndex: "lineNum" },
  { hidden: true, title: "操作时间", dataIndex: "executorDate" },
  { hidden: true, title: "操作人", dataIndex: "executorUser" },
  { hidden: true, title: "手工单号", dataIndex: "customBillNum", className: "v_top" },
  { title: "收货单号", dataIndex: "autoBillNum", className: "v_top" },
  { title: "日期", dataIndex: "rcvDate", format: "YYYY-MM-DD HH:mm:ss", showTime: true },
  { title: "货主", dataIndex: "mtlOwnerName", className: "v_top", width: 180 },
  { title: "单位名称", dataIndex: "shipperName", className: "v_top" },
  { title: "仓库", dataIndex: "warehouseName", className: "v_top" },
  { title: "采购员", dataIndex: "buyerMane", className: "v_top" },
  { title: "验收员", dataIndex: "rqcUser", key: "rqcUser" },
  { title: "收货员", dataIndex: "executorUser", className: "v_top" },
  // {
  //   title: '入库类型', dataIndex: 'rcvType', className: 'v_top',
  //   customRender(text) {
  //     let item = rcvTypeOptions.find(t => t.value == text);
  //     if (item) {
  //       return item.title;
  //     } else return '';
  //   }
  // },
  {
    title: "入库类型",
    dataIndex: "rcvType",
    width: 140,
    // scopedSlots: { customRender: 'rcvReason1' }
    autoLoad: true,
    remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=oms_asn_bill_type", method: "post" },
    replaceFields: {
      value: "dictCode",
      // value: 'dictName',
      label: "dictName",
    },
  },
  { title: "备注", dataIndex: "comments", className: "v_top" },
];
// 商品编码，商品名称，商品规格，商品大类（目前商品分类名字段显示，需更改字段名称），
// 生产厂家，产地，批准文号、上市持有许可人、注册证号、生产企业许可证号、
// 包装单位、包装数量、数量、件数、零散数、批号、显示生产日期、显示有效期至、
// 生产日期、有效期至、库房、容器号、存储分类；其余信息不用显示
let options = [
  // 10收货错误/20验收/30复检/40上架）
  { value: "10", title: "收货错误" },
  { value: "20", title: "验收" },
  { value: "30", title: "复检" },
  { value: "40", title: "上架" },
];
export const extendFormItems = [
  { title: "ASN头ID", dataIndex: "asnHeaderId", hidden: true, value: "" },
  { title: "ASN行ID", dataIndex: "asnLineId", hidden: true, value: "" },
  { title: "批次ID", dataIndex: "batchId", hidden: true, value: "" },
  { title: "商品ID", dataIndex: "itemId", hidden: true, value: "" },
  { title: "收货单头ID", dataIndex: "rcvHeaderId", hidden: true, value: "" },
  { title: "收货行头ID", dataIndex: "rcvLineId", hidden: true, value: "" },
  { title: "来源单类型", dataIndex: "srcBillType", hidden: true, value: "" },
  { title: "来源单头ID", dataIndex: "srcHeaderId", hidden: true, value: "" },
  { title: "来源单行ID", dataIndex: "srcLineId", hidden: true, value: "" },

  { title: "商品编码", dataIndex: "itemNum", key: "itemNum", width: 100, value: "" },
  { title: "商品名称", dataIndex: "itemName", key: "itemName", width: 100, value: "" },
  { title: "商品规格", dataIndex: "specification", key: "specification", width: 100, value: "" },
  { title: "商品大类", dataIndex: "categoryName", key: "categoryName", width: 100, value: "" },
  {
    title: "错误发现节点",
    dataIndex: "nodeType",
    key: "nodeType",
    className: "v_top",
    customRender(text) {
      let item = options.find((t) => t.value == text);
      return item ? item.title : "";
    },
  },
  { title: "生产厂家", dataIndex: "manufacturerName", key: "manufacturerName", width: 120 },
  { title: "产地", dataIndex: "productionPlace", key: "productionPlace", width: 60 },
  { 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: "conversionRate", key: "conversionRate", width: 120 },
  { title: "数量", dataIndex: "pieceQty", key: "pieceQty", width: 120 },
  { title: "件数", dataIndex: "caseQty", key: "caseQty", width: 80 },
  { title: "零散数", dataIndex: "oddPieceQty", key: "oddPieceQty", width: 80 },
  { title: "批号", dataIndex: "manufacturerLotNum", key: "manufacturerLotNum", width: 120 },
  { title: "显示生产日期", dataIndex: "showProductionDateOld", key: "showProductionDateOld", width: 120 },
  { title: "显示有效期至", dataIndex: "showExpiredDateOld", key: "showExpiredDateOld", width: 120 },
  { title: "生产日期", dataIndex: "productionDateOld", key: "productionDateOld", width: 120 },
  { title: "有效期至", dataIndex: "expiredDateOld", key: "expiredDateOld", width: 120, type: "txt" },
  { title: "库房", dataIndex: "whRoom", key: "whRoom", width: 100, value: "" },
  { title: "容器号", dataIndex: "containerCode", key: "containerCode", width: 100, value: "" },
  { title: "存储分类", dataIndex: "itemHealthStoreCategory", key: "itemHealthStoreCategory", width: 120 },

  // { title: '仓库名称', dataIndex: 'warehouseName', width: 100, value: '', },
  // { title: '库区名称', dataIndex: 'warehouseAreaName', width: 100, value: '', },
  // { title: '商品条码', dataIndex: 'itemBarCode', width: 100, value: '', },
  // { title: '状态', dataIndex: 'qcStatus', width: 100, value: '', },
  // { title: '数量', dataIndex: 'qty', width: 100, value: '' },
  // { title: '原因', dataIndex: 'reason', width: 100, value: '', },
  // { title: '余量', dataIndex: 'remainQty', width: 100, value: '' },
  // { title: '执行者', dataIndex: 'executor', width: 100, value: '' },
  // { title: "包装单位", dataIndex: "uom", width: 100, value: '' },
  // { title: "错误原因", dataIndex: "comments", width: 100, value: '' },
  // { title: "包装含量", dataIndex: "conversionRate", width: 100, value: '' },
  // { title: "上架件数", dataIndex: "oddCaseQty", width: 100, value: '' },
  // { title: "上架零散数", dataIndex: "oddPieceQty", width: 100, value: '' },
  // { title: "上架最小单位数量", dataIndex: "pieceQty", width: 100, value: '' },
  // { title: "建筑物", dataIndex: "bldgCode", width: 100, value: '' },
  // { title: "库区编号", dataIndex: "warehouseAreaCodes", width: 100, value: '' },
  // { title: "库房", dataIndex: "whRoom", width: 100, value: '' },
  // { title: "货格编码", dataIndex: "goodsCaseCode", width: 100, value: '' },
  // { title: "容器号", dataIndex: "containerCode", width: 100, value: '' },
  // // { title: "%显示货位", dataIndex: "xxxxx16", value: '' },
  // { title: "库别", dataIndex: "whAreaCatagory", value: '', },
  // { title: "拆分人", dataIndex: "spilterName", value: '', },
  // { type: 'txt', title: "批号", dataIndex: "manufacturerLotNum", key: "manufacturerLotNum", width: 60 },
  // { title: '生产日期', dataIndex: 'productionDateOld', customRender(text) { return text ? text.substring(0, 10) : ''; }, value: '', },
  // { title: '有效期至', dataIndex: 'expiredDateOld', customRender(text) { return text ? text.substring(0, 10) : ''; }, value: '', },
  {
    title: "操作",
    key: "operate",
    align: "center",
    fixed: "right",
    scopedSlots: { customRender: "action" },
  },
];
//qtyOld= pieceQty/conversionRate
// 需要显示商品编码，商品名称，商品规格（目前显示不正确），
// 商品大类（无该字段），生产厂家（有字段未显示），
// 产地（无该字段），批准文号、上市持有许可人（无该字段），均不可修改
export function detailFormItems(_this, object) {
  return [
    { type: "input", name: "itemId", label: "商品ID", value: "" },
    { type: "input", name: "warehouseId", label: "warehouseId", value: "", hidden: true },

    { type: "input", name: "mtlOwnerId", label: "mtlOwnerId", value: "", hidden: true },
    { type: "input", name: "batchIdNew", label: "供应商批次号", hidden: true, value: "" },
    { type: "input", name: "lineNum", label: "行号", hidden: true, value: "" },
    { type: "input", name: "vendorBatchNumNew", label: "供应商批次号", hidden: true, value: "" },
    { type: "input", name: "manufacturerLotNumNew", label: "生产商批号", hidden: true, value: "" },
    { type: "input", name: "nodeType", label: "错误发现节点（收货/验收/质检/上架）", hidden: true, value: "" },
    { type: "input", name: "conversionRate", label: "包装含量", hidden: true, value: "" },
    { type: "input", name: "shelfLifeUnit", label: "保质期单位", hidden: true, value: "" },
    { type: "input", name: "shelfLifeDays", label: "保质期天数", hidden: true, value: "" },

    { type: "input", name: "itemNum", label: "商品编号", value: "", disabled: true },
    { type: "input", name: "itemName", label: "商品名称", value: "", disabled: true },
    { type: "input", name: "specification", label: "商品规格", value: "", disabled: true },
    { type: "input", name: "categoryName", label: "商品大类", value: "", disabled: true },
    { type: "input", name: "manufacturerName", label: "生产厂家", value: "", disabled: true },
    { type: "input", name: "productionPlace", label: "产地", value: "", disabled: true },
    { type: "input", name: "approvalNumber", label: "批准文号", value: "", disabled: true },
    { type: "input", name: "itemHealthListedLicensor", label: "上市许可持有人", value: "", disabled: true },
    { type: "input", name: "uom", label: "单位", value: "", disabled: true },
    { type: "input", name: "qtyNew", label: "包装数量", value: "", disabled: true },
    /* 分割 */
    { type: "row", name: "xxxxxxxxxx", label: "", value: "", labelCol: { span: 0 }, span: 24 },
    /* 分割 */
    {
      type: "number",
      name: "caseQty",
      label: "件数",
      value: "",
      min: 0,
      on: {
        change(value, data) {
          setTimeout(() => {
            let formValues = _this.$refs.formdetail.form.getFieldsValue();
            let pieceQtyNew = formValues.conversionRate * (+formValues.caseQty || 0) + (+formValues.oddPieceQty || 0);
            _this.$refs.formdetail.form.setFieldsValue({ pieceQtyNew: pieceQtyNew });
            let qty = (pieceQtyNew / formValues.conversionRate).toFixed(2);
            let num = formatNumZero(qty);
            _this.$refs.formdetail.form.setFieldsValue({ qty: num });
          }, 20);
        },
      },
    },
    {
      type: "number",
      name: "oddPieceQty",
      label: "零散数量",
      value: "",
      min: 0,
      on: {
        change(value, data) {
          setTimeout(() => {
            let formValues = _this.$refs.formdetail.form.getFieldsValue();
            let pieceQtyNew = formValues.conversionRate * (+formValues.caseQty || 0) + (+formValues.oddPieceQty || 0);
            _this.$refs.formdetail.form.setFieldsValue({ pieceQtyNew: pieceQtyNew });
            let qty = (pieceQtyNew / formValues.conversionRate).toFixed(2);
            let num = formatNumZero(qty);
            _this.$refs.formdetail.form.setFieldsValue({ qty: num });
          }, 20);
        },
      },
    },
    { type: "input", name: "pieceQtyNew", label: "数量", value: "", disabled: true },
    // {
    //     //todo
    //   type: "input",
    //   name: "manufacturerLotNum",
    //   label: "批号",
    //   value: "",
    //   //   disabled: true,
    //   on: {
    //     change(value, label) {},
    //   },
    // },

    ...genManufacturerLotNumSearchModal(
      //批号
      1,
      false,
      false,
      function (vnode, p) {
        // p.data = ;
        let formValues = _this.$refs.formdetail.form.getFieldsValue();
        p.data = { ...p.data, ...formValues };
        delete p.data.manufacturerLotNum;
        delete p.data.manufacturerLotNumNew;
        console.log(p);

        return p;
      },
      function (data, form, event) {
        console.log(arguments);
        // form.setFieldsValue
        // console.log(data[0]);
        if (data && data[0]) {
          form.setFieldsValue({ manufacturerLotNumNew: data[0].manufacturerLotNum });
        } else {
          form.setFieldsValue({ manufacturerLotNumNew: event.target.value });
        }

        // [a, b] = [b, a];
        // manufacturerLotNum
      },

      { span: 10, labelCol: { span: 8 } }
    ),

    // {
    //   type: 'input', name: 'showProductionDateNew', label: '显示生产日期', value: '',
    //   on: {
    //     change() {
    //       setTimeout(() => {
    //         let form = _this.$refs.formdetail.form;
    //         let formValues = form.getFieldsValue();
    //         let value = formValues.showProductionDateNew
    //         if (value === '*') {
    //           form.setFieldsValue({ productionDateNew: moment().format('YYYY-MM-DD') });
    //           return;
    //         }
    //         value = value.replace(/\D/g, '');
    //         form.setFieldsValue({ showProductionDateNew: value });
    //         if (value.length != 8 && value.length != 6) {
    //           let p = {
    //             productionDateNew: '',
    //           };
    //           if (formValues.showExpiredDateNew&&formValues.showExpiredDateNew.length==6){
    //             p.expiredDateNew = '';
    //           }
    //           form.setFieldsValue(p);
    //           return;
    //         }
    //         let t = handleShowProduqctionDate(value);
    //         if (!t || t.indexOf('Invalid') >= 0) {
    //           message.warning('显示生产日期 格式有误，请修改！')
    //           return;
    //         }
    //         form.setFieldsValue({ productionDateNew: t });

    //         // (生产日期+质保期天数) 、有效期至，取最大值。
    //         if (!formValues.showExpiredDateNew|| formValues.showExpiredDateNew.length==8) { return; }
    //         if (formValues.showExpiredDateNew == '*'){
    //           form.setFieldsValue({ expiredDateNew: '2099-01-01' });
    //         }else{
    //           let a = new Date(t);
    //           a.setDate(a.getDate() + (+formValues.shelfLifeDays || 1));
    //           let b = handleShowExpiredDate(formValues.showExpiredDateNew);
    //           if (+(a) > +(new Date(b))) {
    //             form.setFieldsValue({ expiredDateNew: moment(a).format('YYYY-MM-DD') });
    //           } else {
    //             form.setFieldsValue({ expiredDateNew: b });
    //           }
    //         }
    //       }, 20);
    //     },
    //     blur() {
    //       let form = _this.$refs.formdetail.form;
    //       let formValues = form.getFieldsValue();
    //       let value = formValues.showProductionDateNew;
    //       if (value.length < 6 || value.length == 7 || value.length > 8) {
    //         message.warning('显示生产日期格式有误，请修改！')
    //         return;
    //       }
    //     }
    //   }
    // },
    // {
    //   type: 'input', name: 'showExpiredDateNew', label: '显示有效期至', value: '',
    //   on: {
    //     change() {
    //       setTimeout(() => {
    //         let form = _this.$refs.formdetail.form;
    //         let formValues = form.getFieldsValue();
    //         let value = formValues.showExpiredDateNew
    //         if (value === '*') {
    //           form.setFieldsValue({ expiredDateNew: '2099-01-01' });
    //           return;
    //         }
    //         value = value.replace(/\D/g, '');
    //         form.setFieldsValue({ showExpiredDateNew: value });
    //         if (value.length != 6 && value.length != 8) {
    //           form.setFieldsValue({ expiredDateNew: '' });
    //           return;
    //         }
    //         let t = handleShowExpiredDate(value, formValues.shelfLifeUnit, +formValues.shelfLifeDays);
    //         if (!t || t.indexOf('Invalid') >= 0) {
    //           message.warning('显示有效期至格式有误，请修改！')
    //           return;
    //         }
    //         // form.setFieldsValue({ expiredDateNew: t });
    //         // (生产日期+质保期天数（无值取1）) 、有效期至，取最大值。
    //         if(value.length == 8){
    //           form.setFieldsValue({ expiredDateNew: t });
    //         }else{
    //           if (!formValues.productionDateNew) {return;}
    //           let a = new Date(formValues.productionDateNew);
    //           a.setDate(a.getDate() + (+formValues.shelfLifeDays || 1));
    //           if (+(a) > +(new Date(t))) {
    //             form.setFieldsValue({ expiredDateNew: moment(a).format('YYYY-MM-DD') });
    //           } else {
    //             form.setFieldsValue({ expiredDateNew: t });
    //           }
    //         }
    //       }, 20);
    //     },
    //     blur() {
    //       let form = _this.$refs.formdetail.form;
    //       let formValues = form.getFieldsValue();
    //       let value = formValues.showExpiredDateNew;
    //       if (value.length < 6 || value.length == 7 || value.length > 8) {
    //         message.warning('显示有效期至格式有误，请修改！')
    //         return;
    //       }
    //     }
    //   }
    // },
    {
      type: "date",
      format: "YYYY-MM-DD",
      name: "productionDateNew",
      label: "生产日期",
      placeholder: "生产日期",
      value: "",
      // disabled: true
    },
    {
      type: "date",
      format: "YYYY-MM-DD",
      name: "expiredDateNew",
      label: "有效期至",
      placeholder: "有效期至",
      value: "",
      // disabled: true
    },
  ];
}
