<template>
  <!-- 敞口: 现货头寸-->
  <div class="exposure-spot table-height"
       :loading="loading">
    <filterCustomTable @search="updateColumn">
    </filterCustomTable>
    <a-table :columns="columns"
             class="srcollYTable"
             :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             :pagination="false"
             rowKey="key"
             :data-source="tableList">
      <template #decreaseqty="{ record }">
        <span>{{ record.decreaseqty.toFixed(2) + " " + record.enumdicname }}</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)' }">
      </a-table>
      <!-- 类型 -->
      <template #logtype="{ record }">
        <span>{{ getLogType(record.logtype) }}</span>
      </template>
    </Description>
  </div>
</template>

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

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

        // 切换明细
        function changeTab(index: number, current: TabList) {
            const { code, lable } = current;
            if (code === 'exposure_spot_details') {
                // 现货明细
                // 注册表头
                registerColumnDetail('table_pcweb_exposure_spotposition_detail', []);
            } else {
                console.error(`${lable}没有配置对应的code: ${code},`);
                return;
            }
            const deliverygoodsid = selectedRow.value?.deliverygoodsid;
            if (deliverygoodsid) {
                queryResultLoadingAndInfo(QuerySpotPositionDetail, loading, { deliverygoodsid }).then((res) => {
                    detailTableList.value = res;
                });
            }
        }
        watchEffect(() => {
            if (visible.value) {
                if (tabList.value.length) {
                    changeTab(0, tabList.value[0]);
                }
            }
        });
        return { loading, tableList, visible, closeDrawer, columns, changeTab, updateColumn, columnsDetail, detailTableList, expandedRowKeys, selectedRow, Rowclick, tabList, getLogType };
    },
});
</script>

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