"use strict";
const common_vendor = require("../../common/vendor.js");
if (!Math) {
  (teacher + student)();
}
const teacher = () => "./path/teacher.js";
const student = () => "./path/student.js";
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const $api = common_vendor.inject("$api");
    common_vendor.inject("$config");
    const $apifile = common_vendor.inject("$apifile");
    const config = common_vendor.ref({ logo: [], file: [] });
    const form = common_vendor.ref({ icon: [] });
    const errors = common_vendor.ref({});
    const user = common_vendor.computed(() => {
      return common_vendor.index.getStorageSync("user");
    });
    const genderList = common_vendor.ref([]);
    const educationList = common_vendor.ref([]);
    const learnStatusList = common_vendor.ref([]);
    const gradeList = common_vendor.ref([]);
    const cardTypeList = common_vendor.ref([]);
    const subjectList = common_vendor.ref([]);
    const showList = common_vendor.ref([]);
    common_vendor.onShow(async () => {
      await searchConfig();
      await searchOther();
      await search();
    });
    const searchConfig = async () => {
      config.value = common_vendor.index.getStorageSync("config");
    };
    const searchOther = async () => {
      let res;
      res = await $api(`dictData`, "GET", { code: "gender", is_use: "0" });
      if (res.errcode === 0)
        genderList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "education", is_use: "0" });
      if (res.errcode === 0)
        educationList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "grade", is_use: "0" });
      if (res.errcode === 0)
        gradeList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "learnStatus", is_use: "0" });
      if (res.errcode === 0)
        learnStatusList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "cardType", is_use: "0" });
      if (res.errcode === 0)
        cardTypeList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "subject", is_use: "0" });
      if (res.errcode === 0)
        subjectList.value = res.data;
      res = await $api(`dictData`, "GET", { code: "show", is_use: "0" });
      if (res.errcode === 0)
        showList.value = res.data;
    };
    const search = async () => {
      if (user && user.value._id) {
        let res;
        if (user.value.role_type == "Teacher")
          res = await $api(`teacher/${user.value._id}`, "GET", {});
        else
          res = await $api(`student/${user.value._id}`, "GET", {});
        if (res.errcode == "0") {
          if (res.data.cardType) {
            const data = cardTypeList.value.find((i) => i.value === res.data.cardType);
            if (data)
              res.data.cardType_name = data.label;
          }
          if (res.data.education) {
            const data = educationList.value.find((i) => i.value === res.data.education);
            if (data)
              res.data.education_name = data.label;
          }
          if (res.data.learnStatus) {
            const data = learnStatusList.value.find((i) => i.value === res.data.learnStatus);
            if (data)
              res.data.learnStatus_name = data.label;
          }
          if (res.data.grade) {
            const data = gradeList.value.find((i) => i.value === res.data.grade);
            if (data)
              res.data.grade_name = data.label;
          }
          if (res.data.subject) {
            const data = subjectList.value.find((i) => i.value === res.data.subject);
            if (data)
              res.data.subject_name = data.label;
          }
          form.value = res.data;
          common_vendor.index.setStorageSync("user", { ...res.data, role_type: user.value.role_type });
        }
      }
    };
    const deletePic = (event) => {
      form.value.icon = [];
    };
    const afterRead = async (event) => {
      const url = event.file[0].url;
      const result = await $apifile(`/web/learn_user/upload`, "file", url, "file");
      if (result.errcode === 0)
        form.value.icon = [result];
    };
    const cardChange = (e) => {
      const data = cardTypeList.value[e.detail.value];
      if (data) {
        form.value.cardType = data.value;
        form.value.cardType_name = data.label;
      }
    };
    const educationChange = (e) => {
      const data = educationList.value[e.detail.value];
      if (data) {
        form.value.education = data.value;
        form.value.education_name = data.label;
      }
    };
    const learnStatusChange = (e) => {
      const data = learnStatusList.value[e.detail.value];
      if (data) {
        form.value.learnStatus = data.value;
        form.value.learnStatus_name = data.label;
      }
    };
    const gradeChange = (e) => {
      const data = gradeList.value[e.detail.value];
      if (data) {
        form.value.grade = data.value;
        form.value.grade_name = data.label;
      }
    };
    const subjectChange = (e) => {
      const data = subjectList.value[e.detail.value];
      if (data) {
        form.value.subject = data.value;
        form.value.subject_name = data.label;
      }
    };
    const validateObject = (obj) => {
      const errors2 = {};
      if (!obj.nick_name || obj.nick_name.trim() === "") {
        errors2.nick_name = "请填写昵称！";
      }
      if (!obj.age || obj.age.trim() === "") {
        errors2.age = "请填写年龄！";
      }
      if (!obj.gender || obj.gender.trim() === "") {
        errors2.gender = "请填写性别！";
      }
      if (!obj.phone || obj.phone.trim() === "") {
        errors2.phone = "请填写手机号！";
      } else {
        const regex = /^1[3456789]\d{9}$/;
        if (!regex.test(obj.phone))
          errors2.phone = "请填写正确的手机号码！";
      }
      if (user.value.role_type == "Teacher") {
        if (!obj.cardType || obj.cardType.trim() === "") {
          errors2.cardType = "请选择证件类型！";
        }
        if (!obj.card || obj.card.trim() === "") {
          errors2.card = "请填写证件号码！";
        } else {
          let regex;
          if (obj.cardType == "0")
            regex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}([0-9]|X)$/;
          else
            regex = /^\d{9}$/;
          if (!regex.test(obj.card))
            errors2.card = "请填写正确的证件号码！";
        }
        if (!obj.is_show || obj.is_show.trim() === "") {
          errors2.is_show = "请选择是否公开！";
        }
      }
      if (Object.keys(errors2).length > 0) {
        return errors2;
      }
      return null;
    };
    const mergeObjectsWithoutDuplicates = async (obj1, obj2) => {
      return Object.keys({ ...obj1, ...obj2 }).reduce((acc, key) => {
        if (!acc.hasOwnProperty(key)) {
          if (key == "cardType_name" || key == "education_name" || key == "learnStatus_name" || key == "grade_name" || key == "subject_name") {
            delete acc[key];
          } else {
            acc[key] = obj1.hasOwnProperty(key) ? obj1[key] : obj2[key];
          }
        }
        return acc;
      }, {});
    };
    const formSubmit = async (e) => {
      const data = await mergeObjectsWithoutDuplicates(e.detail.value, form.value);
      const errorsInfo = await validateObject(data);
      if (errorsInfo) {
        errors.value = errorsInfo;
        for (const key in errorsInfo) {
          if (errorsInfo.hasOwnProperty(key)) {
            console.error(`${key} 错误: ${errorsInfo[key]}`);
          }
        }
      } else {
        errors.value = {};
        let res;
        if (user.value.role_type == "Teacher")
          res = await $api(`teacher/${form.value._id}`, "POST", data);
        else
          res = await $api(`student/${form.value._id}`, "POST", data);
        if (res.errcode == "0") {
          await search();
          common_vendor.index.showToast({
            title: "保存成功",
            icon: "success"
          });
        }
      }
    };
    common_vendor.provide("form", form);
    common_vendor.provide("errors", errors);
    common_vendor.provide("deletePic", deletePic);
    common_vendor.provide("afterRead", afterRead);
    common_vendor.provide("formSubmit", formSubmit);
    common_vendor.provide("cardChange", cardChange);
    common_vendor.provide("educationChange", educationChange);
    common_vendor.provide("learnStatusChange", learnStatusChange);
    common_vendor.provide("gradeChange", gradeChange);
    common_vendor.provide("subjectChange", subjectChange);
    common_vendor.provide("showList", showList);
    common_vendor.provide("genderList", genderList);
    common_vendor.provide("gradeList", gradeList);
    common_vendor.provide("subjectList", subjectList);
    common_vendor.provide("learnStatusList", learnStatusList);
    common_vendor.provide("cardTypeList", cardTypeList);
    common_vendor.provide("educationList", educationList);
    return (_ctx, _cache) => {
      return {
        a: common_vendor.unref(user).role_type == "Teacher",
        b: common_vendor.unref(user).role_type == "Student"
      };
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e33b06dd"], ["__file", "D:/project/学吧/learn_applet/pagesMy/account/index.vue"]]);
wx.createPage(MiniProgramPage);
