"use strict";
const common_vendor = require("../../common/vendor.js");
if (!Array) {
  const _easycom_up_upload2 = common_vendor.resolveComponent("up-upload");
  _easycom_up_upload2();
}
const _easycom_up_upload = () => "../../node-modules/uview-plus/components/u-upload/u-upload.js";
if (!Math) {
  _easycom_up_upload();
}
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const $api = common_vendor.inject("$api");
    const $apifile = common_vendor.inject("$apifile");
    const form = common_vendor.ref({ file: [] });
    const typeList = common_vendor.ref([]);
    const errors = common_vendor.ref({});
    const user = common_vendor.computed(() => {
      return common_vendor.index.getStorageSync("user");
    });
    common_vendor.onShow(async () => {
      await searchOther();
    });
    const searchOther = async () => {
      let res;
      res = await $api(`dictData`, "GET", { code: "opinionType", is_use: "0" });
      if (res.errcode === 0)
        typeList.value = res.data;
    };
    const typeChange = (e) => {
      const data = typeList.value[e.detail.value];
      if (data) {
        form.value.type = data.value;
        form.value.type_name = data.label;
      }
    };
    const deletePic = (event) => {
      const file = form.value.file.filter((i) => i.url !== event.file.url);
      form.value.file = file;
    };
    const afterRead = async (event) => {
      const url = event.file[0].url;
      const result = await $apifile(`/web/learn_opinion/upload`, "file", url, "file");
      if (result.errcode === 0)
        form.value.file = [...form.value.file, ...[result]];
    };
    const validateObject = (obj) => {
      const errors2 = {};
      if (!obj.type || obj.type.trim() === "") {
        errors2.type = "请填写问题类型！";
      }
      if (!obj.brief || obj.brief.trim() === "") {
        errors2.brief = "请填写问题描述！";
      }
      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 == "type_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 = {};
        data.user = user.value._id;
        if (user.value.role_type == "Student")
          data.userType = "1";
        else
          data.userType = "0";
        data.time = common_vendor.hooks().format("YYYY-MM-DD HH:mm:ss");
        const res = await $api(`opinion`, "POST", data);
        if (res.errcode == "0") {
          common_vendor.index.showToast({
            title: "反馈成功！",
            icon: "success"
          });
          common_vendor.index.navigateBack({
            delta: 1
          });
        }
      }
    };
    const toView = () => {
      common_vendor.index.navigateTo({
        url: `/pagesMy/opinion/list`
      });
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.t(form.value.type_name || "请选择 >"),
        b: common_vendor.o(typeChange),
        c: typeList.value,
        d: errors.value.type
      }, errors.value.type ? {
        e: common_vendor.t(errors.value.type)
      } : {}, {
        f: form.value.brief,
        g: errors.value.brief
      }, errors.value.brief ? {
        h: common_vendor.t(errors.value.brief)
      } : {}, {
        i: common_vendor.o(afterRead),
        j: common_vendor.o(deletePic),
        k: common_vendor.p({
          fileList: form.value.file,
          name: "icon",
          multiple: true,
          maxCount: 3
        }),
        l: common_vendor.o(formSubmit),
        m: common_vendor.o(toView)
      });
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-09ed81ef"], ["__file", "D:/project/学吧/learn_applet/pagesMy/opinion/index.vue"]]);
wx.createPage(MiniProgramPage);
