<template>
  <div class="page">
    <!-- 顶部用户信息 -->
    <div class="user-card">
      <div class="row avatar-row">
        <span class="label">头像 </span>
        <!-- <image class="avatar" :src="userInfo.avatar" mode="aspectFill" /> -->
        <uni-icons type="contact" size="40" color="#ccc"></uni-icons>
      </div>
      <div class="row">
        <span class="label">昵称</span>
        <span class="value">{{ userInfo.nickname }}</span>
      </div>
      <div class="row">
        <span class="label">手机号</span>
        <span class="value">{{ userInfo.phone }}</span>
      </div>
    </div>

    <!-- 中间功能入口（循环渲染） -->
    <div class="menu-card">
      <div class="menu-item" v-for="(item, index) in menuList" :key="index" @click="handleMenuClick(item)">
        <span class="label">{{ item.label }}</span>
        <uni-icons type="right" size="14" color="#ccc"></uni-icons>
      </div>
    </div>

    <!-- 底部退出按钮 -->
    <span class="logout-btn" @click="handleLogout">退出登录</span>
  </div>
</template>

<script setup>
import { ref, defineEmits, defineExpose } from "vue";
import { getLoginUserPhone } from "@/api/login";
import { NOTICE_EVENT_LOGOUT } from "@/constants/constant";
import { useAppStore } from "@/store";

const appStore = useAppStore();
const emits = defineEmits(["close"]);

// 假数据
const userInfo = ref({
  avatar: "/static/default-avatar.png",
  nickname: "微信用户",
  phone: "",
});

// 功能菜单列表（带 type 区分操作类型）
const menuList = ref([
  // { label: '修改手机号', type: 'navigate', url: '/pages/change-phone/change-phone' },
  {
    label: "账号注销",
    type: "action",
    url: "cancelAccount",
  },
  // { label: '营业资质&协议', type: 'navigate', url: '/pages/agreement/agreement' },
  // { label: "联系客服", type: "action", action: "contactService" },
  // { label: "订阅消息", type: "action", action: "subscribeMessage" },
]);

const getLoginUserPhoneInfo = async () => {
  const res = await getLoginUserPhone();

  if (res.code === 0) {
    userInfo.value.phone = res.data;
  }
};

// 处理菜单点击
const handleMenuClick = (item) => {
  if (item.type === "navigate" && item.url) {
    uni.navigateTo({ url: item.url });
  } else if (item.type === "action") {
    if (item.action === "contactService") {
      uni.showToast({ title: "联系客服功能待实现", icon: "none" });
    } else if (item.action === "cancelAccount") {
      handleLogout();
    } else if (item.action === "subscribeMessage") {
      uni.requestSubscribeMessage({
        tmplIds: ["fMIt1q9GgM3Ep0DJSNgVPm4C3lCpQdz2TediETcv3iM"],
        success(res) {
          console.log(res);
        },
      });
    }
  }
};

// 退出登录
const handleLogout = () => { };

defineExpose({ getLoginUserPhoneInfo });
</script>

<style scoped lang="scss">
@import "./styles/index.scss";
</style>
