<?php
// Filename: reset_timetable.php
require 'config.php';
requireAdmin();

$msg = '';
$msg_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reset_timetable') {
    $confirm_code = trim($_POST['confirm_code']);

    if ($confirm_code === 'RESET') {
        try {
            // 1. Disable Foreign Key Checks
            $pdo->exec("SET FOREIGN_KEY_CHECKS=0");

            // 2. Truncate Tables
            $pdo->exec("TRUNCATE TABLE timetable");
            $pdo->exec("TRUNCATE TABLE timetable_requests");
            $pdo->exec("TRUNCATE TABLE relief_assignments");
            $pdo->exec("TRUNCATE TABLE attendance_logs");
            $pdo->exec("TRUNCATE TABLE student_attendance");
            $pdo->exec("TRUNCATE TABLE student_discipline");

            // 3. Re-enable Foreign Keys
            $pdo->exec("SET FOREIGN_KEY_CHECKS=1");
            
            $msg = "Success! Timetable and related history wiped.";
            $msg_type = "success";

        } catch (Exception $e) {
            try { $pdo->exec("SET FOREIGN_KEY_CHECKS=1"); } catch(Exception $x) {}
            $msg = "Error: " . $e->getMessage();
            $msg_type = "danger";
        }
    } else {
        $msg = "Incorrect code. Please type 'RESET'.";
        $msg_type = "danger";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reset Timetable</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        body { background-color: #f8f9fa; font-family: sans-serif; min-height: 100vh; display: flex; flex-direction: column; }
        .card { border: none; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .main-container { flex: 1; display: flex; align-items: center; justify-content: center; }
    </style>
</head>
<body>
    
    <div class="container mt-4">
        <div class="d-flex justify-content-end">
            <a href="admin_settings.php" class="btn btn-outline-secondary btn-sm fw-bold">
                <i class="bi bi-arrow-left me-2"></i>Back to Settings
            </a>
        </div>
    </div>

    <div class="main-container">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-6 col-lg-5">
                    
                    <div class="card border-top border-danger border-5">
                        <div class="card-body p-5 text-center">
                            
                            <div class="mb-4 text-danger">
                                <i class="bi bi-exclamation-triangle-fill" style="font-size: 4rem;"></i>
                            </div>

                            <h3 class="fw-bold text-dark mb-2">Reset Timetable</h3>
                            <p class="text-muted mb-4">
                                Start a new semester/year.<br>Teachers & Students will <strong>NOT</strong> be deleted.
                            </p>

                            <?php if($msg): ?>
                                <div class="alert alert-<?= $msg_type ?> text-start small mb-4 fw-bold"><?= $msg ?></div>
                            <?php endif; ?>

                            <form method="POST">
                                <input type="hidden" name="action" value="reset_timetable">
                                
                                <div class="mb-4">
                                    <input type="text" name="confirm_code" class="form-control form-control-lg border-danger text-center fw-bold text-danger" placeholder='Type "RESET"' required autocomplete="off">
                                </div>

                                <div class="d-grid gap-2">
                                    <button type="submit" class="btn btn-danger btn-lg fw-bold">Wipe Data</button>
                                    <a href="admin_settings.php" class="btn btn-light btn-lg text-muted">Cancel</a>
                                </div>
                            </form>

                        </div>
                    </div>

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