import API from "../../api/default";
import store from "@/store";
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "../../../SearchModal";
let warehouseId = store.getters.warehouse.warehouseId; //仓库id;
let dcId = "";
const items = [
  ...genWarehouseAuthSearchModal(
    1,
    true,
    false,
    null,
    function (modalRowData, form) {
      if (modalRowData) {
        warehouseId = modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId;
        dcId = modalRowData[0].dcId;
        form.setFieldsValue({
          dcId: modalRowData[0].dcId,
          warehouseId: modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId,
        });
      } else {
        form.setFieldsValue({
          warehouseId: "",
          dcId: "",
        });
        dcId = "";
        warehouseId = "";
      }
    },
    { span: 8 }
  ),
  ...genCargoownerAuthSearchModal(1, true, false, null, null, {
    span: 8,
  }),
  { type: "input", name: "dcId", label: "仓配中心ID", hidden: true },
  {
    type: "input",
    name: "departmentId",
    label: "部门ID",
    hidden: true,
    disabled: true,
  },
  {
    type: "input",
    name: "departmentName",
    label: "部门",
    disabled: true,
  },
  // {
  //   type: 'search',
  //   name: 'departmentName',
  //   label: '部门',
  //   targetValueKey:'departmentName',
  //   destroyOnClose: true,
  //   targetClick: true,
  //   span: 8,
  //   value: '',
  //   toolBar: {
  //     items: [
  //       {
  //         type: 'input',
  //         name: 'departmentName',
  //         label: '部门名称',
  //         span: 8,
  //         value: ''
  //       },
  //       {
  //         type: 'input',
  //         name: 'departmentCode',
  //         label: '部门编码',
  //         span: 8,
  //         value: ''
  //       }
  //     ]
  //   },
  //   tableConfig: {
  //     rowKey: 'id',
  //     rowSelectType: 1,
  //     hasRowSelectCheck: true,
  //     columns: [{ dataIndex: 'departmentName', title: '部门名称', key: 'departmentName' },
  //       { dataIndex: 'departmentCode', title: '部门编码', key: 'departmentCode' },
  //       { dataIndex: 'createdate', title: '创建时间', key: 'createdate' },
  //     ]
  //   },
  //   value: '',
  //   remoteUrl: API.departmentPage,
  //   on: {
  //     ok(event, value, rowData, form) {
  //       form.setFieldsValue({
  //         departmentId:rowData[0].id
  //       })
  //     },
  //     change(event, value, rowData, form) {
  //       if (!value) {
  //         form.setFieldsValue({
  //           departmentId:''
  //         })
  //       }
  //     }
  //   }
  // },
  { type: "input", name: "warehouseWorkerId", label: "操作员Id", hidden: true },
  { type: "input", name: "warehouseWorkerName", label: "操作员", disabled: true },

  // { type: 'input', name: 'warehouseWorkerId', label: '操作员Id', hidden: true },
  // {
  //   type: 'search',
  //   name: 'locname',
  //   label: '操作员',
  //   destroyOnClose: true,
  //   targetValueKey: 'locname',
  //   targetClick: true,
  //   required:true,
  //   span: 8,
  //   value: '',
  //   toolBar: {
  //     items: [
  //       {
  //         type: 'input',
  //         name: 'locname',
  //         label: '操作员',
  //         span: 8,
  //         value: ''
  //       },
  //       {
  //         type: 'input',
  //         name: 'jobnumber',
  //         label: '工号',
  //         span: 8,
  //         value: ''
  //       }
  //     ]
  //   },
  //   tableConfig: {
  //     rowKey: 'id',
  //     rowSelectType: 1,
  //     hasRowSelectCheck: true,
  //     columns: [
  //       { dataIndex: 'locname', title: '操作员', key: 'locname' },
  //       { dataIndex: 'jobnumber', title: '工号', key: 'jobnumber' },
  //       { dataIndex: 'id', title: '员工ID', key: 'id' }
  //     ]
  //   },
  //   value: '',
  //   remoteUrl: API.employeePage,
  //   on: {
  //     ok(event, value, rowData, form) {
  //       console.log(rowData, 'rowDatarowData')
  //       form.setFieldsValue({
  //         warehouseWorkerId: rowData[0].userId
  //       })
  //     },
  //     change(event, value, rowData, form) {
  //       if (!value) {
  //         form.setFieldsValue({
  //           warehouseWorkerId: '',
  //           locname: '',
  //         })
  //       }
  //     }
  //   }
  // },
  {
    type: "input",
    name: "today",
    disabled: true,
    span: 8,
    label: "单据日期",
  },
  {
    type: "input",
    name: "customBillNum",
    span: 8,
    label: "单据编号",
  },
];
let itemId = {};
let batchId = {};
let itemHealthCategory = {};
let outwarehouseAreaCategory = {}; // 移出库别
const columns = function (self) {
  return [
    {
      dataIndex: "itemCode",
      width: 150,
      title: "商品编码",
      type: "search",
      targetValueKey: "itemCode",
      modalWidth: "70%",
      destroyOnClose: true,
      targetClick: true,
      toolBar: {
        items: [
          {
            type: "input",
            name: "itemCode",
            label: "商品编码",
            span: 8,
            value: "",
          },
          {
            type: "input",
            name: "itemAssistantCode",
            label: "助记码",
            span: 8,
            value: "",
          },
        ],
      },
      tableConfig: {
        rowKey: "itemId",
        remoteUrl: (res) => {
          return {
            ...API.batchNum,
          };
        },
        remoteBefore: (res, remoteParams) => {
          let refMain = self.queryRefFromName("main");
          let values = refMain.form.getFieldsValue();
          remoteParams.params = {
            ...remoteParams.params,
            ...values,
            itemId: "580488529298198528",
          };
          return remoteParams;
        },
        columns: [
          {
            dataIndex: "itemName",
            title: "商品名称",
            key: "itemName",
          },
          {
            dataIndex: "itemCode",
            title: "商品编码",
            key: "itemCode",
          },
          {
            dataIndex: "itemAssistantCode",
            title: "助记码",
            key: "itemAssistantCode",
          },
          {
            dataIndex: "manufacturerName",
            title: "生产厂家",
            key: "manufacturerName",
          },
        ],
      },
      on: {
        ok(event, value, rowData, name, index, tableRowData) {
          itemId[index] = rowData[0].itemId;
          batchId[index] = rowData[0].batchId;
          itemHealthCategory[index] = rowData[0].itemHealthCategory;
          tableRowData.batchNum = rowData[0].batchNum;
          tableRowData.batchId = rowData[0].batchId;
          tableRowData.itemId = rowData[0].itemId;
          tableRowData.itemName = rowData[0].itemName;
          tableRowData.pieceBarcode = rowData[0].pieceBarcode;
          tableRowData.pieceUom = rowData[0].pieceUom;
          tableRowData.uom = rowData[0].pieceUom;
          tableRowData.caseConversionRate = rowData[0].caseConversionRate;
          tableRowData.conversionRate = rowData[0].caseConversionRate;
          tableRowData.manufacturerName = rowData[0].manufacturerName;
          tableRowData.caseConversionRate = rowData[0].caseConversionRate;
        },
        change({ record, value }) {
          if (!value) {
            record.itemId = "";
            record.batchId = "";
            itemId[index] = "";
            batchId[index] = "";
          }
        },
      },
      required: true,
    },
    { dataIndex: "itemId", title: "商品ID", key: "itemId", hidden: true, type: "txt" },
    { dataIndex: "batchId", title: "批次Id", key: "batchId", hidden: true, type: "txt" },
    { dataIndex: "batchNum", title: "批次号", key: "batchNum", hidden: true, type: "txt" },
    // { dataIndex: 'pieceBarcode', title: '商品条码', key: 'pieceBarcode' },
    { dataIndex: "itemName", title: "商品名称", key: "itemName", type: "txt", width: 200 },
    { dataIndex: "pieceUom", title: "包装单位", key: "pieceUom", type: "txt", width: 80, hidden: true },
    { dataIndex: "uom", title: "包装单位", key: "uom", width: 80, type: "txt" },
    { dataIndex: "caseConversionRate", title: "包装数", key: "caseConversionRate", type: "txt", width: 150 },
    { dataIndex: "conversionRate", title: "包装数", key: "conversionRate", hidden: true, type: "txt" },
    // { dataIndex: 'caseConversionRate', title: '最大单位（箱单位）数量', key: 'caseConversionRate', hidden: true ,type: 'txt'},
    { dataIndex: "outLocatorId", title: "移出货位ID", key: "outLocatorId", hidden: true, type: "txt" },
    { dataIndex: "outStockStatus", type: "txt", title: "下架库存状态", key: "outStockStatus", hidden: true },
    {
      dataIndex: "locatorCode",
      width: 150,
      title: "移出货位编码",
      type: "search",
      remoteUrl: API.getstock,
      targetValueKey: "locatorCode",
      destroyOnClose: true,
      autoLoad: false,
      targetClick: true,
      required: true,
      modalOpenChange: (open) => {
        let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
        if (!itemId[ref.clickRowRecord - 1]) {
          self.$message.warn("请选择商品编码");
          return false;
        } else {
          return true;
        }
      },
      tableConfig: {
        columns: [
          {
            dataIndex: "locatorCode",
            title: "货位编码",
            key: "locatorCode",
          },
          {
            dataIndex: "availableQuantity",
            title: "可用库存数量",
            key: "availableQuantity",
          },
          {
            dataIndex: "warehouseAreaCategoryName",
            title: "移出库别",
            key: "warehouseAreaCategoryName",
          },
        ],
        remoteBefore: (res, remoteParams) => {
          console.log(remoteParams, "remoteParamsremoteParamsremoteParamsremoteParams");
          let refMain = self.queryRefFromName("main");
          let formsVal = refMain.form.getFieldsValue();
          let idx = self.queryRefFromName("baseMtlItemUomConversionsList").clickRowRecord;
          remoteParams.params = {
            ...remoteParams.params,
            ...formsVal,
            itemId: itemId[idx - 1],
            batchId: batchId[idx - 1],
          };
          return remoteParams;
        },
      },
      on: {
        search(val, val1, val2, val3, name, index, tableRowData) {
          console.log("searchsearchsearchsearch", arguments);
          console.log(val, val1, val2, val3, "val, val1, val2val, val1, val2val, val1, val2");
        },
        ok(event, value, rowData, name, index, tableRowData) {
          tableRowData.outLocatorId = rowData[0].locatorId;
          tableRowData.outStockStatus = rowData[0].status;
          tableRowData.availableQuantity = rowData[0].availableQuantity;
          tableRowData.inStockStatus = rowData[0].status;
          tableRowData.batchId = rowData[0].batchId;
          tableRowData.outwarehouseAreaCategoryCode = rowData[0].warehouseAreaCategory;
          tableRowData.outwarehouseAreaCategory = rowData[0].warehouseAreaCategory;
          tableRowData.outwarehouseAreaCategoryName = rowData[0].warehouseAreaCategoryName;
          batchId[index] = rowData[0].batchId;

          outwarehouseAreaCategory[index] = rowData[0].warehouseAreaCategory;
        },
        change({ record, value, index }) {
          // debugger
          if (!value) {
            record.outLocatorId = "";
            record.outStockStatus = "";
            record.inStockStatus = "";
            record.availableQuantity = "";
            record.batchId = "";
            record.outwarehouseAreaCategory = "";
            record.outwarehouseAreaCategoryName = "";
            outwarehouseAreaCategory[index] = "";
          }
        },
      },
    },
    { dataIndex: "inStockStatus", type: "txt", title: "上架库存状态", key: "inStockStatus", hidden: true },
    {
      dataIndex: "outwarehouseAreaCategoryName",
      title: "移出库别",
      key: "outwarehouseAreaCategoryName",
      type: "txt",
      width: 100,
    },

    {
      hidden: true,
      dataIndex: "outwarehouseAreaCategory",
      title: "移出库别",
      key: "outwarehouseAreaCategory",
      type: "txt",
      width: 100,
    },
    {
      hidden: true,
      dataIndex: "outwarehouseAreaCategoryCode",
      title: "移出库别",
      key: "outwarehouseAreaCategoryCode",
      type: "txt",
      width: 100,
    },
    // warehouseAreaCategory
    {
      dataIndex: "warehouseLocatorCode",
      width: 150,
      title: "移入货位编码",
      type: "search",
      remoteUrl: API.locatorPage,
      targetClick: true,
      destroyOnClose: true,
      targetValueKey: "warehouseLocatorCode",
      toolBar: {
        items: [
          {
            type: "input",
            name: "warehouseLocatorName",
            label: "货位名称",
            span: 8,
            value: "",
          },
          {
            type: "input",
            name: "warehouseLocatorCode",
            label: "货位编码",
            span: 8,
            value: "",
          },
          {
            type: "input",
            name: "warehouseName",
            label: "仓库名称",
            span: 8,
            value: "",
          },
        ],
      },
      modalOpenChange: (open) => {
        let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
        if (!outwarehouseAreaCategory[ref.clickRowRecord - 1]) {
          self.$message.warn("请选择移出货位");
          return false;
        } else {
          return true;
        }
      },
      tableConfig: {
        rowKey: "warehouseLocatorId",
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: "warehouseLocatorName", title: "货位名称", key: "warehouseLocatorName" },
          { dataIndex: "warehouseLocatorCode", title: "货位编码", key: "warehouseLocatorCode" },
        ],
        remoteBefore: (res, remoteParams) => {
          console.log(remoteParams, "remoteParamsremoteParamsremoteParamsremoteParams");
          let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
          let refMain = self.queryRefFromName("main");
          let idx = self.queryRefFromName("baseMtlItemUomConversionsList").clickRowRecord;
          let formsVal = refMain.form.getFieldsValue();
          remoteParams.params = {
            ...remoteParams.params,
            warehouseId: warehouseId,
            itemId: itemId[idx - 1],
            batchId: batchId[idx - 1],
            itemHealthCategory: itemHealthCategory[idx - 1],
            cargoOwnerId: formsVal.cargoOwnerId,
            warehouseAreaCategory: outwarehouseAreaCategory[ref.clickRowRecord - 1],
          };
          return remoteParams;
        },
      },
      on: {
        ok(event, value, rowData, name, index, tableRowData) {
          tableRowData.inwarehouseAreaCategoryName = rowData[0].warehouseAreaCategoryName;
          tableRowData.inwarehouseAreaCategory = rowData[0].warehouseAreaCategory;
          tableRowData.inLocatorId = rowData[0].warehouseLocatorId;
          // debugger
          // if (tableRowData.locatorCode == rowData[0].warehouseLocatorCode) {
          //   self.$message.warn('移出货位与移入货位不能相同！')
          //   tableRowData.warehouseLocatorCode = ''
          //   tableRowData.inwarehouseAreaCategory = ''
          //   tableRowData.inLocatorId = ''
          // }
        },
        change({ index, modalRowData, name, record, value }) {
          if (!value) {
            record.inLocatorId = "";
            record.inwarehouseAreaCategory = "";
          }
        },
      },
      //   customColumnProps({ name, value, record, index, props }) {
      //     if (index === 0) {
      //       props.disabled = true;
      //     } else {
      //       props.disabled = false;
      //     }
      //   },
      required: true,
    },
    {
      hidden: true,
      dataIndex: "inwarehouseAreaCategory",
      title: "移入库别编码",
      key: "inwarehouseAreaCategory",
      type: "txt",
      width: 100,
    },

    {
      dataIndex: "inwarehouseAreaCategoryName",
      title: "移入库别",
      key: "inwarehouseAreaCategoryName",
      type: "txt",
      width: 100,
    },
    { dataIndex: "inLocatorId", title: "移入货位ID", key: "inLocatorId", hidden: true },
    { dataIndex: "availableQuantity", title: "可用库存数量", key: "availableQuantity", type: "txt", width: 80 },
    { dataIndex: "numData", title: "数量", key: "numData", type: "txt", width: 80 },
    {
      dataIndex: "caseQuantity",
      title: "件数",
      key: "caseQuantity",
      width: 100,
      type: "number",
      on: {
        change({ event, value, name, index, record }) {
          console.log(arguments, "argumentsargumentsarguments");
          record.numData = record.caseConversionRate * value + Number(record.oddPieceQuantity);
          if (record.availableQuantity < record.numData) {
            self.$message.warn("可用库存数量不足");
            record.caseQuantity = 0;
            return;
          }
        },
      },
    },
    {
      dataIndex: "oddPieceQuantity",
      title: "零散数",
      key: "oddPieceQuantity",
      width: 100,
      type: "number",

      customColumnProps({ name, value, record, index, props }) {
        if (record.outwarehouseAreaCategoryName == "整件库") {
          props.disabled = true;
        } else {
          props.disabled = false;
        }
      },
      on: {
        change({ event, value, name, index, record }) {
          console.log(arguments, "argumentsargumentsarguments");
          record.numData = record.caseConversionRate * Number(record.caseQuantity) + Number(value);
          if (record.inwarehouseAreaCategory == "ZJK" && record.oddPieceQuantity > 0) {
            self.$message.warn("移入库别为整件库时，不能输入零散数！");
            record.oddPieceQuantity = 0;
            return;
          }
          if (record.availableQuantity < record.numData) {
            self.$message.warn("实际库存数量不足");
            record.oddPieceQuantity = 0;
            return;
          }
        },
      },
    },
    { dataIndex: "manufacturerName", title: "生产厂家", key: "manufacturerName", type: "txt", width: 250 },
  ];
};

export function detailConfig(self) {
  return [
    {
      key: "one",
      checkedKey: "main",
      btns: [
        {
          label: "重置",
          click() {
            let refMain = self.queryRefFromName("main");
            let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
            refMain.form.enableItem(["warehouseName", "cargoOwnerName"]);
            refMain.form.setFieldsValue({
              cargoOwnerId: "",
              cargoOwnerName: "",
              customBillNum: "",
            });
            ref.delItems(); // 删除附表信息
          },
        },
        {
          label: "保存",
          click() {
            let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
            self.saveData(ref.getListData(true));
          },
        },
      ],
      list: [
        {
          key: "main",
          tab: "主要信息",
          btns: [
            // { label: '辅助功能1', click() { } },
          ],
          item: { type: "p-grid-form", config: items },
        },
      ],
    },
    {
      key: "two",
      checkedKey: "baseMtlItemUomConversionsList",
      list: [
        {
          key: "baseMtlItemUomConversionsList",
          tab: "移货位明细",
          btns: [
            {
              label: "新增",
              click() {
                let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
                let refMain = self.queryRefFromName("main");
                // console.log(self.getData())
                let formsVal = refMain.form.getFieldsValue();
                if (!formsVal.cargoOwnerId) return self.$message.warn("请选择货主");
                ref.addItem(); // 新增
                // self.formsVal = formsVal  disabled
                refMain.form.disableItem(["warehouseName", "cargoOwnerName"]);
                // refMain.form.disableItem() // form禁用
              },
            },
            {
              label: "删除",
              click() {
                self.$confirm({
                  content: self.$lang("确定删除?"),
                  onOk: () => {
                    let ref = self.queryRefFromName("baseMtlItemUomConversionsList");
                    ref.delSelectedItems();
                  },
                });
              },
            },
          ],
          item: {
            type: "p-table-edit",
            config: columns(self),
            rowKey: "_row_count",
            rowSelectType: 2,
            colChange({ record, name, value }) {
              record.numData = record.caseConversionRate * record.caseQuantity + Number(record.oddPieceQuantity);
              // if (name == 'caseQuantity') {
              //   if (record.quantity < record.numData) {
              //     record.caseQuantity = '0'
              //     self.$message.warn('实际库存数量不足')
              //   }
              // }
            },
          },
        },
      ],
    },
  ];
}
