<?php 

include('includes/config.php');


?>


<?php
include('includes/user_data.php');

?>


<!doctype html>
<html lang="en">


<!-- Mirrored from codervent.com/rocker/demo/vertical/user-profile.html by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 26 Aug 2022 06:47:36 GMT -->
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!--plugins-->
	<link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
	<link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
	<link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link href="assets/css/bootstrap.min.css" rel="stylesheet">
	<link href="assets/css/bootstrap-extended.css" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&amp;display=swap" rel="stylesheet">
	<link href="assets/css/app.css" rel="stylesheet">
	<link href="assets/css/icons.css" rel="stylesheet">
	<!-- Theme Style CSS -->
	<link rel="stylesheet" href="assets/css/dark-theme.css" />
	<link rel="stylesheet" href="assets/css/semi-dark.css" />
	<link rel="stylesheet" href="assets/css/header-colors.css" />
	<title>Staff Management</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
	<!--wrapper-->
	<div class="wrapper">
		<!--sidebar wrapper -->
		<?php include 'includes/side.php'; ?>
		<!--end sidebar wrapper -->
		<!--start header -->
		<?php include 'includes/head.php'; ?>
		<!--end header -->
		<!--start page wrapper -->
		<div class="page-wrapper">
			<div class="page-content">
				<!--breadcrumb-->
				<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
					<div class="breadcrumb-title pe-3"></div>
					<div class="ps-3">
						<nav aria-label="breadcrumb">
							<ol class="breadcrumb mb-0 p-0">
								<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
								</li>
								<li class="breadcrumb-item active" aria-current="page"></li>
							</ol>
						</nav>
					</div>
					
				</div>
				<!--end breadcrumb-->
				<div class="container">
					<div class="main-body">
						<div class="row">
						
							<div class="col-lg-12">
								<div class="card">
									<div class="card-body">
<?php
// subjects.php 
//require_once "config.php";

// Handle Add Subject
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_subject'])) {
    $name = trim($_POST['name']);
    if (!empty($name)) {
        $stmt = $dbh->prepare("INSERT INTO Subjects (name) VALUES (:name)");
        $stmt->execute([':name' => $name]);
    }
   // header("Location: subjects.php");
    	echo "<script type='text/javascript'> document.location = 'subjects.php'; </script>";
    exit;
}

// Handle Delete
if (isset($_GET['delete'])) {
    $id = (int) $_GET['delete'];
    $stmt = $dbh->prepare("DELETE FROM Subjects WHERE id = :id");
    $stmt->execute([':id' => $id]);
   	echo "<script type='text/javascript'> document.location = 'subjects.php'; </script>";
    exit;
}

// Handle Edit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_subject'])) {
    $id = (int) $_POST['id'];
    $name = trim($_POST['name']);
    if (!empty($name)) {
        $stmt = $dbh->prepare("UPDATE Subjects SET name = :name WHERE id = :id");
        $stmt->execute([':name' => $name, ':id' => $id]);
    }
  	echo "<script type='text/javascript'> document.location = 'subjects.php'; </script>";
    exit;
}

// Fetch all subjects
$stmt = $dbh->query("SELECT * FROM Subjects ORDER BY id DESC");
$subjects = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Manage Subjects</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-6">
  <div class="w-full max-w-3xl bg-white shadow-xl rounded-2xl p-6">
    <h1 class="text-2xl font-bold mb-6 text-center text-indigo-600">Subjects Management</h1>
    
    <!-- Add Subject Form -->
    <form method="POST" class="flex items-center gap-3 mb-6">
      <input type="text" name="name" placeholder="Enter subject name"
             class="flex-1 px-4 py-2 border rounded-lg focus:ring-2 focus:ring-indigo-500 outline-none" required>
      <button type="submit" name="add_subject"
              class="bg-indigo-600 text-white px-4 py-2 rounded-lg hover:bg-indigo-700">Add</button>
    </form>

    <!-- Subjects Table -->
    <div class="overflow-x-auto">
      <table class="w-full text-sm text-left border border-gray-200 rounded-lg">
        <thead class="bg-indigo-50 text-indigo-700">
          <tr>
            <th class="px-4 py-2">ID</th>
            <th class="px-4 py-2">Subject Name</th>
            <th class="px-4 py-2 text-center">Actions</th>
          </tr>
        </thead>
        <tbody>
          <?php if (count($subjects) > 0): ?>
            <?php foreach ($subjects as $subject): ?>
              <tr class="border-t hover:bg-gray-50">
                <td class="px-4 py-2"><?= htmlspecialchars($subject['id']) ?></td>
                <td class="px-4 py-2"><?= htmlspecialchars($subject['name']) ?></td>
                <td class="px-4 py-2 text-center">
                  <!-- Edit Button (Modal Trigger) -->
                  <button onclick="openEditModal(<?= $subject['id'] ?>, '<?= htmlspecialchars($subject['name'], ENT_QUOTES) ?>')"
                          class="text-blue-600 hover:underline mr-3">Edit</button>
                  <!-- Delete -->
                  <a href="?delete=<?= $subject['id'] ?>"
                     onclick="return confirm('Are you sure you want to delete this subject?');"
                     class="text-red-600 hover:underline">Delete</a>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="3" class="px-4 py-4 text-center text-gray-500">No subjects added yet.</td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Edit Modal -->
  <div id="editModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center">
    <div class="bg-white p-6 rounded-2xl shadow-xl w-full max-w-md">
      <h2 class="text-lg font-bold mb-4">Edit Subject</h2>
      <form method="POST">
        <input type="hidden" name="id" id="edit_id">
        <input type="text" name="name" id="edit_name"
               class="w-full px-4 py-2 border rounded-lg mb-4 focus:ring-2 focus:ring-indigo-500 outline-none" required>
        <div class="flex justify-end gap-3">
          <button type="button" onclick="closeEditModal()" class="px-4 py-2 border rounded-lg">Cancel</button>
          <button type="submit" name="edit_subject"
                  class="bg-indigo-600 text-white px-4 py-2 rounded-lg hover:bg-indigo-700">Update</button>
        </div>
      </form>
    </div>
  </div>

  <script>
    function openEditModal(id, name) {
      document.getElementById("edit_id").value = id;
      document.getElementById("edit_name").value = name;
      document.getElementById("editModal").classList.remove("hidden");
      document.getElementById("editModal").classList.add("flex");
    }
    function closeEditModal() {
      document.getElementById("editModal").classList.add("hidden");
    }
  </script>


								</div>
								
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--end page wrapper -->
		<!--start overlay-->
		<div class="overlay toggle-icon"></div>
		<!--end overlay-->
		<!--Start Back To Top Button--> <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
		<footer class="page-footer">
			<p class="mb-0">Copyright © 2024. All right reserved.</p>
		</footer>
	</div>
	<!--end wrapper-->
	<!--start switcher-->
	<?php include 'includes/customiser.php'; ?>

	<!--end switcher-->
	<!-- Bootstrap JS -->
	<script src="assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="assets/js/app.js"></script>
</body>


</html>