<!-- 交易商管理-个性化管理-风控个性化-详情 -->
<template>
    <app-drawer :title="t('investor.custom.riskcfg.details.title')" width="900" v-model:show="show" :refresh="refresh">
        <app-table-details :title="t('investor.custom.riskcfg.details.subtitle1')" :data="record" :label-width="160" :cell-props="detailProps1" :column="2" />
        <app-table-details :title="t('investor.custom.riskcfg.details.subtitle2')" :data="data" :label-width="160" :cell-props="detailProps2" :column="2" />
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { getRiskRatioTypeForMarketer } from '@/services/api/investor'
import { CellProp } from '@pc/components/base/table-details/types'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'
import { i18n } from '@/stores'

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

const flagEnum = useEnum('flag') // 确认状态
const customerTypeEnum = useEnum('customerType') // 风险客户类别

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

const { data } = useRequest(getRiskRatioTypeForMarketer, {
    params: {
        riskcontrolmode: 1,
        customertype: props.record.customertype
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const detailProps1: CellProp[] = [
    { prop: 'username', label: 'investor.custom.riskcfg.details.username', },
    { prop: 'accountid', label: 'investor.custom.riskcfg.details.accountid', },
    { prop: 'customertype', label: 'investor.custom.riskcfg.details.customertype', formatValue: (val) => customerTypeEnum.getEnumTypeName(val) },
]

const detailProps2 = computed<CellProp[]>(() => {
    const { notesaferatio } = data.value ?? {}
    return [
        { prop: 'riskcontrolmode', label: 'investor.custom.riskcfg.details.riskcontrolmode', formatValue: (val) => val === 1 ? t('investor.custom.riskcfg.details.riskcontrolmode1') : t('investor.custom.riskcfg.details.riskcontrolmode2') },
        { prop: 'customertype', label: 'investor.custom.riskcfg.details.customertype1', formatValue: (val) => customerTypeEnum.getEnumTypeName(val) },
        { prop: 'riskratiocalcmode', label: 'investor.custom.riskcfg.details.riskratiocalcmode', formatValue: (val) => val === 1 ? t('investor.custom.riskcfg.details.riskratiocalcmode1') : t('investor.custom.riskcfg.details.riskratiocalcmode2') },
        { prop: 'notemarginriskratio', label: 'investor.custom.riskcfg.details.notemarginriskratio', formatValue: (val) => val + '%', show: !notesaferatio },
        { prop: 'addmarginriskratio', label: 'investor.custom.riskcfg.details.addmarginriskratio', formatValue: (val) => val + '%', show: !notesaferatio },
        { prop: 'cutriskratio', label: 'investor.custom.riskcfg.details.cutriskratio', formatValue: (val) => val + '%', show: !notesaferatio },
        { prop: 'cutbackriskratio', label: 'investor.custom.riskcfg.details.cutbackriskratio', formatValue: (val) => val + '%', show: !notesaferatio },
        { prop: 'notesaferatio', label: 'investor.custom.riskcfg.details.notesaferatio', formatValue: (val) => val + '%', show: !!notesaferatio },
        { prop: 'addsaferatio', label: 'investor.custom.riskcfg.details.addsaferatio', formatValue: (val) => val + '%', show: !!notesaferatio },
        { prop: 'recoversaferatio', label: 'investor.custom.riskcfg.details.recoversaferatio', formatValue: (val) => val + '%', show: !!notesaferatio },
        { prop: 'cutsaferatio', label: 'investor.custom.riskcfg.details.cutsaferatio', formatValue: (val) => val + '%', show: !!notesaferatio },
        { prop: 'isdefault', label: 'investor.custom.riskcfg.details.isdefault', formatValue: (val) => flagEnum.getEnumTypeName(val) },
        { prop: 'markets', label: 'investor.custom.riskcfg.details.markets' },
    ]
})

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