"use strict";
var common_vendor = require("../../common/vendor.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 userInfo = common_vendor.reactive({
      avatar: "",
      userName: ""
    });
    const showDialog = common_vendor.ref(false);
    const getUserInfo = () => {
      common_vendor.index.getStorage({
        key: "userData",
        success: ({ data }) => {
          const { avatar_id, username } = data.user_info || {};
          userInfo.avatar = avatar_id || config_index.defaultAvatar;
          userInfo.userName = username || "\u5FAE\u4FE1\u7528\u6237";
        }
      });
    };
    common_vendor.onShow(getUserInfo);
    const updateUserInfo = (user_info) => {
      const { token } = common_vendor.index.getStorageSync("userData");
      common_vendor.index.setStorage({
        key: "userData",
        data: {
          token,
          user_info
        },
        success: getUserInfo
      });
    };
    const onChangeAvatar = () => {
      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(async ({ data }) => {
              var _a;
              try {
                if (data.file_id) {
                  const res = await api_index.saveAvatar({ avatar_id: data.file_id });
                  if (Number(res.status) === 200) {
                    const user_info = ((_a = res.data) == null ? void 0 : _a.user_info) || {};
                    updateUserInfo(user_info);
                  }
                }
              } catch (e) {
                throw new Error(e);
              }
            }).catch(() => {
              common_vendor.index.showToast({
                title: "\u4E0A\u4F20\u56FE\u7247\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",
                icon: "none"
              });
            });
          } else {
            common_vendor.index.showToast({
              title: "\u56FE\u7247\u5927\u5C0F\u4E0D\u80FD\u8D85\u8FC71MB",
              icon: "none"
            });
          }
        }
      });
    };
    const showEditNameDialog = common_vendor.ref(false);
    const userName = common_vendor.ref("");
    const onSaveUserName = () => {
      if (userName.value) {
        api_index.saveUserName({ username: userName.value }).then(({ status, data }) => {
          if (Number(status) === 200) {
            const user_info = data.user_info || {};
            updateUserInfo(user_info);
          }
        }).catch(() => {
          common_vendor.index.showToast({
            title: "\u4FEE\u6539\u6635\u79F0\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",
            icon: "none"
          });
        }).finally(() => {
          showEditNameDialog.value = false;
          userName.value = "";
        });
      } else {
        common_vendor.index.showToast({
          title: "\u8BF7\u8F93\u5165\u6635\u79F0",
          icon: "none"
        });
      }
    };
    const toyNetwork = () => {
      common_vendor.index.navigateTo({
        url: "/pages/bluetooth/index"
      });
    };
    const onSubMenu = (type) => {
      if (type === "protocol") {
        showDialog.value = true;
      } else if (type === "aboutus") {
        common_vendor.index.navigateTo({
          url: "/pages/mine/aboutus"
        });
      } else {
        common_vendor.index.navigateTo({
          url: "/pages/mine/contactus"
        });
      }
    };
    return (_ctx, _cache) => {
      return {
        a: `${common_vendor.unref(config_index.filePrefix)}${common_vendor.unref(userInfo).avatar}`,
        b: common_vendor.o(onChangeAvatar),
        c: common_vendor.t(common_vendor.unref(userInfo).userName),
        d: common_vendor.o(($event) => showEditNameDialog.value = true),
        e: common_vendor.o(($event) => onSubMenu("aboutus")),
        f: common_vendor.o(toyNetwork),
        g: common_vendor.o(($event) => onSubMenu("contactus")),
        h: common_vendor.o(($event) => onSubMenu("protocol")),
        i: common_vendor.o(() => {
        }),
        j: showDialog.value,
        k: common_vendor.o(($event) => showDialog.value = false),
        l: userName.value,
        m: common_vendor.o(($event) => userName.value = $event.detail.value),
        n: common_vendor.o(onSaveUserName),
        o: common_vendor.o(() => {
        }),
        p: showEditNameDialog.value,
        q: common_vendor.o(($event) => showEditNameDialog.value = false)
      };
    };
  }
};
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-4bd6864f"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/mine/index.vue"]]);
wx.createPage(MiniProgramPage);
