<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { changePassword } from '../../api/user'
import { useUserStore } from '../../stores/user'

// 修改密码表单
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 表单规则
const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const passwordFormRef = ref(null)
const loading = ref(false)
const userStore = useUserStore()

// 提交修改密码表单
const submitPasswordForm = () => {
  passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await changePassword({
          oldPassword: passwordForm.oldPassword,
          newPassword: passwordForm.newPassword
        })

        if (res.code === 200) {
          ElMessage.success('密码修改成功')
          resetForm()
        } else {
          ElMessage.error(res.message || '密码修改失败')
        }
      } catch (error) {
        console.error('密码修改失败:', error)
        ElMessage.error('密码修改失败，请稍后再试')
      } finally {
        loading.value = false
      }
    }
  })
}

// 重置表单
const resetForm = () => {
  passwordFormRef.value.resetFields()
}
</script>

<template>
  <div class="password-container">
    <div class="password-header">
      <h2>修改密码</h2>
    </div>

    <div class="password-form-container">
      <el-form
        ref="passwordFormRef"
        :model="passwordForm"
        :rules="passwordRules"
        label-width="100px"
        class="password-form"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            v-model="passwordForm.oldPassword"
            type="password"
            placeholder="请输入原密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitPasswordForm">
            修改密码
          </el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="password-tips">
        <h3>密码修改提示：</h3>
        <ul>
          <li>密码长度应为6-20个字符</li>
          <li>建议使用字母、数字和特殊字符的组合</li>
          <li>不要使用与旧密码相似的新密码</li>
          <li>不要使用容易被猜到的密码，如生日、手机号等</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<style scoped>
.password-container {
  padding: 0;
}

.password-header {
  margin-bottom: 20px;
}

.password-header h2 {
  margin: 0;
  font-size: 1.5rem;
  color: #333;
}

.password-form-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 30px;
  display: flex;
  gap: 50px;
}

.password-form {
  flex: 1;
  max-width: 500px;
}

.password-tips {
  flex: 1;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 8px;
}

.password-tips h3 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #409eff;
}

.password-tips ul {
  padding-left: 20px;
  margin: 0;
}

.password-tips li {
  margin-bottom: 10px;
  color: #666;
}

@media (max-width: 768px) {
  .password-form-container {
    flex-direction: column;
  }

  .password-form {
    max-width: 100%;
  }
}
</style>
