<script setup lang="ts">
const { user, updateProfile } = useAuthSession()

const form = reactive({
  username: user.value?.username || '',
  email: user.value?.email || '',
})

const saving = ref(false)
const success = ref(false)
const error = ref('')

async function handleSave() {
  saving.value = true
  error.value = ''
  success.value = false

  try {
    await updateProfile({
      username: form.username,
      email: form.email,
    })
    success.value = true
    setTimeout(() => { success.value = false }, 3000)
  } catch (err: any) {
    error.value = err?.data?.message || err?.message || '保存失败'
  } finally {
    saving.value = false
  }
}
</script>

<template>
  <div class="profile-page">
    <header class="page-header">
      <h1 class="page-title">个人资料</h1>
      <p class="page-subtitle">管理您的账户信息</p>
    </header>

    <div class="profile-card">
      <div class="avatar-section">
        <div class="avatar">
          {{ user?.username?.charAt(0).toUpperCase() || 'U' }}
        </div>
        <div class="avatar-info">
          <div class="avatar-name">{{ user?.username }}</div>
          <div class="avatar-role">管理员</div>
        </div>
      </div>

      <form class="profile-form" @submit.prevent="handleSave">
        <div v-if="error" class="alert alert-error">{{ error }}</div>
        <div v-if="success" class="alert alert-success">保存成功</div>

        <div class="form-group">
          <label class="form-label">用户名</label>
          <input
            v-model="form.username"
            type="text"
            class="form-input"
            placeholder="输入用户名"
          />
        </div>

        <div class="form-group">
          <label class="form-label">邮箱</label>
          <input
            v-model="form.email"
            type="email"
            class="form-input"
            placeholder="输入邮箱"
          />
        </div>

        <div class="form-actions">
          <button
            type="submit"
            class="btn-primary"
            :disabled="saving"
          >
            {{ saving ? '保存中...' : '保存修改' }}
          </button>
        </div>
      </form>
    </div>

    <section class="danger-section">
      <h2 class="section-title">危险区域</h2>
      <div class="danger-card">
        <div class="danger-info">
          <div class="danger-label">注销账户</div>
          <div class="danger-desc">永久删除您的账户和所有相关数据</div>
        </div>
        <button class="btn-danger">注销账户</button>
      </div>
    </section>
  </div>
</template>

<style scoped>
.profile-page {
  padding: 40px;
  max-width: 640px;
}

.page-header {
  margin-bottom: 32px;
}

.page-title {
  font-family: var(--font-display);
  font-size: 32px;
  font-weight: 400;
  color: var(--color-ink);
  letter-spacing: -0.3px;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 15px;
  color: var(--color-muted);
  margin: 0;
}

.profile-card {
  background: var(--color-surface-card);
  border-radius: 12px;
  overflow: hidden;
}

.avatar-section {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: var(--color-surface-soft);
  border-bottom: 1px solid var(--color-hairline);
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 12px;
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.avatar-name {
  font-size: 18px;
  font-weight: 500;
  color: var(--color-ink);
}

.avatar-role {
  font-size: 14px;
  color: var(--color-muted);
}

.profile-form {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.alert {
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
}

.alert-error {
  background: rgba(198, 69, 69, 0.08);
  color: var(--color-error);
}

.alert-success {
  background: rgba(93, 184, 114, 0.12);
  color: var(--color-success);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-body-strong);
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  font-size: 14px;
  color: var(--color-ink);
  background: var(--color-canvas);
  border: 1px solid var(--color-hairline);
  border-radius: 8px;
  transition: all 0.15s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(204, 120, 92, 0.15);
}

.form-actions {
  padding-top: 8px;
}

.btn-primary {
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  color: var(--color-on-primary);
  background: var(--color-primary);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn-primary:hover {
  background: var(--color-primary-active);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.danger-section {
  margin-top: 32px;
}

.section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0 0 12px 0;
}

.danger-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  background: var(--color-surface-card);
  border-radius: 12px;
  border: 1px solid rgba(198, 69, 69, 0.2);
}

.danger-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.danger-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-error);
}

.danger-desc {
  font-size: 13px;
  color: var(--color-muted);
}

.btn-danger {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--color-error);
  background: transparent;
  border: 1px solid var(--color-error);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-danger:hover {
  background: rgba(198, 69, 69, 0.08);
}

@media (max-width: 640px) {
  .profile-page {
    padding: 24px;
  }

  .danger-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .btn-danger {
    width: 100%;
  }
}
</style>
