"use strict";
var common_vendor = require("../../common/vendor.js");
var tianjia = require("../../tianjia.js");
var config_index = require("../../config/index.js");
var api_index = require("../../api/index.js");
require("../../utils/request.js");
const _sfc_main = {
  setup(__props) {
    const rolesList = common_vendor.reactive([]);
    const getRoleList = () => {
      api_index.getAllRoles().then(({ status, data }) => {
        if (Number(status) === 200 && Array.isArray(data)) {
          rolesList.push(...data);
        }
      }).finally(getDraft);
    };
    const getDraft = () => {
      api_index.getDraftData().then(({ status, data }) => {
        if (Number(status) === 200) {
          const { title: tit, content, role_ids } = (data == null ? void 0 : data.draft_info) || {};
          const detail = typeof content === "string" ? JSON.parse(content) : {};
          imageList.push(...detail.images);
          title.value = tit;
          text.value = detail.text;
          const roles = rolesList.filter((role) => role_ids.includes(role.role_id));
          relateRole.push(...roles);
        }
      });
    };
    common_vendor.onMounted(getRoleList);
    const imageList = common_vendor.reactive([]);
    const onUploadFile = () => {
      common_vendor.index.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["album"],
        success: ({ tempFilePaths, tempFiles }) => {
          const [file] = tempFiles;
          const size = file.size / 1024 / 1024;
          if (size <= 1) {
            api_index.uploadImage(file).then(({ status, data }) => {
              if (Number(status) === 200 && data.file_id) {
                imageList.push(data.file_id);
              }
            });
          } else {
            common_vendor.index.showToast({
              title: "\u56FE\u7247\u5927\u5C0F\u4E0D\u80FD\u8D85\u8FC71MB",
              icon: "none"
            });
          }
        }
      });
    };
    const title = common_vendor.ref("");
    const text = common_vendor.ref("");
    const relateRole = common_vendor.reactive([]);
    const onChangeRole = (e) => {
      const curIndex = e.detail.value;
      const curSelectedRole = rolesList[curIndex];
      const existenceRole = relateRole.find((item) => item.role_id === curSelectedRole.role_id);
      if (!existenceRole) {
        relateRole.push(curSelectedRole);
      }
    };
    const onDeleteRole = (role) => {
      const filterRoles = relateRole.filter((item) => item.role_id !== role.role_id);
      relateRole.splice(0, relateRole.length);
      relateRole.push(...filterRoles);
    };
    const onSubmit = async (type) => {
      let errMessage = "";
      if (imageList.length === 0) {
        errMessage = "\u6700\u5C11\u4E0A\u4F20\u4E00\u5F20\u56FE\u7247";
      } else if (title.value === "") {
        errMessage = "\u8BF7\u586B\u5199\u6807\u9898";
      } else if (text.value === "") {
        errMessage = "\u8BF7\u586B\u5199\u5185\u5BB9";
      } else if (relateRole.length === 0) {
        errMessage = "\u8BF7\u9009\u62E9\u76F8\u5173\u89D2\u8272";
      }
      if (errMessage) {
        common_vendor.index.showToast({
          title: errMessage,
          icon: "none"
        });
      } else {
        const params = {
          title: title.value,
          role_ids: relateRole.map((role) => role.role_id),
          content: JSON.stringify({
            images: imageList,
            text: text.value
          })
        };
        const { status } = type === "release" ? await api_index.releaseContent(params) : await api_index.saveDraft(params);
        if (Number(status) === 200) {
          common_vendor.index.showToast({
            title: "\u4FDD\u5B58\u6210\u529F",
            icon: "none"
          });
          common_vendor.index.navigateBack();
        }
      }
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.f(common_vendor.unref(imageList), (item, k0, i0) => {
          return {
            a: `${common_vendor.unref(config_index.filePrefix)}${item}`,
            b: item
          };
        }),
        b: common_vendor.unref(imageList).length < 5
      }, common_vendor.unref(imageList).length < 5 ? {
        c: common_vendor.o(onUploadFile)
      } : {}, {
        d: title.value,
        e: common_vendor.o(($event) => title.value = $event.detail.value),
        f: text.value,
        g: common_vendor.o(($event) => text.value = $event.detail.value),
        h: common_vendor.f(common_vendor.unref(relateRole), (role, k0, i0) => {
          return {
            a: common_vendor.t(role.role_name),
            b: common_vendor.o(($event) => onDeleteRole(role)),
            c: role.role_id
          };
        }),
        i: tianjia._imports_0,
        j: common_vendor.unref(rolesList),
        k: common_vendor.o(onChangeRole),
        l: common_vendor.o(($event) => onSubmit("draft")),
        m: common_vendor.o(($event) => onSubmit("release"))
      });
    };
  }
};
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-40246838"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/community/release.vue"]]);
wx.createPage(MiniProgramPage);
