<template>
  <!-- 敞口: 期货头寸-->
  <div class="exposure-futures  table-height"
       :loading="loading">
    <filterCustomTable @search="updateColumn">
    </filterCustomTable>
    <a-table :columns="columns"
             class="topTable"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             :pagination="false"
             rowKey="key"
             :data-source="tableList">
        <template #totalydqty="{ record }">
            <span>{{ record.totalydqty + " 手"  }}</span>
        </template>
        <template #increaseqty="{ record }">
            <span>{{ record.increaseqty + " 手"  }}</span>
        </template>
        <template #decreaseqty="{ record }">
            <span>{{ record.decreaseqty + " 手"  }}</span>
        </template>
        <template #totalcurqty="{ record }">
            <span>{{ record.totalcurqty + " 手"  }}</span>
        </template>
    </a-table>
    <!-- 明细 -->
    <Description v-if="visible"
                 @close="closeDrawer"
                 @changeTab="changeTab"
                 :tabList="tabList">
      <a-table :columns="columnsDetail"
               class="topTable"
               :pagination="false"
               rowKey="key"
               :data-source="detailTableList"
               :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }">
          <!-- 买卖方向 -->
          <template #buyorsell="{ record }">
              <span>{{ getBuyOrSellName(record.buyorsell)  }}</span>
          </template>
          <!-- 开屏方向 -->
          <template #channelbuildtype="{ record }">
              <span>{{ getChannelBuildName(record.channelbuildtype) }}</span>
          </template>

      </a-table>


    </Description>
  </div>
</template>

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

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

        // 切换明细
        function changeTab(index: number, current: TabList) {
            const { code, lable } = current;
            if (code === 'exposure_futures_details') {
                // 现货明细
                // 注册表头
                registerColumnDetail('table_pcweb_exposure_futuresposition_detail', []);
            } else {
                console.error(`${lable}没有配置对应的code: ${code},`);
                return;
            }
            const goodsId = selectedRow.value?.goodsid;
            if (goodsId) {
                queryResultLoadingAndInfo(QueryExposureHedgePositionDetail, loading, { goodsId }).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, getBuyOrSellName, getChannelBuildName };
    },
});
</script>

<style lang="less">
.exposure-futures {
    position: relative;
    overflow: hidden;
}
</style
>;
