<?php
// ADD CLASS
if (isset($_POST['add_class_btn'])) {
    $new_class_name = trim($_POST['new_class_name']);

    if (!empty($new_class_name)) {
        $query = "INSERT INTO `classes` (`classes`) VALUES ('{$new_class_name}')";
        $run_query = mysqli_query($connection, $query);

        if ($run_query) {
            $_SESSION['msg'] = "Class Added Successfully";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Error! Could Not Add Class";
            $_SESSION['type'] = "danger";
        }
    } else {
        $_SESSION['msg'] = "Class name cannot be empty";
        $_SESSION['type'] = "danger";
    }
    header("Location: modules.php?view_classes");
    exit();
}


// UPDATE CLASS
if (isset($_POST['update_class_btn'])) {
    $edit_class_id = $_POST['edit_class_id'];
    $edit_class_name = $_POST['edit_class_name'];

    $query = "UPDATE `classes` SET `classes` = '{$edit_class_name}' WHERE `id` = '{$edit_class_id}'";
    $run_query = mysqli_query($connection, $query);

    if ($run_query) {
        $_SESSION['msg'] = "Class Updated Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Could Not Update Record";
        $_SESSION['type'] = "danger";
    }

    header("Location: modules.php?view_classes");
    exit();
}


// DELETE CLASS
if (isset($_POST['delete_class_btn'])) {
    $hide_class_del = $_POST['hide_class_del'];

    $query = "DELETE FROM `classes` WHERE `id` = '{$hide_class_del}'";
    $run_query = mysqli_query($connection, $query);

    if ($run_query) {
        $_SESSION['msg'] = "Class Deleted Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Could Not Delete Record";
        $_SESSION['type'] = "danger";
    }

    header("Location: modules.php?view_classes");
    exit();
}
?>
<div class="card">
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-12'>
            <div class='panel panel-primary ch'>
                <div class='panel-heading'>
                    <h4 class='text-center'>CLASSE MANAGEMENT</h4>
                </div>
                
                <div class='panel-body'>
           			<div class='mb-3 text-right'>
						<button class='btn btn-sm btn-success' data-toggle='modal' data-target='#addClassModal'>➕ Add Class</button>
					</div>

					<!-- Add Class Modal -->
					<div class='modal fade' id='addClassModal' tabindex='-1' role='dialog' aria-labelledby='addClassModalLabel' aria-hidden='true'>
						<div class='modal-dialog' role='document'>
							<div class='modal-content'>
								<form method='POST' action=''>
									<div class='modal-header'>
										<h5 class='modal-title' id='addClassModalLabel'>Add New Class</h5>
										<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
										    <span aria-hidden='true'>&times;</span>
										</button>
									</div>
									<div class='modal-body'>
										<div class='form-group'>
										    <label>Class Name</label>
										    <input type='text' name='new_class_name' class='form-control' placeholder='Enter class name' required />
										</div>
									</div>
									<div class='modal-footer'>
										<button type='submit' name='add_class_btn' class='btn btn-primary'>Add Class</button>
										<button type='button' class='btn btn-secondary' data-dismiss='modal'>Cancel</button>
									</div>
								</form>
							</div>
						</div>
					</div>
                    <?php
                        $query = "SELECT * FROM `classes`";
                        $run_query = mysqli_query($connection, $query);
                        if(mysqli_num_rows($run_query) > 0){
                            $i = 0;
                            echo "
                            <div class='table-responsive'>
                                <table class='table table-hover table-bordered' id='myTable'>
                                    <thead>
                                        <tr>
                                            <th>S/N</th>
                                            <th>Classes</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                            ";
                            while($result = mysqli_fetch_assoc($run_query)){
                                $i++;
                                $class_id = $result['id'];
                                $all_class = $result['classes'];
                                echo "
                                    <tr>
										<td>{$i}</td>
										<td>{$all_class}</td>
										<td>
											<form method='POST' action='' style='display:inline-block'>
												<input type='hidden' name='hide_class_del' value='{$class_id}' />
												<input type='submit' name='delete_class_btn' value='Delete' class='btn btn-xs btn-danger' />
											</form>
											
											<button 
												type='button' 
												class='btn btn-xs btn-primary' 
												data-toggle='modal' 
												data-target='#editClassModal{$class_id}'>
												Edit
											</button>

											<!-- Edit Modal -->
											<div class='modal fade' id='editClassModal{$class_id}' tabindex='-1' role='dialog' aria-labelledby='editClassModalLabel{$class_id}' aria-hidden='true'>
												<div class='modal-dialog' role='document'>
													<div class='modal-content'>
														<form method='POST' action=''>
															<div class='modal-header'>
																<h5 class='modal-title' id='editClassModalLabel{$class_id}'>Edit Class</h5>
																<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
																	<span aria-hidden='true'>&times;</span>
																</button>
															</div>
															<div class='modal-body'>
																<input type='hidden' name='edit_class_id' value='{$class_id}' />
																<div class='form-group'>
																	<label>Class Name</label>
																	<input type='text' name='edit_class_name' class='form-control' value='{$all_class}' required />
																</div>
															</div>
															<div class='modal-footer'>
																<button type='submit' name='update_class_btn' class='btn btn-success'>Save changes</button>
																<button type='button' class='btn btn-secondary' data-dismiss='modal'>Cancel</button>
															</div>
														</form>
													</div>
												</div>
											</div>
										</td>
									</tr>

                                ";
                            }
                            echo "
                                    </tbody>
                                </table>
                            </div>
                            ";
                        }
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>
