import { message } from "ant-design-vue";
import { queryItemInfos } from "../api";
import { upcloud_base_common } from "@/config/serviceSettings";
import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genBldgSearchModal,
  genItemRelatedSearchModal,
} from "../../../SearchModal";

const span8 = { span: 8, labelCol: { span: 6 } };
// let warehouseCategoryList = [];

export const warehouseCategoryCode = function (required = false, hasAll = true) {
  return {
    type: "select",
    name: "warehouseCategoryCode",
    label: "库别",
    value: "",
    ...span8,
    required: required,
    autoLoad: true,
    remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=wms_storeroom", method: "post" },
    replaceFields: {
      value: "dictCode",
      label: "dictName",
    },
    doneBefore(res) {
      // warehouseCategoryList = res.data;
      let data = [...res.data];
      if (hasAll) {
        data.unshift({ dictCode: " ", dictName: "全部" });
      }
      return { ...res, data: data };
    },
    on: {
      change(value, label, options, form) {
        form.setFieldsValue({
          warehouseCategoryName: label,
        });
      },
    },
  };
};

export const itemCategoryCode = function (required = false) {
  return {
    type: "select",
    name: "itemCategoryCode",
    label: "商品大类",
    value: "",
    ...span8,
    required: required,
    autoLoad: true,
    remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=item_health_category", method: "post" },
    replaceFields: {
      value: "dictCode",
      label: "dictName",
    },
    on: {
      change(value, label, options, form) {
        form.setFieldsValue({
          itemCategoryName: label,
          itemId: null,
          itemCode: null,
          itemName: null,
          caseUom: null,
          caseConversionRate: null,
          itemHealthType: null,
          pieceConversionRate: null,
          storeTemperature: null,
        });
      },
    },
  };
};

export const formItems = function (self) {
  return [
    { hidden: true, type: "input", name: "warehouseCategoryName", label: "", value: "" },
    { hidden: true, type: "input", name: "itemCategoryName", label: "", value: "" },
    ...genWarehouseAuthSearchModal(1, true, false, null, null, span8),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, span8),
    itemCategoryCode(true),
    ...genItemRelatedSearchModal(
      1,
      false,
      false,
      function (res, p) {
        let vals = self.$refs.form.form.getFieldsValue();
        if (!vals.warehouseId) {
          message.warning("请先选择仓库");
          return false;
        }
        if (!vals.cargoOwnerId) {
          message.warning("请先选择货主");
          return false;
        }
        p.data.itemCategoryCode = vals.itemCategoryCode;
        p.data.categoryCode = vals.categoryCode;
        p.data.warehouseId = vals.warehouseId;
        p.data.cargoOwnerId = vals.cargoOwnerId;
        return p;
      },
      function (rowData, form) {
        if (rowData) {
          queryItemInfos({ itemIds: [rowData[0].itemId] }).then((res) => {
            if (!res.success) {
              this.$exception(res);
              return;
            }
            form.setFieldsValue({
              caseUom: res.data.list[0].caseUom,
              caseConversionRate: res.data.list[0].caseConversionRate,
              itemHealthType: res.data.list[0].itemHealthType,
              pieceConversionRate: res.data.list[0].pieceConversionRate,
              storeTemperature: res.data.list[0].storeTemperature,
            });
          });
        } else {
          form.setFieldsValue({
            caseUom: null,
            caseConversionRate: null,
            itemHealthType: null,
            pieceConversionRate: null,
            storeTemperature: null,
          });
        }
      },
      span8
    ),
    {
      type: "input",
      name: "caseUom",
      label: "单位",
      value: "",
      disabled: true,
      ...span8,
    },
    {
      type: "input",
      name: "caseConversionRate",
      label: "包装数量",
      value: "",
      disabled: true,
      ...span8,
    },
    {
      type: "input",
      name: "itemHealthType",
      label: "剂型",
      value: "",
      disabled: true,
      ...span8,
    },
    {
      type: "input",
      name: "pieceConversionRate",
      label: "商品规格",
      value: "",
      disabled: true,
      ...span8,
    },
    {
      type: "select",
      name: "storeTemperature",
      label: "存储条件",
      value: "",
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=wms_storagecondition", method: "post" },
      autoLoad: true,
      replaceFields: {
        value: "dictCode",
        label: "dictName",
      },
      doneBefore: (data) => {
        console.log(data);
        data.data = data.data.filter((_) => _.isEnable);
        return data;
      },
      ...span8,
    },

    ...genBldgSearchModal(
      1,
      true,
      false,
      function (res, p) {
        let vals = self.$refs.form.form.getFieldsValue();
        if (!vals.warehouseId) {
          message.warning("请先选择仓库");
          return false;
        }
        p.params.isEnable = "1";
        p.params.warehouseId = vals.warehouseId;
        return p;
      },
      null,
      span8
    ),

    warehouseCategoryCode(true, false),

    {
      type: "number",
      min: 1,
      name: "stockLowerLimit",
      label: "库存下限",
      value: "",
      required: false,
      ...span8,
    },
    {
      type: "number",
      min: 1,
      name: "stockHigherLimit",
      label: "库存上限",
      value: "",
      required: false,
      ...span8,
    },

    {
      type: "select",
      name: "billtype",
      autoLoad: true,
      mode: "multiple",
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=oms_asn_bill_type", method: "post" },
      replaceFields: {
        value: "dictCode",
        label: "dictName",
      },
      doneBefore: (data) => {
        console.log(data);
        data.data = data.data.filter((_) => _.isEnable);
        return data;
      },
      label: "单据类型",
      value: null,
      required: false,

      ...span8,
    },
  ];
};
