<?php

/////////////// POST ACTION TO DELETE ADMINISTRATIVE INFORMATION ///////////////////
if (isset($_POST['subject_delete_btn'])) {
    $subject_hidden_id = $_POST['subject_hidden_id'];

    $query = "DELETE FROM `school_subjects` WHERE `id` = {$subject_hidden_id}";
    $run_query = mysqli_query($connection, $query);

    if ($run_query) {
        $_SESSION['msg'] = "Subject Deleted Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Subject Could Not Be Deleted";
        $_SESSION['type'] = "error";
    }

    header("Location: view_subject");
    exit();
}

if (isset($_POST['add_subject_btn'])) {
    $add_subject = inject_checker($connection, ucwords($_POST['add_subject']));

    if (empty($add_subject)) {
        $_SESSION['msg'] = "Field cannot be empty";
        $_SESSION['type'] = "error";
    } else {
        $query = "SELECT * FROM `school_subjects` WHERE `subjects` = '{$add_subject}'";
        $run_query = mysqli_query($connection, $query);
        if (mysqli_num_rows($run_query) > 0) {
            $_SESSION['msg'] = "Sorry! Subject already exists";
            $_SESSION['type'] = "error";
        } else {
            $query = "INSERT INTO `school_subjects`(`subjects`, `date_saved`) VALUES('{$add_subject}', '{$date}')";
            $run_query = mysqli_query($connection, $query);

            if ($run_query) {
                $_SESSION['msg'] = "{$add_subject} saved successfully into subject list";
                $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg'] = "{$add_subject} failed to save into subject list";
                $_SESSION['type'] = "error";
            }
        }
    }

    header("Location: view_subject");
    exit();
}


if (isset($_POST['edit_btn'])) {
    $edited_subject_id = $_POST['one_subject_hidden_id'];
    $edited_subject = inject_checker($connection, ucwords($_POST['edited_subject']));

    $query = "UPDATE `school_subjects` SET `subjects` = '{$edited_subject}' WHERE `id` = '{$edited_subject_id}'";
    $run_query = mysqli_query($connection, $query);

    if ($run_query) {
        $_SESSION['msg'] = "Changes Saved Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Changes not saved";
        $_SESSION['type'] = "error";
    }

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


 <div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Subject Management</h1>
    <p>Add, Edit and Delete Subject</p>
  </div>
</div>

<!-- Subject Table -->
<div class='panel panel-primary ch'>
  <div class='panel-heading'>					
    <h4 class='text-center'>View all subjects offered in school</h4>
  </div>
  <div class='panel-body'>
  <div class='mb-3 text-right'>
  	<button type="button" class="btn btn-success mb-3" data-toggle="modal" data-target="#addSubjectModal">
				<i class="fa fa-plus"></i> Add Subject
			</button>
  </div>
    <div class='row'>
      <div class='col-xs-12 col-sm-12 col-md-12'>

<?php
$query = "SELECT * FROM `school_subjects`";
$run_query = mysqli_query($connection, $query);

if (mysqli_num_rows($run_query) > 0) {
    $i = 0;
    echo '<div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed table-export" cellspacing="0" width="100%" id="myTable">
                <thead>
                    <tr>
                        <th>S/N</th>
                        <th>Subjects</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>';
    while ($result = mysqli_fetch_assoc($run_query)) {
        $subject_id = $result['id'];
        $subjects = $result['subjects'];
        $i++;
        echo "
            <tr>
                <td>{$i}</td>
                <td>{$subjects}</td>
                <td>
                    <!-- Edit Button -->
                    <button class='btn btn-xs btn-primary' data-toggle='modal' data-target='#editSubjectModal{$subject_id}'>
                        <i class='fa fa-edit'></i> Edit
                    </button>

                    <!-- Delete Form -->
                    <form method='POST' action='' style='display:inline-block;'>
                        <input type='hidden' name='subject_hidden_id' value='{$subject_id}' />
                        <button type='submit' name='subject_delete_btn' class='btn btn-xs btn-danger'>
                            <i class='fa fa-trash'></i> Delete
                        </button>
                    </form>
                </td>
            </tr>

            <!-- Edit Modal for Subject {$subject_id} -->
            <div class='modal fade' id='editSubjectModal{$subject_id}' tabindex='-1' role='dialog'>
              <div class='modal-dialog' role='document'>
                <div class='modal-content'>
                  <form method='POST' action=''>
                    <div class='modal-header'>
                      <h5 class='modal-title'>Edit Subject</h5>
                      <button type='button' class='close' data-dismiss='modal'>&times;</button>
                    </div>
                    <div class='modal-body'>
                      <input type='hidden' name='one_subject_hidden_id' value='{$subject_id}'>
                      <div class='form-group'>
                        <label>Subject Name</label>
                        <input type='text' name='edited_subject' value='{$subjects}' class='form-control' required>
                      </div>
                    </div>
                    <div class='modal-footer'>
                      <button type='submit' name='edit_btn' class='btn btn-primary'>Save Changes</button>
                      <button type='button' class='btn btn-secondary' data-dismiss='modal'>Cancel</button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
        ";
    }
    echo '</tbody></table></div>';
}
?>
      </div>
    </div>
  </div>
</div>

<!-- Add Subject Modal -->
<div class="modal fade" id="addSubjectModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <form method="POST" action="">
        <div class="modal-header">
          <h5 class="modal-title">Add New Subject</h5>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>Subject Name</label>
            <input type="text" name="add_subject" class="form-control" required>
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" name="add_subject_btn" class="btn btn-success">Add Subject</button>
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        </div>
      </form>
    </div>
  </div>
</div>

