<?php
session_start();

// Check if the user is an admin
if (!isset($_SESSION['admin_id']) || $_SESSION['user_type'] !== 'admin') {
    // If not an admin, redirect to a different page (e.g., sub-user dashboard)
    header("Location: sub_dashboard.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>Mips Sub Users</title>
    <link rel="stylesheet" href="../Css/dash.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap" rel="stylesheet">
    <!-- Boxicons CDN Link -->
    <link href='https://unpkg.com/boxicons@2.0.7/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <title>Sub User Management</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Material+Icons');
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #007BFF;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #ddd;
        }

        .button {
            padding: 4px 4px;
            margin-right: 5px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: #fff;
        }

        .edit-button {
            background-color: #007BFF;
        }

        .delete-button {
            background-color: #DC3545;
        }
    </style>
</head>
<body>
    <div class="sidebar">
    <div class="logo-details">
      <i class='bx bx-desktop'></i>
      <span class="logo_name">Mips</span>
    </div>
      <ul class="nav-links">
        <li>
          <a href="dashboard.php" >
            <i class='bx bx-grid-alt' ></i>
            <span class="links_name">Dashboard</span>
          </a>
        </li>
        <li>
          <a href="index.php" >
            <i class='bx bx-box' ></i>
            <span class="links_name">Register</span>
          </a>
        </li>
        <li>
          <a href="users.php">
            <i class='bx bx-list-ul' ></i>
            <span class="links_name">Participants</span>
          </a>
        </li>
         <li>
                <a href="attendance_scanner.php">
                    <i class='bx bxs-user-detail'></i>
                    <span class="links_name">Attendance</span>
                </a>
            </li>
            <li>
                <a href="meals_deduction.php">
                    <i class='fa-solid fa-bowl-food'></i>
                    <span class="links_name">Meals</span>
                </a>
            </li>
            <li>
                <a href="promotion_scanner.php">
                    <i class='bx bx-list-ul'></i>
                    <span class="links_name">Promotions</span>
                </a>
            </li>
        <li>
          <a href="sub_user.php">
            <i class="fas fa-user"></i>
            <span class="links_name">Create User</span>
          </a>
        </li>
        <li>
          <a href="Disubuser.php" class="active">
            <i class="fas fa-user-check"></i>
            <span class="links_name">Sub User</span>
          </a>
        </li>
               
        <li class="log_out">
          <a href="logout.php">
            <i class='bx bx-log-out'></i>
            <span class="links_name">Log out</span>
          </a>
        </li>
      </ul>
  </div>
  <section class="home-section">
    <nav>
      <div class="sidebar-button">
        <i class='bx bx-menu sidebarBtn'></i>
        <span class="dashboard">Sub User</span>
      </div>
      <div class="search-box">
        <input input type="text" id="search-input" class="form-control form-control-sm mx-2" placeholder="Search...">
        <i class='bx bx-search' ></i>
</div>
</nav>
<div class="home-content">
    <div class="container">

        <h2>Sub User Management</h2>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Full Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Gender</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php
                require 'db_config.php';

                // Fetch sub users from the database
                $sql = "SELECT id, fullname, email, phone, gender FROM sub_user";
                $result = $conn->query($sql);

                if ($result->num_rows > 0) {
                    // Output data of each row
                    while ($row = $result->fetch_assoc()) {
                        echo "<tr>";
                        echo "<td>" . $row['id'] . "</td>";
                        echo "<td>" . $row['fullname'] . "</td>";
                        echo "<td>" . $row['email'] . "</td>";
                        echo "<td>" . $row['phone'] . "</td>";
                        echo "<td>" . $row['gender'] . "</td>";
                        echo "<td>";
                        echo "<button class='button edit-button' onclick=\"window.location.href='edit_sub_user.php?id=" . $row['id'] . "'\">Edit</button>";
                        echo "<button class='button delete-button' onclick=\"deleteSubUser(" . $row['id'] . ")\">Delete</button>";
                        echo "</td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='6'>No sub users found.</td></tr>";
                }

                $conn->close();
                ?>
            </tbody>
        </table>


    <script>
        function deleteSubUser(id) {
            if (confirm("Are you sure you want to delete this sub user?")) {
                window.location.href = 'delete_sub_user.php?id=' + id;
            }
        }
    </script>
</div>
</div>
</section>
<script>
 
let sidebar = document.querySelector(".sidebar");
let sidebarBtn = document.querySelector(".sidebarBtn");
sidebarBtn.onclick = function() {
  sidebar.classList.toggle("active");
  if(sidebar.classList.contains("active")){
  sidebarBtn.classList.replace("bx-menu" ,"bx-menu-alt-right");
}else
  sidebarBtn.classList.replace("bx-menu-alt-right", "bx-menu");
}
 </script>
</body>
</html>
