<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="(record, index) => index" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <BtnList :btnList="auth" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <!-- 挂牌有效期 -->
        <template #validtime="{ text }">
          <span>{{ formatTime(text, 'd') }}</span>
        </template>
        <!-- 挂牌类型 -->
        <template #buyorsell="{ text }">
          <span>{{ text ? '买' : '卖' }}</span>
        </template>
        <!--  委托状态-->
        <!-- <template #wrtradeorderstatus="{ record }">
        <a>{{ getWrTradeOrderStatusName(record.wrtradeorderstatus) }}</a>
      </template> -->
        <!-- 挂牌类型 -->
        <template #wrtradetype="{ record }">
          <a>{{ getWrOrderTypeName(record.buyorsell, record.wrtradetype) }}</a>
        </template>
        <template #applystatus="{ text }">
          <span>{{ getBarmaginApplyStatus(text) }}</span>
        </template>
        <template #applytime="{ text }">
          <span>{{ formatTime(text) }}</span>
        </template>
        <template #marginvalue="{ text }">
          <span>{{ (text * 100).toFixed(2) }}%</span>
        </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 { queryTableList, BtnList, defineAsyncComponent, defineComponent } from '@/common/export/commonTable';
import { queryQueryWrTradeBargainApply } from '@/services/go/wrtrade';
import { QueryWrTradeBargainApplyQsp, QueryWrTradeBargainApplyReq } from '@/services/go/wrtrade/interface';
import { getRecordItemTab } from '@/common/setup/order/orderData';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { getWrOrderTypeName, getWrTradeOrderStatusName, getBarmaginApplyStatus } from '@/common/constants/enumsName';
import { expandIcon } from '@/common/setup/table/clolumn';
import { getSpotWarrantBargainColumns } from './setup';
import { formatTime } from '@/common/methods';
import { getUserId } from '@/services/bus/user';
import { DynamicComponent } from '@/@next/hooks/common/interface'
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import Bus from '@/utils/eventBus';

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

    // 获取列表数据
    const queryTableAction = () => {
      const param: QueryWrTradeBargainApplyReq = {
        userid: getUserId(),
        haswr: 0,
      };
      queryTable(queryQueryWrTradeBargainApply, param);
    };

    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: queryTableAction,
      recordList: getRecordItemTab(),
    };

    const {
      contextMenu,
      openContext,
      closeContext, // 右键
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId,
      closeComponent,
      openComponent, // 控制异步组件
    } = handleComposeOrderTable<QueryWrTradeBargainApplyQsp>(param);

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

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

    return {
      contextMenu,
      openContext,
      closeContext, // 右键
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId,
      closeComponent,
      openComponent, // 控制异步组件
      auth, // 表格按钮
      columns: getSpotWarrantBargainColumns(),
      loading,
      tableList,
      getWrTradeOrderStatusName,
      getWrOrderTypeName,
      expandIcon,
      formatTime,
      getBarmaginApplyStatus,
    }
  }
})
</script>