<template>
  <div class="user-setting">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px">
        <el-form-item label="头像：" prop="avatar">
          <material-picker v-model="formData.avatar" type="image" :limit="1" />
        </el-form-item>
        <el-form-item label="账号：" prop="account">
          <el-input style="width: 400px" v-model="formData.account" disabled />
        </el-form-item>
        <el-form-item label="名称：" prop="name">
          <el-input style="width: 400px" v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="当前密码：" prop="password_old">
          <el-input
            style="width: 400px"
            v-model.trim="formData.password_old"
            placeholder="修改密码时必填, 不修改密码时留空"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label="新的密码：" prop="password">
          <el-input
            style="width: 400px"
            v-model.trim="formData.password"
            placeholder="修改密码时必填, 不修改密码时留空"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label="确定密码：" prop="password_confirm">
          <el-input
            style="width: 400px"
            v-model.trim="formData.password_confirm"
            placeholder="修改密码时必填, 不修改密码时留空"
            type="password"
            show-password
          />
        </el-form-item>
      </el-form>
      <div class="flex align-center">
        <el-button type="primary" @click="handleSubmit">保存</el-button>
        <el-button @click="getUser">重置</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="UserSetting">
import type { FormInstance } from 'element-plus'
import { setUserInfo } from '@/api/user'
import { useUserStore } from '@/stores/modules/user'

// 组件参数
const formRef = shallowRef<FormInstance>()
const userStore = useUserStore()
// 表单数据
const formData = reactive({
  avatar: '', // 头像
  account: '', // 账号
  name: '', // 名称
  password_old: '', // 当前密码
  password: '', // 新的密码
  password_confirm: '' // 确定密码
})
// 表单校验规则
const formRules = reactive<object>({
  avatar: [{ required: true, message: '头像不能为空', trigger: ['change', 'blur'] }],
  name: [{ required: true, message: '请输入名称', trigger: ['change', 'blur'] }]
})

// 获取个人设置
const getUser = async () => {
  const userInfo = userStore.userInfo
  for (const key in formData) {
    //@ts-ignore
    formData[key] = userInfo[key]
  }
}
// 设置个人设置
const setUser = async () => {
  if (formData.password_old || formData.password || formData.password_confirm) {
    if (!formData.password_old) {
      ElMessage.error('请输入当前密码')
      return
    }
    if (!formData.password) {
      ElMessage.error('请输入新的密码')
      return
    }
    if (!formData.password_confirm) {
      ElMessage.error('请输入确定密码')
      return
    }
    if (formData.password_confirm != formData.password) {
      ElMessage.error('两次输入的密码不一样')
      return
    }
  }
  if (formData.password_old && formData.password && formData.password_confirm) {
    if (formData.password_old.length < 6 || formData.password_old.length > 32) {
      ElMessage.error('密码长度在6到32之间')
      return
    }
    if (formData.password.length < 6 || formData.password.length > 32) {
      ElMessage.error('密码长度在6到32之间')
      return
    }
    if (formData.password_confirm.length < 6 || formData.password_confirm.length > 32) {
      ElMessage.error('密码长度在6到32之间')
      return
    }
  }
  await setUserInfo(formData)
  ElMessage.success('设置成功')
  userStore.getUserInfo()
}
// 提交数据
const handleSubmit = async () => {
  await formRef.value?.validate()
  setUser()
}

onMounted(() => {
  getUser()
})
</script>
