<template>
  <!-- 实时敞口-->
  <div class="exposure-real-time table-height"
       :loading="loading">
    <filterCustomTable @search="updateColumn">
    </filterCustomTable>
    <a-table :columns="columns"
             class="topOrderTable"
             :pagination="false"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             rowKey="key"
             :data-source="tableList"
             :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }">
    </a-table>
    <!-- 明细 -->
    <Description v-if="visible"
                 @close="closeDrawer"
                 @changeTab="changeTab"
                 :columns="columnsDetail"
                 :tabList="tabList"
                 :tableList="detailTableList" />
  </div>
</template>

<script lang="ts">
import { defineComponent, watchEffect } from 'vue';
import filterCustomTable from '../../components/filterTable/index.vue';
import { QueryActualExposure, QueryActualExposureDetail, QueryAutualExposurePosition } from '@/services/go/ermcp/exposure/index';
import { ErmcpRealExposureModel } from '@/services/go/ermcp/exposure/interface';
import Description from '@/common/components/description/index.vue';
import { queryTableList } from '@/common/setup/table';
import { ComposeTableDetailParam } from '@/common/setup/table/interface';
import { TabList } from '@/common/components/description/interface';
import { handleComposeTable_detail } from '@/common/setup/table/compose';
import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { EnumRouterName } from '@/common/constants/enumRouterName';

export default defineComponent({
    name: EnumRouterName.exposure_realtime,
    components: {
        Description,
        filterCustomTable,
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<ErmcpRealExposureModel>();
        // 获取列表数据
        const queryTableAction = () => queryTable(QueryActualExposure);
        const param: ComposeTableDetailParam = {
            queryFn: queryTableAction, // 查询表格数据
            tableName: 'table_pcweb_exposure', // 表头key
            tableFilterKey: ['MiddleGoodsName'], // 表格过滤字段
            menuType: EnumRouterName.exposure_realtime, // 当前tab页对应的code
        };
        const {
            visible,
            closeDrawer, // 控制 drawer 组件是否显示
            columns,
            updateColumn, //  表头数据
            columnsDetail,
            registerColumnDetail,
            detailTableList, // 明细表头数据
            expandedRowKeys,
            selectedRow,
            Rowclick, // 表格事件
            tabList,
        } = handleComposeTable_detail<ErmcpRealExposureModel>(param);

        // 切换明细
        function changeTab(index: number, current: TabList) {
            const { code, lable } = current;
            let fn = null;
            if (code === 'exposure_realtime_spot_details') {
                // 现货明细信息
                // 注册表头
                registerColumnDetail('table_pcweb_exposure_detail', []);
                fn = QueryActualExposureDetail;
            } else if (code === 'exposure_realtime_futures_details') {
                // 期货明细
                // 注册表头
                registerColumnDetail('table_pcweb_exposure_futures_detail', []);
                fn = QueryAutualExposurePosition;
            } else {
                console.error(`${lable}没有配置对应的code: ${code},`);
                return;
            }
            const middlegoodsid = selectedRow.value?.MiddleGoodsID;
            if (middlegoodsid) {
                queryResultLoadingAndInfo(fn, loading, { middlegoodsid }).then((res) => {
                    detailTableList.value = res;
                });
            }
        }
        watchEffect(() => {
            if (visible.value) {
                if (tabList.value.length) {
                    changeTab(0, tabList.value[0]);
                }
            }
        });

        return { loading, tableList, visible, closeDrawer, columns, updateColumn, columnsDetail, detailTableList, expandedRowKeys, selectedRow, Rowclick, tabList, changeTab };
    },
});
</script>

<style lang="less">
.exposure-real-time {
    position: relative;
    overflow: hidden;
    .bottomTable {
        width: 100%;
        .flex;
        flex-direction: column;
        .topTable {
            height: calc(100% - 46px);
            .ant-table {
                background-color: transparent;
            }
        }
    }
}
</style>;
