<!-- 会员机构管理-机构管理-风控个性化设置-详情 -->
<template>
    <app-drawer :title="t('member.institution.riskcfg.details.title')" width="900" v-model:show="show" :refresh="refresh">
        <app-table-details :title="t('member.institution.riskcfg.details.subtitle')" :data="record" :label-width="160" :cell-props="detailProps1" :column="2" />
        <app-table-details :title="t('member.institution.riskcfg.details.subtitle1')" :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<Member.AccountRiskConfigRsp>,
        required: true
    }
})

const { global: { t } } = i18n

const flagEnum = useEnum('flag') // 确认状态
const marketerTypeEnum = useEnum('marketerType') // 风险率类型

const show = ref(true)
const refresh = ref(false)

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

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

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

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