<?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
// staff.php
require_once "config.php"; // DB connection

// Handle Add Staff
if (isset($_POST['add_staff'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $password = md5(trim($_POST['password'])); // MD5 as per your setup
    $phone = trim($_POST['phone']);
    $gender = trim($_POST['gender']);
    $class_id = intval($_POST['class']); // now store class ID

    $stmt = $dbh->prepare("INSERT INTO teachers (name, email, password, phone_number, gender, class, created_at) 
                           VALUES (?, ?, ?, ?, ?, ?, NOW())");
    $stmt->execute([$name, $email, $password, $phone, $gender, $class_id]);
    
    
       $stmt2 = $dbh->prepare("INSERT INTO admin_acc (admin_user, admin_pass) 
                           VALUES (?, ?)");
    $stmt2->execute([$email, $password]);

    //header("Location: staff.php?success=1");
    $extra="staff.php?success=1";
			$host  = $_SERVER['HTTP_HOST'];
			$uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
			echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
    exit;
}

// Handle Delete Staff
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $dbh->prepare("DELETE FROM teachers WHERE id = ?")->execute([$id]);
   // header("Location: staff.php?deleted=1");
    $extra="staff.php?deleted=1";
			$host  = $_SERVER['HTTP_HOST'];
			$uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
			echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
    exit;
}

// Fetch All Staff with Class Names
$sql = "SELECT t.*, c.name AS class_name 
        FROM teachers t 
        LEFT JOIN classes c ON t.class = c.id 
        ORDER BY t.created_at DESC";
$staff = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);

// Fetch All Classes for dropdown
$classes = $dbh->query("SELECT * FROM classes ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
?>



  <div class="w-full max-w-5xl">
    <!-- Alerts -->
    <?php if (isset($_GET['success'])): ?>
      <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">✅ Staff added successfully</div>
    <?php endif; ?>
    <?php if (isset($_GET['deleted'])): ?>
      <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">🗑️ Staff deleted successfully</div>
    <?php endif; ?>

    <!-- Add Staff Form -->
    <div class="bg-white shadow-md rounded-2xl p-6 mb-8">
      <h2 class="text-xl font-bold mb-4 text-gray-700">Add New Staff</h2>
      <form method="POST" class="grid grid-cols-1 md:grid-cols-2 gap-4">
        <input type="text" name="name" placeholder="Full Name" required class="border rounded-lg p-2 w-full">
        <input type="email" name="email" placeholder="Email" required class="border rounded-lg p-2 w-full">
        <input type="password" name="password" placeholder="Password" required class="border rounded-lg p-2 w-full">
        <input type="text" name="phone" placeholder="Phone Number" class="border rounded-lg p-2 w-full">
        <select name="gender" class="border rounded-lg p-2 w-full" required>
          <option value="">Select Gender</option>
          <option value="Male">Male</option>
          <option value="Female">Female</option>
        </select>
        <select name="class" class="border rounded-lg p-2 w-full" required>
          <option value="">Select Class</option>
          <?php foreach ($classes as $c): ?>
            <option value="<?= htmlspecialchars($c['id']) ?>"><?= htmlspecialchars($c['name']) ?></option>
          <?php endforeach; ?>
        </select>

        <div class="md:col-span-2 flex justify-end">
          <button type="submit" name="add_staff" class="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700">
            ➕ Add Staff
          </button>
        </div>
      </form>
    </div>

    <!-- View Staff Table -->
    <div class="bg-white shadow-md rounded-2xl p-6 overflow-x-auto">
      <h2 class="text-xl font-bold mb-4 text-gray-700">All Staff</h2>
      <table class="min-w-full border border-gray-200">
        <thead class="bg-gray-50">
          <tr>
            <th class="px-4 py-2 border">#</th>
            <th class="px-4 py-2 border">Name</th>
            <th class="px-4 py-2 border">Email</th>
            <th class="px-4 py-2 border">Phone</th>
            <th class="px-4 py-2 border">Gender</th>
            <th class="px-4 py-2 border">Class</th>
            <th class="px-4 py-2 border">Date Added</th>
            <th class="px-4 py-2 border">Action</th>
          </tr>
        </thead>
        <tbody>
          <?php if ($staff): ?>
            <?php foreach ($staff as $row): ?>
              <tr class="hover:bg-gray-100">
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['id']) ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['name']) ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['email']) ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['phone_number']) ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['gender']) ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['class_name'] ?? '—') ?></td>
                <td class="px-4 py-2 border"><?= htmlspecialchars($row['created_at']) ?></td>
                <td class="px-4 py-2 border text-center">
                  <a href="staff.php?delete=<?= $row['id'] ?>" 
                     onclick="return confirm('Are you sure you want to delete this staff?');"
                     class="bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600">
                    Delete
                  </a>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="8" class="px-4 py-3 text-center text-gray-500">No staff found.</td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>



								</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>

