<?php
session_start();
require_once 'config.php';
require_once 'functions.php';

requireLogin();
$user = getConnectedUser($pdo);
requireAdmin($user);

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $rId     = (int)($_POST['report_id'] ?? 0);
    $status  = in_array($_POST['new_status']??'',['new','pending','closed']) ? $_POST['new_status'] : 'pending';
    $notes   = trim(htmlspecialchars($_POST['admin_notes'] ?? ''));

    if ($rId) {
        $pdo->prepare("UPDATE reports SET status=?, notes=? WHERE id=?")->execute([$status, $notes, $rId]);
        $msg = "Report #{$rId} mis à jour.";
    }
}

$fStatus = $_GET['status'] ?? '';
$where   = $fStatus ? "WHERE r.status = '{$fStatus}'" : '';

$stmt = $pdo->query("
    SELECT r.*, u.username as reporter_name
    FROM reports r
    LEFT JOIN users u ON u.id = r.reporter_id
    $where
    ORDER BY r.created_at DESC
");
$reports = $stmt->fetchAll();

include 'header.php';
?>

<div class="container-fluid mt-4 px-4">
    <h5 class="oracle-text text-danger mb-3"> GESTION DES REPORTS</h5>

    <?php if ($msg): ?><div class="alert alert-success small py-2"><?= $msg ?></div><?php endif; ?>

    <div class="d-flex gap-2 mb-3">
        <a href="admin_reports.php"             class="btn btn-sm <?= !$fStatus?'btn-danger':'btn-outline-secondary' ?>">Tous</a>
        <a href="?status=new"     class="btn btn-sm <?= $fStatus==='new'?'btn-danger':'btn-outline-secondary' ?>">Nouveaux</a>
        <a href="?status=pending" class="btn btn-sm <?= $fStatus==='pending'?'btn-warning':'btn-outline-secondary' ?>">En cours</a>
        <a href="?status=closed"  class="btn btn-sm <?= $fStatus==='closed'?'btn-secondary':'btn-outline-secondary' ?>">Fermés</a>
    </div>

    <?php if (empty($reports)): ?>
        <div class="alert alert-secondary small">Aucun report.</div>
    <?php else: ?>
    <div class="row g-3">
    <?php foreach ($reports as $r): ?>
        <div class="col-md-6">
            <div class="card bg-dark border-<?= $r['status']==='new'?'danger':($r['status']==='pending'?'warning':'secondary') ?> shadow">
                <div class="card-header py-2 d-flex justify-content-between align-items-center">
                    <span class="small fw-bold text-white">
                        Report #<?= $r['id'] ?> —
                        <span class="text-danger"><?= htmlspecialchars($r['reason']) ?></span>
                        <span class="badge bg-secondary ms-1" style="font-size:0.55rem;"><?= strtoupper($r['target_type']) ?></span>
                    </span>
                    <small class="text-muted" style="font-size:0.65rem;"><?= date('d M H:i', strtotime($r['created_at'])) ?></small>
                </div>
                <div class="card-body p-3">
                    <p class="small text-muted mb-1">
                        Par : <strong class="text-white"><?= htmlspecialchars($r['reporter_name'] ?? 'Visiteur') ?></strong>
                        — Cible : ID <?= $r['target_id'] ?>
                    </p>
                    <?php if (!empty($r['notes'])): ?>
                        <p class="small text-white mb-2"><?= nl2br(htmlspecialchars($r['notes'])) ?></p>
                    <?php endif; ?>

                    <form method="POST" class="d-flex gap-2 flex-wrap align-items-end">
                        <input type="hidden" name="report_id" value="<?= $r['id'] ?>">
                        <div>
                            <label class="small text-white d-block mb-1">Statut</label>
                            <select name="new_status" class="form-select form-select-sm" style="width:130px;">
                                <option value="new"     <?= $r['status']==='new'?'selected':'' ?>>Nouveau</option>
                                <option value="pending" <?= $r['status']==='pending'?'selected':'' ?>>En cours</option>
                                <option value="closed"  <?= $r['status']==='closed'?'selected':'' ?>>Fermé</option>
                            </select>
                        </div>
                        <div class="flex-grow-1">
                            <label class="small text-white d-block mb-1">Note admin</label>
                            <input type="text" name="admin_notes" class="form-control form-control-sm"
                                   placeholder="Commentaire…" value="<?= htmlspecialchars($r['notes']??'') ?>">
                        </div>
                        <button type="submit" class="btn btn-warning btn-sm fw-bold">SAUVER</button>
                    </form>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>

<?php include 'footer.php'; ?>
