<template>
    <el-main>
        <el-card shadow="never">
            <template #header>
                <span>佣金明细</span>
            </template>

            <el-table v-loading="loading" :data="list">
                <el-table-column prop="id" label="ID" width="80" />
                <el-table-column label="用户" min-width="100" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ row.uid || row.user_id || "-" }}
                    </template>
                </el-table-column>
                <el-table-column label="标题/关联" min-width="160" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ row.title || row.mark || row.link_id || "-" }}
                    </template>
                </el-table-column>
                <el-table-column label="金额" width="120">
                    <template #default="{ row }">
                        {{ row.number ?? row.price ?? row.brokerage ?? row.amount ?? "-" }}
                    </template>
                </el-table-column>
                <el-table-column label="时间" min-width="160" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ formatTime(row.add_time || row.create_time || row.created_at) }}
                    </template>
                </el-table-column>
            </el-table>

            <div class="pager">
                <el-pagination
                    background
                    layout="total, prev, pager, next, sizes"
                    :total="total"
                    :current-page="query.page"
                    :page-size="query.limit"
                    :page-sizes="[5, 10, 20, 50]"
                    @current-change="handlePageChange"
                    @size-change="handleSizeChange"
                />
            </div>
        </el-card>
    </el-main>
</template>

<script>
export default {
    name: "BrokerageList",
    data() {
        return {
            loading: false,
            list: [],
            total: 0,
            query: { page: 1, limit: 10 }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        formatTime(val) {
            if (val == null || val === "") return "-";
            const n = Number(val);
            if (!Number.isNaN(n) && String(val).length <= 12) {
                const ms = n < 1e12 ? n * 1000 : n;
                const d = new Date(ms);
                if (!Number.isNaN(d.getTime())) return d.toLocaleString();
            }
            return String(val);
        },
        async getList() {
            this.loading = true;
            try {
                const res = await this.$API.finance.brokerage.list.get(this.query);
                if (res.code !== 1) {
                    this.$message.error(res.msg || "获取佣金明细失败");
                    return;
                }
                const data = res.data || {};
                this.list = data.data || data.list || [];
                this.total = Number(data.count || data.total || this.list.length || 0);
            } finally {
                this.loading = false;
            }
        },
        handlePageChange(page) {
            this.query.page = page;
            this.getList();
        },
        handleSizeChange(limit) {
            this.query.limit = limit;
            this.query.page = 1;
            this.getList();
        }
    }
};
</script>

<style scoped>
.pager {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
}
</style>
