<?php
session_start();
require_once 'config.php';
require_once 'functions.php';
requireLogin();
$user = getConnectedUser($pdo);
requireAdmin($user);

$msg = '';

// Simuler exactement le début de admin_trophees.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "POST recu<br>";
}

try {
    $trophies = $pdo->query("SELECT * FROM trophy_levels ORDER BY requirement_value ASC")->fetchAll();
} catch(Exception $e) {
    $trophies = [];
}

include 'header.php';
?>

<div class="container mt-4">
    <h5 class="text-warning">GESTION DES TROPHEES</h5>
    
    <!-- Formulaire ajout -->
    <div class="card bg-dark border-warning mb-4">
        <div class="card-header text-warning">Ajouter un trophee</div>
        <div class="card-body">
            <form method="POST" enctype="multipart/form-data">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="small text-white d-block mb-1">Titre</label>
                        <input type="text" name="title" class="form-control form-control-sm" required>
                    </div>
                    <div class="col-md-8">
                        <label class="small text-white d-block mb-1">Description</label>
                        <input type="text" name="description" class="form-control form-control-sm" required>
                    </div>
                    <div class="col-md-4">
                        <label class="small text-white d-block mb-1">Type de condition</label>
                        <select name="requirement_type" class="form-select form-select-sm">
                            <option value="win_count">Nombre de victoires</option>
                            <option value="win_percent">Pourcentage victoires</option>
                            <option value="interaction">Points interaction</option>
                            <option value="reliability">Score fiabilite</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="small text-white d-block mb-1">Valeur requise</label>
                        <input type="number" name="requirement_value" class="form-control form-control-sm" min="1" value="10" required>
                    </div>
                    <div class="col-md-4">
                        <label class="small text-white d-block mb-1">Emoji OU image</label>
                        <div class="input-group input-group-sm">
                            <input type="text" name="icon_emoji" class="form-control form-control-sm text-center" placeholder="ex: T" maxlength="5" id="emojiInput">
                            <span id="emojiPreview" style="font-size:1.4rem;padding:0 8px;"></span>
                        </div>
                        <input type="file" name="icon" class="form-control form-control-sm mt-1" accept="image/*">
                    </div>
                    <div class="col-12">
                        <button type="submit" name="add_trophy" class="btn btn-warning btn-sm fw-bold">AJOUTER</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Liste -->
    <div class="table-responsive">
        <table class="table table-dark table-hover small">
            <thead><tr><th>Icone</th><th>Titre</th><th>Description</th><th>Condition</th><th>Valeur</th><th></th></tr></thead>
            <tbody>
            <?php if (empty($trophies)): ?>
                <tr><td colspan="6" class="text-center text-muted py-3">Aucun trophee.</td></tr>
            <?php endif; ?>
            <?php foreach ($trophies as $t): ?>
                <tr>
                    <td>
                        <?php
                        $ip = $t['icon_path'] ?? '';
                        if (strpos($ip, 'emoji:') === 0) {
                            echo '<span style="font-size:1.4rem;">' . htmlspecialchars(substr($ip, 6)) . '</span>';
                        } elseif (!empty($ip) && @file_exists(__DIR__.'/'.$ip)): ?>
                            <img src="<?= htmlspecialchars($ip) ?>" style="width:28px;height:28px;">
                        <?php else: ?><span class="text-muted">-</span><?php endif; ?>
                    </td>
                    <td class="fw-bold"><?= htmlspecialchars($t['title']) ?></td>
                    <td class="text-muted"><?= htmlspecialchars($t['description']) ?></td>
                    <td><?= $t['requirement_type'] ?></td>
                    <td class="text-success"><?= $t['requirement_value'] ?></td>
                    <td>
                        <form method="POST">
                            <input type="hidden" name="trophy_id" value="<?= $t['id'] ?>">
                            <button type="submit" name="delete_trophy" class="btn btn-xs btn-outline-danger py-0"
                                    onclick="return confirm('Supprimer ?')">X</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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