<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="提现金额" width="120">
                    <template #default="{ row }">
                        {{ row.extract_price ?? row.price ?? row.amount ?? "-" }}
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="120">
                    <template #default="{ row }">
                        <el-tag :type="statusType(row.status)" size="small">
                            {{ formatStatus(row.status) }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="备注" min-width="140" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ row.mark || row.remark || row.fail_msg || "-" }}
                    </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-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <div class="table-row-actions">
                            <template v-if="isPending(row)">
                                <el-button type="success" size="small" @click="openRemark(row, 'agree')">同意</el-button>
                                <el-button type="danger" size="small" plain @click="openRemark(row, 'refuse')">拒绝</el-button>
                            </template>
                            <span v-else class="muted">已处理</span>
                        </div>
                    </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-dialog v-model="remarkDialog" :title="remarkTitle" width="420px" destroy-on-close @closed="resetRemark">
            <el-input
                v-model="remark"
                type="textarea"
                :rows="4"
                :placeholder="remarkPlaceholder"
                maxlength="200"
                show-word-limit
            />
            <template #footer>
                <el-button @click="remarkDialog = false">取消</el-button>
                <el-button type="primary" :loading="submitting" @click="submitRemark">确定</el-button>
            </template>
        </el-dialog>
    </el-main>
</template>

<script>
const STATUS_MAP = {
    "-1": "已拒绝",
    0: "审核中",
    1: "已通过",
    2: "已打款"
};

export default {
    name: "ExtractList",
    data() {
        return {
            loading: false,
            submitting: false,
            list: [],
            total: 0,
            query: { page: 1, limit: 10, status: 0 },
            remarkDialog: false,
            remarkTitle: "",
            remarkPlaceholder: "",
            remark: "",
            remarkAction: "",
            currentRow: null
        };
    },
    created() {
        this.getList();
    },
    methods: {
        formatStatus(status) {
            if (status == null || status === "") return "-";
            const key = String(status);
            return STATUS_MAP[key] ?? STATUS_MAP[status] ?? key;
        },
        statusType(status) {
            const n = Number(status);
            if (n === -1) return "danger";
            if (n === 1 || n === 2) return "success";
            return "warning";
        },
        isPending(row) {
            return Number(row.status) === 0;
        },
        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.extract.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();
        },
        openRemark(row, action) {
            this.currentRow = row;
            this.remarkAction = action;
            this.remark = "";
            this.remarkTitle = action === "agree" ? "同意提现" : "拒绝提现";
            this.remarkPlaceholder = action === "agree" ? "请输入通过备注（选填）" : "请输入拒绝理由（必填）";
            this.remarkDialog = true;
        },
        resetRemark() {
            this.currentRow = null;
            this.remarkAction = "";
            this.remark = "";
            this.remarkTitle = "";
            this.remarkPlaceholder = "";
        },
        async submitRemark() {
            if (!this.currentRow) return;
            const id = this.currentRow.id;
            if (id == null) {
                this.$message.error("缺少提现记录ID");
                return;
            }
            const failMsg = (this.remark || "").trim();
            if (this.remarkAction === "refuse" && !failMsg) {
                this.$message.warning("请填写拒绝理由");
                return;
            }
            this.submitting = true;
            try {
                const payload = { fail_msg: failMsg || "通过" };
                const res =
                    this.remarkAction === "agree"
                        ? await this.$API.finance.extract.agree.post(id, payload)
                        : await this.$API.finance.extract.refuse.post(id, payload);
                if (res.code !== 1) {
                    this.$message.error(res.msg || "操作失败");
                    return;
                }
                this.$message.success(res.msg || "操作成功");
                this.remarkDialog = false;
                this.getList();
            } finally {
                this.submitting = false;
            }
        }
    }
};
</script>

<style scoped>
.pager {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
}
.muted {
    color: var(--el-text-color-secondary);
    font-size: 12px;
}
</style>
