<?php include("../database/auth_check.php"); ?>

<?php
$con = mysqli_connect("localhost","root","","WSAS");
if(!$con){
    die("Connection failed");
}

// Handle Add School Year
if(isset($_POST['add_sy'])){
    $sy_begin = (int) $_POST['sy_begin'];
    $sy_end   = $sy_begin + 1;

    // check current active semester
    $sy_query = mysqli_query($con, "SELECT * FROM school_year WHERE is_active = 1 LIMIT 1");
    $current = mysqli_fetch_assoc($sy_query);

    if($current && $current['semester'] == '1st Semester'){
        echo "<script>alert('You must finish 1st Semester before adding a new school year.'); window.history.back();</script>";
        exit;
    }

    // validate duplicate school year
    $check = mysqli_query($con, "SELECT * FROM school_year WHERE sy_begin='$sy_begin' AND sy_end='$sy_end'");
    if(mysqli_num_rows($check) > 0){
        echo "<script>alert('This School Year already exists!'); window.history.back();</script>";
        exit;
    }

    // deactivate previous
    mysqli_query($con, "UPDATE school_year SET is_active = 0");

    // insert new
    mysqli_query($con, "
        INSERT INTO school_year (sy_begin, sy_end, semester, is_active)
        VALUES ('$sy_begin','$sy_end','1st Semester',1)
    ");

    header("Location: schoolyear.php");
    exit;
}

// fetch all school years
$sy_result = mysqli_query($con, "SELECT * FROM school_year ORDER BY sy_begin DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage School Year - Admin</title>
    <link rel="shortcut icon" type="x-icon" href="../images/logo.png">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
<link rel="stylesheet" href="../Style/navstyle.css">
<style>
/* Dashboard layout */
.dashboard-container {
    display: flex;
    min-height: 100vh;
    font-family: "Segoe UI", sans-serif;
    background: #f4f6f8;
}

.mainbar {
    margin-left: 280px;
    width: calc(100% - 280px);
    flex: 1;
    display: flex;
    flex-direction: column;
}
.dashboard-btn {
    margin: 0; /* Remove top margin */
    font-size: 14px;
    padding: 8px 16px;
}

.open-btn {
    display: inline-block;
    background-color: #4a9c3f;
    color: #fff;
    padding: 10px 18px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    margin-top: 10px;
    text-decoration: none;
    transition: background 0.3s;
}
.open-btn:hover { background-color: #3e8c36; }

form {
    margin-top: 20px;
    background: #f9f9f9;
    padding: 20px;
    border-radius: 10px;
}

form label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
    color: #555;
}

form input[type="number"] {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border: 1px solid #ccc;
    border-radius: 6px;
}

form button {
    width: 100%;
    padding: 12px;
    background: #4a9c3f;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 15px;
    cursor: pointer;
}
form button:hover { background: #43a047; }

/* Table styling */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

table th, table td {
    padding: 12px;
    border: 1px solid #ccc;
    text-align: center;
}

table th {
    background-color: #2b5820;
    color: white;
    font-weight: 600;
}

table tr:nth-child(even) {
    background: #f8f8f8;
}

table tr:hover {
    background: rgba(74,156,63,0.1);
    transition: 0.3s;
}

/* Responsive */
@media (max-width:1024px){
    .mainbar { margin-left:0; margin-top:70px; }
}
</style>
</head>
<body>
<div class="dashboard-container">
    <?php include('adminnav.php'); ?>
    <aside class="mainbar">
        <h3>Add New School Year</h3>
        <form method="POST">
            <label for="sy_begin">Start Year:</label>
            <input type="number" name="sy_begin" id="sy_begin" required>
            <label for="sy_end">End Year:</label>
            <input type="number" name="sy_end" id="sy_end" readonly>
            <button type="submit" name="add_sy" onclick="return confirm('Are you sure you want to add this school year?')">Add School Year</button>
        </form>

        <h3>All School Years</h3>
        <table>
            <tr>
                <th>School Year</th>
                <th>Semester</th>
            </tr>
            <?php while($row = mysqli_fetch_assoc($sy_result)): ?>
                <tr>
                    <td>A.Y. <?php echo $row['sy_begin']." - ".$row['sy_end']; ?></td>
                    <td><?php echo $row['semester']; ?></td>
                </tr>
            <?php endwhile; ?>
        </table>
    </aside>
</div>

<script>
// Auto-calculate end year
const syBegin = document.getElementById("sy_begin");
const syEnd = document.getElementById("sy_end");

syBegin.addEventListener("input", () => {
    const val = parseInt(syBegin.value);
    syEnd.value = !isNaN(val) ? val + 1 : '';
});
</script>
</body>
</html>