<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="buttons.length" #expandedRowRender="{ record }">
          <BtnList :btnList="filterBtnList(buttons, record)" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <!-- 类型 -->
        <template #buildtype="{ record }">
          <a>{{ getBuyOrSellTypeName(record.buyorsell) }}</a>
        </template>
        <!-- 状态 -->
        <template #orderstatus="{ record }">
          <a>{{ getOrderStatusName(record.orderstatus) }}</a>
        </template>
        <template #ordertime="{ 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 MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { BtnListType } from '@/common/components/btnList/interface';
import { TradeMode } from '@/common/constants/enumCommon';
import { getBuyOrSellTypeName, getOrderStatusName } from '@/common/constants/enumsName';
import { BtnList, defineAsyncComponent, 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 { queryTradeOrderDetail } from '@/services/go/ermcp/order';
import { QueryTradeOrderDetailRsp } from '@/services/go/ermcp/order/interface';
import { WrPerformancePlan } from '@/services/go/wrtrade/interface';
import Bus from '@/utils/eventBus/index';
import { DynamicComponent } from '@/@next/hooks/common/interface'

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTableScroll,
    BtnList,
    cancel: defineAsyncComponent(() => import('./components/cancel/index.vue')), // 撤单
  },
  setup(props) {
    const { buttons } = props.parentComponent;
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryTradeOrderDetailRsp>();

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

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

    function filterBtnList(list: BtnListType[], record: QueryTradeOrderDetailRsp) {
      //委托状态 - 1:委托请求 2:待冻结 3:委托成功 4:委托失败 5:配对成功 6:已撤 7:已成 8:成交失败 9:委托拒绝 1number;
      const arr = [3, 7];
      if (!arr.includes(record.orderstatus)) {
        return list.filter((e) => e.code !== 'cancel');
      } else {
        return list;
      }
    }

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

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

    return {
      ...handleComposeOrderTable<WrPerformancePlan>(param),
      loading,
      tableList,
      buttons,
      formatTime,
      expandIcon,
      getBuyOrSellTypeName,
      getOrderStatusName,
      filterBtnList,
    }
  }
})
</script>