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

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

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

<script setup>
import { ref, onMounted, defineEmits } from "vue";
import { getLoginUserPhone } from "@/request/api/LoginApi";
import { NOTICE_EVENT_LOGOUT } from "@/constant/constant";
import { useAppStore } from "@/store";

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

// 假数据
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" },
]);

// 组件挂载时调用
onMounted(() => {
  getLoginUserPhoneInfo();
});

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 = () => {
  uni.showModal({
    title: "温馨提示",
    content: "确定要退出登录吗？",
    success: (res) => {
      if (res.confirm) {
        uni.clearStorageSync();
        appStore.setHasToken(false);
        emits("closeDrawer");
        uni.$emit(NOTICE_EVENT_LOGOUT);
      }
    },
  });
};
</script>

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