<template>
  <!-- 敞口: 现货头寸-->
  <mtp-table-scroll>
    <template #header>
      <filterCustomTable @search="updateColumn" @update="queryTableAction"></filterCustomTable>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :pagination="false" :rowKey="(record,index)=>index" :data-source="tableList"></a-table>
    </template>
  </mtp-table-scroll>
  <!-- 明细 -->
  <mtp-table-detail :columns="columnsDetail" :data-source="detailTableList" :tabs="tabList" v-model:visible="visible" @change="changeTab">
    <!-- 类型 -->
    <template #logtype="{ record }">
      <span>{{ getLogType(record.logtype) }}</span>
    </template>
  </mtp-table-detail>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { defineComponent, ref, watch } from 'vue';
import filterCustomTable from './filter.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 { EnumRouterName } from '@/common/constants/enumRouterName';
import { getLogType } from '@/common/constants/enumsName';
import { getTableButton } from '@/common/setup/table/button';
import { useColumn } from './setup';
import MtpTableDetail from '@/common/components/tableDetail/index.vue';

export default defineComponent({
  name: EnumRouterName.exposure_spot,
  components: {
    MtpTableScroll,
    MtpTableDetail,
    filterCustomTable,
  },
  setup() {
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3AreaSpot>(true, 2);
    // 控制明细显示/隐藏
    const visible = ref(true);
    // 用户id
    const userId = ref(0);
    // 获取列表数据
    const queryTableAction = (userid = 0) => {
      userId.value = userid;
      queryTable(QuerySpotPosition, userid).then((res) => {
        if (res.length) {
          selectedRow.value = res[0];
        }
      })
    }
    const param: ComposeTableDetailParam = {
      queryFn: queryTableAction, // 查询表格数据
      tableName: 'table_pcweb_exposure_spotposition', // 表头key
      menuType: EnumRouterName.exposure_spot, // 当前tab页对应的code
    };
    const {
      registerColumnDetail,
      detailTableList, // 明细表头数据
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格事件
    } = handleComposeTable_detail<Ermcp3AreaSpot>(param);

    // 底部明细标签
    const tabList = getTableButton();
    // 当前选中的标签
    const tabIndex = ref(0);

    // 切换明细
    function changeTab(index: number) {
      tabIndex.value = index;
      detailTableList.value = [];
      visible.value = true;

      const { code, lable } = tabList[index];
      if (code === 'position_spot_details') {
        // 现货明细
        // 注册表头
        registerColumnDetail('table_pcweb_exposure_spotposition_detail');
      } else {
        console.error(`${lable}没有配置对应的code: ${code},`);
        return;
      }

      if (selectedRow.value) {
        const wrstandardid = selectedRow.value.wrstandardid;
        if (wrstandardid) {
          queryResultLoadingAndInfo(QuerySpotPositionDetail, loading, { userid: userId.value, wrstandardid }).then((res) => {
            detailTableList.value = res;
          })
        }
      }
    }
    watch(selectedRow, () => changeTab(tabIndex.value))
    return {
      ...useColumn(),
      loading,
      tableList,
      visible,
      queryTableAction,
      changeTab,
      detailTableList,
      expandedRowKeys,
      selectedRow,
      Rowclick,
      tabList,
      getLogType,
    };
  },
});
</script>