<?php

// DELETE handler
if (isset($_POST['teachers_delete_btn'])) {
    $delete_id = $_POST['teachers_hidden_id'];
    $query = "DELETE FROM `teacher_classes` WHERE `id` = '{$delete_id}'";
    $run_query = mysqli_query($connection, $query);

    if ($run_query == true) {
        $_SESSION['msg'] = "Teacher's Assignment Deleted Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Could Not Delete Record";
        $_SESSION['type'] = "error";
    }

    header("Location: assign_class_to_staff");
    exit();
}
?>

<?php
// SAVE handler
if (isset($_POST['save_btn'])) {
    $teacher_name = inject_checker($connection, $_POST['teacher_name']);
    $class = inject_checker($connection, $_POST['class']);
    $term = inject_checker($connection, $_POST['term']);

    // Get teacher_id from staff table based on name and role
    $teacher_query = "SELECT staff.id FROM staff 
                      INNER JOIN role ON staff.role = role.id 
                      WHERE staff.name = '{$teacher_name}' 
                      AND role.role_name = 'teacher'";
    $teacher_result = mysqli_query($connection, $teacher_query);

    if (mysqli_num_rows($teacher_result) > 0) {
        $teacher_row = mysqli_fetch_assoc($teacher_result);
        $teacher_id = $teacher_row['id'];

        // Check if teacher is already assigned to this class and term
        $query = "SELECT * FROM `teacher_classes`
                  WHERE `class` = '{$class}' 
                  AND `term` = '{$term}'";
        $run_query = mysqli_query($connection, $query);

        if (mysqli_num_rows($run_query) > 0) {
            $_SESSION['msg'] = "Another teacher has already been assigned to {$class} for {$term}";
            $_SESSION['type'] = "error";
        } else {
            // Insert assignment using teacher_id only
            $query = "INSERT INTO `teacher_classes`(`teacher_id`, `class`, `term`) 
                      VALUES('{$teacher_id}', '{$class}', '{$term}')";
            $run_query = mysqli_query($connection, $query);

            if ($run_query == true) {
                $_SESSION['msg'] = "{$teacher_name} assigned to {$class} for {$term} successfully";
                $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg'] = "Failed to assign teacher to {$class}";
                $_SESSION['type'] = "error";
            }
        }
    } else {
        $_SESSION['msg'] = "Teacher {$teacher_name} not found in staff records";
        $_SESSION['type'] = "error";
    }

    header("Location: assign_class_to_staff");
    exit();
}
?>


<div class="page-title">
    <div>
        <h1><i class="fa fa-calendar"></i> Teachers</h1>
        <p>Assign teachers to a class</p>
    </div>
    <div>
        <ul class="breadcrumb">
            <li><a href='modules.php'><i class="fa fa-home fa-lg"></i></a></li>
            <li><a href="#">Teachers and their classes</a></li>
        </ul>
    </div>
</div>
<div class="card">
    <form action='' method='POST'>
        <div class='panel panel-primary ch'>
            <div class='panel-heading'>
                <h4 class='text-center'>Assign Teachers to their class</h4>
            </div>
            <div class='panel-body'>
                <div class='row'>
                    <div class='col-xs-12 col-sm-12 col-md-12'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='sizing-addon2'>Teachers</span>
                            <select class='form-control' name='teacher_name' required>
                                <option><?php echo $select; ?></option>
								<?php
								$query = "SELECT staff.*, role.role_name 
										  FROM staff 
										  LEFT JOIN role ON staff.role = role.id 
										  WHERE role.role_name = 'teacher'";

								$run_query = mysqli_query($connection, $query);

								if (mysqli_num_rows($run_query) > 0) {
									while ($result = mysqli_fetch_assoc($run_query)) {
										$staff_id = $result['id'];
										$name = $result['name'];
										$role_name = $result['role_name'];

										echo "<option value='{$staff_id}'>{$name} - {$role_name}</option>";
									}
								}
								?>
							</select>
                        </div>
                        <br />
                        <div class='input-group'>
                            <span class='input-group-addon' id='sizing-addon2'>Class</span>
                            <select class='form-control' name='class' required>
                                <option><?php echo $select; ?></option>
                                <?php
                                $query = " SELECT * FROM `classes` ";
                                $run_query = mysqli_query($connection, $query);
                                if (mysqli_num_rows($run_query) > 0) {
                                    while ($result = mysqli_fetch_assoc($run_query)) {
                                        $publish_result_classes = $result['classes'];
                                        echo "
                                                            <option>{$publish_result_classes}</option>
                                                        ";
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <br />
                        <div class='input-group'>
                            <span class='input-group-addon' id='sizing-addon2'>Term</span>
                            <select class='form-control' name='term' required>
                                <option><?php echo $select; ?></option>
                                <?php
                                $term_array = array("First Term", "Second Term", "Third Term");
                                foreach ($term_array as $term) {
                                    echo "<option>{$term}</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <br />
                        <div class='col-xs-12 col-sm-12 col-md-6'>
                            <button type='submit' name='save_btn' class='btn btn-primary btn-md'><span class='glyphicon glyphicon-user'></span>&nbsp; SAVE</button><br />
                        </div>                    
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php
    $query = " SELECT * FROM `teacher_classes` ORDER BY `class` ASC ";
    $run_query = mysqli_query($connection, $query);

    if (mysqli_num_rows($run_query) > 0) {
        $i = 0;
        echo "
                <div class='table-responsive'>
                    <table class='table table-striped'>
                        <thead>
                            <tr>
                                <th>S/N</th>
                                <th>NAME</th>
                                <th>ASSIGNED CLASS</th>
                                <th>TERM</th>
                                <th>ACTION</th>
                            </tr>
                        </thead>
            ";
        while ($result = mysqli_fetch_assoc($run_query)) {
            $i++;
            $teachers_id = $result['id'];
            $teacher_name = $result['name'];
            $teacher_class = $result['class'];
            $assigned_term = $result['term'];
            echo "
                    <tbody>
                        <tr>
                            <td>{$i}</td>
                            <td>{$teacher_name}</td>
                            <td>{$teacher_class}</td>
                            <td>{$assigned_term}</td>
                            <td>
                                <form method='POST' action=''>
                                    <input type='hidden' name='teachers_hidden_id' value='{$teachers_id}' />
                                    <input type='submit' name='teachers_delete_btn' id='demoSwal' value='Delete' class='btn btn-xs btn-danger' />
                                </form>
                            </td>
                        </tr>
                    </tbody>
                ";
        }
    }
    ?>
</div>
