import { createValidator } from "../../utils/validator";
import {
  getCargoOwnerProp,
  vaildateItemPurchaseAndSales,
  vaildateItemCertificate,
  vaildateItemVolInfo,
  vaildateItemCategory,
  vaildateCustomer,
  vaildateSupplier,
  vaildateCargoOwnerLicense,
  vaildateCargoOwnerScope,
} from "../api/validte";

export let inboundValidateModule = {
  props: {
    /* 
        <!-- 是否校验商品证照 -->'0' as vaildateItemLicense
        <!-- 是否校验商品体积 -->'0' as vaildateItemVol
        <!-- 是否校验商品大类 -->'0' as vaildateItemCategory
        <!-- 是否校验商品停售召回 -->'0' as vaildateItemInbound
        <!-- 是否校验货主证照 -->'0' as vaildateLicense
        <!-- 是否校验货主经营范围 -->'0' as vaildateCargoOwnerScop
        <!-- 是否校验供应商/客户证照 -->'0' as vaildateSuppilerAndCustomer
        */
    ibValidates: {
      type: Array, // 传字符串数组，看注释
      default: null,
    },
  },

  methods: {
    getCargoOwnerProp(cargoOwnerId, items, ibValidates) {
      if (!this.ibValidates && !ibValidates) {
        return Promise.resolve(true);
      }
      let arr = ibValidates || this.ibValidates;
      return getCargoOwnerProp(cargoOwnerId).then((res) => {
        let cargoOwnerProp = res.data[0];
        console.log(cargoOwnerProp, "====");
        let list = [];
        if (arr.indexOf("vaildateItemLicense") >= 0 && cargoOwnerProp.vaildateItemLicense != "0") {
            console.log(111);
          list.push({ fn: this.vaildateItemCertificate, tags: ["int"] });
        }
        if (arr.indexOf("vaildateItemVol") >= 0 && cargoOwnerProp.vaildateItemVol != "0") {
          list.push({ fn: this.vaildateItemVolInfo, tags: ["int", "cube"] });
        }
        if (arr.indexOf("vaildateItemCategory") >= 0 && cargoOwnerProp.vaildateItemCategory != "0") {
          list.push({ fn: this.vaildateItemCategory, tags: ["int"] });
        }
        // 英特不需要这个校验
        // if (arr.indexOf('vaildateItemInbound') >= 0 && cargoOwnerProp.vaildateItemInbound != '0') {
        //     list.push({ fn: this.vaildateItemPurchaseAndSales, tags: ['int'] })
        // }
        // console.log('items', items)
        if (arr.indexOf("vaildateSuppilerAndCustomer") >= 0 && cargoOwnerProp.vaildateSuppilerAndCustomer != "0") {
          if (items[0].shipperType == "1") list.push({ fn: this.vaildateCustomer, tags: ["int"] });
          if (items[0].shipperType == "2") list.push({ fn: this.vaildateSupplier, tags: ["int"] });
        }
        if (arr.indexOf("vaildateLicense") >= 0 && cargoOwnerProp.vaildateLicense != "0") {
          list.push({ fn: this.vaildateCargoOwnerLicense, tags: ["int"] });
        }
        if (arr.indexOf("vaildateCargoOwnerScop") >= 0 && cargoOwnerProp.vaildateCargoOwnerScop != "0") {
          list.push({ fn: this.vaildateCargoOwnerScope, tags: ["int"] });
        }
        return createValidator(list).execute(items);
      });
    },
    //  是否校验商品停售召回
    vaildateItemPurchaseAndSales(items) {
      return vaildateItemPurchaseAndSales(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleResult(res.data);
      });
    },
    //  是否校验商品证照
    vaildateItemCertificate(items) {
      return vaildateItemCertificate(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleResult(res.data);
      });
    },
    //  是否校验商品体积
    vaildateItemVolInfo(items) {
      return vaildateItemVolInfo(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleResult(res.data);
      });
    },
    //  是否校验商品大类
    vaildateItemCategory(items) {
      return vaildateItemCategory(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleResult(res.data);
      });
    },

    // shipperType 1客户 2供应商  ---  shipperid
    //  是否校验供应商证照
    vaildateSupplier(items) {
      return vaildateSupplier(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleShipper(res.data);
      });
    },
    //  是否校验客户证照
    vaildateCustomer(items) {
      return vaildateCustomer(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleShipper(res.data);
      });
    },

    //  是否校验货主证照
    vaildateCargoOwnerLicense(items) {
      return vaildateCargoOwnerLicense(items).then((res) => {
        if (res) {
          return true;
        } else {
          this.$confirm({
            content: "货主证照已过期",
          });
          return false;
        }
      });
    },

    //  是否校验货主经营范围
    vaildateCargoOwnerScope(items) {
      return vaildateCargoOwnerScope(items).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        return this.ibHandleResult(res.data);
      });
    },

    ibHandleResult(data) {
      if (!data) {
        this.$message.error("请求无返回数据");
        return false;
      }
      let text = "";
      data.queryMap.forEach((tt) => {
        if (!tt.flag) {
          text = "商品:  " + tt.refId + ", " + tt.message;
        }
      });
      if (text) {
        this.$confirm({
          content: text,
        });
        return false;
      } else {
        return true;
      }
    },

    ibHandleShipper(data) {
      if (!data) {
        this.$message.error("请求无返回数据");
        return;
      }
      let name = Object.keys(data)[0];
      let val = Object.values(data)[0];
      if (val.success) {
        return true;
      } else {
        this.$confirm({
          content: "商品: " + name + " " + val.message,
        });
        return false;
      }
    },
  },
};
