<?php
// admin/computer_quiz_setup.php
session_start();
require_once __DIR__ . '/../config.php';

// 🔐 Auth guard
if (!isset($_SESSION['institution_id']) || $_SESSION['user_role'] !== 'admin') {
    header("Location: login.php");
    exit;
}

$institutionId = $_SESSION['institution_id'];
$institutionName = $_SESSION['institution_name'] ?? 'Your Institution';

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $quiz_name = trim($_POST['quiz_name']);
    $total_questions = 20; // FIXED for learning style

    if ($quiz_name === '') {
        $error = "Quiz name is required.";
    } else {

        try {
            // 1️⃣ Deactivate existing quizzes (only one active)
            $stmt = $conn->prepare(
                "UPDATE computer_quizzes 
                 SET is_active = 0 
                 WHERE institution_id = ?"
            );
            $stmt->bind_param("i", $institutionId);
            $stmt->execute();
            $stmt->close();

            // 2️⃣ Create new active quiz
            $stmt = $conn->prepare(
                "INSERT INTO computer_quizzes 
                (institution_id, quiz_name, total_questions, is_active, created_at)
                VALUES (?, ?, ?, 1, NOW())"
            );
            $stmt->bind_param("isi", $institutionId, $quiz_name, $total_questions);
            $stmt->execute();
            $stmt->close();

            $message = "Computer quiz started successfully.";

        } catch (Throwable $e) {
            $error = "Something went wrong. Please try again.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Start Computer Quiz | RS Learning Lab</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

<style>
*{box-sizing:border-box;font-family:'Inter',system-ui}
body{margin:0;background:#0b1220;color:#e5e7eb}

.wrapper{display:flex;min-height:100vh}

/* Sidebar */
.sidebar{
    width:240px;
    background:#0f172a;
    padding:24px 18px;
}
.logo{
    font-size:20px;
    font-weight:700;
    color:#22d3ee;
    margin-bottom:32px;
}
.nav a{
    display:block;
    padding:12px 14px;
    margin-bottom:6px;
    border-radius:10px;
    color:#cbd5f5;
    text-decoration:none;
    font-size:14px;
}
.nav a.active,
.nav a:hover{
    background:#1e293b;
    color:#22d3ee;
}

/* Main */
.main{flex:1;display:flex;flex-direction:column}

/* Topbar */
.topbar{
    background:#0f172a;
    padding:18px 28px;
    display:flex;
    justify-content:space-between;
    align-items:center;
    border-bottom:1px solid #1e293b;
}
.topbar .title{font-size:18px;font-weight:600}
.topbar .right{font-size:13px;color:#94a3b8}
.logout{
    margin-left:16px;
    padding:8px 12px;
    background:#1e293b;
    border-radius:8px;
    text-decoration:none;
    color:#e5e7eb;
}

/* Content */
.content{padding:36px;max-width:720px}

/* Card */
.card{
    background:#0f172a;
    border:1px solid #1e293b;
    border-radius:16px;
    padding:32px;
}
.card h2{margin-top:0;margin-bottom:6px}
.card p{color:#94a3b8;margin-bottom:26px}

label{display:block;margin-top:18px;color:#cbd5f5}
input{
    width:100%;
    padding:14px;
    border-radius:12px;
    border:1px solid #1e293b;
    background:#020617;
    color:#e5e7eb;
    margin-top:6px;
}

.note{
    margin-top:10px;
    font-size:13px;
    color:#64748b;
}

button{
    margin-top:28px;
    padding:14px 20px;
    border-radius:14px;
    border:0;
    background:#22d3ee;
    color:#001018;
    font-weight:700;
    cursor:pointer;
}

.success{margin-top:16px;color:#86efac}
.error{margin-top:16px;color:#fca5a5}
</style>
</head>

<body>
<div class="wrapper">

    <!-- Sidebar -->
    <aside class="sidebar">
        <div class="logo">RS Learning Lab</div>
        <nav class="nav">
            <a href="index.php">Dashboard</a>
            <a class="active" href="#">Computer Quiz</a>
            <a href="#">Manual Quiz</a>
            <a href="#">Students</a>
            <a href="#">Reports</a>
            <a href="#">Coding Challenges</a>
        </nav>
    </aside>

    <!-- Main -->
    <div class="main">

        <!-- Topbar -->
        <div class="topbar">
            <div class="title">Start Computer Quiz</div>
            <div class="right">
                <?= htmlspecialchars($institutionName) ?> · Admin
                <a class="logout" href="logout.php">Logout</a>
            </div>
        </div>

        <!-- Content -->
        <div class="content">
            <div class="card">
                <h2>Create Learning Style Quiz</h2>
                <p>
                    This quiz identifies learning patterns and behaviour.<br>
                    No marks. No answer key. No subject.
                </p>

                <?php if ($message): ?>
                    <div class="success"><?= htmlspecialchars($message) ?></div>
                <?php endif; ?>

                <?php if ($error): ?>
                    <div class="error"><?= htmlspecialchars($error) ?></div>
                <?php endif; ?>

                <form method="post">
                    <label>Quiz Name</label>
                    <input name="quiz_name" placeholder="Learning Style Assessment – Class 8" required>

                    <div class="note">
                        • Total questions fixed at 20<br>
                        • Only one active quiz per institution
                    </div>

                    <button type="submit">Start Computer Quiz</button>
                </form>
            </div>
        </div>

    </div>
</div>
</body>
</html>
