<!-- 市场运行管理-监控管理-做市会员风险监控-持仓汇总 -->
<template>
    <app-drawer :title="t('marketrun.monitor.marketer.positionsumm.title')" width="1200" v-model:show="show">
        <app-table :data="dataList" :columns="tableColumns">
            <template #headerLeft>
                <span>{{ t('marketrun.monitor.marketer.positionsumm.accountid1') }}{{ record.accountid }}</span>
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <el-button size="small" icon="Search" @click="showComponent('Details', row)" circle plain />
            </template>
        </app-table>
        <template #footer>
            <el-button @click="onCancel">{{ t('operation.close') }}</el-button>
        </template>
        <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ record: selectedItem }"
            @closed="closeComponent" v-if="componentId" />
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, defineAsyncComponent, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useCountDown } from '@/hooks/countdown'
import { useRequest } from '@/hooks/request'
import { useComponent } from '@/hooks/component'
import { marketRunAccpage } from '@/services/api/market'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import { i18n } from '@/stores'

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

const componentMap = new Map<string, unknown>([
    ['Details', defineAsyncComponent(() => import('./details/index.vue'))], // 详情
])

const { global: { t } } = i18n

const show = shallowRef(true)
const selectedItem = shallowRef<Model.MarketRunAccpageRsp>()
const countDown = useCountDown(10)

const { componentRef, componentId, openComponent, closeComponent } = useComponent()

const { dataList, run } = useRequest(marketRunAccpage, {
    params: {
        accountid: props.record.accountid
    },
    onError: (err) => {
        ElMessage.error(err)
    },
    onFinally: () => {
        startCountDown()
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountid', label: 'marketrun.monitor.marketer.positionsumm.accountid' },
    { field: 'parentname', label: 'marketrun.monitor.marketer.positionsumm.parentname' },
    { field: 'goodsname', label: 'marketrun.monitor.marketer.positionsumm.goodsname' },
    { field: 'buy_hold_qty', label: 'marketrun.monitor.marketer.positionsumm.buy_hold_qty' },
    { field: 'buy_hold_value', label: 'marketrun.monitor.marketer.positionsumm.buy_hold_value' },
    { field: 'buy_avg_price', label: 'marketrun.monitor.marketer.positionsumm.buy_avg_price' },
    { field: 'buy_float_pl', label: 'marketrun.monitor.marketer.positionsumm.buy_float_pl' },
    { field: 'sell_hold_qty', label: 'marketrun.monitor.marketer.positionsumm.sell_hold_qty' },
    { field: 'sell_hold_value', label: 'marketrun.monitor.marketer.positionsumm.sell_hold_value' },
    { field: 'sell_avg_price', label: 'marketrun.monitor.marketer.positionsumm.sell_avg_price' },
    { field: 'sell_float_pl', label: 'marketrun.monitor.marketer.positionsumm.sell_float_pl' },
    { field: 'total_float_pl', label: 'marketrun.monitor.marketer.positionsumm.total_float_pl' },
    { field: 'quote_price_last', label: 'marketrun.monitor.marketer.positionsumm.quote_price_last' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const showComponent = (code: string, row?: Model.MarketRunAccpageRsp) => {
    selectedItem.value = row
    openComponent(code)
}

const startCountDown = () => {
    countDown.start(() => run())
}

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

onUnmounted(() => {
    countDown.stop()
})
</script>