<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="handleBtnLsit(record)" :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 #marginvalue="{ text }">
          <span>{{ (text * 100).toFixed(2) }}%</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>
        <!-- 挂牌价格 -->
      </a-table>
    </template>
  </mtp-table-scroll>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { enumOrderComponents } from '@/common/constants/enumOrderComponents';
import { getBarmaginApplyStatus, getWrOrderTypeName, getWrTradeOrderStatusName } from '@/common/constants/enumsName';
import { BtnList, defineAsyncComponent, defineComponent, queryTableList } from '@/common/export/commonTable';
import { formatTime } from '@/common/methods';
import { getRecordItemTab } from '@/common/setup/order/orderData';
import { expandIcon } from '@/common/setup/table/clolumn';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { getUserId } from '@/services/bus/account';
import { queryQueryWrTradeBargainApply } from '@/services/go/wrtrade';
import { QueryWrTradeBargainApplyQsp, QueryWrTradeBargainApplyReq } from '@/services/go/wrtrade/interface';
import Bus from '@/utils/eventBus';
import { getSpotWarrantBargainColumns } from './setup';

export default defineComponent({
  name: enumOrderComponents.spot_warrant_pending_order,
  components: {
    BtnList,
    MtpTableScroll,
    detail: defineAsyncComponent(() => import('./components/detail/index.vue')),
  },
  setup() {
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryWrTradeBargainApplyQsp>();
    // 获取列表数据
    const queryTableAction = () => {
      const param: QueryWrTradeBargainApplyReq = {
        userid: getUserId(),
        haswr: 1,
      };
      queryTable(queryQueryWrTradeBargainApply, param);
    };
    Bus.$on('blocsTrade', () => {
      queryTableAction();
    });

    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: queryTableAction,
      recordList: getRecordItemTab(),
    };
    const {
      contextMenu,
      openContext,
      closeContext, // 右键
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId,
      closeComponent,
      openComponent, // 控制异步组件
      btnList, // 表格按钮
    } = handleComposeOrderTable<QueryWrTradeBargainApplyQsp>(param);
    function handleBtnLsit(record: QueryWrTradeBargainApplyQsp) {
      // applystatus: number;//申请状态(议价状态) - 1:待确认 2:已确认 3:已拒绝 4:已撤销 5:系统撤销 6:处理失败 7:确认中
      const arr = [2, 3];
      return arr.includes(record.applystatus) ? btnList : [];
    }
    return {
      contextMenu,
      openContext,
      closeContext, // 右键
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格折腾面板数据与单击、双击事件
      componentId,
      closeComponent,
      openComponent, // 控制异步组件
      btnList, // 表格按钮
      columns: getSpotWarrantBargainColumns(),
      loading,
      tableList,
      getWrTradeOrderStatusName,
      getWrOrderTypeName,
      expandIcon,
      formatTime,
      getBarmaginApplyStatus,
      handleBtnLsit,
    };
  },
});
</script>