<template>
  <!-- 现货合同: 待审核-已审核-->
  <mtp-table-scroll>
    <template #header>
      <filterCustomTable @search="updateColumn"></filterCustomTable>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :pagination="false" :rowKey="(record,index)=>index" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :data-source="tableList" :scroll="scroll">
        <!-- 额外的展开行 -->
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="buttons" :record="record" @click="openComponent" />
        </template>
        <template #biztype="{ text }">
          <a>{{ getBizTypeName(text) }}</a>
        </template>
        <template #pricetype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>
        <template #contracctstatus="{ text }">
          <a>{{ getContractStatusName(text) }}</a>
        </template>
        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>
        <!-- 对手方 -->
        <template #negative="{ record }">
          <a>{{ record.contracttype === 1 ? record.sellusername : record.buyusername }}</a>
        </template>
        <template #startdate="{ record }">
          <span>{{ formatValue(formatTime(record.startdate, 'd') + '--' + formatTime(record.enddate, 'd')) }}</span>
        </template>
        <!-- 交收期 -->
        <template #deliverystartdate="{ record }">
          <span>{{ formatValue(formatTime(record.deliverystartdate, 'd') + '--' + formatTime(record.deliveryenddate, 'd')) }}</span>
        </template>
        <!-- 业务员 -->
        <template #saleuserid="{ record }">
          <span>{{ findManagerName(record.saleuserid) }}</span>
        </template>
        <!-- 跟单员 -->
        <template #meruserid="{ record }">
          <span>{{ findManagerName(record.meruserid) }}</span>
        </template>
        <!-- 合同量 -->
        <template #qty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</span>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 右键 -->
  <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="buttons"> </contextMenu>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import { filterCustomTable } from '../../components';
import { getBizTypeName, getContractStatusName, getContractTypeName, getPriceTypeName } from '@/common/constants/enumsName';
import { formatTime, formatValue } from '@/common/methods';
import { queryBusinessManager, findManagerName, tableFilterCB, handleEnumdic } from '../setup';
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { QuerySpotContract } from '@/services/go/ermcp/spot-contract';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { getTableButton } from '@/common/setup/table/button';
import { useRoute } from 'vue-router';
import moment from 'moment';

export default defineComponent({
  name: EnumRouterName.spot_contract_checkpending,
  components: {
    MtpTableScroll,
    filterCustomTable,
    contextMenu,
    detail: defineAsyncComponent(() => import('../../components/detail/index.vue')),
    check: defineAsyncComponent(() => import('../../components/check/index.vue')), // 审核
    cancel: defineAsyncComponent(() => import('../../components/cancel/index.vue')), // 撤销
    MtpTableButton,
  },
  setup() {
    const { name: routeName } = useRoute();
    // 权限按钮
    const buttons = getTableButton();

    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3ContractRsp>();

    // 获取列表数据
    const queryTableAction = () => {
      switch (routeName) {
        case 'contract_checkpending':
          queryTable(QuerySpotContract, { querytype: 2 }).then((res) => {
            tableList.value.sort((a, b) => {
              return moment(b.createtime).valueOf() - moment(a.createtime).valueOf();
            });
          });
          break;
        case 'contract_performance':
          queryTable(QuerySpotContract, { querytype: 3 });
          break;
      }
      // 获取 业务账户
      queryBusinessManager();
    };

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 现货合同待审核
        case 'contract_checkpending':
          return 'table_pcweb_delivery_dsh';
        // 现货合同已审核
        case 'contract_performance':
          return 'table_pcweb_delivery_ysh';
      }
    };

    // 表格通用逻辑
    const param: ComposeTableParam = {
      queryFn: queryTableAction,
      tableName: getTableName(),
      tableFilterCB,
    };

    return {
      ...handleComposeTable<Ermcp3ContractRsp>(param),
      loading,
      tableList,
      queryTable,
      getBizTypeName,
      getPriceTypeName,
      getContractStatusName,
      getContractTypeName,
      formatTime,
      formatValue,
      findManagerName,
      handleEnumdic,
      buttons,
    };
  },
});
</script>