"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_api = require("../../utils/api.js");
if (!Array) {
  const _easycom_wd_input2 = common_vendor.resolveComponent("wd-input");
  const _easycom_wd_cell_group2 = common_vendor.resolveComponent("wd-cell-group");
  const _easycom_wd_button2 = common_vendor.resolveComponent("wd-button");
  const _easycom_wd_form2 = common_vendor.resolveComponent("wd-form");
  (_easycom_wd_input2 + _easycom_wd_cell_group2 + _easycom_wd_button2 + _easycom_wd_form2)();
}
const _easycom_wd_input = () => "../../uni_modules/wot-design-uni/components/wd-input/wd-input.js";
const _easycom_wd_cell_group = () => "../../uni_modules/wot-design-uni/components/wd-cell-group/wd-cell-group.js";
const _easycom_wd_button = () => "../../uni_modules/wot-design-uni/components/wd-button/wd-button.js";
const _easycom_wd_form = () => "../../uni_modules/wot-design-uni/components/wd-form/wd-form.js";
if (!Math) {
  (_easycom_wd_input + _easycom_wd_cell_group + _easycom_wd_button + _easycom_wd_form)();
}
const _sfc_main = {
  __name: "handlePeople",
  setup(__props) {
    console.log(common_vendor.index.getStorageSync("carUserInfo").id, "新增");
    const formValue = common_vendor.ref({
      uid: common_vendor.index.getStorageSync("carUserInfo").id,
      username: "",
      sfzh: "",
      mobile: "",
      cardNumber: ""
    });
    const form = common_vendor.ref(null);
    common_vendor.onLoad((options) => {
      if (options.info) {
        common_vendor.index.setNavigationBarTitle({
          title: "修改乘客信息"
        });
        formValue.value = JSON.parse(decodeURIComponent(options.info));
      } else {
        common_vendor.index.setNavigationBarTitle({
          title: "添加乘客信息"
        });
      }
    });
    function handleSubmit() {
      form.value.validate().then(({ valid, errors }) => {
        if (valid) {
          if (formValue.value.id) {
            submitEditReq();
          } else {
            submitAddReq();
          }
        }
      }).catch((error) => {
        console.log(error, "error");
      });
    }
    const submitAddReq = async () => {
      const res = await utils_api.myRequest({
        url: "/ctUserinsert.action",
        method: "post",
        data: formValue.value
      });
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "success"
        });
        setTimeout(() => {
          common_vendor.index.reLaunch({
            url: "/pages/commonPeople/commonPeople"
          });
        }, 1500);
      }
    };
    const submitEditReq = async () => {
      const res = await utils_api.myRequest({
        url: "/ctUserupdate.action",
        method: "post",
        data: formValue.value
      });
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "success"
        });
        setTimeout(() => {
          common_vendor.index.reLaunch({
            url: "/pages/commonPeople/commonPeople"
          });
        }, 1500);
      }
    };
    const validatorName = (val) => {
      const result = val.replace(/\s+/g, "");
      if (result) {
        if (result.length > 1 && result.length < 5) {
          return Promise.resolve();
        } else {
          return Promise.reject("姓名为2-4个字符");
        }
      }
    };
    const validatorNum = (val) => {
      if (val) {
        const cleanedId = val.replace(/\s+/g, "");
        const idReg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (!idReg.test(cleanedId)) {
          return Promise.reject("请输入有效的18位身份证号码");
        }
        const factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        const parityBits = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
        let sum = 0;
        const code = cleanedId.toUpperCase();
        for (let i = 0; i < 17; i++) {
          sum += parseInt(code.charAt(i)) * factors[i];
        }
        const checkCode = parityBits[sum % 11];
        if (code.charAt(17) !== checkCode) {
          return Promise.reject("身份证号码校验无效");
        }
        const year = parseInt(code.substr(6, 4));
        const month = parseInt(code.substr(10, 2));
        const day = parseInt(code.substr(12, 2));
        const birthDate = new Date(year, month - 1, day);
        if (birthDate.getFullYear() !== year || birthDate.getMonth() + 1 !== month || birthDate.getDate() !== day) {
          return Promise.reject("身份证号码中的出生日期无效");
        }
        return Promise.resolve();
      }
    };
    const validatorPhone = (val) => {
      const phoneReg = /^1[3-9]\d{9}$/;
      if (val) {
        if (phoneReg.test(val)) {
          return Promise.resolve();
        } else {
          return Promise.reject("手机号码格式错误");
        }
      }
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.o(($event) => formValue.value.username = $event),
        b: common_vendor.p({
          label: "中文姓名",
          ["label-width"]: "100px",
          prop: "username",
          clearable: true,
          placeholder: "请输入姓名",
          rules: [{
            required: false,
            validator: validatorName,
            message: "请输入姓名"
          }],
          modelValue: formValue.value.username
        }),
        c: common_vendor.o(($event) => formValue.value.sfzh = $event),
        d: common_vendor.p({
          label: "身份证",
          ["label-width"]: "100px",
          prop: "sfzh",
          clearable: true,
          placeholder: "请输入身份证件号码",
          rules: [{
            required: false,
            validator: validatorNum,
            message: "请输入身份证件号码"
          }],
          modelValue: formValue.value.sfzh
        }),
        e: common_vendor.o(($event) => formValue.value.mobile = $event),
        f: common_vendor.p({
          label: "手机号",
          ["label-width"]: "100px",
          prop: "mobile",
          clearable: true,
          placeholder: "请输入手机号码",
          rules: [{
            required: false,
            validator: validatorPhone,
            message: "请输入手机号码"
          }],
          modelValue: formValue.value.mobile
        }),
        g: common_vendor.o(($event) => formValue.value.cardNumber = $event),
        h: common_vendor.p({
          label: "学号",
          ["label-width"]: "100px",
          prop: "mobile",
          clearable: true,
          placeholder: "",
          modelValue: formValue.value.cardNumber
        }),
        i: !formValue.value.cardNumber
      }, !formValue.value.cardNumber ? {} : {}, {
        j: common_vendor.p({
          border: true
        }),
        k: common_vendor.o(handleSubmit),
        l: common_vendor.p({
          type: "primary",
          size: "large",
          block: true
        }),
        m: common_vendor.sr(form, "148e6f85-0", {
          "k": "form"
        }),
        n: common_vendor.p({
          model: formValue.value
        })
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-148e6f85"], ["__file", "D:/创海项目/CH2025/约车小程序/yueche_app/pages/handlePeople/handlePeople.vue"]]);
wx.createPage(MiniProgramPage);
