
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Class Routine</title>
    <style>
        /* General Page Styling */
body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    color: #333;
    margin: 0;
    padding: 20px;
}

/* Heading Styling */
h1, h2 {
    color: #2c3e50;
    text-align: center;
}

/* Form Styling */
form {
    max-width: 600px;
    margin: 0 auto 30px auto;
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

/* Label and Input Field Styling */
label {
    font-weight: bold;
    display: block;
    margin-bottom: 8px;
}

select {
    width: 100%;
    padding: 8px;
    margin-bottom: 15px;
    border-radius: 4px;
    border: 1px solid #ccc;
    font-size: 14px;
}

/* Button Styling */
button {
    background-color: #3498db;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
}

button:hover {
    background-color: #2980b9;
}

/* Table Styling */
table {
    width: 100%;
    margin-top: 30px;
    border-collapse: collapse;
    background-color: #fff;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

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

th {
    background-color: #3498db;
    color: white;
    font-weight: bold;
}

td {
    background-color: #f9f9f9;
}

/* Hover effect for table rows */
tr:hover {
    background-color: #f1f1f1;
}

/* Action Links (Edit/Delete) Styling */
a {
    text-decoration: none;
    color: #3498db;
    font-weight: bold;
}

a:hover {
    color: #2980b9;
}

a:active {
    color: #e74c3c;
}

    </style>
</head>
<body>
    <h1>Create or Update Class Routine</h1>
    <?php
include './login/connect.php';

// ক্লাস লিস্ট পেতে SQL কোড
$class_query = "SELECT * FROM classes";
$class_result = $conn->query($class_query);

// বিষয় লিস্ট পেতে SQL কোড
$subject_query = "SELECT * FROM subjects";
$subject_result = $conn->query($subject_query);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // রুটিন সেভ করার কোড
    $class_id = $_POST['class_id'];
    $day = $_POST['day'];

    // প্রতিটি পিরিয়ডের জন্য বিষয় ইনপুট নেয়া
    $period_1_subject = $_POST['period_1_subject'];
    $period_2_subject = $_POST['period_2_subject'];
    $period_3_subject = $_POST['period_3_subject'];
    $period_4_subject = $_POST['period_4_subject'];
    $period_5_subject = $_POST['period_5_subject'];
    $period_6_subject = $_POST['period_6_subject'];

    // Prepared statement ব্যবহার
    $sql = $conn->prepare("INSERT INTO class_routine (class_id, day, period_1, period_2, period_3, period_4, period_5, period_6)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    
    // পারামিটার বেঁধে দিন
    $sql->bind_param("isssssss", $class_id, $day, $period_1_subject, $period_2_subject, $period_3_subject, $period_4_subject, $period_5_subject, $period_6_subject);
    
    // স্টেটমেন্ট চালান
    if ($sql->execute()) {
        echo "Routine Added Successfully";
    } else {
        echo "Error: " . $conn->error;
    }
}

// ডিলিট কোড
if (isset($_GET['delete_id'])) {
    $delete_id = $_GET['delete_id'];
    
    // Prepared statement ব্যবহার করে ডিলিট
    $delete_query = $conn->prepare("DELETE FROM class_routine WHERE id = ?");
    $delete_query->bind_param("i", $delete_id);
    
    if ($delete_query->execute()) {
        echo "Routine Deleted Successfully";
    } else {
        echo "Error: " . $conn->error;
    }
}

// ক্লাস রুটিন দেখানোর কোড
$join_routine_query = "
    SELECT cr.id, cr.class_id, cr.day, cr.period_1, cr.period_2, cr.period_3, cr.period_4, cr.period_5, cr.period_6, 
           s1.subject_name as period_1_name, s2.subject_name as period_2_name, s3.subject_name as period_3_name,
           s4.subject_name as period_4_name, s5.subject_name as period_5_name, s6.subject_name as period_6_name
    FROM class_routine cr
    LEFT JOIN subjects s1 ON cr.period_1 = s1.id
    LEFT JOIN subjects s2 ON cr.period_2 = s2.id
    LEFT JOIN subjects s3 ON cr.period_3 = s3.id
    LEFT JOIN subjects s4 ON cr.period_4 = s4.id
    LEFT JOIN subjects s5 ON cr.period_5 = s5.id
    LEFT JOIN subjects s6 ON cr.period_6 = s6.id
";
$join_routine_result = $conn->query($join_routine_query);
?>

    <form method="POST" action="">
        <label for="class_id">Select Class:</label>
        <select name="class_id" id="class_id">
            <?php while ($class_row = $class_result->fetch_assoc()): ?>
                <option value="<?= $class_row['id']; ?>"><?= $class_row['class_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>
        
        <label for="day">Day:</label>
        <select name="day" id="day">
            <option value="Saturday">Saturday</option>
            <option value="Sunday">Sunday</option>
            <option value="Monday">Monday</option>
            <option value="Tuesday">Tuesday</option>
            <option value="Wednesday">Wednesday</option>
            <option value="Thursday">Thursday</option>
        </select>
        <br><br>

        <!-- প্রতিটি পিরিয়ডের জন্য বিষয় নির্বাচন -->
        <label for="period_1_subject">1st Period Subject:</label>
        <select name="period_1_subject" id="period_1_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>
        
        <label for="period_2_subject">2nd Period Subject:</label>
        <select name="period_2_subject" id="period_2_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_3_subject">3rd Period Subject:</label>
        <select name="period_3_subject" id="period_3_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_4_subject">4th Period Subject:</label>
        <select name="period_4_subject" id="period_4_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_5_subject">5th Period Subject:</label>
        <select name="period_5_subject" id="period_5_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_6_subject">6th Period Subject:</label>
        <select name="period_6_subject" id="period_6_subject">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>"><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <button type="submit">Save Routine</button>
    </form>
    
    <h2>Existing Class Routine</h2>
    <table border="1">
        <thead>
            <tr>
                <th>Class</th>
                <th>Day</th>
                <th>1st Period</th>
                <th>2nd Period</th>
                <th>3rd Period</th>
                <th>4th Period</th>
                <th>5th Period</th>
                <th>6th Period</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($routine_row = $join_routine_result->fetch_assoc()): ?>
                <tr>
                    <td><?= $routine_row['class_id']; ?></td>
                    <td><?= $routine_row['day']; ?></td>
                    <td><?= $routine_row['period_1_name']; ?></td>
                    <td><?= $routine_row['period_2_name']; ?></td>
                    <td><?= $routine_row['period_3_name']; ?></td>
                    <td><?= $routine_row['period_4_name']; ?></td>
                    <td><?= $routine_row['period_5_name']; ?></td>
                    <td><?= $routine_row['period_6_name']; ?></td>
                    <td>
                        <a href="edit_routine.php?id=<?= $routine_row['id']; ?>">Edit</a> |
                        <a href="?delete_id=<?= $routine_row['id']; ?>" onclick="return confirm('Are you sure you want to delete this routine?')">Delete</a>
                    </td>
                </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</body>
</html>
