<!-- 账号管理-资金账户管理-锁定/解锁 -->
<template>
    <app-drawer :title="t('common.alert')" v-model:show="show" :loading="loading" :refresh="refresh">
        <div class="g-text-message">
            <span v-if="isLock">{{ t('account.tradingacct.lock.message1') }}</span>
            <span v-else>{{ t('account.tradingacct.lock.message2') }}</span>
        </div>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.confirm') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { tradingAccLockAccount } from '@/services/api/account'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

const { global: { t } } = i18n
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)

const isLock = computed(() => props.record.loginstatus === 2)

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

const onSubmit = () => {
    loading.value = true
    tradingAccLockAccount({
        data: {
            loginid: props.record.loginid,
            status: isLock.value ? 1 : 2
        }
    }).then(() => {
        ElMessage.success(isLock.value ? t('account.tradingacct.lock.tips1') : t('account.tradingacct.lock.tips2'))
        onCancel(true)
    }).catch((err) => {
        ElMessage.error(isLock.value ? t('account.tradingacct.lock.tips3') : t('account.tradingacct.lock.tips4') + err)
        onCancel()
    }).finally(() => {
        loading.value = false
    })
}
</script>