<!-- 交易商管理-个性化管理-资金账户个性化 -->
<template>
    <div class="g-view-tree">
        <app-view>
            <el-tree ref="treeRef" :data="nodeList" node-key="id" :props="{ label: 'name' }"
                :default-expanded-keys="['-1']" @node-click="nodeClick" highlight-current />
        </app-view>
        <app-view>
            <div style="display: flex;justify-content: space-between;">
                <app-operation :data-list="getActionButtons(['investor_custom_accountcfg_add'])"
                    @click="openComponent" />
                <app-operation
                    :data-list="getActionButtons(['investor_custom_accountcfg_modify', 'investor_custom_accountcfg_delete'])"
                    @click="openComponent" v-if="data" />
            </div>
            <template v-if="data">
                <app-table-details :title="t('investor.custom.accountcfg.baseinfo')" :data="currentInfo" :label-width="160" :cell-props="detailProps"
                    :column="2" />
                <app-table :data="data.druleList" :columns="ruleColumns">
                    <template #headerLeft>
                        <b>{{ t('investor.custom.accountcfg.traderule') }}</b>
                    </template>
                    <!-- 平台 -->
                    <template #paramvalue="{ row }">
                        <span v-if="row.ruleid === 105">{{ feetypeEnum.getEnumTypeName(row.paramvalue) }}</span>
                        <span v-else>{{ row.paramvalue || 0 }}</span>
                    </template>
                    <!-- 会员 -->
                    <template #dvalue="{ row }">
                        <span v-if="row.ruleid === 105">
                            {{ handleNoneValue(feetypeEnum.getEnumTypeName(row.dvalue)) }}
                        </span>
                        <span v-else>{{ handleNoneValue(row.dvalue) }}</span>
                    </template>
                    <!-- 分组 -->
                    <template #gvalue="{ row }">
                        <span v-if="row.ruleid === 105">
                            {{ handleNoneValue(feetypeEnum.getEnumTypeName(row.gvalue)) }}
                        </span>
                        <span v-else>{{ handleNoneValue(row.gvalue) }}</span>
                    </template>
                    <!-- 个性化 -->
                    <template #cvalue="{ row }">
                        <span v-if="row.ruleid === 105">
                            {{ handleNoneValue(feetypeEnum.getEnumTypeName(row.cvalue)) }}
                        </span>
                        <span v-else>{{ handleNoneValue(row.cvalue) }}</span>
                    </template>
                </app-table>
                <app-table :data="data.dfeeList" :columns="feeColumns">
                    <template #headerLeft>
                        <b>{{ t('investor.custom.accountcfg.tradefee') }}</b>
                    </template>
                    <!-- 平台 -->
                    <template #exchangevalue="{ row }">
                        <template v-if="row.feealgorithm">
                            <span>{{ scfRiskMode.getEnumTypeName(row.feealgorithm) }}：</span>
                            <span>{{ row.exchangevalue + (row.feealgorithm === 1 && '‱') }}</span>
                        </template>
                        <template v-else>
                            {{ handleNoneValue() }}
                        </template>
                    </template>
                    <!-- 会员 -->
                    <template #dvalue="{ row }">
                        <template v-if="row.drithm">
                            <span>{{ scfRiskMode.getEnumTypeName(row.drithm) }}：</span>
                            <span>{{ row.dvalue + (row.drithm === 1 && '‱') }}</span>
                        </template>
                        <template v-else>
                            {{ handleNoneValue() }}
                        </template>
                    </template>
                    <!-- 分组 -->
                    <template #gvalue="{ row }">
                        <template v-if="row.grithm">
                            <span>{{ scfRiskMode.getEnumTypeName(row.grithm) }}：</span>
                            <span>{{ row.gvalue + (row.grithm === 1 && '‱') }}</span>
                        </template>
                        <template v-else>
                            {{ handleNoneValue() }}
                        </template>
                    </template>
                    <!-- 个性化 -->
                    <template #cvalue="{ row }">
                        <template v-if="row.crithm">
                            <span>{{ scfRiskMode.getEnumTypeName(row.crithm) }}：</span>
                            <span>{{ row.cvalue + (row.crithm === 1 && '‱') }}</span>
                        </template>
                        <template v-else>
                            {{ handleNoneValue() }}
                        </template>
                    </template>
                </app-table>
            </template>
            <el-empty v-else />
        </app-view>
        <component :is="componentMap.get(componentId)" v-bind="{ record: toRaw(data) }" @closed="closeComponent"
            v-if="componentId" />
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, toRaw, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import type Node from 'element-plus/es/components/tree/src/model/node'
import { handleNoneValue, buildTree } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { getAccTradeTree, accTradeConfigView } from '@/services/api/investor'
import { CellProp } from '@pc/components/base/table-details/types'
import AppTable from '@pc/components/base/table/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'
import { i18n } from '@/stores'

const customerTypeEnum = useEnum('customerType')
const feetypeEnum = useEnum('feetype')
const scfRiskMode = useEnum('SCFRiskMode')
const traderule = useEnum('traderule')
const tradefee = useEnum('tradefee')

const { global: { t } } = i18n

const treeRef = ref()
const nodeList = ref<(Investor.AccTradeTreeRsp & { children: Investor.AccTradeTreeRsp[]; })[]>([])
const selectedNode = ref<Node>()

const currentInfo = reactive({
    userName: '',
    accountName: '',
    goodsName: '',
    customerType: ''
})

const qs = computed<Investor.AccTradeConfigViewReq>(() => {
    const data = selectedNode.value?.data
    return {
        userid: data?.userid,
        accountid: data?.accountid,
        marketid: data?.marketid,
        goodsid: data?.goodsid,
        tradetype: data?.tradetype
    }
})

const { componentMap, componentId, openComponent, closeComponent, getActionButtons } = useOperation<Investor.AccTradeConfigViewRsp>({
    onClose: (componentId) => {
        getNodeList()
        if (componentId === 'investor_custom_accountcfg_modify') {
            getDetails(qs.value)
        }
        if (componentId === 'investor_custom_accountcfg_delete') {
            data.value = undefined
        }
    }
})

const { run: getNodeList } = useRequest(getAccTradeTree, {
    onSuccess: (res) => {
        res.data.push(
            {
                accountid: 0,
                goodsid: 0,
                id: '-1',
                level: 0,
                marketid: 0,
                name: t('investor.custom.accountcfg.name'),
                pid: '0',
                tradetype: 1,
                userid: 0,
            }
        )
        nodeList.value = buildTree(res.data, 'id', 'pid', (node) => node.pid === '0')

        // 设置选中节点
        nextTick(() => {
            const data = selectedNode.value?.data
            if (data) {
                treeRef.value.setCurrentKey(data.id)
            }
        })
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const { data, run: getDetails } = useRequest(accTradeConfigView, {
    manual: true,
    onError: (err) => {
        ElMessage.error(err)
    }
})

const detailProps = computed<CellProp[]>(() => [
    { prop: 'userName', label: 'investor.custom.accountcfg.userName' },
    { prop: 'accountName', label: 'investor.custom.accountcfg.accountName' },
    { prop: 'goodsName', label: 'investor.custom.accountcfg.goodsName' },
    { prop: 'customerType', label: 'investor.custom.accountcfg.customerType', formatValue: () => customerTypeEnum.getEnumTypeName(data.value?.paramid) },
])

const ruleColumns = computed<Model.TableColumn[]>(() => [
    { field: 'ruleid', label: 'investor.custom.accountcfg.rulename' },
    { field: 'paramvalue', label: 'investor.custom.accountcfg.paramvalue', formatValue: (val) => traderule.getEnumTypeName(val) },
    { field: 'dvalue', label: 'investor.custom.accountcfg.dvalue' },
    { field: 'gvalue', label: 'investor.custom.accountcfg.gvalue' },
    { field: 'cvalue', label: 'investor.custom.accountcfg.cvalue' },
])

const feeColumns = computed<Model.TableColumn[]>(() => [
    { field: 'tradefeeid', label: 'investor.custom.accountcfg.tradefeename', formatValue: (val) => tradefee.getEnumTypeName(val) },
    { field: 'exchangevalue', label: 'investor.custom.accountcfg.exchangevalue' },
    { field: 'dvalue', label: 'investor.custom.accountcfg.dvalue' },
    { field: 'gvalue', label: 'investor.custom.accountcfg.gvalue' },
    { field: 'cvalue', label: 'investor.custom.accountcfg.cvalue' },
])

const nodeClick = (data: Member.CreateTreeRsp, node: Node) => {
    if (data.goodsid) {
        selectedNode.value = node
        currentInfo.goodsName = node.label
        currentInfo.accountName = node.parent.label
        currentInfo.userName = node.parent.parent.label
        getDetails(qs.value)
    }
}
</script>