<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5">
{
  style: {
    navigationBarTitleText: '修改密码',
  },
}
</route>
<template>
  <view class="min-h-screen flex items-center justify-center bg-gray-200 px-4">
    <view
      class="w-full max-w-md mx-auto bg-white rounded-2xl shadow-lg p-8 border border-gray-100 box-border mx-4"
    >
      <view class="mb-6 text-center">
        <text class="text-2xl font-bold text-gray-800">修改密码</text>
      </view>
      <view class="mb-4">
        <input
          v-model="oldPassword"
          type="password"
          placeholder="请输入旧密码"
          class="w-full h-12 px-4 border-2 border-gray-300 rounded-lg focus:border-pink-400 outline-none bg-gray-50 text-base transition-all duration-200 box-border"
        />
      </view>
      <view class="mb-6">
        <input
          v-model="newPassword"
          type="password"
          placeholder="请输入新密码"
          class="w-full h-12 px-4 border-2 border-gray-300 rounded-lg focus:border-pink-400 outline-none bg-gray-50 text-base transition-all duration-200 box-border"
        />
      </view>
      <button
        class="w-full h-12 bg-black text-white rounded-lg text-base font-semibold flex items-center justify-center"
        @click="handleChangePassword"
      >
        <span class="w-full text-center">确定</span>
      </button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { changePasswordAPI } from '@/service/user'

const oldPassword = ref('')
const newPassword = ref('')

const handleChangePassword = async () => {
  if (!oldPassword.value || !newPassword.value) {
    uni.showToast({ title: '请填写完整', icon: 'none' })
    return
  }
  try {
    const res = await changePasswordAPI({
      oldPassword: oldPassword.value,
      newPassword: newPassword.value,
    })
    if (res.code === 0) {
      uni.showToast({ title: '修改成功', icon: 'success' })
      setTimeout(() => {
        uni.navigateBack()
      }, 1000)
    } else {
      uni.showModal({ title: '修改失败', content: res.msg || '修改失败', showCancel: false })
    }
  } catch (e: any) {
    uni.showModal({ title: '修改失败', content: e?.msg || '修改失败', showCancel: false })
  }
}
</script>
