<!-- 交易商管理-交易商管理-交易商管理-资金账户-编辑 -->
<template>
    <app-drawer :title="t('investor.manage.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">
            <el-form-item :label="t('investor.manage.user.account.edit.accountid')">
                {{ selectedItem.accountid }}
            </el-form-item>
            <el-form-item :label="t('investor.manage.user.account.edit.tradestatus')" prop="tradestatus">
                <el-select v-model="formData.tradestatus">
                    <el-option v-for="item in tradestatusEnum.getEnumOptions([1, 4, 5])" :key="item.value"
                        :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('investor.manage.user.account.edit.taaccounttype')">
                {{ accounttypeEnum.getEnumTypeName(selectedItem.taaccounttype) }}
            </el-form-item>
            <el-form-item :label="t('investor.manage.user.account.edit.currencyid')">
                {{ currencyEnum.getEnumTypeName(selectedItem.currencyid) }}
            </el-form-item>
            <el-form-item :label="t('investor.manage.user.account.edit.outthreshold')" prop="outthreshold">
                <el-input-number v-model="formData.outthreshold" :placeholder="t('common.pleaseenter')" :min="0" />
            </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, toRaw } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { investorAccountEdit } from '@/services/api/investor'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    selectedItem: {
        type: Object as PropType<Investor.ShowAccountRsp>,
        required: true
    }
})

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

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

const formData = ref<Investor.InvestorAccountEditReq>({
    accountid: props.selectedItem.accountid,
    outthreshold: props.selectedItem.outthreshold,
    parentaccountid: props.selectedItem.parentaccountid,
    tradestatus: props.selectedItem.tradestatus
})

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

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

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