"use strict";
const common_vendor = require("../../../common/vendor.js");
const common_js_net_shop = require("../../../common/js/net/shop.js");
const common_js_validator = require("../../../common/js/validator.js");
const _sfc_main = {
  setup() {
  },
  data() {
    return {
      personType: [{
        dataName: "自然人",
        dataId: "1"
      }, {
        dataName: "法人",
        dataId: "2"
      }, {
        dataName: "非法人组织或其他",
        dataId: "3"
      }],
      selPersonType: null,
      creType: [],
      areaList: [],
      //省
      cityList: [],
      //市
      orgName: "",
      //著作权人姓名
      orgPhone: "",
      //联系电话
      cardNumber: "",
      //证件号码
      selRightType: [],
      //选中分类
      rightPrice: "",
      //售价
      rightStopDate: "",
      //停止日期
      selCreType: null,
      //选中证件类型
      showArea: false,
      prevValue: [0, 0],
      // 用于存储上一次的值
      selArea: null,
      selCity: null,
      msgHint: "",
      msgType: "info",
      msgShow: false,
      showType: false,
      orderId: ""
    };
  },
  onLoad(options) {
    common_vendor.index.setNavigationBarTitle({
      title: "受让人信息"
    });
    common_vendor.index.setNavigationBarColor({
      frontColor: "#000000",
      backgroundColor: "#FFFFFF",
      animation: {
        duration: 500,
        timingFunc: "easeIn"
      }
    });
    var orderId = options.orderId;
    if (orderId && orderId != "") {
      this.orderId = orderId;
      this.selPersonType = this.personType[0];
      this.getDic();
    } else {
      this.msgHint = "数据有误,请稍后重试";
      this.msgType = "error";
      this.$refs.msg.open();
      setTimeout(() => {
        common_vendor.index.navigateBack();
      }, 1200);
    }
  },
  methods: {
    inputRightName(e) {
      this.rightName = e.detail.value;
    },
    doClearRightName() {
      this.rightName = "";
    },
    inputRightPrice(e) {
      this.rightPrice = e.detail.value;
    },
    doClearPrice() {
      this.rightPrice = 0;
    },
    inputOrgName(e) {
      this.orgName = e.detail.value;
    },
    inputCardNumber(e) {
      this.cardNumber = e.detail.value;
    },
    // 著作权人类别
    bindChangePersonType(e) {
      this.selPersonType = this.personType[e.detail.value];
    },
    clearPersonType() {
      this.selPersonType = null;
    },
    // 证件类型
    bindChangeCreType(e) {
      this.selCreType = this.creType[e.detail.value];
    },
    clearCreType() {
      this.selCreType = null;
    },
    // 选择软著分类
    onShowRightType() {
      this.showType = true;
    },
    // 选择软著分类
    bindChangeRightType(e) {
      const selectedIds = e.detail.value;
      const selectedItems = [];
      this.rightType.forEach((item) => {
        item.checked = selectedIds.includes(item.dataId);
        if (item.checked) {
          selectedItems.push(item);
        }
      });
      this.rightType = [...this.rightType];
      this.selRightType = selectedItems;
    },
    clearRightType() {
      this.rightType.forEach((item) => {
        item.checked = false;
      });
      this.selRightType = [];
      this.rightType = [...this.rightType];
    },
    confirmSelRightType() {
      this.showType = false;
    },
    // 显示地区选择
    onShowArea() {
      this.$refs.areaDialog.open();
    },
    closeDialog() {
      this.$refs.areaDialog.close();
    },
    clearArea() {
      this.selArea = null;
      this.selCity = null;
    },
    confirmSelArea() {
      this.showArea = false;
    },
    inputOrgPhone(e) {
      this.orgPhone = e.detail.value;
    },
    doClearPhone() {
      this.orgPhone = "";
    },
    // 获取字典
    async getDic() {
      try {
        const [right, area] = await Promise.all([
          common_js_net_shop.Shop.doGetGoodsDic("ce3ded65-68ed-4f42-89da-de1b813b8f7e"),
          // 证件类型
          common_js_net_shop.Shop.doGetAreaList("0")
        ]);
        if (right && area) {
          this.creType = right;
          this.areaList = area;
          this.selCreType = this.creType[0];
          this.selArea = this.areaList[0];
          this.doGetArea(this.selArea.areaId);
        }
      } catch (err) {
        this.msgHint = "数据加载错误,请稍后重试";
        this.msgType = "error";
        this.$refs.msg.open();
        setTimeout(() => {
          common_vendor.index.navigateBack();
        }, 1500);
      }
    },
    // 获取区域
    async doGetArea(pId) {
      common_vendor.index.showLoading({
        title: "加载中..."
      });
      try {
        const res = await common_js_net_shop.Shop.doGetAreaList(pId);
        common_vendor.index.hideLoading();
        if (res) {
          this.cityList = res;
          this.selCity = res[0];
        }
      } catch (err) {
        common_vendor.index.hideLoading();
        this.msgHint = "数据加载错误,请稍后重试";
        this.msgType = "error";
        this.$refs.msg.open();
      }
    },
    // 选中省市
    areaChange(e) {
      const currentValue = e.detail.value;
      const prevValue = this.prevValue;
      this.prevValue = currentValue;
      for (let i = 0; i < currentValue.length; i++) {
        if (currentValue[i] !== prevValue[i]) {
          if (i === 0) {
            const area = this.areaList[currentValue[0]];
            this.selArea = area;
            this.doGetArea(area.areaId);
          } else {
            const city = this.cityList[currentValue[1]];
            this.selCity = city;
          }
          break;
        }
      }
    },
    // 校验参数
    checkParams() {
      const rules = [
        {
          field: "selPersonType",
          message: "请选择受让人类别",
          validator: (v) => v !== null
        },
        {
          field: "orgName",
          message: "请输入姓名或机构名称",
          validator: (v) => typeof v === "string" && v.trim().length > 0
        },
        {
          field: ["selArea", "selCity"],
          message: "请选择所在省市",
          validator: (_, data) => data.selArea !== null && data.selCity !== null
        },
        {
          field: "orgPhone",
          message: "请输入合法的联系电话",
          validator: (v) => typeof v === "string" && v.trim().length > 0 && common_js_validator.isValidPhone(v.trim())
        },
        {
          field: "selCreType",
          message: "请选择证件类型",
          validator: (v) => v !== null
        },
        {
          field: "cardNumber",
          message: "请输入证件号码",
          validator: (v) => typeof v === "string" && v.trim().length > 0
        }
      ];
      for (const rule of rules) {
        let isValid;
        if (Array.isArray(rule.field)) {
          isValid = rule.validator(null, this);
        } else {
          const value = this[rule.field];
          isValid = rule.validator(value, this);
        }
        if (!isValid) {
          this.msgType = "error";
          this.msgHint = rule.message;
          this.$refs.msg.open();
          return false;
        }
      }
      return true;
    },
    buildParams() {
      const {
        selArea,
        selCity
      } = this;
      return {
        buyCity: `${selArea.areaId},${selCity.areaId}`,
        buyIdcard: this.cardNumber,
        buyIdcardType: this.selCreType.dataId,
        buyName: this.orgName,
        buyPhone: this.orgPhone,
        buyType: this.selPersonType.dataId
      };
    },
    backPageRefresh() {
      const pages = getCurrentPages();
      const beforePage = pages[pages.length - 2];
      beforePage.$vm.needRefresh = true;
      common_vendor.index.navigateBack();
    },
    // 提交
    async doSave() {
      try {
        const isLegal = this.checkParams();
        if (isLegal) {
          common_vendor.index.showLoading({
            title: "上报中..."
          });
          const data = this.buildParams();
          common_vendor.index.__f__("log", "at pages/shop/addAssigneeInfo/addAssigneeInfo.vue:437", data);
          await common_js_net_shop.Shop.doSaveBuyPersonInfo(this.orderId, data);
          common_vendor.index.hideLoading();
          this.msgHint = "上报成功";
          this.msgType = "success";
          this.$refs.msg.open();
          setTimeout(() => {
            this.backPageRefresh();
          }, 1200);
        }
      } catch (err) {
        common_vendor.index.__f__("log", "at pages/shop/addAssigneeInfo/addAssigneeInfo.vue:449", err);
        common_vendor.index.hideLoading();
        this.msgHint = err.msg || "网络错误,请稍后重试";
        this.msgType = "error";
        this.$refs.msg.open();
      }
    }
  }
};
if (!Array) {
  const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
  const _easycom_uni_popup_message2 = common_vendor.resolveComponent("uni-popup-message");
  (_easycom_uni_popup2 + _easycom_uni_popup_message2)();
}
const _easycom_uni_popup = () => "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
const _easycom_uni_popup_message = () => "../../../uni_modules/uni-popup/components/uni-popup-message/uni-popup-message.js";
if (!Math) {
  (_easycom_uni_popup + _easycom_uni_popup_message)();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_vendor.t($data.selPersonType ? $data.selPersonType.dataName : "请选择类别"),
    b: !$data.selPersonType ? 1 : "",
    c: $data.personType,
    d: common_vendor.o((...args) => $options.bindChangePersonType && $options.bindChangePersonType(...args)),
    e: $data.selPersonType
  }, $data.selPersonType ? {
    f: common_vendor.o((...args) => $options.clearPersonType && $options.clearPersonType(...args))
  } : {}, {
    g: $data.orgName ? 1 : "",
    h: common_vendor.o([($event) => $data.orgName = $event.detail.value, (...args) => $options.inputOrgName && $options.inputOrgName(...args)]),
    i: $data.orgName,
    j: common_vendor.t($data.selArea && $data.selCity ? $data.selArea.areaName + "/" + $data.selCity.areaName : "请选择所在省市"),
    k: common_vendor.o((...args) => $options.onShowArea && $options.onShowArea(...args)),
    l: !$data.selArea ? 1 : "",
    m: $data.selArea
  }, $data.selArea ? {
    n: common_vendor.o((...args) => $options.clearArea && $options.clearArea(...args))
  } : {}, {
    o: $data.orgPhone ? 1 : "",
    p: common_vendor.o([($event) => $data.orgPhone = $event.detail.value, (...args) => $options.inputOrgPhone && $options.inputOrgPhone(...args)]),
    q: $data.orgPhone,
    r: $data.orgPhone.length > 0
  }, $data.orgPhone.length > 0 ? {
    s: common_vendor.o((...args) => $options.doClearPhone && $options.doClearPhone(...args))
  } : {}, {
    t: common_vendor.t($data.selCreType ? $data.selCreType.dataName : "请选择证件类型"),
    v: !$data.selCreType ? 1 : "",
    w: $data.creType,
    x: common_vendor.o((...args) => $options.bindChangeCreType && $options.bindChangeCreType(...args)),
    y: $data.selCreType
  }, $data.selCreType ? {
    z: common_vendor.o((...args) => $options.clearCreType && $options.clearCreType(...args))
  } : {}, {
    A: $data.cardNumber ? 1 : "",
    B: common_vendor.o([($event) => $data.cardNumber = $event.detail.value, (...args) => $options.inputCardNumber && $options.inputCardNumber(...args)]),
    C: $data.cardNumber,
    D: common_vendor.o((...args) => $options.doSave && $options.doSave(...args)),
    E: common_vendor.o((...args) => $options.closeDialog && $options.closeDialog(...args)),
    F: common_vendor.f($data.areaList, (item, index, i0) => {
      return {
        a: index,
        b: common_vendor.t(item.areaName)
      };
    }),
    G: common_vendor.f($data.cityList, (item, index, i0) => {
      return {
        a: index,
        b: common_vendor.t(item.areaName)
      };
    }),
    H: common_vendor.o((...args) => $options.areaChange && $options.areaChange(...args)),
    I: common_vendor.o((...args) => $options.closeDialog && $options.closeDialog(...args)),
    J: common_vendor.sr("areaDialog", "dec5a290-0"),
    K: common_vendor.p({
      type: "bottom",
      ["background-color"]: "#fff",
      ["border-radius"]: "15rpx 15rpx 0rpx 0rpx"
    }),
    L: common_vendor.p({
      type: $data.msgType,
      duration: 2e3,
      message: $data.msgHint
    }),
    M: common_vendor.sr("msg", "dec5a290-1"),
    N: common_vendor.p({
      type: "message"
    })
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-dec5a290"]]);
swan.createPage(MiniProgramPage);
//# sourceMappingURL=../../../../.sourcemap/mp-baidu/pages/shop/addAssigneeInfo/addAssigneeInfo.js.map
