Mini Shell

Direktori : /home/mhcadmin/www/Portal/
Upload File :
Current File : /home/mhcadmin/www/Portal/Departments.php

<?php
session_start();
$rootPath = realpath(dirname(__FILE__) . '/..');
require_once $rootPath . '/Portal/config/config.php';
require_once $rootPath . '/Portal/include/auth_validate.php';

//if ($ManageUser == 1) {

    /////// Code for new access control on header menu /////////////////////////
    $User = $_SESSION['id'];
    $UserAccessName = $query3 = $result3 = $Status = "";

    $result = mysqli_query($conn, "SELECT * FROM admin_accounts WHERE id = '$User'");
    $num = mysqli_num_rows($result);

    if ($num > 0) {
        $rows = mysqli_fetch_array($result);
        $UserAccessName = $rows['Access_Level'];
    }

    $db = getDbInstance();

    // Get data from query string
    $search_string = filter_input(INPUT_GET, 'search_string', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $del_id = filter_input(INPUT_GET, 'del_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $filter_col = filter_input(INPUT_GET, 'filter_col', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $order_by = filter_input(INPUT_GET, 'order_by', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT);
    
    $pagelimit = 10;
    if (!$page) {
        $page = 1;
    }
    
    // If filter types are not selected we show latest added data first
    if (!$filter_col) {
        $filter_col = "id";
    }
    if (!$order_by) {
        $order_by = "desc";
    }
    
    // Select the columns
    $select = array('id', 'Name', 'Comment');
    
    
    // If user searches 
    if ($search_string) {
        $db->where('Name', '%' . $search_string . '%', 'like');
    }

    if ($order_by) {
        $db->orderBy($filter_col, $order_by);
    }

    $db->pageLimit = $pagelimit;
    $result = $db->arraybuilder()->paginate("Department", $page, $select);
    $total_pages = $db->totalPages;

    // Get columns for order filter
    $filter_options = array();
    if (!empty($result)) {
        foreach ($result as $value) {
            foreach ($value as $col_name => $col_value) {
                $filter_options[$col_name] = $col_name;
            }
            // Execute only once
            break;
        } 
    }

    //Adding Department Record
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Add_Department'])) {
   
        // Sanitize and validate input
        $Dept_Name = mysqli_real_escape_string($conn, $_POST['Dept_Name']);
        $Comment = mysqli_real_escape_string($conn, $_POST['Comment']);
        
        // Check if Department already exists
        $check_sql = "SELECT * FROM Department WHERE Name = '$Dept_Name'";
        $check_result = mysqli_query($conn, $check_sql);
        
        if (mysqli_num_rows($check_result) > 0) {
            $_SESSION['failure'] = "Department already exists!";
        } else {
            // Updated INSERT query to include HOD column
            $sql = mysqli_query($conn, "INSERT INTO Department (Name, Comment) 
                VALUES ('$Dept_Name','$Comment')");

            if ($sql) {
                // Event Trail
                date_default_timezone_set('Africa/Blantyre');
                $EventTime = date('d-m-Y H:i', time());

                $Action = 'Adding Record';
                $RecordType = 'Department';
                $Details = "$Dept_Name";
                $Date = "$EventTime";
                $User = $_SESSION['Full_Name'];

                $event_sql = mysqli_query($conn, "INSERT INTO eventtrail (Action, RecordType, Details, Date, User) 
                    VALUES ('$Action','$RecordType','$Details','$Date','$User')");

                $_SESSION['success'] = "department added successfully!";
            } else {
                $_SESSION['failure'] = "Error adding user: " . mysqli_error($conn);
            }
        }
        header('location: Departments.php');
        exit();
   
    }
	
	
	//Updating Department Record
	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Update_Department'])) {
		
		// Sanitize and validate input
        $Dept_Name = mysqli_real_escape_string($conn, $_POST['Dept_Name']);
        $Comment = mysqli_real_escape_string($conn, $_POST['Comment']);
        $DPTID = mysqli_real_escape_string($conn, $_POST['DPTID']);
        
        
        $sql = mysqli_query($conn,"UPDATE Department SET  Name = '$Dept_Name', Comment = '$Comment'  WHERE id = '$DPTID'"); 	
	   
	   $_SESSION['success'] = "department Record Updated successfully!";
        header('location: Departments.php');
        exit();
    }

	//Delete Department Record
	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Delete_Department'])) {
		
		// Sanitize and validate input
        $DPTID = mysqli_real_escape_string($conn, $_POST['DPTID']);
        
        $sql = mysqli_query($conn,"UPDATE Department SET  Deleted = 'Yes'  WHERE id = '$DPTID'"); 	
	   
	   $_SESSION['success'] = "department Record Updated successfully!";
        header('location: Departments.php');
        exit();
    }
	
    $query3 = "SELECT * FROM `Department`";
    $result3 = mysqli_query($conn, $query3);

    include_once 'include/AdminHeader.php';
?>

<!-- Add Font Awesome and Bootstrap Select -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.18/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.18/js/bootstrap-select.min.js"></script>

<style>
    /* BEAUTIFUL STYLING - Based on reference file */
    :root {
        --primary-green: #2ecc71;
        --dark-green: #27ae60;
        --light-green: #d4edda;
        --soft-white: #f8f9fa;
        --pure-white: #ffffff;
        --light-gray: #e9ecef;
        --medium-gray: #ced4da;
        --dark-gray: #495057;
        --charcoal: #343a40;
        --black: #212529;
        --shadow: 0 2px 4px rgba(0,0,0,0.1);
        --warning-color: #ffc107;
        --danger-color: #dc3545;
    }

    body {
        background-color: #f0f2f5;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    #page-wrapper {
        min-height: 100vh;
        padding: 15px;
    }

    /* Main Container */
    .form-container {
        max-width: 1400px;
        margin: 0 auto;
    }

    /* Header Styles */
    .page-header {
        background: var(--pure-white);
        padding: 15px 25px;
        border-radius: 8px;
        margin: 0 0 20px 0;
        box-shadow: var(--shadow);
        border-left: 4px solid var(--primary-green);
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 15px;
    }

    .page-header h4 {
        margin: 0;
        font-size: 20px;
        font-weight: 600;
        color: var(--black);
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .page-header h4 i {
        color: var(--primary-green);
        font-size: 24px;
    }

    /* Action Buttons */
    .action-buttons {
        display: flex;
        gap: 10px;
        flex-wrap: wrap;
    }

    .btn-custom {
        padding: 8px 16px;
        border-radius: 6px;
        font-weight: 500;
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        transition: all 0.2s ease;
        border: none;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 6px;
        text-decoration: none;
    }

    .btn-custom i {
        font-size: 14px;
    }

    .btn-custom-primary {
        background: var(--pure-white);
        color: var(--black);
        border: 1px solid var(--light-gray);
    }

    .btn-custom-primary:hover {
        background: var(--light-gray);
        text-decoration: none;
        color: var(--black);
    }

    .btn-custom-success {
        background: var(--primary-green);
        color: var(--pure-white);
    }

    .btn-custom-success:hover {
        background: var(--dark-green);
        text-decoration: none;
        color: var(--pure-white);
    }

    /* Alert Messages */
    .alert-custom {
        padding: 12px 18px;
        border-radius: 6px;
        margin-bottom: 20px;
        border: none;
        display: flex;
        align-items: center;
        gap: 12px;
        font-size: 14px;
        box-shadow: var(--shadow);
    }

    .alert-custom i {
        font-size: 18px;
    }

    .alert-custom-success {
        background: var(--light-green);
        color: var(--dark-green);
        border-left: 4px solid var(--primary-green);
    }

    .alert-custom-danger {
        background: #f8d7da;
        color: #721c24;
        border-left: 4px solid var(--danger-color);
    }

    /* Filter Section - One Line */
    .filter-section {
        background: var(--pure-white);
        border-radius: 8px;
        padding: 15px 20px;
        margin-bottom: 20px;
        box-shadow: var(--shadow);
        border: 1px solid var(--light-gray);
        overflow-x: auto;
    }

    .filter-form {
        display: flex;
        align-items: center;
        gap: 15px;
        min-width: min-content;
    }

    .filter-form .form-control {
        height: 38px;
        border: 1px solid var(--light-gray);
        border-radius: 6px;
        padding: 0 10px;
        font-size: 13px;
        background: var(--pure-white);
    }

    .filter-form .form-control:focus {
        border-color: var(--primary-green);
        outline: none;
        box-shadow: 0 0 0 2px rgba(46, 204, 113, 0.1);
    }

    .filter-form .btn-primary {
        height: 38px;
        padding: 0 20px;
        background: var(--primary-green);
        border: none;
        border-radius: 6px;
        color: white;
        font-weight: 500;
        font-size: 13px;
        display: flex;
        align-items: center;
        gap: 5px;
        white-space: nowrap;
        cursor: pointer;
    }

    .filter-form .btn-primary:hover {
        background: var(--dark-green);
    }

    .filter-label {
        display: flex;
        align-items: center;
        gap: 5px;
        color: var(--dark-gray);
        font-weight: 500;
        font-size: 13px;
        white-space: nowrap;
    }

    .filter-label i {
        color: var(--primary-green);
        font-size: 14px;
    }

    /* Table Styles - Beautiful Design */
    .table-container {
        background: var(--pure-white);
        border-radius: 10px;
        padding: 15px;
        box-shadow: var(--shadow);
        margin-bottom: 20px;
        overflow-x: auto;
    }

    .table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 0;
    }

    .table thead tr {
        background: linear-gradient(135deg, var(--charcoal) 0%, var(--black) 100%);
    }

    .table thead th {
        padding: 12px 10px;
        color: var(--pure-white);
        font-weight: 500;
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        border: none;
        white-space: nowrap;
    }

    .table thead th:first-child {
        border-radius: 6px 0 0 6px;
    }

    .table thead th:last-child {
        border-radius: 0 6px 6px 0;
    }

    .table tbody tr {
        border-bottom: 1px solid var(--light-gray);
        transition: background-color 0.2s ease;
    }

    .table tbody tr:hover {
        background-color: rgba(46, 204, 113, 0.05);
    }

    .table tbody td {
        padding: 12px 10px;
        color: var(--dark-gray);
        font-size: 13px;
        vertical-align: middle;
    }

    .table tbody td h5 {
        margin: 0;
        font-size: 13px;
        font-weight: 500;
        color: var(--black);
    }

    /* HOD Badge */
    .hod-badge {
        background: var(--primary-green);
        color: white;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 10px;
        font-weight: 600;
        text-transform: uppercase;
        margin-left: 5px;
        display: inline-block;
    }

    /* Action Dropdown - Fixed Version */
    .action-dropdown {
        position: relative;
        display: inline-block;
    }

    .action-toggle {
        background: var(--light-gray);
        border: none;
        padding: 8px 12px;
        border-radius: 6px;
        color: var(--dark-gray);
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 5px;
        font-size: 12px;
        transition: all 0.2s ease;
    }

    .action-toggle:hover {
        background: var(--primary-green);
        color: white;
    }

    /* Invisible bridge between button and menu */
    .action-dropdown::after {
        content: '';
        position: absolute;
        top: 100%;
        right: 0;
        width: 100%;
        height: 10px;
        background: transparent;
        z-index: 999;
    }

    .action-menu {
        position: absolute;
        right: 0;
        top: 100%;
        background: var(--pure-white);
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        min-width: 200px;
        z-index: 1000;
        visibility: hidden;
        opacity: 0;
        margin-top: 0;
        border: 1px solid var(--light-gray);
        transition: all 0.2s ease;
        transform: translateY(-10px);
        pointer-events: none;
    }

    /* Show menu on hover */
    .action-dropdown:hover .action-menu {
        visibility: visible;
        opacity: 1;
        transform: translateY(5px);
        pointer-events: all;
    }

    /* Also show when hovering menu itself */
    .action-menu:hover {
        visibility: visible;
        opacity: 1;
        transform: translateY(5px);
        pointer-events: all;
    }

    /* Keep menu open when it's active (for click interaction) */
    .action-dropdown.action-open .action-menu {
        visibility: visible;
        opacity: 1;
        transform: translateY(5px);
        pointer-events: all;
    }

    .action-menu-item {
        padding: 12px 15px;
        display: flex;
        align-items: center;
        gap: 12px;
        color: var(--dark-gray);
        text-decoration: none;
        font-size: 13px;
        transition: all 0.2s ease;
        border-bottom: 1px solid var(--light-gray);
        cursor: pointer;
        white-space: nowrap;
    }

    .action-menu-item:last-child {
        border-bottom: none;
    }

    .action-menu-item:hover {
        background: rgba(46, 204, 113, 0.1);
        color: var(--primary-green);
        text-decoration: none;
    }

    .action-menu-item i {
        width: 18px;
        font-size: 14px;
        text-align: center;
    }

    /* Status Badges */
    .status-badge {
        padding: 4px 8px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.2px;
        display: inline-block;
    }

    .status-active {
        background: var(--light-green);
        color: var(--dark-green);
    }

    .status-inactive {
        background: #f8d7da;
        color: #721c24;
    }

    /* Modal Styles - Beautiful Design */
    .modal-content-custom {
        border-radius: 12px;
        border: none;
        box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        overflow: hidden;
    }

    .modal-header-custom {
        background: linear-gradient(135deg, var(--charcoal) 0%, var(--black) 100%);
        color: var(--pure-white);
        padding: 15px 20px;
        border-bottom: none;
        position: relative;
    }

    .modal-header-custom h4, .modal-header-custom h5 {
        margin: 0;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .modal-header-custom h4 i, .modal-header-custom h5 i {
        color: var(--primary-green);
        font-size: 20px;
    }

    .modal-header-custom .close {
        color: var(--pure-white);
        opacity: 0.8;
        text-shadow: none;
    }

    .modal-header-custom .close:hover {
        opacity: 1;
    }

    .modal-body-custom {
        padding: 20px;
        background: var(--soft-white);
    }

    .modal-footer-custom {
        padding: 15px 20px;
        background: var(--pure-white);
        border-top: 1px solid var(--light-gray);
    }

    /* Form Group Styles */
    .form-group-custom {
        margin-bottom: 15px;
    }

    .form-label {
        display: block;
        margin-bottom: 5px;
        color: var(--dark-gray);
        font-weight: 500;
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.2px;
    }

    .form-label i {
        color: var(--primary-green);
        margin-right: 5px;
    }

    .required-field::after {
        content: "*";
        color: #e74c3c;
        margin-left: 3px;
        font-weight: bold;
    }

    .input-group-custom {
        position: relative;
        display: flex;
        align-items: center;
    }

    .input-icon {
        position: absolute;
        left: 10px;
        color: var(--primary-green);
        font-size: 14px;
        z-index: 2;
    }

    .form-control-custom {
        width: 100%;
        padding: 8px 10px 8px 35px;
        font-size: 13px;
        color: var(--black);
        background: var(--pure-white);
        border: 1px solid var(--light-gray);
        border-radius: 6px;
        transition: all 0.2s ease;
        outline: none;
    }

    .form-control-custom:focus {
        border-color: var(--primary-green);
        box-shadow: 0 0 0 2px rgba(46, 204, 113, 0.1);
    }

    .form-control-custom[readonly] {
        background-color: var(--soft-white);
        cursor: not-allowed;
    }

    select.form-control-custom {
        padding: 8px 30px 8px 35px;
        appearance: none;
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 10px center;
        background-size: 14px;
    }

    /* Checkbox Group Styles */
    .checkbox-group {
        margin-top: 25px;
        padding: 15px;
        background: var(--pure-white);
        border-radius: 6px;
        border: 1px solid var(--light-gray);
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .checkbox-group input[type="checkbox"] {
        width: 18px;
        height: 18px;
        cursor: pointer;
        accent-color: var(--primary-green);
    }

    .checkbox-group label {
        color: var(--dark-gray);
        font-weight: 500;
        font-size: 14px;
        cursor: pointer;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 5px;
    }

    .checkbox-group label i {
        color: var(--primary-green);
        font-size: 16px;
    }

    /* Two Column Layout */
    .two-column-row {
        display: flex;
        flex-wrap: wrap;
        margin: 0 -10px;
    }

    .two-column-col {
        flex: 1;
        padding: 0 10px;
        min-width: 250px;
    }

    /* Modal Buttons */
    .modal-btn {
        padding: 8px 20px;
        border-radius: 6px;
        font-weight: 500;
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        transition: all 0.2s ease;
        border: none;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .modal-btn-primary {
        background: var(--primary-green);
        color: var(--pure-white);
    }

    .modal-btn-primary:hover {
        background: var(--dark-green);
    }

    .modal-btn-default {
        background: var(--light-gray);
        color: var(--dark-gray);
    }

    .modal-btn-default:hover {
        background: var(--medium-gray);
    }

    .modal-btn-danger {
        background: var(--danger-color);
        color: white;
    }

    .modal-btn-danger:hover {
        background: #c82333;
    }

    .modal-btn-warning {
        background: var(--warning-color);
        color: var(--black);
    }

    .modal-btn-warning:hover {
        background: #e0a800;
    }

    /* Pagination */
    .pagination-container {
        text-align: center;
        margin-top: 20px;
    }

    .pagination {
        display: inline-flex;
        gap: 5px;
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .pagination li {
        display: inline;
    }

    .pagination li a {
        display: inline-block;
        padding: 8px 12px;
        background: var(--pure-white);
        border: 1px solid var(--light-gray);
        border-radius: 6px;
        color: var(--dark-gray);
        font-size: 13px;
        text-decoration: none;
        transition: all 0.2s ease;
    }

    .pagination li.active a {
        background: var(--primary-green);
        color: white;
        border-color: var(--primary-green);
    }

    .pagination li a:hover {
        background: var(--light-gray);
    }

    /* Responsive */
    @media (max-width: 992px) {
        .filter-section {
            overflow-x: auto;
        }
        
        .filter-form {
            min-width: 800px;
        }
    }

    @media (max-width: 768px) {
        .page-header {
            flex-direction: column;
            text-align: center;
        }
        
        .two-column-col {
            flex: 100%;
        }
    }
</style>

<div id="page-wrapper">
    <div class="form-container">
        <!-- Header Section -->
        <div class="page-header">
            <h4>
                <i class="fas fa-users-cog"></i>
                Manage User Departments
            </h4>
            <div class="action-buttons">
                <a href="#" data-toggle="modal" data-target="#Adding-Department" class="btn-custom btn-custom-success">
                    <i class="fas fa-plus-circle"></i>
                    Add Department
                </a>
                <a href="" class="btn-custom btn-custom-primary">
                    <i class="fas fa-sync-alt"></i>
                    Refresh
                </a>
            </div>
        </div>
        
        <!-- Alert Messages -->
        <?php include('include/flash_messages.php'); ?>
        
        <!-- Filter Section - All in One Line -->
        <div class="filter-section">
            <form class="filter-form" action="">
                <div class="filter-label">
                    <i class="fas fa-search"></i>
                    <span>Search</span>
                </div>
                <input type="text" class="form-control" id="input_search" placeholder="Department Name" name="search_string" 
                       value="<?php echo htmlspecialchars($search_string ?? ''); ?>" style="width: 180px;">
                
                <div class="filter-label" style="margin-left: 5px;">
                    <i class="fas fa-sort"></i>
                    <span>Order By</span>
                </div>
                <select name="filter_col" class="form-control" style="width: 120px;">
                    <?php
                    foreach ($filter_options as $option) {
                        $selected = ($filter_col === $option) ? "selected" : "";
                        echo '<option value="' . htmlspecialchars($option) . '" ' . $selected . '>' . htmlspecialchars($option) . '</option>';
                    }
                    ?>
                </select>

                <select name="order_by" class="form-control" id="input_order" style="width: 100px;">
                    <option value="Asc" <?php echo ($order_by == 'Asc') ? "selected" : ""; ?>>Asc</option>
                    <option value="Desc" <?php echo ($order_by == 'Desc') ? "selected" : ""; ?>>Desc</option>
                </select>
                
                <button type="submit" class="btn-primary">
                    <i class="fas fa-filter"></i> Go
                </button>
            </form>
        </div>
        
        <!-- Table Section -->
        <div class="table-container">
            <table class="table">
                <thead>
                    <tr>
                        <th>Department Name</th>
                        <th>Comment</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($result)): ?>
                        <?php foreach ($result as $row): ?>
                            <tr>
                                <td><h5><?php echo htmlspecialchars($row['Name'] ?? ''); ?></h5></td>
                                <td><h5><?php echo htmlspecialchars($row['Comment'] ?? ''); ?></h5></td>
                                <td>
                                    <div class="action-dropdown">
                                        <button class="action-toggle">
                                            <i class="fas fa-cog"></i> Actions <i class="fas fa-chevron-down"></i>
                                        </button>
                                        <div class="action-menu">
                                            <a href="#" data-toggle="modal" data-target="#Update-Department-<?php echo $row['id']; ?>" class="action-menu-item">
                                                <i class="fas fa-edit" style="color: var(--primary-green);"></i>
                                                Update Record
                                            </a>
                                            
                                            <a href="#" data-toggle="modal" data-target="#confirm-delete-<?php echo $row['id']; ?>" class="action-menu-item">
                                                <i class="fas fa-trash-alt" style="color: var(--danger-color);"></i>
                                                Delete Record
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>

                            <!-- Update Modal - Beautiful Design -->
                            <div class="modal fade" id="Update-Department-<?php echo $row['id']; ?>" tabindex="-1" role="dialog">
                                <div class="modal-dialog modal-lg" role="document">
                                    <div class="modal-content modal-content-custom">
                                        <div class="modal-header modal-header-custom">
                                            <h5 class="modal-title">
                                                <i class="fas fa-user-edit"></i>
                                                Update Department
                                            </h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <form action="" method="POST">
                                            <div class="modal-body modal-body-custom">
                                                <?php
                                                $Dept = $row['id'];
                                                $queryv = "SELECT * FROM `Department` WHERE id = '$Dept'";
                                                $resultv = mysqli_query($conn, $queryv);
                                                
                                                if ($resultv && mysqli_num_rows($resultv) > 0) {
                                                    $rowsv = mysqli_fetch_array($resultv);
                                                } else {
                                                    $rowsv = array();
                                                }
                                                ?>
                                                
                                                <input type="hidden" name="DPTID" value="<?php echo htmlspecialchars($rowsv['id'] ?? ''); ?>">
                                                
                                                <div class="form-group-custom">
                                                    <label class="form-label required-field">
                                                        <i class="fas fa-user"></i> Department Name
                                                    </label>
                                                    <div class="input-group-custom">
                                                        <i class="fas fa-user input-icon"></i>
                                                        <input type="text" name="Dept_Name" value="<?php echo htmlspecialchars($rowsv['Name'] ?? ''); ?>" class="form-control-custom" required>
                                                    </div>
                                                </div>
                                                
                                                <div class="form-group-custom">
                                                    <label class="form-label required-field">
                                                        <i class="fas fa-lock"></i> Additional Comment
                                                    </label>
                                                    <div class="input-group-custom">
                                                        <i class="fas fa-lock input-icon"></i>
                                                        <input type="text" name="Comment" value="<?php echo htmlspecialchars($rowsv['Comment'] ?? ''); ?>" class="form-control-custom">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="modal-footer modal-footer-custom">
                                                <button type="button" class="modal-btn modal-btn-default" data-dismiss="modal">
                                                    <i class="fas fa-times"></i> Cancel
                                                </button>
                                                <button type="submit" name="Update_Department" class="modal-btn modal-btn-primary">
                                                    <i class="fas fa-save"></i> Save Changes
                                                </button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <!-- Delete Confirmation Modal -->
                            <div class="modal fade" id="confirm-delete-<?php echo $row['id']; ?>" tabindex="-1" role="dialog">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content modal-content-custom">
                                        <div class="modal-header modal-header-custom">
                                            <h5 class="modal-title">
                                                <i class="fas fa-exclamation-triangle" style="color: var(--danger-color);"></i>
                                                Confirm Deletion
                                            </h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <form action="" method="POST">
                                            <div class="modal-body modal-body-custom">
                                                <input type="hidden" name="DPTID" value="<?php echo $row['id']; ?>">
                                                <div style="text-align: center; padding: 20px;">
                                                    <i class="fas fa-user-slash" style="font-size: 48px; color: var(--danger-color); margin-bottom: 15px;"></i>
                                                    <p style="font-size: 16px; color: var(--dark-gray);">
                                                        Are you sure you want to delete the Department <strong><?php echo htmlspecialchars($row['Name']); ?></strong>?
                                                    </p>
                                                    <p style="font-size: 13px; color: #999;">This action cannot be undone.</p>
                                                </div>
                                            </div>
                                            <div class="modal-footer modal-footer-custom">
                                                <button type="button" class="modal-btn modal-btn-default" data-dismiss="modal">
                                                    <i class="fas fa-times"></i> Cancel
                                                </button>
                                                <button type="submit" name="Delete_Department" class="modal-btn modal-btn-danger">
                                                    <i class="fas fa-trash-alt"></i> Delete
                                                </button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="3" style="text-align: center; padding: 30px;">
                                <i class="fas fa-users" style="font-size: 48px; color: var(--light-gray); margin-bottom: 15px; display: block;"></i>
                                <h5 style="color: var(--dark-gray);">No Departments found</h5>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Add Department Modal -->
        <div class="modal fade" id="Adding-Department" tabindex="-1" role="dialog">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content modal-content-custom">
                    <div class="modal-header modal-header-custom">
                        <h5 class="modal-title">
                            <i class="fas fa-user-plus"></i>
                            Add New Department
                        </h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="" method="post" id="contact_form" enctype="multipart/form-data">
                        <div class="modal-body modal-body-custom">
                            <div class="form-group-custom">
                                <label class="form-label required-field">
                                    <i class="fas fa-user"></i> Department Name
                                </label>
                                <div class="input-group-custom">
                                    <i class="fas fa-user input-icon"></i>
                                    <input type="text" name="Dept_Name" placeholder="Enter Department name" class="form-control-custom" required>
                                </div>
                            </div>
                            
                            <div class="form-group-custom">
                                <label class="form-label">
                                    <i class="fas fa-lock"></i> Additional Comment
                                </label>
                                <div class="input-group-custom">
                                    <i class="fas fa-lock input-icon"></i>
                                    <input type="text" name="Comment" placeholder="Optional Comment" class="form-control-custom">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer modal-footer-custom">
                            <button type="button" class="modal-btn modal-btn-default" data-dismiss="modal">
                                <i class="fas fa-times"></i> Cancel
                            </button>
                            <button type="submit" name="Add_Department" class="modal-btn modal-btn-primary">
                                <i class="fas fa-user-plus"></i> Add Department
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Pagination -->
        <?php if (isset($total_pages) && $total_pages > 1): ?>
            <div class="pagination-container">
                <?php
                // Build query string
                $query_params = $_GET;
                unset($query_params['page']);
                $query_string = !empty($query_params) ? '&' . http_build_query($query_params) : '';
                ?>
                <ul class="pagination">
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="<?php echo ($page == $i) ? 'active' : ''; ?>">
                            <a href="?page=<?php echo $i . $query_string; ?>"><?php echo $i; ?></a>
                        </li>
                    <?php endfor; ?>
                </ul>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Add jQuery Validation -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js"></script>

<script>
$(document).ready(function() {
    // Initialize Bootstrap Select
    $('.selectpicker').selectpicker({
        size: 5,
        dropupAuto: false
    });
    
    // Form validation for add user
    $("#contact_form").validate({
        rules: {
            Dept_Name: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            Dept_Name: {
                required: "Department name is required",
                minlength: "Minimum 2 characters"
            }
        },
        errorElement: "span",
        errorClass: "help-block",
        highlight: function(element) {
            $(element).closest('.form-group-custom').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group-custom').removeClass('has-error');
        },
        errorPlacement: function(error, element) {
            error.insertAfter(element.closest('.input-group-custom'));
        },
        submitHandler: function(form) {
            $('.modal-btn-primary').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Saving...');
            form.submit();
        }
    });

    // Action dropdown click handling for better mobile support
    $('.action-toggle').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        
        var $dropdown = $(this).closest('.action-dropdown');
        
        // Close all other dropdowns
        $('.action-dropdown').not($dropdown).removeClass('action-open');
        
        // Toggle current dropdown
        $dropdown.toggleClass('action-open');
    });

    // Close dropdown when clicking outside
    $(document).on('click', function(e) {
        if (!$(e.target).closest('.action-dropdown').length) {
            $('.action-dropdown').removeClass('action-open');
        }
    });

    // Prevent dropdown from closing when clicking inside menu
    $('.action-menu').on('click', function(e) {
        e.stopPropagation();
    });
});
</script>

<?php 
include_once 'include/footer.php'; 
?>