<template>
    <el-main>
        <el-card shadow="never">
            <template #header>
                <span>剪辑师列表</span>
            </template>

            <el-table v-loading="loading" :data="list">
                <el-table-column prop="uid" label="UID" width="72" />
                <el-table-column label="头像" width="72" align="center">
                    <template #default="{ row }">
                        <el-avatar v-if="row.avatar" :size="36" :src="row.avatar" />
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column prop="nickname" label="昵称" min-width="120" show-overflow-tooltip />
                <el-table-column label="真实姓名" min-width="100" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ applyField(row, "real_name") }}
                    </template>
                </el-table-column>
                <el-table-column label="手机号" width="130">
                    <template #default="{ row }">
                        {{ displayPhone(row) }}
                    </template>
                </el-table-column>
                <el-table-column label="微信号" min-width="120" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ applyField(row, "wechat") }}
                    </template>
                </el-table-column>
                <el-table-column label="作品链接" min-width="100">
                    <template #default="{ row }">
                        <el-link v-if="row.cutter_apply && row.cutter_apply.works" :href="row.cutter_apply.works" target="_blank" type="primary">
                            打开
                        </el-link>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="资料审核" width="100">
                    <template #default="{ row }">
                        <el-tag v-if="row.cutter_apply" size="small" :type="applyStatusType(row.cutter_apply.status)">
                            {{ applyStatusText(row.cutter_apply.status) }}
                        </el-tag>
                        <span v-else class="muted">无资料</span>
                    </template>
                </el-table-column>
                <el-table-column label="账号状态" width="100">
                    <template #default="{ row }">
                        <el-tag v-if="row.status !== undefined && row.status !== null" size="small" :type="Number(row.status) === 1 ? 'success' : 'danger'">
                            {{ formatAccountStatus(row.status) }}
                        </el-tag>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column prop="brokerage_price" label="佣金余额" width="100" align="right" />
                <el-table-column prop="login_ip" label="登录IP" width="130" show-overflow-tooltip />
                <el-table-column label="最后登录" min-width="160" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ formatDispTime(row.login_at) }}
                    </template>
                </el-table-column>
                <el-table-column label="注册时间" min-width="160" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ formatDispTime(row.created_at) }}
                    </template>
                </el-table-column>
                <el-table-column label="更新时间" min-width="160" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ formatDispTime(row.updated_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: "CutterList",
    data() {
        return {
            loading: false,
            list: [],
            total: 0,
            query: { page: 1, limit: 10 }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        applyField(row, key) {
            const a = row.cutter_apply;
            if (!a || a[key] === undefined || a[key] === null || a[key] === "") return "-";
            return a[key];
        },
        displayPhone(row) {
            const fromApply = row.cutter_apply && row.cutter_apply.phone;
            const v = fromApply || row.phone;
            if (v === undefined || v === null || v === "") return "-";
            return v;
        },
        formatAccountStatus(v) {
            const n = Number(v);
            const map = { 0: "禁用", 1: "正常" };
            return map[n] !== undefined ? map[n] : String(v);
        },
        applyStatusText(status) {
            const n = Number(status);
            const map = { 0: "待审核", 1: "已通过", 2: "已拒绝" };
            return map[n] !== undefined ? map[n] : status === undefined || status === null ? "-" : String(status);
        },
        applyStatusType(status) {
            const n = Number(status);
            if (n === 1) return "success";
            if (n === 2) return "danger";
            return "warning";
        },
        /** 后端为日期字符串 "2026-04-29 12:17:09" 或时间戳 */
        formatDispTime(val) {
            if (val === undefined || val === null || val === "") return "-";
            if (typeof val === "string" && /^\d{4}-\d{2}-\d{2}/.test(val.trim())) {
                return val.trim();
            }
            const num = Number(val);
            if (!Number.isFinite(num) || num <= 0) return String(val);
            const sec = num > 1e12 ? Math.floor(num / 1000) : num;
            return this.$TOOL.getTime(sec, "YYYY-MM-DD HH:mm:ss");
        },
        async getList() {
            this.loading = true;
            try {
                const res = await this.$API.cutter.list.get(this.query);
                if (res.code !== 1) {
                    this.$message.error(res.msg || "获取剪辑师列表失败");
                    return;
                }
                const data = res.data || {};
                this.list = data.list || data.data || [];
                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;
}
.muted {
    color: var(--el-text-color-secondary);
    font-size: 12px;
}
</style>
