import moment from "moment";
import store from "@/store";
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "@/views/kzyWMS/SearchModal";
// upcloud_base_common
import { upcloud_base_wh, upcloud_base_common, upcloud_base_wh_control } from "@/config/serviceSettings";
const grid = { span: 8, labelCol: { span: 6 } };

let warehouseId = store.state.user.warehouse.warehouseId; //仓库id;

const items = [
  // 仓库
  ...genWarehouseAuthSearchModal(
    1,
    true,
    false,
    null,
    function (rowData, form) {
      if (!rowData) {
        warehouseId = "";
        form.setFieldsValue({
          companyId: "",
          companyCode: "",
          companyName: "",
        });
      } else {
        warehouseId = rowData[0].companyId;
        form.setFieldsValue({
          warehouseId: rowData[0].companyId,
          warehouseCode: rowData[0].companyCode,
          warehouseName: rowData[0].companyName,
        });
      }
    },
    grid
  ),

  /* {type:'select',name:'containerCode',label: '容器', ...remote('pub_container_containertype') }, */
  // 货主(与账户权限关联)
  ...genCargoownerAuthSearchModal(
    1,
    true,
    false,
    function (vn, p) {
      console.log("remoteBefore参数回调：", vn, p);
    },
    null,
    grid
  ),
  //todo  目标货位编码
  //   目标货位编码
  //   {
  //     // type: "searchModel",
  //     type: "searchModal",
  //     name: "toWhLocatorCode",
  //     label: "目标货位编码",
  //     required: true,
  //     dataIndex: "toWhLocatorCode",
  //     // title: "目的货位编码",
  //     align: "center",
  //     targetClick: true,
  //     // disabled: true,
  //     targetValueKey: "warehouseLocatorCode",
  //     remoteUrl: { url: upcloud_base_wh + "/locator/page", method: "get" },
  //     // remoteData: { consignorId: consignorId || that?.consignorId },

  //     modalOpenChange(open) {
  //       return open;
  //     },
  //     rowKey: "id",
  //     toolBar: {
  //       items: [
  //         { type: "input", name: "warehouseLocatorName", label: "货位名称", value: "" },
  //         { type: "input", name: "warehouseLocatorCode", label: "货位编码", value: "" },
  //       ],
  //     },
  //     on: {
  //       search: () => {
  //         // this.remoteParams = {}
  //       },
  //       ok(event, value, rowData, form) {
  //         // console.log(event, value, rowData, form)
  //       },
  //       async change(event, value, rowData, form) {},
  //     },
  //     relatedQuery: [{ key: "toWhAreaCategory", target: "warehouseAreaCategory" }],
  //     multipleKey: [{ key: "warehouseLocatorId", target: "toWhLocatorId" }],

  //     tableConfig: {
  //       rowKey: "warehouseLocatorId",
  //       columns: [
  //         { dataIndex: "warehouseLocatorId", title: "ID", hidden: true },
  //         { dataIndex: "warehouseLocatorName", title: "货位名称" },
  //         { dataIndex: "warehouseLocatorCode", title: "货位编码" },
  //         {
  //           dataIndex: "warehouseLocatorType",
  //           title: "货位类型",
  //           remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=wms_loctype", method: "post" },
  //           replaceFields: {
  //             value: "dictCode",
  //             label: "dictName",
  //           },
  //         },
  //       ],
  //     },
  //     ...grid,
  //   },
  {
    type: "select",
    name: "businessType",
    label: "补货类型",
    value: "",
    options: [
      { label: "波次补货", value: "WAVE" },
      { label: "主动补货", value: "INITIATIVE" },
      { label: "被动补货", value: "PASSIVE" },
      { label: "紧急补货", value: "URGENT" },
      { label: "顺带补货", value: "INCIDENTALLY" },
    ],
    ...grid,
  },
  {
    type: "select",
    name: "status",
    label: "状态",
    value: "1",
    options: [
      // 0:新生成 1:下架 2:上架
      { label: "新生成", value: "0" },
      { label: "下架", value: "1" },
      { label: "上架", value: "2" },
    ],
    ...grid,
  },
  {
    type: "input",
    name: "erpSrcNo",
    label: "源单号",
    value: "",
    ...grid,
  },
  {
    ...grid,
    type: "buttonGroup",
    style: { textAlign: "center" },
    options: [
      { value: "查询", icon: "search", btnType: "primary", style: "margin-right:16px", htmlType: "submit" },
      {
        value: "重置",
        name: "ccz",
        icon: "redo",
        on: {
          click(event, form) {
            form.resetFields();
            form.setFieldsValue({
              warehouseName: "",
              warehouseId: "",
            });
          },
        },
      },
    ],
  },
];

const columns = function (that) {
  return [
    { dataIndex: "erpSrcId", title: "源单id", hidden: true },
    { dataIndex: "erpSrcLineId", title: "源单行id", hidden: true },
    { dataIndex: "batchId", title: "批次id", hidden: true },
    { dataIndex: "cargoOwnerId", title: "货主id", hidden: true },
    { dataIndex: "itemId", title: "商品id", hidden: true },
    { dataIndex: "detailId", hidden: true },
    { dataIndex: "headerId", hidden: true },
    { dataIndex: "toWhAreaId", hidden: true },
    { dataIndex: "toWhLocatorId", title: "toWhLocatorId", hidden: true },
    { dataIndex: "warehouseId", hidden: true },
    { dataIndex: "whAreaId", hidden: true },
    { dataIndex: "whLocatorId", hidden: true },
    { dataIndex: "omsBillNo", title: "oms源单号", hidden: true },

    { dataIndex: "erpSrcNo", title: "源单号" },
    { dataIndex: "itemCode", title: "商品编码" },
    { dataIndex: "itemName", title: "商品名称", ellipsis: true },
    // { dataIndex:'specification', title:'规格',ellipsis:true },
    { dataIndex: "conversionRate", title: "包装数量" },
    { dataIndex: "manufacturerLotNum", title: "批号" },
    { dataIndex: "batchProductionDate", title: "生产日期", customRender: (text) => moment(text).format("YYYY-MM-DD") },
    { dataIndex: "batchExpiredDate", title: "有效期至", customRender: (text) => moment(text).format("YYYY-MM-DD") },
    {
      dataIndex: "businessType",
      title: "补货类型",
      customRender: (text, rows, index) => {
        switch (text) {
          case "WAVE":
            return "波次补货";
            break;
          case "INITIATIVE":
            return "主动补货";
            break;
          case "PASSIVE":
            return "被动补货";
            break;
          case "URGENT":
            return "紧急补货";
            break;
        }
      },
    },
    {
      dataIndex: "status",
      title: "状态",
      customRender: (text, rows, index) => {
        // 0:新生成 1:下架 2:上架
        switch (text) {
          case "0":
            return "新生成";
            break;
          case "1":
            return "下架";
            break;
          case "2":
            return "上架";
            break;
        }
      },
    },
    // { dataIndex:'caseQuantity', title:'整件数' },
    { dataIndex: "pieceQuantity", title: "零散数" },
    { dataIndex: "quantity", title: "数量" },
    { dataIndex: "uom", title: "单位" },
    { dataIndex: "warehouseName", title: "仓库名称" },
    { dataIndex: "cargoOwnerName", title: "货主" },
    { dataIndex: "sourceType", title: "源类型" },
    { dataIndex: "whAreaCategory", title: "源库别" },
    { dataIndex: "whAreaCode", title: "源库区编码" },
    { dataIndex: "whLocatorCode", title: "源货位编码" },
    { dataIndex: "toWhAreaCategory", title: "目的库别" },
    { dataIndex: "toWhAreaCode", title: "目的库区编码" },
    //   { dataIndex: "toWhLocatorCode", title: "目的货位编码" },
    {
      required: true,
      dataIndex: "toWhLocatorCode",
      title: "目的货位编码",
      align: "center",
      width: 150,
      type: "searchModal",
      targetClick: true,
      // disabled: true,
      targetValueKey: "warehouseLocatorCode",
      remoteUrl: { url: upcloud_base_wh_control + "/item-locator/v1/locator-limit-inbound", method: "get" },
      // remoteData: { consignorId: consignorId || that?.consignorId },

      modalOpenChange(open) {
        return open;
      },
      rowKey: "id",
      toolBar: {
        items: [
          { type: "input", name: "warehouseLocatorName", label: "货位名称", value: "" },
          { type: "input", name: "warehouseLocatorCode", label: "货位编码", value: "" },
        ],
      },
      on: {
        search: () => {
          // this.remoteParams = {}
        },
        ok(event, value, rowData, form) {
          // console.log(event, value, rowData, form)
        },
        async change(event, value, rowData, form) {},
      },
      relatedQuery: [
        { key: "toWhAreaCategory", target: "warehouseAreaCategory" },
        //   cargoOwnerId
        { key: "cargoOwnerId", target: "cargoOwnerId" },
        { key: "itemId", target: "itemId" },
        { key: "warehouseId", target: "warehouseId" },
        // { key: "warehouseAreaCategory", target: "warehouseAreaCategory" },
        { key: "itemName", target: "itemHealthCategory" },
      ],
      multipleKey: [{ key: "warehouseLocatorId", target: "toWhLocatorId" }],

      tableConfig: {
        rowKey: "warehouseLocatorId",
        columns: [
          { dataIndex: "warehouseLocatorId", title: "ID", hidden: true },
          { dataIndex: "warehouseLocatorName", title: "货位名称" },
          { dataIndex: "warehouseLocatorCode", title: "货位编码" },
          {
            dataIndex: "warehouseLocatorType",
            title: "货位类型",
            // remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=wms_loctype", method: "post" },
            // replaceFields: {
            //   value: "dictCode",
            //   label: "dictName",
            // },
            customRender(text) {
              let a = that.WmsLoctypeOptions.find((i) => {
                if (i.dictCode == text) {
                  return true;
                }
              });

              return a.dictName;
            },
          },
        ],
      },
    },
    { dataIndex: "batchNum", title: "批次号", width: 100 },
  ];
};
export { items, columns };
