<?php
/**
 * HuSNS - 举报管理后台页面
 */
?>
<style>
.report-page .stats-cards {
    display: flex;
    gap: 15px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}
.report-page .stats-cards .stat-card {
    flex: 1;
    min-width: 160px;
    background: #fff;
    padding: 15px 18px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    display: flex;
    align-items: center;
    gap: 12px;
}
.report-page .stats-cards .stat-icon {
    width: 42px;
    height: 42px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: #fff;
    flex-shrink: 0;
}
.report-page .stats-cards .stat-icon.bg-blue { background: #3498db; }
.report-page .stats-cards .stat-icon.bg-warning { background: #f39c12; }
.report-page .stats-cards .stat-icon.bg-success { background: #27ae60; }
.report-page .stats-cards .stat-icon.bg-gray { background: #95a5a6; }
.report-page .stats-cards .stat-info {
    display: flex;
    flex-direction: column;
}
.report-page .stats-cards .stat-value {
    font-size: 22px;
    font-weight: 600;
    color: #2c3e50;
    line-height: 1.2;
}
.report-page .stats-cards .stat-label {
    font-size: 12px;
    color: #7f8c8d;
    margin-top: 2px;
}
.report-page .admin-search {
    background: #fff;
    padding: 15px 20px;
    border-radius: 8px;
    margin-bottom: 15px;
}
.report-page .admin-table {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
}
.report-page .empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #999;
}
.report-page .empty-state i {
    font-size: 48px;
    color: #ddd;
    margin-bottom: 10px;
    display: block;
}
.report-page .post-preview {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.report-page .post-preview a {
    display: block;
    margin-top: 3px;
}
</style>
<div class="admin-content report-page">
    <div class="admin-header">
        <h1>举报管理</h1>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-cards">
        <div class="stat-card">
            <div class="stat-icon bg-blue">
                <i class="fa fa-flag"></i>
            </div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $stats['total']; ?></div>
                <div class="stat-label">总举报数</div>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-warning">
                <i class="fa fa-clock-o"></i>
            </div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $stats['pending']; ?></div>
                <div class="stat-label">待处理</div>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-success">
                <i class="fa fa-check"></i>
            </div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $stats['handled']; ?></div>
                <div class="stat-label">已处理</div>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-gray">
                <i class="fa fa-times"></i>
            </div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $stats['rejected']; ?></div>
                <div class="stat-label">已驳回</div>
            </div>
        </div>
    </div>
    
    <!-- 筛选 -->
    <div class="admin-search">
        <form method="get" action="">
            <div class="input-group">
                <select name="status" class="form-control" style="width: auto;">
                    <option value="-1" <?php echo $status == -1 ? 'selected' : ''; ?>>全部状态</option>
                    <option value="0" <?php echo $status == 0 ? 'selected' : ''; ?>>待处理</option>
                    <option value="1" <?php echo $status == 1 ? 'selected' : ''; ?>>已处理</option>
                    <option value="2" <?php echo $status == 2 ? 'selected' : ''; ?>>已驳回</option>
                </select>
                <button type="submit" class="btn btn-default">筛选</button>
            </div>
        </form>
    </div>
    
    <!-- 举报列表 -->
    <div class="admin-table">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>举报ID</th>
                    <th>举报人</th>
                    <th>举报内容</th>
                    <th>举报原因</th>
                    <th>状态</th>
                    <th>举报时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($reports as $report): ?>
                <tr>
                    <td>#<?php echo $report['id']; ?></td>
                    <td>
                        <img src="<?php echo $report['reporter_avatar'] ? $this->uploadUrl($report['reporter_avatar']) : $this->assetUrl('images/default-avatar.png'); ?>" 
                             alt="" class="avatar-xs">
                        <?php echo htmlspecialchars($report['reporter_name']); ?>
                    </td>
                    <td>
                        <div class="post-preview">
                            <?php echo htmlspecialchars($report['post_content']); ?>...
                            <a href="#" onclick="viewPost(<?php echo $report['post_id']; ?>)" class="text-primary">查看完整内容</a>
                        </div>
                    </td>
                    <td>
                        <span class="badge badge-info">
                            <?php echo $reasonOptions[$report['reason']] ?? $report['reason']; ?>
                        </span>
                    </td>
                    <td><?php echo $report['status_label']; ?></td>
                    <td><?php echo $report['created_at_text']; ?></td>
                    <td>
                        <?php if ($report['status'] == 0): ?>
                        <div class="btn-group">
                            <button class="btn btn-sm btn-success" onclick="handleReport(<?php echo $report['id']; ?>, 1)">
                                <i class="fa fa-check"></i> 处理
                            </button>
                            <button class="btn btn-sm btn-secondary" onclick="handleReport(<?php echo $report['id']; ?>, 2)">
                                <i class="fa fa-times"></i> 驳回
                            </button>
                        </div>
                        <?php else: ?>
                        <button class="btn btn-sm btn-default" onclick="viewReport(<?php echo $report['id']; ?>)">
                            <i class="fa fa-eye"></i> 详情
                        </button>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <?php if (empty($reports)): ?>
        <div class="empty-state">
            <i class="fa fa-flag-o"></i>
            <p>暂无举报数据</p>
        </div>
        <?php endif; ?>
    </div>
    
    <?php if ($totalPages > 1): ?>
    <div class="pagination-wrap">
        <?php echo $this->pagination($page, $totalPages, 'admin/reports?status=' . $status); ?>
    </div>
    <?php endif; ?>
</div>

<!-- 处理弹窗 -->
<div class="modal fade" id="handleModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">处理举报</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="reportId">
                <input type="hidden" id="handleStatus">
                
                <div class="form-group">
                    <label>处理结果说明</label>
                    <textarea id="handleResult" class="form-control" rows="3" placeholder="请输入处理结果说明（可选）"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="confirmHandle()">确认处理</button>
            </div>
        </div>
    </div>
</div>

<script>
function handleReport(id, status) {
    $('#reportId').val(id);
    $('#handleStatus').val(status);
    $('#handleResult').val('');
    $('#handleModal').modal('show');
}

function confirmHandle() {
    var id = $('#reportId').val();
    var status = $('#handleStatus').val();
    var result = $('#handleResult').val();
    
    $.post('<?php echo Helper::url('admin/reports/handle'); ?>', {
        id: id,
        status: status,
        result: result,
        csrf_token: '<?php echo $_SESSION['csrf_token']; ?>'
    }, function(res) {
        if (res.success) {
            alert(res.message);
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
    
    $('#handleModal').modal('hide');
}

function viewReport(id) {
    window.location.href = '<?php echo Helper::url('admin/reports/detail'); ?>?id=' + id;
}

function viewPost(postId) {
    window.open('<?php echo Helper::url('post/detail'); ?>?id=' + postId, '_blank');
}
</script>
