<!-- 账号管理-资金账户管理-修改密码 -->
<template>
    <app-drawer :title="t('account.fundacct.password.title')" width="480" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item :label="t('account.fundacct.password.accountId')">
                {{ record.accountId }}
            </el-form-item>
            <el-form-item :label="t('account.fundacct.password.oldPwd')" prop="oldPwd">
                <el-input type="password" v-model="formData.oldPwd" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('account.fundacct.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('account.fundacct.password.confirmPassword')" prop="confirmPassword">
                <el-input type="password" v-model="formData.confirmPassword" maxlength="50"
                    :placeholder="t('common.pleaseenter')" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

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

const props = defineProps({
    record: {
        type: Object as PropType<Model.TaAccountRsp>,
        required: true
    }
})

const { global: { t } } = i18n

const formRef = ref<FormInstance>()
const show = ref(true)
const refresh = ref(false)
const loading = ref(false)

const formData = ref<Model.TaAccountPwdReq>({
    accountId: props.record.accountId,
    oldPwd: '',
    newPwd: '',
    confirmPassword: ''
})

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

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = { ...formData.value }
            rawData.oldPwd = cryptojs.SHA256(rawData.accountId + rawData.oldPwd).toString()
            rawData.newPwd = cryptojs.SHA256(rawData.accountId + rawData.newPwd).toString()
            rawData.confirmPassword = cryptojs.SHA256(rawData.accountId + rawData.confirmPassword).toString()
            loading.value = true

            updateTaAccountPwd({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips1'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips2') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>