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

$user_id = $_SESSION['user_id'] ?? 0;

$student_name = $_GET['name'] ?? ($_SESSION['student_name'] ?? '');
$roll_number  = $_GET['roll'] ?? ($_SESSION['roll_number'] ?? '');

$_SESSION['student_name'] = $student_name;
$_SESSION['roll_number']  = $roll_number;

$result = "";
$saved  = false;

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

    $scores = [
        'Visual' => 0,
        'Practical' => 0,
        'Logical' => 0,
        'Reflective' => 0
    ];

    foreach ($_POST as $key => $value) {

        $value = (int)$value;

        if (strpos($key, 'visual_') === 0) {
            $scores['Visual'] += $value;
        }

        if (strpos($key, 'practical_') === 0) {
            $scores['Practical'] += $value;
        }

        if (strpos($key, 'logical_') === 0) {
            $scores['Logical'] += $value;
        }

        if (strpos($key, 'reflective_') === 0) {
            $scores['Reflective'] += $value;
        }
    }

    arsort($scores);
    $result = array_key_first($scores);

    /* SAVE */
    $stmt = $pdo->prepare("
        INSERT INTO learning_styles 
        (user_id, student_name, roll_number, learning_style)
        VALUES (?, ?, ?, ?)
    ");

    $stmt->execute([
        $user_id,
        $student_name,
        $roll_number,
        $result
    ]);

    $saved = true;
}
?>

<?php require_once 'includes/header.php'; ?>

<style>
body{
    background: radial-gradient(circle at top, #020617, #0f172a);
    font-family: 'Segoe UI', sans-serif;
    color:#e5e7eb;
}

.learning-wrap{
    max-width:900px;
    margin:auto;
    padding:40px 20px;
}

.result-card{
    background: rgba(255,255,255,0.05);
    backdrop-filter: blur(12px);
    padding:40px;
    border-radius:20px;
    text-align:center;
    box-shadow:0 10px 40px rgba(0,0,0,0.6);
    border:1px solid rgba(255,255,255,0.08);
}

.badge{
    font-size:28px;
    padding:15px;
    border-radius:14px;
    background: linear-gradient(135deg,#22c55e,#06b6d4);
    color:#020617;
    font-weight:bold;
    margin:25px 0;
}

.btn{
    display:inline-block;
    padding:12px 28px;
    border-radius:999px;
    margin-top:18px;
    background: linear-gradient(135deg,#22c55e,#06b6d4);
    color:#020617;
    text-decoration:none;
    font-weight:600;
}
</style>

<div class="learning-wrap">

<h2>🧠 Learning Style Assessment</h2>

<p>
Student: <b><?= htmlspecialchars($student_name) ?></b>
<?php if ($roll_number): ?>
| Roll: <b><?= htmlspecialchars($roll_number) ?></b>
<?php endif; ?>
</p>

<?php if ($saved): ?>

<div class="result-card">

<p>Assessment Completed ✅</p>

<div class="badge">
<?= htmlspecialchars($result) ?>
</div>

<a href="student_list.php" class="btn">⬅ Back</a>

<br>

<a href="generate_learning_style_pdf.php?
name=<?= urlencode($student_name) ?>&
roll=<?= urlencode($roll_number) ?>&
style=<?= urlencode($result) ?>"
class="btn">
⬇ Download Report
</a>

</div>

<?php else: ?>

<form method="POST">

<!-- your existing questions (NO CHANGE) -->

<button type="submit">Submit Assessment</button>

</form>

<?php endif; ?>

</div>

</body>
</html>