<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 #index="{ index }">
          <span>{{ index + 1 }}</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 { queryTableList, MtpTableButton, defineComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import filterCustomTable from './components/filter/index.vue';
import { formatTime, formatValue } from '@/common/methods';
import { useColumn } from './setup';
import { Ermcp3HedgePlan } from '@/services/go/ermcp/plan/interface';
import { QueryHedgePlan } from '@/services/go/ermcp/plan';
import moment from 'moment';

export default defineComponent({
  name: 'search-plan',
  components: {
    MtpTableScroll,
    filterCustomTable,
    MtpTableButton,
  },
  setup() {
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3HedgePlan>();

    // 获取列表数据
    const queryTableAction = () => queryTable(QueryHedgePlan, '2,3').then((res) => {
      // 执行中在上面， 完结的排在下面， 执行中的， 按照单号或者时间，进行倒序排列
      tableList.value = []
      tableList.value = res.sort((a, b) => {
        if (a.hedgeplanstatus !== b.hedgeplanstatus) {
          return a.hedgeplanstatus - b.hedgeplanstatus
        }
        return moment(b.createtime).valueOf() - moment(a.createtime).valueOf()
      })
    });

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

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