<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { api } from '../api/request'

const router = useRouter()

const form = ref({
  current_password: '',
  new_password: '',
  confirm_password: '',
})
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
}

async function savePassword() {
  message.value = ''
  if (!form.value.current_password) {
    message.value = '请输入当前密码'
    return
  }
  if (!form.value.new_password) {
    message.value = '请输入新密码'
    return
  }
  if (form.value.new_password.length < 6) {
    message.value = '新密码至少 6 位'
    return
  }
  if (form.value.new_password !== form.value.confirm_password) {
    message.value = '两次输入的新密码不一致'
    return
  }

  loading.value = true
  try {
    await api.updateMe({
      current_password: form.value.current_password,
      new_password: form.value.new_password,
    })
    form.value.current_password = ''
    form.value.new_password = ''
    form.value.confirm_password = ''
    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">
        <div class="cell-left">
          <span class="cell-title">当前密码</span>
          <span class="cell-desc">请先输入旧密码</span>
        </div>
        <input
          v-model="form.current_password"
          type="password"
          class="cell-input"
          placeholder="当前密码"
          autocomplete="current-password"
        />
      </div>
      <div class="divider"></div>
      <div class="cell">
        <div class="cell-left">
          <span class="cell-title">新密码</span>
          <span class="cell-desc">至少 6 位</span>
        </div>
        <input
          v-model="form.new_password"
          type="password"
          class="cell-input"
          placeholder="新密码"
          autocomplete="new-password"
        />
      </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.confirm_password"
          type="password"
          class="cell-input"
          placeholder="确认密码"
          autocomplete="new-password"
        />
      </div>
    </div>

    <button class="btn btn-primary save-btn" :disabled="loading" @click="savePassword">
      {{ 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;
}

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