"use strict";
const common_vendor = require("../../common/vendor.js");
const api_user = require("../../api/user.js");
const _sfc_main = {
  data() {
    return {
      formData: {
        name: "",
        gender: "",
        phone: "",
        email: "",
        idCard: "",
        emergencyContact: "",
        emergencyPhone: "",
        relation: ""
      },
      relationOptions: ["父母", "配偶", "子女", "兄弟姐妹", "朋友", "其他"],
      relationIndex: 0,
      isAgreed: false,
      userInfoFilled: false,
      jobList: [],
      selectedJobId: null,
      selectedJob: null
    };
  },
  onLoad() {
    this.checkUserInfo();
    this.fetchJobList();
  },
  computed: {
    canSubmit() {
      return this.formData.name.trim() && this.formData.gender && this.formData.phone.trim() && /^1\d{10}$/.test(this.formData.phone) && (!this.formData.email || /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.formData.email)) && this.formData.idCard.trim() && this.formData.emergencyContact.trim() && this.formData.emergencyPhone.trim() && /^1\d{10}$/.test(this.formData.emergencyPhone) && this.formData.relation && this.isAgreed;
    }
  },
  methods: {
    goHome() {
      common_vendor.index.navigateBack({
        delta: 1
      });
    },
    toggleAgreement() {
      this.isAgreed = !this.isAgreed;
    },
    relationChange(e) {
      this.relationIndex = e.detail.value;
      this.formData.relation = this.relationOptions[this.relationIndex];
    },
    checkUserInfo() {
      common_vendor.index.showLoading({
        title: "加载中..."
      });
      console.log("id:", JSON.parse(common_vendor.index.getStorageSync("userInfo")).id);
      api_user.getUserInfo(JSON.parse(common_vendor.index.getStorageSync("userInfo")).id).then((res) => {
        common_vendor.index.hideLoading();
        if (res.code === 200 && res.data) {
          const userData = res.data;
          if (userData.name && userData.phone) {
            this.userInfoFilled = true;
            this.formData.name = userData.name || "";
            this.formData.gender = userData.gender || "";
            this.formData.phone = userData.phone || "";
            this.formData.idCard = userData.id_card || "";
            this.formData.emergencyContact = userData.emergency_contact || "";
            this.formData.emergencyPhone = userData.emergency_phone || "";
            this.formData.relation = userData.relation || "";
            if (userData.relation) {
              const index = this.relationOptions.findIndex((item) => item === userData.relation);
              if (index !== -1) {
                this.relationIndex = index;
              }
            }
            common_vendor.index.navigateTo({
              url: "/pages/success/success"
            });
          }
        }
      }).catch((err) => {
        common_vendor.index.hideLoading();
        console.error("获取用户信息失败:", err);
        common_vendor.index.showToast({
          title: "获取用户信息失败",
          icon: "none"
        });
      });
    },
    fetchJobList() {
      common_vendor.index.showLoading({
        title: "加载职位列表..."
      });
      api_user.getJobList().then((res) => {
        common_vendor.index.hideLoading();
        console.log(res);
        this.jobList = res;
      }).catch((err) => {
        common_vendor.index.hideLoading();
        console.error("获取职位列表失败:", err);
        common_vendor.index.showToast({
          title: "网络错误，请稍后重试",
          icon: "none"
        });
      });
    },
    selectJob(job) {
      this.selectedJobId = job.id;
      this.selectedJob = job;
    },
    applyForJob() {
      if (!this.selectedJobId) {
        common_vendor.index.showToast({
          title: "请选择一个职位",
          icon: "none"
        });
        return;
      }
      common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.selectedJob));
      api_user.applyJob({
        job_id: this.selectedJobId,
        tenant_id: 1,
        openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
      }).then((res) => {
        common_vendor.index.navigateTo({
          url: "/pages/interview-notice/interview-notice",
          fail: (err) => {
            console.error("页面跳转失败:", err);
            common_vendor.index.showToast({
              title: "页面跳转失败",
              icon: "none"
            });
          }
        });
      }).catch((err) => {
        console.error("申请职位失败:", err);
      });
    },
    submitForm() {
      if (!this.formData.name.trim()) {
        common_vendor.index.showToast({
          title: "请输入姓名",
          icon: "none"
        });
        return;
      }
      if (!this.formData.gender) {
        common_vendor.index.showToast({
          title: "请选择性别",
          icon: "none"
        });
        return;
      }
      if (!this.formData.phone.trim()) {
        common_vendor.index.showToast({
          title: "请输入手机号",
          icon: "none"
        });
        return;
      }
      if (!/^1\d{10}$/.test(this.formData.phone)) {
        common_vendor.index.showToast({
          title: "请输入正确的手机号",
          icon: "none"
        });
        return;
      }
      if (this.formData.email && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.formData.email)) {
        common_vendor.index.showToast({
          title: "请输入正确的邮箱",
          icon: "none"
        });
        return;
      }
      if (!this.formData.idCard.trim()) {
        common_vendor.index.showToast({
          title: "请输入身份证号",
          icon: "none"
        });
        return;
      }
      const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!idCardReg.test(this.formData.idCard)) {
        common_vendor.index.showToast({
          title: "请输入正确的身份证号",
          icon: "none"
        });
        return;
      }
      if (!this.formData.emergencyContact.trim()) {
        common_vendor.index.showToast({
          title: "请输入紧急联系人",
          icon: "none"
        });
        return;
      }
      if (!this.formData.emergencyPhone.trim()) {
        common_vendor.index.showToast({
          title: "请输入紧急联系人电话",
          icon: "none"
        });
        return;
      }
      if (!/^1\d{10}$/.test(this.formData.emergencyPhone)) {
        common_vendor.index.showToast({
          title: "请输入正确的紧急联系人电话",
          icon: "none"
        });
        return;
      }
      if (!this.formData.relation) {
        common_vendor.index.showToast({
          title: "请选择与紧急联系人关系",
          icon: "none"
        });
        return;
      }
      if (!this.isAgreed) {
        common_vendor.index.showToast({
          title: "请阅读并同意相关协议",
          icon: "none"
        });
        return;
      }
      const submitData = {
        openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "",
        name: this.formData.name,
        phone: this.formData.phone,
        id_card: this.formData.idCard,
        status: 1,
        source: "mini",
        examine: 0,
        tenant_id: "1",
        emergency_contact: this.formData.emergencyContact,
        emergency_phone: this.formData.emergencyPhone,
        relation: this.formData.relation,
        age: "20",
        job_id: this.selectedJobId
      };
      common_vendor.index.showLoading({
        title: "提交中..."
      });
      api_user.fillUserInfo(submitData).then((res) => {
        common_vendor.index.hideLoading();
        this.updateLocalUserInfo();
        common_vendor.index.showToast({
          title: "提交成功",
          icon: "success",
          duration: 1500,
          success: () => {
            setTimeout(() => {
              common_vendor.index.navigateTo({
                url: "/pages/success/success",
                fail: (err) => {
                  console.error("页面跳转失败:", err);
                  common_vendor.index.showToast({
                    title: "页面跳转失败",
                    icon: "none"
                  });
                }
              });
            }, 1500);
          }
        });
      }).catch((err) => {
        common_vendor.index.hideLoading();
        console.error("提交表单失败:", err);
        common_vendor.index.showToast({
          title: "网络错误，请稍后重试",
          icon: "none"
        });
      });
    },
    updateLocalUserInfo() {
      api_user.getUserInfo().then((res) => {
        if (res.code === 200 && res.data) {
          let userInfo = {};
          try {
            userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
          } catch (e) {
            console.error("解析本地存储用户信息失败:", e);
            userInfo = {};
          }
          const updatedUserInfo = {
            ...userInfo,
            ...res.data
          };
          common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
        }
      }).catch((err) => {
        console.error("更新本地用户信息失败:", err);
      });
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: !$data.userInfoFilled
  }, !$data.userInfoFilled ? {
    b: common_vendor.f($data.jobList, (job, index, i0) => {
      return {
        a: common_vendor.t(job.title),
        b: common_vendor.t(job.publish_date),
        c: common_vendor.t(job.location),
        d: index,
        e: $data.selectedJobId === job.id ? 1 : "",
        f: common_vendor.o(($event) => $options.selectJob(job), index)
      };
    }),
    c: !$data.selectedJobId,
    d: common_vendor.o((...args) => $options.applyForJob && $options.applyForJob(...args))
  } : {}, {
    e: $data.userInfoFilled
  }, $data.userInfoFilled ? {
    f: $data.formData.name,
    g: common_vendor.o(($event) => $data.formData.name = $event.detail.value),
    h: $data.formData.gender === "男" ? 1 : "",
    i: common_vendor.o(($event) => $data.formData.gender = "男"),
    j: $data.formData.gender === "女" ? 1 : "",
    k: common_vendor.o(($event) => $data.formData.gender = "女"),
    l: $data.formData.idCard,
    m: common_vendor.o(($event) => $data.formData.idCard = $event.detail.value),
    n: $data.formData.phone,
    o: common_vendor.o(($event) => $data.formData.phone = $event.detail.value),
    p: $data.formData.emergencyContact,
    q: common_vendor.o(($event) => $data.formData.emergencyContact = $event.detail.value),
    r: $data.formData.emergencyPhone,
    s: common_vendor.o(($event) => $data.formData.emergencyPhone = $event.detail.value),
    t: common_vendor.t($data.formData.relation || "请选择关系"),
    v: common_vendor.o((...args) => $options.relationChange && $options.relationChange(...args)),
    w: $data.relationIndex,
    x: $data.relationOptions,
    y: $data.isAgreed,
    z: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args)),
    A: !$options.canSubmit,
    B: common_vendor.o((...args) => $options.submitForm && $options.submitForm(...args))
  } : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
