<template>
  <div class="user-profile">
    <div class="user-card" v-if="authStore.isLoggedIn && user">
      <van-image
        round
        width="80"
        height="80"
        :src="user.avatar || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'"
      />
      <div class="info">
        <h3>{{ user.name }}</h3>
        <p>{{ user.role === 1 ? '管理员' : '学生' }}</p>
        <p v-if="user.studentId">ID: {{ user.studentId }}</p>
      </div>
    </div>

    <div class="user-card" v-else @click="router.push('/login')">
      <van-image
        round
        width="80"
        height="80"
        src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"
      />
      <div class="info">
        <h3>未登录</h3>
        <p>点击登录/注册</p>
      </div>
    </div>

    <van-cell-group class="menu-group" inset v-if="authStore.isLoggedIn">
      <van-cell title="我的报名" is-link to="/registrations/my" />
      <van-cell title="我的评价" is-link to="/reviews/my" />
      <!-- <van-cell title="修改密码" is-link to="/auth/password" /> -->
    </van-cell-group>

    <van-cell-group class="menu-group" inset v-if="user?.role === 1">
      <van-cell title="管理后台" is-link to="/admin/dashboard" icon="setting-o" />
    </van-cell-group>

    <div class="logout-btn" v-if="authStore.isLoggedIn">
      <van-button block color="#ee0a24" @click="handleLogout">退出登录</van-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import { showDialog } from 'vant';

const router = useRouter();
const authStore = useAuthStore();
const user = computed(() => authStore.user);

const handleLogout = () => {
  showDialog({
    title: '退出登录',
    message: '确定要退出登录吗？',
    showCancelButton: true,
  }).then(() => {
    authStore.logout();
    router.replace('/login');
  }).catch(() => {
    // on cancel
  });
};
</script>

<style scoped lang="scss">
.user-profile {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding-top: 20px;

  .user-card {
    display: flex;
    align-items: center;
    background: #fff;
    padding: 20px;
    margin: 0 16px 20px;
    border-radius: 8px;
    
    .info {
      margin-left: 16px;
      
      h3 {
        margin: 0 0 5px;
        font-size: 18px;
      }
      
      p {
        margin: 0;
        font-size: 14px;
        color: #666;
      }
    }
  }

  .menu-group {
    margin-bottom: 20px;
  }

  .logout-btn {
    padding: 0 16px;
  }
}
</style>
