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

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

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,
      nickname: form.nickname,
    })
    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" :style="user?.avatar ? `background-image: url(${user.avatar}); background-size: cover;` : ''">
          <template v-if="!user?.avatar">{{ user?.username?.charAt(0).toUpperCase() || 'U' }}</template>
        </div>
        <div class="avatar-info">
          <div class="avatar-name">{{ user?.nickname || user?.username }}</div>
          <div class="avatar-role badge-admin">{{ user?.role === 'admin' ? '管理员' : '用户' }}</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-row">
          <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.nickname"
              type="text"
              class="form-input"
              placeholder="输入昵称"
            />
          </div>
        </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="section-row">
      <section class="card-section">
        <h2 class="section-title">安全设置</h2>
        <div class="card-list">
          <div class="list-item">
            <div class="list-item-icon">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                <path d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <div class="list-item-content">
              <div class="list-item-label">修改密码</div>
              <div class="list-item-desc">定期更换密码可以保护账户安全</div>
            </div>
            <button class="btn-outline">修改</button>
          </div>
        </div>
      </section>

      <section class="card-section">
        <h2 class="section-title">危险区域</h2>
        <div class="card-list">
          <div class="list-item list-item-danger">
            <div class="list-item-icon">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                <path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <div class="list-item-content">
              <div class="list-item-label">注销账户</div>
              <div class="list-item-desc">永久删除您的账户和所有相关数据</div>
            </div>
            <button class="btn-danger-outline">注销</button>
          </div>
        </div>
      </section>
    </section>
  </div>
</template>

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

.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;
  background-size: cover;
  background-position: 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);
}

.badge-admin {
  display: inline-flex;
  padding: 2px 10px;
  background: rgba(204, 120, 92, 0.12);
  color: var(--color-primary);
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 500;
  width: fit-content;
}

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

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.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;
}

.section-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 24px;
}

.card-section {
  background: var(--color-surface-card);
  border-radius: 12px;
  padding: 20px;
}

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

.card-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  background: var(--color-canvas);
  border-radius: 10px;
  border: 1px solid var(--color-hairline);
}

.list-item-danger {
  border-color: rgba(198, 69, 69, 0.2);
}

.list-item-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--color-surface-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.list-item-icon svg {
  width: 20px;
  height: 20px;
  color: var(--color-muted);
}

.list-item-danger .list-item-icon {
  background: rgba(198, 69, 69, 0.08);
}

.list-item-danger .list-item-icon svg {
  color: var(--color-error);
}

.list-item-content {
  flex: 1;
  min-width: 0;
}

.list-item-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-ink);
  margin-bottom: 2px;
}

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

.btn-outline {
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-ink);
  background: transparent;
  border: 1px solid var(--color-hairline);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-outline:hover {
  background: var(--color-surface-soft);
}

.btn-danger-outline {
  padding: 8px 16px;
  font-size: 13px;
  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-outline:hover {
  background: rgba(198, 69, 69, 0.08);
}

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

  .form-row {
    grid-template-columns: 1fr;
  }

  .section-row {
    grid-template-columns: 1fr;
  }
}
</style>
