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

const router = useRouter()

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

function getErrorMessage(error: unknown, fallback: string) {
  const detail = (error as { response?: { data?: { detail?: unknown } } })?.response?.data?.detail
  return typeof detail === 'string' && detail.trim() ? detail : fallback
}

onMounted(async () => {
  const { data } = await api.me()
  profile.value = data
  form.value.username = data.username
})

async function saveUsername() {
  const username = form.value.username.trim()
  message.value = ''
  if (!username) {
    message.value = '用户名不能为空'
    return
  }

  loading.value = true
  try {
    const { data } = await api.updateMe({ username })
    profile.value = data
    form.value.username = data.username
    setSavedUsername(data.username)
    message.value = '用户名已更新 ✅'
  } catch (error) {
    message.value = getErrorMessage(error, '更新用户名失败')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="page wechat-page">
    <div class="topbar">
      <button class="back-btn" @click="router.push('/settings')">← 返回</button>
      <div>
        <h1 class="page-title">修改用户名</h1>
        <p class="page-subtitle">只改这一项</p>
      </div>
    </div>

    <div v-if="message" :class="['message', message.includes('✅') ? 'success' : 'error']">
      {{ message }}
    </div>

    <div class="group">
      <div class="cell plain-cell">
        <div class="cell-left">
          <span class="cell-title">当前用户名</span>
          <span class="cell-desc">{{ profile.username || '加载中' }}</span>
        </div>
      </div>
      <div class="divider"></div>
      <div class="cell">
        <div class="cell-left">
          <span class="cell-title">新用户名</span>
          <span class="cell-desc">请输入新的用户名</span>
        </div>
        <input v-model="form.username" class="cell-input" placeholder="新用户名" autocomplete="username" />
      </div>
    </div>

    <button class="btn btn-primary save-btn" :disabled="loading" @click="saveUsername">
      {{ loading ? '更新中...' : '保存用户名' }}
    </button>
  </div>
</template>

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

.topbar {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.back-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
}

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

.group {
  background: #fff;
  border-radius: 14px;
  overflow: hidden;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
}

.plain-cell {
  padding-bottom: 12px;
}

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

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

.cell-input {
  width: 160px;
  height: 32px;
  border: 1px solid #e4e4e7;
  border-radius: 8px;
  font-size: 14px;
  text-align: right;
  padding: 0 10px;
  background: #fafafa;
  outline: none;
}

.cell-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79, 110, 247, 0.12);
}

.save-btn {
  width: auto;
  min-height: 34px;
  margin-top: 12px;
  padding: 7px 12px;
  font-size: 12px;
  border-radius: 10px;
}
</style>
