"use strict";
const common_vendor = require("../../common/vendor.js");
const api_dining = require("../../api/dining.js");
const store_user = require("../../store/user.js");
if (!Math) {
  BasicModal();
}
const BasicModal = () => "../../components/Modal/BasicModal.js";
const _sfc_main = {
  __name: "LoginModal",
  props: {
    open: {
      type: Boolean,
      default: false
    }
  },
  emits: ["update:open", "success", "cancel"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const agreed = common_vendor.ref(false);
    const userStore = store_user.useUserStore();
    const getOpen = common_vendor.computed({
      get: () => props.open,
      set: (val) => emit("update:open", val)
    });
    const toggleAgree = () => {
      agreed.value = !agreed.value;
    };
    const handleClose = () => {
      getOpen.value = false;
      emit("cancel");
    };
    const handleGetPhoneNumber = async (e) => {
      if (!agreed.value) {
        common_vendor.index.showToast({
          title: "请先同意用户协议和隐私协议",
          icon: "none"
        });
        return;
      }
      if (!e.detail.code) {
        common_vendor.index.showToast({
          title: "授权失败，请重试",
          icon: "none"
        });
        return;
      }
      try {
        common_vendor.index.login({
          success: async (loginRes) => {
            try {
              const res = await api_dining.DiningUserWechatLogin({
                code: loginRes.code,
                phoneCode: e.detail.code
              });
              if (res && res.token) {
                userStore.login(res);
                getOpen.value = false;
                emit("success", res);
              } else {
                common_vendor.index.showToast({
                  title: (res == null ? void 0 : res.msg) || "登录失败，请重试",
                  icon: "none"
                });
              }
            } catch (error) {
              common_vendor.index.__f__("error", "at pages/components/LoginModal.vue:115", "登录失败:", error);
              common_vendor.index.showToast({
                title: "登录失败，请重试",
                icon: "none"
              });
            }
          },
          fail: () => {
            common_vendor.index.showToast({
              title: "获取登录凭证失败",
              icon: "none"
            });
          }
        });
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/components/LoginModal.vue:130", "获取手机号失败:", error);
        common_vendor.index.showToast({
          title: "授权失败，请重试",
          icon: "none"
        });
      }
    };
    const handlePrivacy = () => {
      common_vendor.index.showToast({
        title: "隐私协议",
        icon: "none"
      });
    };
    const handleUserAgreement = () => {
      common_vendor.index.showToast({
        title: "用户协议",
        icon: "none"
      });
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.o(handleClose),
        b: common_vendor.o(handleGetPhoneNumber),
        c: !agreed.value,
        d: agreed.value
      }, agreed.value ? {} : {}, {
        e: agreed.value ? 1 : "",
        f: common_vendor.o(toggleAgree),
        g: common_vendor.o(handlePrivacy),
        h: common_vendor.o(handleUserAgreement),
        i: common_vendor.o(($event) => getOpen.value = $event),
        j: common_vendor.p({
          type: "bottom",
          isMack: true,
          open: getOpen.value
        })
      });
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-65cbbddc"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/components/LoginModal.js.map
