<template>
    <!-- 合同 -->
    <section>
        <a-table
            :columns="columns"
            class="srcollYTable expandLeftTable"
            :scroll="{ x: '100%', y: '190px' }"
            :pagination="false"
            :loading="loading"
            :expandedRowKeys="expandedRowKeys"
            :customRow="Rowclick"
            :expandIcon="expandIcon"
            :expandIconAsCell="false"
            rowKey="key"
            :data-source="tableList"
        >
            <!-- 额外的展开行 -->
            <template v-if="btnList.length" #expandedRowRender="{ record }">
                <BtnList
                    :btnList="handleBtnList(btnList, 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>
        <component
            :is="componentId"
            v-if="componentId"
            :selectedRow="selectedRow"
            @cancel="closeComponent"
        ></component>
    </section>
</template>

<script lang="ts">
import { enumOrderComponents } from '@/common/constants/enumOrderComponents';
import { queryTableList, BtnList, defineAsyncComponent, defineComponent, ModalEnum } from '@/common/export/commonTable';
import { queryWrScfContract } from '@/services/go/wrtrade';
import { WrScfContract } from '@/services/go/wrtrade/interface';
import { getRecordItemTab } from '@/common/setup/order/orderData';
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({
    name: enumOrderComponents.financing_manager_contract,
    components: {
        BtnList,
        [ModalEnum.financing_manager_contract_repurchase_warrant]: defineAsyncComponent(() => import('./components/buyback/index.vue')),
        [ModalEnum.financing_manager_contract_pay_arrears]: defineAsyncComponent(() => import('./components/pay/index.vue')),
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<WrScfContract>();
        // 获取列表数据
        const queryTableAction = () => {
            queryTable(queryWrScfContract);
        };
        // 表格通用逻辑
        const param: ComposeOrderTableParam = {
            queryFn: queryTableAction,
            tableName: 'table_pcweb_financing_manage_bottom_contract',
            recordList: getRecordItemTab(),
        };
        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,
            formatTime,
            expandIcon,
            getContractName,
            getFinanceTypeName,
            handleBtnList,
            getScfContractTypeName,
        };
    },
});
</script>

<style lang="less">
</style>;
