<!-- 修改密码 -->
<template>
    <app-drawer :title="t('auth.password.title')" width="480" v-model:show="show" :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item :label="t('auth.password.membername')" prop="membername">
                <span>{{ userStore.userInfo.username }}</span>
            </el-form-item>
            <el-form-item :label="t('auth.password.membertags')" prop="membertags">
                <span>{{ userStore.userInfo.logincode }}</span>
            </el-form-item>
            <el-form-item :label="t('auth.password.oldPwd')" prop="oldPwd">
                <el-input type="password" v-model="formData.oldPwd" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('auth.password.newPwd')" prop="newPwd">
                <el-popover placement="top" trigger="click" :width="244" :hide-after="0"
                    :content="validateRules.password.message">
                    <template #reference>
                        <el-input type="password" v-model="formData.newPwd" maxlength="50"
                            :placeholder="t('common.pleaseenter')" />
                    </template>
                </el-popover>
            </el-form-item>
            <el-form-item :label="t('auth.password.checkPwd')" prop="checkPwd">
                <el-input type="password" v-model="checkPwd" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { validateRules } from '@/constants/regex'
import { updatePwd } from '@/services/api/user'
import { useUserStore, i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const t = i18n.global.t
const userStore = useUserStore()
const formRef = ref<FormInstance>()
const show = ref(true)
const loading = ref(false)
const checkPwd = ref('')

const formData = ref<Model.UpdatePwdReq>({
    newPwd: '',
    oldPwd: ''
})

// 表单验证规则
const formRules: FormRules = {
    oldPwd: [{
        required: true,
        trigger: 'blur',
        validator: (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                callback(t('auth.password.tips1'))
            }
        },
    }],
    newPwd: [{
        required: true,
        trigger: 'blur',
        validator: (rule, value, callback) => {
            if (validateRules.password.validate(value)) {
                callback()
            } else {
                callback(t('auth.password.tips2'))
            }
        },
    }],
    checkPwd: [{
        required: true,
        trigger: 'blur',
        validator: (rule, value, callback) => {
            if (formData.value.newPwd === checkPwd.value) {
                callback()
            } else {
                callback(t('auth.password.tips3'))
            }
        },
    }],
}

const onCancel = () => {
    show.value = false
}

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            updatePwd({
                data: formData.value
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel()
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>