"use strict";
const common_vendor = require("../../common/vendor.js");
if (!Array) {
  const _easycom_up_input2 = common_vendor.resolveComponent("up-input");
  const _easycom_up_form_item2 = common_vendor.resolveComponent("up-form-item");
  const _easycom_up_icon2 = common_vendor.resolveComponent("up-icon");
  const _easycom_up_textarea2 = common_vendor.resolveComponent("up-textarea");
  const _easycom_up_radio2 = common_vendor.resolveComponent("up-radio");
  const _easycom_up_radio_group2 = common_vendor.resolveComponent("up-radio-group");
  const _easycom_up_form2 = common_vendor.resolveComponent("up-form");
  const _easycom_up_datetime_picker2 = common_vendor.resolveComponent("up-datetime-picker");
  (_easycom_up_input2 + _easycom_up_form_item2 + _easycom_up_icon2 + _easycom_up_textarea2 + _easycom_up_radio2 + _easycom_up_radio_group2 + _easycom_up_form2 + _easycom_up_datetime_picker2)();
}
const _easycom_up_input = () => "../../node-modules/uview-plus/components/u-input/u-input.js";
const _easycom_up_form_item = () => "../../node-modules/uview-plus/components/u-form-item/u-form-item.js";
const _easycom_up_icon = () => "../../node-modules/uview-plus/components/u-icon/u-icon.js";
const _easycom_up_textarea = () => "../../node-modules/uview-plus/components/u-textarea/u-textarea.js";
const _easycom_up_radio = () => "../../node-modules/uview-plus/components/u-radio/u-radio.js";
const _easycom_up_radio_group = () => "../../node-modules/uview-plus/components/u-radio-group/u-radio-group.js";
const _easycom_up_form = () => "../../node-modules/uview-plus/components/u-form/u-form.js";
const _easycom_up_datetime_picker = () => "../../node-modules/uview-plus/components/u-datetime-picker/u-datetime-picker.js";
if (!Math) {
  (_easycom_up_input + _easycom_up_form_item + _easycom_up_icon + _easycom_up_textarea + _easycom_up_radio + _easycom_up_radio_group + _easycom_up_form + _easycom_up_datetime_picker)();
}
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "add",
  setup(__props) {
    const $api = common_vendor.inject("$api");
    const user = common_vendor.computed(() => {
      return common_vendor.index.getStorageSync("user");
    });
    const id = common_vendor.ref("");
    const form = common_vendor.ref({ name: "", subject: "", grade: "", type: "", money: "", start_time: "", end_time: "", is_show: "" });
    const rules = common_vendor.ref({
      name: [{ required: true, message: "请输入姓名", trigger: ["blur", "change"] }],
      subject: [{ required: true, message: "请选择学科", trigger: ["blur", "change"] }],
      grade: [{ required: true, message: "请选择年级", trigger: ["blur", "change"] }],
      type: [{ required: true, message: "请选择类型", trigger: ["blur", "change"] }],
      money: [{ required: true, message: "请输入金额", trigger: ["blur", "change"] }],
      start_time: [{ required: true, message: "请选择开始时间", trigger: ["blur", "change"] }],
      end_time: [
        { required: true, message: "请选择结束时间", trigger: ["blur", "change"] },
        { validator: (rule, value, callback) => {
          return common_vendor.hooks(form.value.start_time).isBefore(form.value.end_time);
        }, message: "开始时间必须大于结束时间！" }
      ],
      is_show: [{ required: true, message: "请选择是否公开", trigger: ["blur", "change"] }]
    });
    const uFormRef = common_vendor.ref(null);
    const subjectList = common_vendor.ref([]);
    const gradeList = common_vendor.ref([]);
    const showList = common_vendor.ref([]);
    const typeList = common_vendor.ref([]);
    const showStart = common_vendor.ref(false);
    const showEnd = common_vendor.ref(false);
    common_vendor.onLoad(async (options) => {
      id.value = options && options.id;
      await searchOther();
      await search();
    });
    common_vendor.onReady(() => {
      uFormRef.value.setRules(rules.value);
    });
    const searchOther = async () => {
      let res;
      res = await $api(`dictData`, "GET", { code: "subject", is_use: "0" });
      if (res.errcode === 0)
        subjectList.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: "courseType", is_use: "0" });
      if (res.errcode === 0)
        typeList.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 (id.value) {
        const res = await $api(`course/${id.value}`, "GET", {});
        if (res.errcode === 0) {
          if (res.data.subject) {
            const data = subjectList.value.find((i) => i.value === res.data.subject);
            if (data)
              res.data.subject_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.type) {
            const data = typeList.value.find((i) => i.value === res.data.type);
            if (data)
              res.data.type_name = data.label;
          }
          form.value = res.data;
        } else {
          common_vendor.index.showToast({
            title: res.errmsg || "",
            icon: "error"
          });
        }
      }
    };
    const typeChange = (e) => {
      const data = typeList.value[e.detail.value];
      if (data) {
        form.value.type = data.value;
        form.value.type_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 gradeChange = (e) => {
      const data = gradeList.value[e.detail.value];
      if (data) {
        form.value.grade = data.value;
        form.value.grade_name = data.label;
      }
    };
    const StartClose = async () => {
      showStart.value = false;
      uFormRef.value.validateField("start_time");
    };
    const StartConfirm = async (e) => {
      showStart.value = false;
      form.value.start_time = common_vendor.index.$u.timeFormat(e.value, "yyyy-mm-dd hh:MM");
      uFormRef.value.validateField("start_time");
    };
    const EndClose = async () => {
      showEnd.value = false;
      uFormRef.value.validateField("end_time");
    };
    const EndConfirm = async (e) => {
      showEnd.value = false;
      form.value.end_time = common_vendor.index.$u.timeFormat(e.value, "yyyy-mm-dd hh:MM");
      uFormRef.value.validateField("end_time");
    };
    const submit = async () => {
      uFormRef.value.validate().then(async (valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(form.value));
          data.teacher = user.value._id;
          delete data.subject_name;
          delete data.grade_name;
          delete data.type_name;
          let res;
          if (id.value)
            res = await $api(`course/${id.value}`, "POST", data);
          else
            res = await $api(`course`, "POST", data);
          if (res.errcode == "0") {
            common_vendor.index.showToast({
              title: "维护信息成功",
              icon: "none"
            });
            common_vendor.index.navigateBack({
              delta: 1
            });
          } else {
            common_vendor.index.showToast({
              title: res.errmsg,
              icon: "none"
            });
          }
        } else {
          common_vendor.index.$u.toast("校验失败");
        }
      }).catch(() => {
        common_vendor.index.$u.toast("校验失败");
      });
    };
    const hideKeyboard = () => {
      common_vendor.index.hideKeyboard();
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.o(($event) => form.value.name = $event),
        b: common_vendor.p({
          placeholder: "请输入课程名称",
          modelValue: form.value.name
        }),
        c: common_vendor.p({
          label: "课程名称",
          labelWidth: "80",
          prop: "name",
          required: true
        }),
        d: common_vendor.t(form.value.subject_name || "请选择学科"),
        e: common_vendor.o(subjectChange),
        f: subjectList.value,
        g: common_vendor.p({
          label: "学科",
          prop: "subject",
          required: true
        }),
        h: common_vendor.t(form.value.grade_name || "请选择年级"),
        i: common_vendor.o(gradeChange),
        j: gradeList.value,
        k: common_vendor.p({
          label: "年级",
          prop: "grade",
          required: true
        }),
        l: common_vendor.t(form.value.type_name || "请选择年级"),
        m: common_vendor.o(typeChange),
        n: typeList.value,
        o: common_vendor.p({
          label: "类型",
          prop: "type",
          required: true
        }),
        p: common_vendor.o(($event) => form.value.money = $event),
        q: common_vendor.p({
          placeholder: "请输入金额(元)",
          modelValue: form.value.money
        }),
        r: common_vendor.p({
          label: "金额(元)",
          prop: "money",
          labelWidth: "80",
          required: true
        }),
        s: common_vendor.o(($event) => form.value.start_time = $event),
        t: common_vendor.p({
          disabled: true,
          disabledColor: "#ffffff",
          placeholder: "请选择开始时间",
          modelValue: form.value.start_time
        }),
        v: common_vendor.p({
          name: "arrow-right"
        }),
        w: common_vendor.o(($event) => {
          showStart.value = true;
          hideKeyboard();
        }),
        x: common_vendor.p({
          label: "开始时间",
          labelWidth: "80",
          prop: "start_time",
          required: true
        }),
        y: common_vendor.o(($event) => form.value.end_time = $event),
        z: common_vendor.p({
          disabled: true,
          disabledColor: "#ffffff",
          placeholder: "请选择结束时间",
          modelValue: form.value.end_time
        }),
        A: common_vendor.p({
          name: "arrow-right"
        }),
        B: common_vendor.o(($event) => {
          showEnd.value = true;
          hideKeyboard();
        }),
        C: common_vendor.p({
          label: "结束时间",
          labelWidth: "80",
          prop: "end_time",
          required: true
        }),
        D: common_vendor.o(($event) => form.value.brief = $event),
        E: common_vendor.p({
          placeholder: "请输入简介",
          count: true,
          modelValue: form.value.brief
        }),
        F: common_vendor.p({
          label: "简介",
          prop: "brief"
        }),
        G: common_vendor.f(showList.value, (item, index, i0) => {
          return {
            a: index,
            b: "abcf3dc4-18-" + i0 + ",abcf3dc4-17",
            c: common_vendor.p({
              customStyle: {
                marginRight: "16px"
              },
              label: item.label,
              name: item.value
            })
          };
        }),
        H: common_vendor.o(($event) => form.value.is_show = $event),
        I: common_vendor.p({
          placement: "row",
          modelValue: form.value.is_show
        }),
        J: common_vendor.p({
          label: "是否公开",
          labelWidth: "80",
          prop: "is_show",
          required: true
        }),
        K: common_vendor.sr(uFormRef, "abcf3dc4-0", {
          "k": "uFormRef"
        }),
        L: common_vendor.p({
          model: form.value,
          rules: rules.value
        }),
        M: common_vendor.o(submit),
        N: common_vendor.o(StartConfirm),
        O: common_vendor.o(StartClose),
        P: common_vendor.o(StartClose),
        Q: common_vendor.p({
          show: showStart.value,
          value: form.value.start_time,
          mode: "datetime",
          closeOnClickOverlay: true,
          minDate: Number(/* @__PURE__ */ new Date())
        }),
        R: common_vendor.o(EndConfirm),
        S: common_vendor.o(EndClose),
        T: common_vendor.o(EndClose),
        U: common_vendor.p({
          show: showEnd.value,
          value: form.value.end_time,
          mode: "datetime",
          closeOnClickOverlay: true,
          minDate: Number(/* @__PURE__ */ new Date())
        })
      };
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-abcf3dc4"], ["__file", "D:/project/学吧/learn_applet/pagesMy/course/add.vue"]]);
wx.createPage(MiniProgramPage);
