<template>
  <Filter @update="search" @search="updateColumn"></Filter>
  <!-- 敞口报表 -->
  <mtp-table-scroll>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :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 #contracttype="{ text }">
      <a>{{ getContractTypeName(text) }}</a>
    </template>
    <template #biztype="{ text }">
      <a>{{ getBizTypeName(text) }}</a>
    </template>
    <template #buyorsell="{ text }">
      <a>{{ getBuyOrSellName(text) }}</a>
    </template>
  </mtp-table-detail>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import Filter from '../../components/filterTable/index.vue';
import { defineComponent, queryTableList } from '@/common/export/table';
import { TypeAndTime } from '@/views/report/interface';
import { handleInitTypeAndTime } from '@/views/report/setup';
import { AreaExpourseReportReq, Ermcp3ExpourseReport, QryAreaExpourseHedgeplanDetailReq } from '@/services/go/ermcp/report/interface';
import { qryAreaExpourseContractDetail, qryAreaExpourseFutuDetail, qryAreaExpourseHedgeplanDetail, qryAreaExpourseParamChLogDetail, qryAreaExpourseReport } from '@/services/go/ermcp/report';
import { ComposeTableDetailParam, handleComposeTable_detail } from '@/common/setup/table/compose';
import { ref, watch } from 'vue';
import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { getBizTypeName, getBuyOrSellName, getContractTypeName } from '@/common/constants/enumsName';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { getTableButton } from '@/common/setup/table/button';
import MtpTableDetail from '@/common/components/tableDetail/index.vue';

export default defineComponent({
  name: EnumRouterName.exposure_report_exposure,
  components: {
    MtpTableScroll,
    MtpTableDetail,
    Filter,
  },
  setup() {
    let cycletime = '';
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3ExpourseReport>(true, 2);
    // 控制明细显示/隐藏
    const visible = ref(true);
    // 获取列表数据
    const queryTableAction = () => {
      const { getInitTime, getInitType } = handleInitTypeAndTime();
      cycletime = getInitTime();
      const param: AreaExpourseReportReq = {
        cycletype: getInitType(),
        cycletime,
        querytype: 1,
      };
      // 获取列表数据
      queryTable(qryAreaExpourseReport, param).then((res) => {
        if (res.length) {
          selectedRow.value = res[0];
        } else {
          changeTab(tabIndex.value)
        }
      });
    };
    const param: ComposeTableDetailParam = {
      queryFn: queryTableAction, // 查询表格数据
      tableName: 'table_pcweb_exposure_report', // 表头key
      menuType: EnumRouterName.exposure_report_exposure, // 当前tab页对应的code
    };
    const {
      columns,
      updateColumn, //  表头数据
      columnsDetail,
      registerColumnDetail,
      detailTableList, // 明细表头数据
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格事件
    } = handleComposeTable_detail<Ermcp3ExpourseReport>(param);
    const chaceSearchValue = ref<TypeAndTime>();

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

    // 切换明细
    function changeTab(index: number) {
      const { code, lable } = tabList[index];
      tabIndex.value = index;
      detailTableList.value = [];
      visible.value = true;

      let fn = null;
      if (code === 'exposure_report_exposure_contract_changes') {
        // 现货合同变动量明细
        // 注册表头
        registerColumnDetail('table_pcweb_change_in_spot_contract');
        fn = qryAreaExpourseContractDetail;
      } else if (code === 'exposure_report_exposure_hedging_plan') {
        // 套保计划变动量明细
        // 注册表头
        registerColumnDetail('table_pcweb_change_in_hedging_plan');
        fn = qryAreaExpourseHedgeplanDetail;
      } else if (code === 'exposure_report_exposure_futures_changes') {
        // 期货变动量明细
        // 注册表头
        registerColumnDetail('table_pcweb_change_in_future');
        fn = qryAreaExpourseFutuDetail;
      } else if (code === 'exposure_report_exposure_parameter_adjustment_changes') {
        // 参数调整变动量明细
        // 注册表头
        registerColumnDetail('table_pcweb_change_in_parameter_adjustment');
        fn = qryAreaExpourseParamChLogDetail;
      } else {
        console.error(`${lable}没有配置对应的code: ${code},`);
        return;
      }

      if (selectedRow.value) {
        const data = selectedRow.value as Ermcp3ExpourseReport;
        const { cycletype, middlegoodsid } = data;
        const param: QryAreaExpourseHedgeplanDetailReq = {
          middlegoodsid,
          cycletype,
          cycletime,
          userid: chaceSearchValue.value?.userid,
        };
        // 查询明细数据
        queryResultLoadingAndInfo(fn, loading, param).then((res) => {
          detailTableList.value = res;
        });
      }
    }

    function search(value: TypeAndTime) {
      detailTableList.value = [];
      chaceSearchValue.value = value;
      cycletime = value.cycletime;
      const obj = Object.assign(value, { querytype: 1 });
      queryTable(qryAreaExpourseReport, obj).then((res) => {
        if (res.length) {
          selectedRow.value = res[0];
        }
      });
    }

    watch(selectedRow, () => changeTab(tabIndex.value))

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