"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_icon2 = common_vendor.resolveComponent("wd-icon");
  const _easycom_wd_upload2 = common_vendor.resolveComponent("wd-upload");
  const _easycom_wd_cell2 = common_vendor.resolveComponent("wd-cell");
  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_icon2 + _easycom_wd_upload2 + _easycom_wd_cell2 + _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_icon = () => "../../uni_modules/wot-design-uni/components/wd-icon/wd-icon.js";
const _easycom_wd_upload = () => "../../uni_modules/wot-design-uni/components/wd-upload/wd-upload.js";
const _easycom_wd_cell = () => "../../uni_modules/wot-design-uni/components/wd-cell/wd-cell.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_icon + _easycom_wd_upload + _easycom_wd_cell + _easycom_wd_cell_group + _easycom_wd_button + _easycom_wd_form)();
}
const _sfc_main = {
  __name: "authenticate",
  setup(__props) {
    const cardType = common_vendor.ref("");
    const formValue = common_vendor.ref({
      id: common_vendor.index.getStorageSync("carUserInfo").id,
      username: "",
      cardNumber: "",
      college: "",
      professor: "",
      // instructor: '',
      mobile: "",
      file1: "",
      file2: ""
    });
    common_vendor.ref({});
    const frontFileList = common_vendor.ref([]);
    const backFileList = common_vendor.ref([]);
    const formData2 = common_vendor.computed(() => ({
      type: "idCardBack"
      // 身份证反面标识，固定参数
    }));
    const form = common_vendor.ref(null);
    const ifXiu = common_vendor.ref(false);
    common_vendor.onLoad((options) => {
      console.log(options);
      if (options.cardType) {
        cardType.value = options.cardType;
        if (options.info) {
          ifXiu.value = true;
          common_vendor.index.setNavigationBarTitle({
            title: "修改认证"
          });
          formValue.value = JSON.parse(decodeURIComponent(options.info));
          console.log(formValue.value);
          if (formValue.value.file1) {
            frontFileList.value.push(formValue.value.file1);
          }
          if (formValue.value.file2) {
            backFileList.value.push(formValue.value.file2);
          }
        } else {
          ifXiu.value = false;
          common_vendor.index.setNavigationBarTitle({
            title: cardType.value
          });
        }
      }
    });
    const beforeChoose1 = async ({ file, formData, resolve }) => {
      const tempPath = file.path || file.url;
      const lastDotIndex = tempPath.lastIndexOf(".");
      const fileExt = lastDotIndex > -1 ? tempPath.slice(lastDotIndex + 1).toLowerCase() : "";
      console.log("文件信息：", file, "路径：", tempPath, "后缀：", fileExt);
      if (frontFileList.value.some((item) => item.name === file.name)) {
        common_vendor.index.showToast({ title: "当前图片已上传，请选择其他照片", icon: "none" });
        resolve(false);
        return;
      }
      if (file.size >= 5 * 1024 * 1024) {
        common_vendor.index.showToast({ title: "请上传小于5M的图片", icon: "none" });
        resolve(false);
        return;
      }
      if (!["png", "jpg", "jpeg"].includes(fileExt)) {
        common_vendor.index.showToast({ title: "请上传png/jpg/jpeg格式的图片", icon: "none" });
        resolve(false);
        return;
      }
      resolve({
        ...formData,
        allowUpload: true
        // 显式允许上传
      });
      try {
        const res = await utils_api.myUploadFile({
          url: "/filesload.action",
          filePath: file.path || file.url,
          // 临时文件路径
          name: "myFile",
          // 后端接收文件的字段名
          formData: formData2.value
          // 额外参数
        });
        if (res.code === 200) {
          frontFileList.value.push(res.data.url);
          common_vendor.index.showToast({ title: "上传成功", icon: "none" });
          console.log("上传成功，图片链接：", res.data.url, frontFileList);
        } else {
          common_vendor.index.showToast({ title: res.message || "上传失败2", icon: "none" });
        }
      } catch (err) {
        console.error("上传失败：", err);
        common_vendor.index.showToast({ title: "网络异常，请重试", icon: "none" });
      }
    };
    const handleFrontChange = async ({ fileList }) => {
      frontFileList.value = fileList;
    };
    const beforeChoose = async ({ file, formData, resolve }) => {
      const tempPath = file.path || file.url;
      const lastDotIndex = tempPath.lastIndexOf(".");
      const fileExt = lastDotIndex > -1 ? tempPath.slice(lastDotIndex + 1).toLowerCase() : "";
      console.log("文件信息：", file, "路径：", tempPath, "后缀：", fileExt);
      if (backFileList.value.some((item) => item.name === file.name)) {
        common_vendor.index.showToast({ title: "当前图片已上传，请选择其他照片", icon: "none" });
        resolve(false);
        return;
      }
      if (file.size >= 5 * 1024 * 1024) {
        common_vendor.index.showToast({ title: "请上传小于5M的图片", icon: "none" });
        resolve(false);
        return;
      }
      if (!["png", "jpg", "jpeg"].includes(fileExt)) {
        common_vendor.index.showToast({ title: "请上传png/jpg/jpeg格式的图片", icon: "none" });
        resolve(false);
        return;
      }
      resolve({
        ...formData,
        allowUpload: true
        // 显式允许上传
      });
      console.log("beforeChoose校验通过，允许上传");
      try {
        const res = await utils_api.myUploadFile({
          url: "/filesload.action",
          filePath: file.path || file.url,
          // 临时文件路径
          name: "myFile",
          // 后端接收文件的字段名
          formData: formData2.value
          // 额外参数
        });
        if (res.code === 200) {
          console.log(122);
          backFileList.value.push(res.data.url);
          common_vendor.index.showToast({ title: "上传成功", icon: "none" });
          console.log("上传成功，图片链接：", res.data.url, backFileList);
        } else {
          common_vendor.index.showToast({ title: res.message || "上传失败2", icon: "none" });
        }
      } catch (err) {
        console.error("上传失败：", err);
        common_vendor.index.showToast({ title: "网络异常，请重试", icon: "none" });
      }
    };
    const handleFileChange = ({ fileList }) => {
      backFileList.value = fileList;
    };
    const handleSubmit = () => {
      console.log(cardType.value, "k");
      if (cardType.value == "教师认证") {
        form.value.validate().then(({ valid, errors }) => {
          if (valid) {
            if (ifXiu.value) {
              submitEditReq();
            } else {
              submitAddReq();
            }
          }
        }).catch((error) => {
          console.log(error, "error");
        });
      } else if (cardType.value == "暖心认证") {
        form.value.validate().then(({ valid, errors }) => {
          if (valid) {
            if (ifXiu.value) {
              submitEditReqStu();
            } else {
              console.log(cardType.value, "k      xxx");
              submitAddReqStu();
            }
          }
        }).catch((error) => {
          console.log(error, "error");
        });
      }
    };
    const submitAddReq = async () => {
      const leng = frontFileList.value.length - 1;
      const leng2 = frontFileList.value.length - 1;
      if (frontFileList.value.length > 0) {
        formValue.value.file1 = frontFileList.value[leng];
      } else {
        formValue.value.file1 = "";
        common_vendor.index.showToast({ title: "请上传身份证正面照片", icon: "none" });
      }
      if (backFileList.value.length > 0) {
        formValue.value.file2 = backFileList.value[leng2];
      } else {
        formValue.value.file2 = "";
        common_vendor.index.showToast({ title: "请上传身份证反面照片", icon: "none" });
      }
      const requestData = {
        username: formValue.value.username,
        cardNumber: formValue.value.cardNumber,
        college: formValue.value.college,
        professor: formValue.value.professor,
        mobile: formValue.value.mobile,
        file1: formValue.value.file1,
        file2: formValue.value.file2
        // 先不包含 professor
      };
      console.log(formValue.value, "aaammmmm");
      const res = await utils_api.myRequest({
        url: "/vertifyinsertTea.action",
        method: "post",
        data: requestData
      });
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "success"
        });
        setTimeout(() => {
          common_vendor.index.reLaunch({
            url: "/pages/transportation/transportation"
          });
        }, 1500);
      }
    };
    const submitEditReq = async () => {
      frontFileList.value.length - 1;
      frontFileList.value.length - 1;
      const requestData = {
        id: formValue.value.id,
        username: formValue.value.username,
        cardNumber: formValue.value.cardNumber,
        college: formValue.value.college,
        professor: formValue.value.professor,
        mobile: formValue.value.mobile,
        file1: formValue.value.file1,
        file2: formValue.value.file2
        // 先不包含 professor
      };
      console.log(formValue.value, "aaammmmmxiu");
      const res = await utils_api.myRequest({
        url: "/vertifyupdateTea.action",
        method: "post",
        data: requestData
      });
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "success"
        });
        setTimeout(() => {
          common_vendor.index.reLaunch({
            url: "/pages/transportation/transportation"
          });
        }, 1500);
      } else {
        common_vendor.index.showToast({
          title: res.message,
          icon: "err"
        });
      }
    };
    const submitAddReqStu = async () => {
      frontFileList.value.length - 1;
      frontFileList.value.length - 1;
      ({
        username: formValue.value.username,
        cardNumber: formValue.value.cardNumber,
        // college: formValue.value.college,
        // instructor: formValue.value.instructor,
        mobile: formValue.value.mobile
        // file1: formValue.value.file1,
        // file2: formValue.value.file2
        // 先不包含 professor
      });
      console.log(formValue.value, "aaammmmm");
      const res = await utils_api.myRequest({
        url: "/vertifyinsertPtu.action",
        method: "post",
        data: formValue.value
      });
      console.log(res, "lll", res.code == 200);
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "success"
        });
        setTimeout(() => {
          common_vendor.index.reLaunch({
            url: "/pages/transportation/transportation"
          });
        }, 1500);
      } else {
        console.log(res, "lll");
        common_vendor.index.showToast({
          title: res.message,
          icon: "err"
        });
      }
    };
    const submitEditReqStu = async () => {
      const leng = frontFileList.value.length - 1;
      const leng2 = frontFileList.value.length - 1;
      if (frontFileList.value.length > 0) {
        formValue.value.file1 = frontFileList.value[leng];
      } else {
        formValue.value.file1 = "";
        common_vendor.index.showToast({ title: "请上传身份证正面照片", icon: "none" });
      }
      if (backFileList.value.length > 0) {
        formValue.value.file2 = backFileList.value[leng2];
      } else {
        formValue.value.file2 = "";
        common_vendor.index.showToast({ title: "请上传身份证反面照片", icon: "none" });
      }
      ({
        id: formValue.value.id,
        username: formValue.value.username,
        cardNumber: formValue.value.cardNumber,
        // college: formValue.value.college,
        // instructor: formValue.value.instructor,
        mobile: formValue.value.mobile
        // file1: formValue.value.file1,
        // file2: formValue.value.file2
        // 先不包含 professor
      });
      console.log(formValue.value, "aaammmmm");
      const res = await utils_api.myRequest({
        url: "/vertifyupdatePtu.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/transportation/transportation"
          });
        }, 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 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: cardType.value == "教师认证"
      }, cardType.value == "教师认证" ? {
        d: common_vendor.o(($event) => formValue.value.cardNumber = $event),
        e: common_vendor.p({
          label: "工号",
          ["label-width"]: "100px",
          prop: "cardNumber",
          clearable: true,
          placeholder: "请输入工号",
          rules: [{
            required: false,
            message: "请输入工号"
          }],
          modelValue: formValue.value.cardNumber
        })
      } : {}, {
        f: cardType.value == "暖心认证"
      }, cardType.value == "暖心认证" ? {
        g: common_vendor.o(($event) => formValue.value.cardNumber = $event),
        h: common_vendor.p({
          label: "学号",
          ["label-width"]: "100px",
          prop: "cardNumber",
          clearable: true,
          placeholder: "请输入学号",
          rules: [{
            required: false,
            message: "请输入学号"
          }],
          modelValue: formValue.value.cardNumber
        })
      } : {}, {
        i: cardType.value == "教师认证"
      }, cardType.value == "教师认证" ? {
        j: common_vendor.o(($event) => formValue.value.college = $event),
        k: common_vendor.p({
          label: "所在院系",
          ["label-width"]: "100px",
          prop: "college",
          clearable: true,
          placeholder: "请输入所在院系",
          rules: [{
            required: false,
            message: "请输入所在院系"
          }],
          modelValue: formValue.value.college
        })
      } : {}, {
        l: cardType.value == "教师认证"
      }, cardType.value == "教师认证" ? {
        m: common_vendor.o(($event) => formValue.value.professor = $event),
        n: common_vendor.p({
          label: "职称",
          ["label-width"]: "100px",
          prop: "professor",
          clearable: true,
          placeholder: "请输入职称",
          rules: [{
            required: false,
            message: "请输入职称"
          }],
          modelValue: formValue.value.professor
        })
      } : {}, {
        o: common_vendor.o(($event) => formValue.value.mobile = $event),
        p: common_vendor.p({
          label: "手机号",
          ["label-width"]: "100px",
          prop: "mobile",
          clearable: true,
          placeholder: "请输入手机号码",
          rules: [{
            required: false,
            validator: validatorPhone,
            message: "请输入手机号码"
          }],
          modelValue: formValue.value.mobile
        }),
        q: cardType.value == "教师认证"
      }, cardType.value == "教师认证" ? {
        r: common_vendor.p({
          color: "#ABA6A6",
          name: "photo",
          size: "27"
        }),
        s: common_vendor.o(handleFrontChange),
        t: common_vendor.p({
          ["file-list"]: frontFileList.value,
          ["custom-upload"]: _ctx.customUpload1,
          limit: 1,
          ["build-form-data"]: beforeChoose1,
          formData: formData2.value
        }),
        v: common_vendor.p({
          prop: "imgurl"
        })
      } : {}, {
        w: cardType.value == "教师认证"
      }, cardType.value == "教师认证" ? {
        x: common_vendor.p({
          color: "#ABA6A6",
          name: "photo",
          size: "27"
        }),
        y: common_vendor.o(handleFileChange),
        z: common_vendor.p({
          ["file-list"]: backFileList.value,
          ["custom-upload"]: _ctx.customUpload,
          limit: 1,
          ["build-form-data"]: beforeChoose,
          formData: formData2.value
        }),
        A: common_vendor.p({
          prop: "imgurlBack"
        })
      } : {}, {
        B: common_vendor.p({
          border: true
        }),
        C: common_vendor.o(handleSubmit),
        D: common_vendor.p({
          type: "primary",
          size: "large",
          block: true
        }),
        E: common_vendor.sr(form, "7a246550-0", {
          "k": "form"
        }),
        F: common_vendor.p({
          model: formValue.value
        })
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7a246550"], ["__file", "D:/创海项目/CH2025/约车小程序/yueche_app/pages/authenticate/authenticate.vue"]]);
wx.createPage(MiniProgramPage);
