<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'db.php';

$message = "";

// 1. SAVE TIMING METRIC PARAMETER
if (isset($_POST['save_duration'])) {
    try {
        $stmt = $pdo->prepare("INSERT INTO exam_settings (course_id, duration_minutes) VALUES (?, ?) ON DUPLICATE KEY UPDATE duration_minutes = ?");
        $stmt->execute([intval($_POST['course_id']), intval($_POST['duration']), intval($_POST['duration'])]);
        $message = "<div class='alert alert-success'>✅ Success: Examination duration parameter locked down successfully!</div>";
    } catch (\PDOException $e) {
        $message = "<div class='alert alert-danger'>❌ Error: Configuration failed. " . htmlspecialchars($e->getMessage()) . "</div>";
    }
}

// 2. FETCH ALL COURSE DURATIONS
try {
    $courses = $pdo->query("SELECT c.*, IFNULL(e.duration_minutes, 60) as duration FROM courses c LEFT JOIN exam_settings e ON c.id = e.course_id ORDER BY c.course_code ASC")->fetchAll();
} catch (\PDOException $e) {
    $courses = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Set Exam Duration</title>
    <style>
        body { font-family: Arial, sans-serif; background: #fff; padding: 15px; color: #333; margin: 0; }
        .card { background: #fdfdfd; padding: 22px; border: 1px solid #dee2e6; border-radius: 6px; margin-bottom: 20px; border-top: 4px solid #ffc107; }
        input, select { width: 100%; padding: 11px; margin: 8px 0; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; background: white; }
        th, td { border: 1px solid #dee2e6; padding: 11px; text-align: left; font-size: 13px; } th { background: #e9ecef; }
        .btn { padding: 11px 20px; background: #ffc107; color: #333; border: none; cursor: pointer; border-radius: 4px; font-weight: bold; font-size: 14px; width: 100%; }
        .btn:hover { background: #e0a800; }
        .alert { padding: 12px; border-radius: 4px; margin-bottom: 18px; font-weight: bold; font-size: 14px; }
        .alert-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .alert-danger { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
    </style>
</head>
<body>

    <h2 style="color: #333; border-bottom: 1px solid #eee; padding-bottom: 8px; font-size: 20px;">Set Examination Duration</h2>
    <?php echo $message; ?>

    <div class="card">
        <h4 style="margin-top:0; color:#e0a800; font-size: 15px;">Configure Allowed Time Metric per Course</h4>
        <form method="POST" action="set_duration.php">
            <label style="font-size:12px; font-weight:bold;">Select Target Course Unit:</label>
            <select name="course_id" required>
                <?php foreach ($courses as $c) { ?>
                    <option value="<?php echo $c['id']; ?>">[<?php echo htmlspecialchars($c['course_code']); ?>] - <?php echo htmlspecialchars($c['course_title']); ?></option>
                <?php } ?>
            </select>

            <label style="font-size:12px; font-weight:bold;">Allowed Test Duration Time (In Minutes):</label>
            <input type="number" name="duration" placeholder="e.g., 60 for 1 hour, 90 for 1.5 hours" min="1" max="180" required>
            
            <button type="submit" name="save_duration" class="btn">Update Exam Duration Limit</button>
        </form>
    </div>

    <div class="card" style="border-top-color: #343a40;">
        <h4 style="margin-top:0; color:#343a40; font-size: 15px;">Active Configured Course Durations</h4>
        <table>
            <thead>
                <tr>
                    <th>Course Code</th>
                    <th>Course Title</th>
                    <th>Configured Exam Duration</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($courses as $c) { ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($c['course_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($c['course_title']); ?></td>
                    <td><span style="background: #eef2f7; padding: 4px 10px; border-radius: 4px; font-weight: bold; color: #0056b3;"><?php echo $c['duration']; ?> Minutes</span></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>

</body>
</html>
