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

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

$msg = '';

// Changer le statut
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
    $bugId  = (int)$_POST['bug_id'];
    $status = in_array($_POST['status'], ['nouveau','en_cours','resolu']) ? $_POST['status'] : 'nouveau';
    try {
        $pdo->prepare("UPDATE bug_reports SET status=? WHERE id=?")->execute([$status, $bugId]);
        $msg = "Statut mis a jour.";
    } catch (Exception $e) {}
}

$fStatus = $_GET['status'] ?? '';
$where   = $fStatus ? "WHERE status=?" : '';
$params  = $fStatus ? [$fStatus] : [];

try {
    $bugs = $pdo->prepare("
        SELECT br.*, u.username
        FROM bug_reports br
        JOIN users u ON u.id = br.user_id
        {$where}
        ORDER BY br.id DESC LIMIT 100
    ");
    $bugs->execute($params);
    $bugList = $bugs->fetchAll();
} catch (Exception $e) { $bugList = []; }

include 'header.php';
?>

<div class="container mt-4 px-4">
    <h5 class="text-danger fw-bold mb-3">Bugs Rapportés (<?= count($bugList) ?>)</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_bugs.php" class="btn btn-sm <?= !$fStatus?'btn-danger':'btn-outline-danger' ?>">Tous</a>
        <a href="?status=nouveau" class="btn btn-sm <?= $fStatus=='nouveau'?'btn-warning':'btn-outline-warning' ?>">Nouveaux</a>
        <a href="?status=en_cours" class="btn btn-sm <?= $fStatus=='en_cours'?'btn-info':'btn-outline-info' ?>">En cours</a>
        <a href="?status=resolu" class="btn btn-sm <?= $fStatus=='resolu'?'btn-success':'btn-outline-success' ?>">Résolus</a>
    </div>

    <div class="table-responsive">
        <table class="table table-dark table-hover small border-secondary">
            <thead>
                <tr><th>#</th><th>Utilisateur</th><th>Menu</th><th>Section</th><th>Type</th><th>Description</th><th>Date</th><th>Statut</th></tr>
            </thead>
            <tbody>
            <?php foreach ($bugList as $b): ?>
            <tr>
                <td class="text-muted"><?= $b['id'] ?></td>
                <td class="text-info"><?= htmlspecialchars($b['username']) ?></td>
                <td><?= htmlspecialchars($b['menu'] ?? '-') ?></td>
                <td class="text-muted" style="font-size:0.65rem;"><?= htmlspecialchars($b['section'] ?? '-') ?></td>
                <td><span class="badge bg-secondary" style="font-size:0.55rem;"><?= $b['bug_type'] ?></span></td>
                <td style="max-width:300px;font-size:0.72rem;"><?= htmlspecialchars(mb_substr($b['description'],0,100)) ?><?= strlen($b['description'])>100?'...':'' ?></td>
                <td class="text-muted" style="font-size:0.62rem;"><?= date('d M H:i', strtotime($b['created_at'])) ?></td>
                <td>
                    <form method="POST" class="d-inline">
                        <input type="hidden" name="bug_id" value="<?= $b['id'] ?>">
                        <select name="status" class="form-select form-select-sm bg-dark text-white border-secondary d-inline-block" style="width:auto;font-size:0.62rem;"
                                onchange="this.form.submit()">
                            <option value="nouveau" <?= $b['status']=='nouveau'?'selected':'' ?>>Nouveau</option>
                            <option value="en_cours" <?= $b['status']=='en_cours'?'selected':'' ?>>En cours</option>
                            <option value="resolu" <?= $b['status']=='resolu'?'selected':'' ?>>Resolu</option>
                        </select>
                        <input type="hidden" name="update_status" value="1">
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($bugList)): ?>
            <tr><td colspan="8" class="text-center text-muted py-3">Aucun bug rapporte.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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