<!-- 结算设置 -->
<template>
    <div class="partner-settlement">
        <!-- 搜索表单 -->
        <partner-settlement-search ref="searchRef" @search="handleSearch" />

        <!-- 表格 -->
        <CommonTable
            ref="tableRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :bodyStyle="{ padding: 0 }"
        >
            <template #toolbar>
                <el-button type="primary" plain @click="handleBatchSettlement">
                    批量结算
                </el-button>
            </template>

            <template #status="{ row }">
                <dict-data
                    code="partner_settle_status"
                    type="tag"
                    :model-value="row.status"
                />
            </template>

            <template #type="{ row }">
                <dict-data
                    code="partner_settle_order_type"
                    type="text"
                    :model-value="row.type"
                />
            </template>
            <template #action="{ row }">
                <div class="action-buttons">
                    <el-button
                        type="primary"
                        link
                        v-if="row.status === '1'"
                        @click="handleSettlement(row)"
                    >
                        手工结算
                    </el-button>
                </div>
            </template>
        </CommonTable>
    </div>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import { ElMessage } from 'element-plus';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import PartnerSettlementSearch from './partner-settlement-search.vue';
    import request from '@/utils/request';

    // 表格实例
    const tableRef = ref(null);
    const searchRef = ref(null);

    // 页面配置
    const pageConfig = reactive({
        pageUrl: '/user/userInviteLog/pageList',
        cacheKey: 'partnerSettlementTable',
        rowKey: 'beInviteOrderId'
    });

    // 表格列配置
    const columns = ref([
        {
            type: 'selection',
            columnKey: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
        },
        { label: '订单编号', prop: 'beInviteOrderId', minWidth: 120 },
        { label: '订单类型', prop: 'type', slot: 'type', width: 100 },
        { label: '用户ID', prop: 'inviteUserId', width: 120 },
        { label: '下单人姓名', prop: 'orderName', width: 120 },
        { label: '下单人手机号', prop: 'orderMobile', width: 120 },
        { label: '合伙人姓名', prop: 'partnerName', width: 120 },
        { label: '合伙人手机号', prop: 'partnerMobile', width: 120 },
        { label: '订单金额', prop: 'orderExpectMoney', minWidth: 100 },
        { label: '结算金额', prop: 'orderFinalMoney', minWidth: 100 },
        { label: '预估收入', prop: 'expectSettlementMoney', minWidth: 100 },
        { label: '结算收入', prop: 'settlementMoney', minWidth: 100 },
        { label: '结算状态', prop: 'status', slot: 'status', minWidth: 100 },
        { label: '创建时间', prop: 'createTime', minWidth: 160 },
        { label: '结算时间', prop: 'settlementTime', minWidth: 160 },
        {
            columnKey: 'action',
            label: '操作',
            width: 120,
            align: 'center',
            slot: 'action',
            fixed: 'right'
        }
    ]);

    // 搜索
    const handleSearch = (where) => {
        tableRef.value?.reload(where);
    };

    // 手工结算相关
    const handleSettlement = (row) => {
        tableRef.value?.messageBoxConfirm({
            message:
                '请确认是否手工结算？\n为避免造成损失，请确保订单已完成且不会造成重复结算',
            fetch: () =>
                request.post('/user/userInviteLog/adminSettlement', {
                    orderIds: [row.beInviteOrderId]
                })
        });
    };

    // 批量结算
    const handleBatchSettlement = () => {
        const selections = tableRef.value?.getSelections();
        if (!selections?.length) {
            return ElMessage.warning('请至少选择一条数据');
        }

        const orderIds = selections.map((row) => row.beInviteOrderId);
        tableRef.value?.messageBoxConfirm({
            message:
                '请确认是否手工结算？\n为避免造成损失，请确保订单已完成且不会造成重复结算',
            fetch: () =>
                request.post('/user/userInviteLog/adminSettlement', {
                    orderIds
                })
        });
    };
</script>

<style lang="scss" scoped>
    .dialog-content {
        text-align: center;
        .warning {
            color: #f56c6c;
            font-size: 12px;
            margin-top: 10px;
        }
    }
</style>
