<template>
    <!-- 履约信息-卖履约 -->
    <mtp-table-scroll>
        <template #default="{ scroll }">
            <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading"
                :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :expandIcon="expandIcon"
                :expandIconAsCell="false" :rowKey="(record, index) => index" :data-source="tableList">
                <!-- 额外的展开行 -->
                <template v-if="auth.length" #expandedRowRender="{ record }">
                    <BtnList :btnList="handleBtn(auth, record)" :record="record" class="btn-list-sticky"
                        @click="openComponent" />
                </template>
                <!-- 履约类型 -->
                <template #performancetype="{ record }">
                    <a>{{ getPerformanceTypeName(record.performancetype, record.haswr) }}</a>
                </template>
                <!-- 付款方式 -->
                <template #paymenttype="{ record }">
                    <a>{{ getPaymentTypeName(record.paymenttype) }}</a>
                </template>
                <!-- 履约状态 -->
                <template #performancestatus="{ record }">
                    <a>{{ getPerformanceStatusName(record.performancestatus) }}</a>
                </template>
                <!-- 当前步骤 -->
                <template #curstepname="{ record }">
                    <a>{{ record.curstepname }}</a>
                </template>
            </a-table>
        </template>
    </mtp-table-scroll>
    <component :is="componentId" v-if="componentId" :buyOrSell="BuyOrSell.sell" :selectedRow="selectedRow"
        @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { PropType } from 'vue';
import { DynamicComponent } from '@/@next/hooks/common/interface';
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, BtnList, defineAsyncComponent, defineComponent, ModalEnum } from '@/common/export/commonTable';
import { QueryPerformancePlan } from '@/services/go/wrtrade';
import { QueryPerformancePlanReq, WrPerformancePlan } from '@/services/go/wrtrade/interface';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { expandIcon } from '@/common/setup/table/clolumn';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { BtnListType } from '@/common/components/btnList/interface';
import { BuyOrSell } from '@/common/constants/enumCommon';
import { getPaymentTypeName, getPerformanceStatusName, getPerformanceTypeName } from '@/common/constants/enumsName';

export default defineComponent({
    props: {
        parentComponent: {
            type: Object as PropType<DynamicComponent>,
            required: true,
        },
    },
    components: {
        MtpTableScroll,
        BtnList,
        pay: defineAsyncComponent(() => import('../components/pay/index.vue')), // 付款
        extension: defineAsyncComponent(() => import('../components/extension/index.vue')), // 延期
        breach: defineAsyncComponent(() => import('../components/breach_contract/index.vue')), // 违约
        detail: defineAsyncComponent(() => import('../components/detail/index.vue')), // 详情
        settlement: defineAsyncComponent(() => import('../components/settlement/index.vue')), // 交收
    },
    setup(props) {
        const { auth } = props.parentComponent;
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<WrPerformancePlan>();

        // 获取列表数据
        const queryTableAction = () => {
            const param: QueryPerformancePlanReq = {
                buyorsell: 1,
            };
            queryTable(QueryPerformancePlan, param);
        };

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

        function handleBtn(btnList: BtnListType[], record: WrPerformancePlan): BtnListType[] {
            //  performancestatus: number;//履约状态 - 1:初始化 2:正常 3:处理错误 4:违约待处理 5:违约处理中 6:完成 7.违约已完成 8:释放冻结失败 9:超时待处理 1number;
            const { curstepname, performancestatus } = record;
            let result: string[] = [];
            if (performancestatus === 1) {
                result = ['breach'];
            } else if (performancestatus === 2) {
                switch (curstepname) {
                    case '卖方发货':
                    case '买方支付':
                    case '卖方发票':
                    case '仓单转移': {
                        result = ['extension', 'breach', 'detail'];
                    }
                }
            }
            return btnList.filter((e) => result.includes(e.code));
        }

        return {
            ...handleComposeOrderTable<WrPerformancePlan>(param),
            loading,
            tableList,
            auth,
            getPerformanceTypeName,
            getPaymentTypeName,
            getPerformanceStatusName,
            expandIcon,
            handleBtn,
            BuyOrSell,
        };
    },
});
</script>