"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");
    const $apifile = common_vendor.inject("$apifile");
    const config = common_vendor.ref({ logo: [], file: [] });
    const form = common_vendor.ref({ icon: [], card: [] });
    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 showList = common_vendor.ref([]);
    common_vendor.onLoad(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: "learnStatus", is_use: "0" });
      if (res.errcode === 0)
        learnStatusList.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.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;
          }
          form.value = res.data;
          common_vendor.index.setStorageSync("user", { ...res.data, role_type: user.value.role_type });
        }
      }
    };
    const deletePic = (event) => {
      const file = form.value[event.name].filter((i) => i.url !== event.file.url);
      form.value[event.name] = file;
    };
    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[event.name] = [...form.value[event.name], ...[result]];
    };
    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 bindDateChange = (e) => {
      form.value.birth = e.detail.value;
    };
    const validateObject = (obj) => {
      const errors2 = {};
      if (!obj.nick_name || obj.nick_name.trim() === "") {
        errors2.nick_name = "请填写昵称！";
      }
      if (!obj.birth || obj.birth.trim() === "") {
        errors2.birth = "请选择出生日期！";
      }
      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.is_show || obj.is_show.trim() === "") {
          errors2.is_show = "请选择是否公开！";
        }
        if (!obj.education || obj.education.trim() === "") {
          errors2.education = "请选择学历！";
        }
        if (!obj.college || obj.college.trim() === "") {
          errors2.college = "请输入院校！";
        }
        if (!obj.card || !obj.card.some((value) => value !== void 0)) {
          errors2.card = "请上传身份证照片！";
        }
      }
      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 == "education_name" || key == "learnStatus_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;
        data.status = "0";
        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();
          if (user.value.role_type == "Student") {
            common_vendor.index.showToast({
              title: "保存成功",
              icon: "success"
            });
          } else {
            common_vendor.index.showToast({
              title: "保存成功，等待管理员审核方可展示！",
              icon: "none"
            });
          }
        }
      }
    };
    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("educationChange", educationChange);
    common_vendor.provide("learnStatusChange", learnStatusChange);
    common_vendor.provide("bindDateChange", bindDateChange);
    common_vendor.provide("showList", showList);
    common_vendor.provide("genderList", genderList);
    common_vendor.provide("learnStatusList", learnStatusList);
    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);
