<!-- 交易商管理-交易商管理-交易商管理-查询个性化设置 -->
<template>
    <app-drawer :title="`${t('investor.manage.user.config.title')}${record.accountName}`" width="900"
        v-model:show="show" :loading="loading" :refresh="refresh">
        <app-filter :option="filterOption" />
        <template v-if="data">
            <app-table-details :title="t('investor.manage.user.config.subtitle')" :data="data" :label-width="160"
                :cell-props="detailProps" :column="3" />
            <app-table :data="data.extendRule" :columns="ruleColumns">
                <template #headerLeft>
                    <b>{{ t('investor.manage.user.config.subtitle1') }}</b>
                </template>
                <!-- 值 -->
                <template #paramvalue="{ row }">
                    <span v-if="row.ruleid === 105">{{ feetypeEnum.getEnumTypeName(row.paramvalue) }}</span>
                    <span v-else-if="[107, 108, 110, 1111].includes(row.ruleid)">{{ row.paramvalue * 100 }}</span>
                    <span v-else>{{ row.paramvalue || 0 }}</span>
                </template>
            </app-table>
            <app-table :data="data.extendFee" :columns="feeColumns">
                <template #headerLeft>
                    <b>{{ t('investor.manage.user.config.subtitle2') }}</b>
                </template>
            </app-table>
        </template>
        <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 { parsePercent } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { showAccount, accountQuery } from '@/services/api/investor'
import { useRequest } from '@/hooks/request'
import { CellProp } from '@pc/components/base/table-details/types'
import { useDataFilter } from '@/hooks/datatable-v2'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

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

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

const feetypeEnum = useEnum('feetype')

const { dataList } = useRequest(showAccount, {
    params: {
        userid: props.record.userId,
    }
})

const { data, loading, run } = useRequest(accountQuery, {
    manual: true
})

const detailProps = computed<CellProp[]>(() => {
    const { tradeProperty } = data.value ?? {}
    return [
        { prop: 'marginalgorithm', label: 'investor.manage.user.config.marginalgorithm', formatValue: (val) => val === 1 ? t('investor.manage.user.config.marginalgorithm1') : t('investor.manage.user.config.marginalgorithm2') },
        { prop: 'deposit', label: 'investor.manage.user.config.deposit', formatValue: () => '100%', show: tradeProperty === 2 },
        { prop: 'marketmarginvalue', label: 'investor.manage.user.config.marketmarginvalue', formatValue: (val) => parsePercent(val), show: tradeProperty === 1 },
        { prop: 'reckonmarginvalue', label: 'investor.manage.user.config.reckonmarginvalue', formatValue: (val) => parsePercent(val), show: tradeProperty === 1 },
        { prop: 'lockmarginvalue', label: 'investor.manage.user.config.lockmarginvalue', formatValue: (val) => parsePercent(val), show: tradeProperty === 3 },
    ]
})

const ruleColumns = computed<Model.TableColumn[]>(() => [
    { field: 'rulename', label: 'investor.manage.user.config.rulename' },
    { field: 'paramvalue', label: 'investor.manage.user.config.paramvalue' },
])

const feeColumns = computed<Model.TableColumn[]>(() => [
    { field: 'feename', label: 'investor.manage.user.config.feename' },
    { field: 'feealgorithm', label: 'investor.manage.user.config.feealgorithm', formatValue: (val) => val === 1 ? t('investor.manage.user.config.feealgorithm1') : t('investor.manage.user.config.feealgorithm2') },
    { field: 'exchangevalue', label: 'investor.manage.user.config.exchangevalue' },
    { field: 'memberdefaultvalue', label: 'investor.manage.user.config.memberdefaultvalue' },
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Investor.AccountQueryReq>({
    filters: [
        {
            field: 'accountid',
            label: t('investor.manage.user.config.accountid'),
            options: () => dataList.value.map((e) => ({
                label: e.accountid.toString(),
                value: e.accountid
            })),
            required: true
        },
        {
            field: 'goodsid',
            label: t('investor.manage.user.config.goodsid'),
            placeholder: t('investor.manage.user.config.tips1'),
            required: true
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

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

const onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>