<template>
    <!-- 套期交易：未关联采购合同-未关联销售合同 -->
    <mtp-table-scroll>
        <template #header>
            <Filter @search="updateColumn"></Filter>
        </template>
        <template #default="{ scroll }">
            <a-table :columns="columns" class="srcollYTable" :pagination="false" :loading="loading"
                :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record, index) => index"
                :data-source="tableList" :scroll="scroll">
                <template #expandedRowRender="{ record }">
                    <mtp-table-button class="btn-list-sticky" :buttons="auth" :record="record" @click="openComponent" />
                </template>
                <!-- 合同类型 -->
                <template #contracttype="{ text }">
                    <span>{{ getSpotContractTypeName(text) }}</span>
                </template>
                <!-- 定价类型 -->
                <template #pricetype="{ text }">
                    <span>{{ getPriceTypeName(text) }}</span>
                </template>
            </a-table>
        </template>
    </mtp-table-scroll>
    <!-- 右键 -->
    <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="auth"> </contextMenu>
    <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { queryTableList, contextMenu, defineComponent, MtpTableButton, defineAsyncComponent, handleComposeTable } from '@/common/export/commonTable'
import { UnLinkSpotContractReq, UnLinkSpotContractRsp } from '@/services/go/ermcp/hedgedItem/interface'
import { queryUnLinkSpotContract } from '@/services/go/ermcp/hedgedItem'
import { useMenu } from '@/@next/hooks/common'
import { getPriceTypeName } from '@/@next/constants/enum/priceType'
import { SpotContractType, getSpotContractTypeName } from '@/@next/constants/enum/spotContractType'
import { getUserId } from '@/services/bus/user'
import MtpTableScroll from '@/common/components/tableScroll/index.vue'
import Filter from './components/filter/index.vue';

export default defineComponent({
    components: {
        MtpTableScroll,
        Filter,
        MtpTableButton,
        contextMenu,
        detail: defineAsyncComponent(() => import('@/views/information/spot-contract/components/detail/index.vue')), // 详情
        bind: defineAsyncComponent(() => import('./components/bind/index.vue')), // 关联项目
    },
    setup() {
        const { route, auth } = useMenu();
        const { loading, tableList, queryTable } = queryTableList<UnLinkSpotContractRsp>(true, 2); // 表格列表数据

        const queryFn = () => {
            const param: UnLinkSpotContractReq = {
                userid: getUserId(),
                contracttype: 0
            }
            switch (route.name) {
                // 采购合同
                case 'hedgeditem_spot_purchase': {
                    param.contracttype = SpotContractType.Buy;
                    break;
                }
                // 销售合同
                case 'hedgeditem_spot_sell': {
                    param.contracttype = SpotContractType.Sell;
                    break;
                }
            }
            queryTable(queryUnLinkSpotContract, param);
        }

        // 表格通用逻辑
        const composeTable = handleComposeTable<UnLinkSpotContractRsp>({
            queryFn,
            tableName: 'table_pcweb_hedgeditem_spot_contract',
        })

        return {
            ...composeTable,
            loading,
            tableList,
            auth,
            getSpotContractTypeName,
            getPriceTypeName,
        }
    }
})
</script>