<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { api, type UserProfile } from '../api/request'
import { clearAuth } from '../utils/auth'

const router = useRouter()

const profile = ref<UserProfile>({
  id: 0,
  username: '',
  created_at: '',
})

const loading = ref(false)
const usernameInitial = computed(() => (profile.value.username ? profile.value.username[0].toUpperCase() : '?'))

onMounted(async () => {
  loading.value = true
  try {
    const { data: profileData } = await api.me()
    profile.value = profileData
  } finally {
    loading.value = false
  }
})

function logout() {
  clearAuth()
  router.push('/login')
}
</script>

<template>
  <div class="page wechat-page">
    <div class="header">
      <h1 class="page-title">设置</h1>
      <p class="page-subtitle">账号与安全</p>
    </div>

    <section class="section">
      <div class="section-title">账号安全</div>
      <div class="group">
        <button class="profile-entry cell-button" @click="router.push('/settings/username')">
          <div class="avatar">{{ usernameInitial }}</div>
          <span class="cell-left profile-text">
            <span class="cell-title">{{ profile.username || '加载中' }}</span>
            <span class="cell-desc">点击修改用户名</span>
          </span>
          <span class="chevron">›</span>
        </button>
        <div class="divider"></div>
        <button class="cell cell-button" @click="router.push('/settings/password')">
          <span class="cell-left">
            <span class="cell-title">登录密码</span>
            <span class="cell-desc">点击进入修改界面</span>
          </span>
          <span class="cell-right">
            <span class="chevron">›</span>
          </span>
        </button>
      </div>
    </section>

    <button class="logout-row" @click="logout">
      退出登录
    </button>
  </div>
</template>

<style scoped>
.wechat-page {
  padding-top: 10px;
  background: #f5f5f7;
}

.header {
  margin-bottom: 14px;
}

.page-subtitle {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
}

.section {
  margin-top: 18px;
}

.section-title {
  font-size: 12px;
  color: #8a8a8f;
  margin: 0 0 8px 4px;
}

.group {
  background: #fff;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

.profile-entry {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border: none;
  background: #fff;
  text-align: left;
  cursor: pointer;
}

.profile-entry:active,
.cell-button:active {
  background: #f2f2f2;
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: linear-gradient(135deg, #68c3ff, #4f6ef7);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
}

.profile-text {
  flex: 1;
}

.cell {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 14px;
  border: none;
  background: #fff;
  text-align: left;
}

.cell-button {
  cursor: pointer;
}

.cell-left {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cell-title {
  font-size: 15px;
  color: var(--text);
}

.cell-desc {
  font-size: 12px;
  color: var(--muted);
}

.cell-right {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #b2b2b7;
}

.chevron {
  font-size: 20px;
  line-height: 1;
  margin-top: -1px;
}

.divider {
  height: 1px;
  background: #ececf0;
  margin-left: 14px;
}

.logout-row {
  width: 100%;
  margin-top: 18px;
  border: none;
  background: #fff;
  border-radius: 14px;
  padding: 14px 16px;
  font-size: 14px;
  color: #ff3b30;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
  cursor: pointer;
}

.logout-row:active {
  background: #f2f2f2;
}
</style>
