<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>
      </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, defineComponent, defineAsyncComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import filterCustomTable from './components/filter/index.vue';
import { formatTime, formatValue } from '@/common/methods';
import { useColumn } from './setup';
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { QuerySpotContract } from '@/services/go/ermcp/spot-contract';
import { getTableButton } from '@/common/setup/table/button';
import { useRoute } from 'vue-router';
import moment from 'moment';

export default defineComponent({
  name: 'spot-contract-search',
  components: {
    MtpTableScroll,
    filterCustomTable,
    MtpTableButton,
    contextMenu,
    detail: defineAsyncComponent(() => import('@/views/information/spot-contract/components/detail/index.vue')),
  },
  setup() {
    const { name: routeName } = useRoute();
    const buttons = getTableButton();
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3ContractRsp>();

    // 获取列表数据
    const queryTableAction = () => {
      // 执行中在上面， 完结的排在下面， 执行中的， 按照单号或者时间，进行倒序排列
      const tableSort = (data: Ermcp3ContractRsp[]) => {
        // FIXME: - 这里有覆盖先后顺序的问题
        tableList.value = []
        tableList.value = data.sort((a, b) => {
          if (a.contracctstatus !== b.contracctstatus) {
            return a.contracctstatus - b.contracctstatus
          }
          return moment(b.createtime).valueOf() - moment(a.createtime).valueOf()
        })
      }
      switch (routeName) {
        case 'search_purchase':
          queryTable(QuerySpotContract, { contracttype: 1, querytype: '3,4' }).then(res => {
            tableSort(res);
          });
          break;
        case 'search_sell':
          queryTable(QuerySpotContract, { contracttype: -1, querytype: '3,4' }).then(res => {
            tableSort(res);
          });
          break;
      }
    };

    // 表格通用逻辑
    const param: ComposeTableParam = {
      queryFn: queryTableAction,
      tableName: 'table_pcweb_delivery',
    };

    return {
      ...handleComposeTable<Ermcp3ContractRsp>(param),
      ...useColumn(),
      loading,
      tableList,
      buttons,
      queryTable,
      formatTime,
      formatValue,
    };
  },
});
</script>