<template>
  <!-- 掉期订单-成交 -->
  <mtp-table-scroll>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading"
        :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :expandIcon="expandIcon" :expandIconAsCell="false"
        rowKey="key" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template v-if="auth.length" #expandedRowRender="{ record }">
          <BtnList :btnList="auth" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <!-- 类型 -->
        <template #buyorsell="{ text }">
          <span>{{ getBuyOrSellTypeName(text) }}</span>
        </template>
        <!-- 成交类型 -->
        <template #buildtype="{ text }">
          <span>{{ getChannelBuildName(text) }}</span>
        </template>
        <template #tradeprice="{ text, record }">
          <span>{{ handleType(record, text) }}</span>
        </template>
        <template #matchaccountid="{ text, record }">
          <span>{{ handleType(record, text) }}</span>
        </template>
        <!-- 状态 -->
        <template #orderstatus="{ record }">
          <a>{{ getOrderStatusName(record.orderstatus) }}</a>
        </template>
        <template #tradetime="{ text }">
          <a>{{ formatTime(text) }}</a>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { PropType, onBeforeUnmount } from 'vue';
import { DynamicComponent } from '@/@next/hooks/common/interface'
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { TradeMode } from '@/common/constants/enumCommon';
import { getBuildTypeName, getBuyOrSellTypeName, getOrderStatusName, getChannelBuildName } from '@/common/constants/enumsName';
import { BtnList, defineComponent, queryTableList } from '@/common/export/commonTable';
import { formatTime } from '@/common/methods';
import { expandIcon } from '@/common/setup/table/clolumn';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { handleNoneValue } from '@/common/setup/table/tableQuote';
import { queryTradeDetail } from '@/services/go/ermcp/order';
import { QueryTradeDetailRsp } from '@/services/go/ermcp/order/interface';
import Bus from '@/utils/eventBus/index';

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTableScroll,
    BtnList,
  },
  setup(props) {
    const { auth } = props.parentComponent;
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryTradeDetailRsp>();

    // 获取列表数据
    const queryTableAction = () => {
      queryTable(queryTradeDetail, { tradeMode: TradeMode.DiaoQi.toString() });
    }

    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: queryTableAction,
      tableName: 'table_pcweb_spot_trade_bottom_commodity_contract_make_deal',
    }

    // fix: #2063
    function handleType(record: QueryTradeDetailRsp, value: string | number) {
      let result: number | string = '--';
      if (value) {
        result = record.tradetype === 22 ? '--' : value;
      }
      return result;
    }

    const stopNotify = Bus.$on('spotTrade', () => {
      queryTableAction();
    })

    onBeforeUnmount(() => {
      stopNotify();
    })

    return {
      ...handleComposeOrderTable<QueryTradeDetailRsp>(param),
      loading,
      tableList,
      auth,
      formatTime,
      expandIcon,
      getBuildTypeName,
      getOrderStatusName,
      getBuyOrSellTypeName,
      handleNoneValue,
      handleType,
      getChannelBuildName,
    }
  }
})
</script>