<template>
  <!-- 计划: 未提交-->
  <mtp-table-scroll>
    <template #header>
      <Filter @search="updateColumn">
        <mtp-table-button class="btn-list-sticky" :buttons="firstBtn" @click="openComponent" />
      </Filter>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handleBtnList(record)" :record="record" @click="openComponent" />
        </template>
        <template #contracttype="{ text }">
          <a>{{ getPlanContractType(text) }}</a>
        </template>
        <template #hedgeplanstatus="{ text }">
          <a>{{ getPlanStatusName(text) }}</a>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 右键 -->
  <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="handleBtnList(selectedRow)"></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 { Filter } from '../../components';
import { getPlanContractType, getPlanStatusName } from '@/views/business/plan/setup';
import { Ermcp3HedgePlan } from '@/services/go/ermcp/plan/interface';
import { QueryHedgePlan } from '@/services/go/ermcp/plan';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { getTableButton } from '@/common/setup/table/button';

export default defineComponent({
  name: EnumRouterName.plan_uncommitted,
  components: {
    MtpTableScroll,
    contextMenu,
    MtpTableButton,
    Filter,
    detail: defineAsyncComponent(() => import('../../components/detail/index.vue')), // 详情
    add: defineAsyncComponent(() => import('../../components/add/index.vue')), // 新增
    delete: defineAsyncComponent(() => import('../../components/delete/index.vue')), // 删除
    modify: defineAsyncComponent(() => import('../../components/modify/index.vue')), // 修改
    resubmit: defineAsyncComponent(() => import('../../components/recommit/index.vue')), // 重新提交
    cancel: defineAsyncComponent(() => import('../../components/cancel/index.vue')), // 撤销
    check: defineAsyncComponent(() => import('../../components/audit/index.vue')), // 审核
  },
  setup() {
    // 权限按钮
    const firstBtn = getTableButton(['add']);
    const secondBtn = getTableButton(['add'], true);
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3HedgePlan>();

    // 获取列表数据
    const queryTableAction = () => queryTable(QueryHedgePlan, '1,4,6');

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

    function handleBtnList(item: Ermcp3HedgePlan) {
      if (item) {
        switch (item.hedgeplanstatus) {
          case 1: // 待审核
            return secondBtn.filter((item) => ['check', 'cancel', 'detail'].includes(item.code));
          case 4: // 拒绝审核
          case 6: // 已撤回
            return secondBtn.filter((item) => ['resubmit', 'delete', 'detail'].includes(item.code));
        }
      }
    }

    return {
      ...handleComposeTable<Ermcp3HedgePlan>(param),
      loading,
      tableList,
      getPlanStatusName,
      getPlanContractType,
      handleBtnList,
      firstBtn,
      secondBtn,
    };
  },
});
</script>