<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="handleBtnList(auth, record)" :record="record" class="btn-list-sticky" @click="openComponent" />
                </template>
                <!-- 类型 -->
                <template #financetype="{ record }">
                    <a>{{ getFinanceTypeName(record.financetype) }}</a>
                </template>
                <!-- 状态 -->
                <template #scfcontractstatus="{ text }">
                    <a>{{ getContractName(text) }}</a>
                </template>
                <template #createtime="{ record }">
                    <a>{{ formatTime(record.createtime) }}</a>
                </template>
                <template #scfcontracttype="{ record }">
                    <a>{{ getScfContractTypeName(record.scfcontracttype) }}</a>
                </template>
            </a-table>
        </template>
    </mtp-table-scroll>
    <component :is="componentId" v-if="componentId" :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 { queryWrScfContract } from '@/services/go/wrtrade';
import { WrScfContract } from '@/services/go/wrtrade/interface';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { formatTime } from '@/common/methods';
import { expandIcon } from '@/common/setup/table/clolumn';
import { getContractName, getFinanceTypeName, getScfContractTypeName } from '@/common/constants/enumsName';
import { BtnListType } from '@/common/components/btnList/interface';

export default defineComponent({
    props: {
        parentComponent: {
            type: Object as PropType<DynamicComponent>,
            required: true,
        },
    },
    components: {
        MtpTableScroll,
        BtnList,
        repurchase_warrant: defineAsyncComponent(() => import('./components/buyback/index.vue')), // 回购仓单
        pay_arrears: defineAsyncComponent(() => import('./components/pay/index.vue')), // 支付欠款
    },
    setup(props) {
        const { auth } = props.parentComponent;
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<WrScfContract>();

        // 获取列表数据
        const queryTableAction = () => {
            queryTable(queryWrScfContract);
        };

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

        function handleBtnList(btnList: BtnListType[], item: WrScfContract) {
            const { remainwrpositionqty, interestdebt } = item;
            let result: BtnListType[] = btnList;
            if (interestdebt === 0) {
                // 费用欠款”不为0时出现“支付欠款”按钮
                result = btnList.filter((e) => e.code !== ModalEnum.financing_manager_contract_pay_arrears);
            } else if (remainwrpositionqty === 0) {
                // “剩余数量”不为0时出现“回购仓单”按钮
                result = btnList.filter((e) => e.code !== ModalEnum.financing_manager_contract_repurchase_warrant);
            }
            return result;
        }

        return {
            ...handleComposeOrderTable<WrScfContract>(param),
            loading,
            tableList,
            auth,
            formatTime,
            expandIcon,
            getContractName,
            getFinanceTypeName,
            handleBtnList,
            getScfContractTypeName,
        };
    },
});
</script>