<?php
require_once '../config/database.php';
require_once '../includes/functions.php';
$pageTitle = 'Manage Trivia';
include 'header.php';

$conn = getConnection();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $question = sanitize($_POST['question']);
    $options = json_encode([$_POST['opt1'], $_POST['opt2'], $_POST['opt3'], $_POST['opt4']]);
    $correct = sanitize($_POST['correct_answer']);
    $reward = floatval($_POST['reward']);
    
    $stmt = $conn->prepare("INSERT INTO trivia (question, options, correct_answer, reward) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("sssd", $question, $options, $correct, $reward);
    $stmt->execute();
    setFlash('success', 'Trivia question added!');
    redirect('trivia.php');
}

$trivias = $conn->query("SELECT * FROM trivia ORDER BY created_at DESC");
?>
<div class="container">
    <div class="card">
        <h3>❓ Manage Trivia Questions</h3>
        <form method="POST" style="background:#f9f9f9;padding:20px;border-radius:8px;margin:20px 0">
            <div class="form-group">
                <label>Question</label>
                <input type="text" name="question" class="form-control" required>
            </div>
            <div class="row">
                <div class="col-md-6"><input type="text" name="opt1" class="form-control" placeholder="Option 1" required></div>
                <div class="col-md-6"><input type="text" name="opt2" class="form-control" placeholder="Option 2" required></div>
                <div class="col-md-6"><input type="text" name="opt3" class="form-control" placeholder="Option 3" required></div>
                <div class="col-md-6"><input type="text" name="opt4" class="form-control" placeholder="Option 4" required></div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Correct Answer</label>
                        <input type="text" name="correct_answer" class="form-control" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Reward (KSH)</label>
                        <input type="number" step="0.01" name="reward" class="form-control" required>
                    </div>
                </div>
            </div>
            <button type="submit" class="btn btn-success">Add Question</button>
        </form>
        
        <table class="table">
            <tr><th>Question</th><th>Reward</th><th>Status</th><th>Actions</th></tr>
            <?php while ($trivia = $trivias->fetch_assoc()): ?>
            <tr>
                <td><?php echo htmlspecialchars(substr($trivia['question'], 0, 50)); ?>...</td>
                <td>Ksh <?php echo number_format($trivia['reward'], 2); ?></td>
                <td><?php echo $trivia['status']; ?></td>
                <td><a href="?delete=<?php echo $trivia['id']; ?>" class="btn btn-danger" style="padding:5px 10px">Delete</a></td>
            </tr>
            <?php endwhile; ?>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>
