<template>
  <!-- 掉期订单-合约汇总 -->
  <mtp-table-scroll>
    <template #default="{ scroll }">
      <a-table :columns="getTableColums()" class="srcollYTableExpendBgColor srcollYTable" :scroll="scroll" :pagination="false" :loading="loading" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :expandIcon="expandIcon" :expandIconAsCell="false" rowKey="key" :data-source="positionList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <a-table :columns="getDetailColums()" class="expandBottomTable" :pagination="false" :loading="detailLoading" :expandedRowKeys="expandedRowKeys" :expandIcon="expandIcon" :expandIconAsCell="false" rowKey="key" :data-source="getTradeHolderDetail(record)">
            <template #operate="sub">
              <BtnList :btnList="handleBtnList(record, buttons)" :record="sub.record" class="btn-list-sticky" @click="openComponent" />
            </template>
            <!-- 持仓盈亏 -->
            <template #profitloss="{ record }">
              <span :class="[+getDetailProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) > 0 ? 'up-quote-color' : +getDetailProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) < 0 ? 'down-quote-color' : '']">{{ getDetailProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) }}</span>
            </template>
            <!-- 市值 -->
            <template #marketamount="{ record }">
              <span>{{ getDetailMarketAmount(record) }}</span>
            </template>
            <template #expiredate="{ text }">
              <span>{{ handleNoneValue(text) }}</span>
            </template>
          </a-table>
        </template>
        <template #createtime="{ record }">
          <a>{{ formatTime(record.createtime) }}</a>
        </template>
        <!-- 现价 -->
        <template #lastprice="{ record }">
          <a>{{ getLastprice(record) }}</a>
        </template>
        <!-- 均价 -->
        <template #averageprice="{ text }">
          <a>{{ handleNoneValue(text) }}</a>
        </template>
        <!-- 持仓盈亏 -->
        <template #profitloss="{ record }">
          <span :class="[+useProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) > 0 ? 'up-quote-color' : +useProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) < 0 ? 'down-quote-color' : '']">{{ record.averageprice ? useProfitloss(record, findGoodsCode(record.goodsid, record.goodscode, swapList)) : '--' }}</span>
        </template>
        <template #buyorsell="{ record }">
          <span>{{ getBuyOrSellName(record.buyorsell) }}</span>
        </template>
        <!-- <template #marketamount="{ record, text }">
        <span>{{ isDiaoQi(record) ? '--' : text }}</span>
      </template>-->
      </a-table>
    </template>
  </mtp-table-scroll>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" :tableList="positionList" :swapList="swapList" @cancel="clsoeAction"></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 { getBuyOrSellName } from '@/common/constants/enumsName';
import { BtnList, defineAsyncComponent, defineComponent, queryTableList } from '@/common/export/commonTable';
import { formatTime } from '@/common/methods';
import { handleDeliveryRelation } from '@/common/setup/deliveryRelation';
import { expandIcon } from '@/common/setup/table/clolumn';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { handleNoneValue, handleQuotePriceColor } from '@/common/setup/table/tableQuote';
import { getQuoteDayInfoByCodeFindPrice } from '@/services/bus/goods';
import { useHolderprice } from '@/services/bus/holdPosition';
import { QueryTradePositionRsp } from '@/services/go/ermcp/order/interface';
import Bus from '@/utils/eventBus/index';
import { isInvestment } from '@/views/market/market-spot/spot_trade_order_transaction/spot_trade_order_transaction_swap/components/setup';
import { useHazardRates } from '@/views/order/funding_information/components/funding_information_funding_summary/setup'
import { findGoodsCode, getTableColums, useDetail } from './setup';
import { DynamicComponent } from '@/@next/hooks/common/interface'

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTableScroll,
    BtnList,
    deal_closed: defineAsyncComponent(() => import('./components/swap_commodity_contract_summary_deal_closed/index.vue')), // 协议平仓
    order_closed: defineAsyncComponent(() => import('./components/swap_commodity_contract_summary_order_closed/index.vue')), // 平仓
  },
  setup(props) {
    const { buttons } = props.parentComponent;
    const { positionList, swapList, getHoldsList, getSwapList, getTradeHolderDetail, useProfitloss } = useHazardRates();
    // 表格列表数据
    const { loading } = queryTableList<QueryTradePositionRsp>();

    // 交割商品
    handleDeliveryRelation();
    getSwapList();

    // 获取列表数据
    const queryTableAction = () => getHoldsList(loading, TradeMode.DiaoQi.toString());

    // 明细 逻辑
    const { detailLoading, getDetailColums, getDetailProfitloss, getDetailMarketAmount } = useDetail();
    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: queryTableAction,
      tableName: 'table_pcweb_spot_trade_bottom_commodity_contract_summary',
    }

    // 现价
    function getLastprice(record: QueryTradePositionRsp) {
      let result: number | string = '--';
      const goodscode = findGoodsCode(record.goodsid, record.goodscode, swapList.value);
      if (goodscode) {
        result = getQuoteDayInfoByCodeFindPrice(goodscode);
      }
      return result;
    }

    function handleBtnList(record: QueryTradePositionRsp, btnList: BtnListType[]) {
      // 贸易圈
      // 只有机构 才有平仓
      const diaoqi = isInvestment() ? ['deal_closed', 'order_closed'] : ['deal_closed'];
      return btnList.filter((e) => diaoqi.includes(e.code));
    }
    const {
      contextMenu, openContext, closeContext, // 右键
      columns, registerColumn, updateColumn,  // 表头
      expandedRowKeys, selectedRow, Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId, closeComponent, openComponent,  // 控制异步组件
    } = handleComposeOrderTable<QueryTradePositionRsp>(param)

    function clsoeAction(value: boolean) {
      closeComponent(value)
    }

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

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

    return {
      contextMenu, openContext, closeContext, // 右键
      columns, registerColumn, updateColumn,  // 表头
      expandedRowKeys, selectedRow, Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId, closeComponent, openComponent,  // 控制异步组件
      buttons, // 表格按钮
      loading,
      positionList,
      formatTime,
      expandIcon,
      getLastprice,
      useProfitloss,
      useHolderprice,
      handleBtnList,
      getTableColums,
      getBuyOrSellName,
      swapList,
      handleNoneValue,
      detailLoading,
      getDetailColums,
      getTradeHolderDetail,
      findGoodsCode,
      getDetailProfitloss, getDetailMarketAmount, handleQuotePriceColor, clsoeAction
    }
  }
})
</script>