<!-- 会员机构管理-机构管理-机构资料管理-资金账户-修改 -->
<template>
    <app-drawer :title="t('member.institution.user.account.edit.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"
            :show-message="false">
            <el-form-item :label="t('member.institution.user.account.edit.accountId')">
                {{ record.accountId }}
            </el-form-item>
            <el-form-item :label="t('member.institution.user.account.edit.tradestatus')" prop="tradestatus">
                <el-select v-model="formData.tradestatus">
                    <template v-for="item in traderstatusEnum.getEnumOptions([1, 4, 5])" :key="item.value">
                        <el-option :label="item.label" :value="item.value" />
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item :label="t('member.institution.user.account.edit.taAccountType')">
                {{ accounttypeEnum.getEnumTypeName(record.taAccountType) }}
            </el-form-item>
            <el-form-item :label="t('member.institution.user.account.edit.currency')">
                {{ currencyEnum.getEnumTypeName(record.currencyid) }}
            </el-form-item>
            <el-form-item :label="t('member.institution.user.account.edit.outthreshold')" prop="outthreshold">
                <el-input-number v-model="formData.outthreshold" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ 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 { shallowRef, reactive, PropType, toRaw } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { taaccountEdit } from '@/services/api/member'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'

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

// 内/外部
const accounttypeEnum = useEnum('accounttype')
// 交易状态
const traderstatusEnum = useEnum('traderstatus')
// 币种
const currencyEnum = useEnum('currency')

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

const formData = reactive<Member.TaaccountEditReq>({
    accountid: props.record.accountId,
    outthreshold: props.record.outThreshold,
    parentaccountid: props.record.parentAccountId,
    tradestatus: props.record.tradeStatus
})

// 表单验证规则
const formRules: FormRules = {
    outthreshold: [{ required: true }],
    tradestatus: [{ required: true }]
}

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

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