<?php
include 'db_connection.php';

/* =========================
   HANDLE SUBMIT
========================= */
if (isset($_POST['save_break'])) {

    $day = $_POST['day'];
    $period = $_POST['period'];
    $title = $_POST['title'];

    $stmt = $conn->prepare("
        INSERT INTO break_periods (day, period, title)
        VALUES (?, ?, ?)
    ");

    $stmt->bind_param("sis", $day, $period, $title);
    $stmt->execute();

    echo "<script>alert('Break period added successfully!');</script>";
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Manage Break Periods</title>

<style>
body {
    font-family: Arial;
    background: #f4f6f9;
    margin: 0;
}

/* HEADER */
.header {
    background: #1e293b;
    color: white;
    padding: 20px;
    text-align: center;
}

/* CONTAINER */
.container {
    width: 40%;
    margin: 40px auto;
}

/* CARD */
.card {
    background: white;
    padding: 25px;
    border-radius: 10px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
}

/* FORM */
label {
    font-weight: bold;
    display: block;
    margin-top: 12px;
}

select, input {
    width: 100%;
    padding: 10px;
    margin-top: 5px;
    border: 1px solid #ccc;
    border-radius: 6px;
}

/* BUTTON */
button {
    margin-top: 20px;
    width: 100%;
    padding: 12px;
    background: #f59e0b;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 15px;
    cursor: pointer;
}

button:hover {
    background: #d97706;
}

/* TABLE */
table {
    width: 100%;
    margin-top: 30px;
    border-collapse: collapse;
    background: white;
}

th {
    background: #1e293b;
    color: white;
    padding: 10px;
}

td {
    padding: 10px;
    border-bottom: 1px solid #ddd;
    text-align: center;
}
</style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>
    <p>Manage Break Periods (Timetable Module)</p>
</div>

<div class="container">

<!-- FORM -->
<div class="card">

<h3>Add Break Period</h3>

<form method="POST">

<label>Day</label>
<select name="day" required>
    <option value="">Select Day</option>
    <option>Monday</option>
    <option>Tuesday</option>
    <option>Wednesday</option>
    <option>Thursday</option>
    <option>Friday</option>
</select>

<label>Period</label>
<select name="period" required>
    <option value="">Select Period</option>
    <option value="1">Period 1</option>
    <option value="2">Period 2</option>
    <option value="3">Period 3</option>
    <option value="4">Period 4</option>
    <option value="5">Period 5</option>
    <option value="6">Period 6</option>
    <option value="6">Period 7</option>
    <option value="6">Period 8</option>
    <option value="6">Period 9</option>
</select>

<label>Break Title</label>
<input type="text" name="title" placeholder="e.g. Lunch Break" required>

<button type="submit" name="save_break">Save Break</button>

</form>

</div>

<!-- EXISTING BREAKS -->
<div class="card">

<h3>Existing Break Periods</h3>

<table>
<tr>
    <th>Day</th>
    <th>Period</th>
    <th>Title</th>
</tr>

<?php
$res = $conn->query("SELECT * FROM break_periods ORDER BY day, period");

while ($row = $res->fetch_assoc()) {
    echo "<tr>
            <td>{$row['day']}</td>
            <td>Period {$row['period']}</td>
            <td>{$row['title']}</td>
          </tr>";
}
?>

</table>

</div>

</div>

</body>
</html>