Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/location-management.php

<?php
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit();
}

// Check permission for location management (admin or superuser only)
if (!checkPermission('admin') && $_SESSION['user_level'] !== 'superuser') {
    header('Location: ' . $_SESSION['user_level'] . '.php?error=access_denied');
    exit();
}

$page_title = 'Location Management';
$page_description = 'Manage areas, districts, and assemblies';

$success_message = '';
$error_message = '';

// Handle form submissions
if ($_POST) {
    // Handle Area operations
    if (isset($_POST['create_area'])) {
        $name = sanitizeInput($_POST['area_name']);
        
        if (empty($name)) {
            $error_message = 'Area name is required.';
        } else {
            $check_query = "SELECT id FROM areas WHERE name = :name";
            $check_stmt = $db->prepare($check_query);
            $check_stmt->bindParam(':name', $name);
            $check_stmt->execute();
            
            if ($check_stmt->rowCount() > 0) {
                $error_message = 'Area name already exists.';
            } else {
                $insert_query = "INSERT INTO areas (name) VALUES (:name)";
                $insert_stmt = $db->prepare($insert_query);
                $insert_stmt->bindParam(':name', $name);
                
                if ($insert_stmt->execute()) {
                    $success_message = 'Area created successfully.';
                    logAudit('CREATE', 'areas', $db->lastInsertId(), null, $_POST);
                } else {
                    $error_message = 'Failed to create area.';
                }
            }
        }
    } elseif (isset($_POST['edit_area'])) {
        $area_id = (int)$_POST['area_id'];
        $name = sanitizeInput($_POST['area_name']);
        
        if (empty($name)) {
            $error_message = 'Area name is required.';
        } else {
            $update_query = "UPDATE areas SET name = :name WHERE id = :id";
            $update_stmt = $db->prepare($update_query);
            $update_stmt->bindParam(':name', $name);
            $update_stmt->bindParam(':id', $area_id);
            
            if ($update_stmt->execute()) {
                $success_message = 'Area updated successfully.';
                logAudit('UPDATE', 'areas', $area_id, null, $_POST);
            } else {
                $error_message = 'Failed to update area.';
            }
        }
    } elseif (isset($_POST['delete_area'])) {
        $area_id = (int)$_POST['area_id'];
        
        // Check if area has districts
        $check_query = "SELECT COUNT(*) as count FROM districts WHERE area_id = :area_id";
        $check_stmt = $db->prepare($check_query);
        $check_stmt->bindParam(':area_id', $area_id);
        $check_stmt->execute();
        $result = $check_stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result['count'] > 0) {
            $error_message = 'Cannot delete area. It contains districts.';
        } else {
            $delete_query = "DELETE FROM areas WHERE id = :id";
            $delete_stmt = $db->prepare($delete_query);
            $delete_stmt->bindParam(':id', $area_id);
            
            if ($delete_stmt->execute()) {
                $success_message = 'Area deleted successfully.';
                logAudit('DELETE', 'areas', $area_id, null, null);
            } else {
                $error_message = 'Failed to delete area.';
            }
        }
    }
    
    // Handle District operations
    elseif (isset($_POST['create_district'])) {
        $name = sanitizeInput($_POST['district_name']);
        $area_id = (int)$_POST['area_id'];
        
        if (empty($name) || empty($area_id)) {
            $error_message = 'District name and area are required.';
        } else {
            $insert_query = "INSERT INTO districts (name, area_id) VALUES (:name, :area_id)";
            $insert_stmt = $db->prepare($insert_query);
            $insert_stmt->bindParam(':name', $name);
            $insert_stmt->bindParam(':area_id', $area_id);
            
            if ($insert_stmt->execute()) {
                $success_message = 'District created successfully.';
                logAudit('CREATE', 'districts', $db->lastInsertId(), null, $_POST);
            } else {
                $error_message = 'Failed to create district.';
            }
        }
    } elseif (isset($_POST['edit_district'])) {
        $district_id = (int)$_POST['district_id'];
        $name = sanitizeInput($_POST['district_name']);
        $area_id = (int)$_POST['area_id'];
        
        if (empty($name) || empty($area_id)) {
            $error_message = 'District name and area are required.';
        } else {
            $update_query = "UPDATE districts SET name = :name, area_id = :area_id WHERE id = :id";
            $update_stmt = $db->prepare($update_query);
            $update_stmt->bindParam(':name', $name);
            $update_stmt->bindParam(':area_id', $area_id);
            $update_stmt->bindParam(':id', $district_id);
            
            if ($update_stmt->execute()) {
                $success_message = 'District updated successfully.';
                logAudit('UPDATE', 'districts', $district_id, null, $_POST);
            } else {
                $error_message = 'Failed to update district.';
            }
        }
    } elseif (isset($_POST['delete_district'])) {
        $district_id = (int)$_POST['district_id'];
        
        // Check if district has assemblies
        $check_query = "SELECT COUNT(*) as count FROM assemblies WHERE district_id = :district_id";
        $check_stmt = $db->prepare($check_query);
        $check_stmt->bindParam(':district_id', $district_id);
        $check_stmt->execute();
        $result = $check_stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result['count'] > 0) {
            $error_message = 'Cannot delete district. It contains assemblies.';
        } else {
            $delete_query = "DELETE FROM districts WHERE id = :id";
            $delete_stmt = $db->prepare($delete_query);
            $delete_stmt->bindParam(':id', $district_id);
            
            if ($delete_stmt->execute()) {
                $success_message = 'District deleted successfully.';
                logAudit('DELETE', 'districts', $district_id, null, null);
            } else {
                $error_message = 'Failed to delete district.';
            }
        }
    }
    
    // Handle Assembly operations
    elseif (isset($_POST['create_assembly'])) {
        $name = sanitizeInput($_POST['assembly_name']);
        $district_id = (int)$_POST['district_id'];
        
        if (empty($name) || empty($district_id)) {
            $error_message = 'Assembly name and district are required.';
        } else {
            $insert_query = "INSERT INTO assemblies (name, district_id) VALUES (:name, :district_id)";
            $insert_stmt = $db->prepare($insert_query);
            $insert_stmt->bindParam(':name', $name);
            $insert_stmt->bindParam(':district_id', $district_id);
            
            if ($insert_stmt->execute()) {
                $success_message = 'Assembly created successfully.';
                logAudit('CREATE', 'assemblies', $db->lastInsertId(), null, $_POST);
            } else {
                $error_message = 'Failed to create assembly.';
            }
        }
    } elseif (isset($_POST['edit_assembly'])) {
        $assembly_id = (int)$_POST['assembly_id'];
        $name = sanitizeInput($_POST['assembly_name']);
        $district_id = (int)$_POST['district_id'];
        
        if (empty($name) || empty($district_id)) {
            $error_message = 'Assembly name and district are required.';
        } else {
            $update_query = "UPDATE assemblies SET name = :name, district_id = :district_id WHERE id = :id";
            $update_stmt = $db->prepare($update_query);
            $update_stmt->bindParam(':name', $name);
            $update_stmt->bindParam(':district_id', $district_id);
            $update_stmt->bindParam(':id', $assembly_id);
            
            if ($update_stmt->execute()) {
                $success_message = 'Assembly updated successfully.';
                logAudit('UPDATE', 'assemblies', $assembly_id, null, $_POST);
            } else {
                $error_message = 'Failed to update assembly.';
            }
        }
    } elseif (isset($_POST['delete_assembly'])) {
        $assembly_id = (int)$_POST['assembly_id'];
        
        $delete_query = "DELETE FROM assemblies WHERE id = :id";
        $delete_stmt = $db->prepare($delete_query);
        $delete_stmt->bindParam(':id', $assembly_id);
        
        if ($delete_stmt->execute()) {
            $success_message = 'Assembly deleted successfully.';
            logAudit('DELETE', 'assemblies', $assembly_id, null, null);
        } else {
            $error_message = 'Failed to delete assembly.';
        }
    }
}

// Get all areas with counts
$areas_query = "SELECT a.*, 
                COUNT(DISTINCT d.id) as district_count,
                COUNT(DISTINCT ass.id) as assembly_count,
                'System' as first_name, 
                'Admin' as last_name
                FROM areas a 
                LEFT JOIN districts d ON a.id = d.area_id
                LEFT JOIN assemblies ass ON d.id = ass.district_id
                GROUP BY a.id
                ORDER BY a.name";
$areas_stmt = $db->prepare($areas_query);
$areas_stmt->execute();
$areas = $areas_stmt->fetchAll(PDO::FETCH_ASSOC);

// Get all districts with area names
$districts_query = "SELECT d.*, a.name as area_name, 
                    COUNT(ass.id) as assembly_count,
                    'System' as first_name, 
                    'Admin' as last_name
                    FROM districts d 
                    JOIN areas a ON d.area_id = a.id
                    LEFT JOIN assemblies ass ON d.id = ass.district_id
                    GROUP BY d.id
                    ORDER BY a.name, d.name";
$districts_stmt = $db->prepare($districts_query);
$districts_stmt->execute();
$districts = $districts_stmt->fetchAll(PDO::FETCH_ASSOC);

// Get all assemblies with district and area names
$assemblies_query = "SELECT ass.*, d.name as district_name, a.name as area_name,
                     'System' as first_name, 
                     'Admin' as last_name
                     FROM assemblies ass 
                     JOIN districts d ON ass.district_id = d.id
                     JOIN areas a ON d.area_id = a.id
                     ORDER BY a.name, d.name, ass.name";
$assemblies_stmt = $db->prepare($assemblies_query);
$assemblies_stmt->execute();
$assemblies = $assemblies_stmt->fetchAll(PDO::FETCH_ASSOC);

include __DIR__ . '/../includes/header.php';
?>

<?php if ($success_message): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6 alert-auto-hide">
    <div class="flex items-center">
        <i class="fas fa-check-circle mr-2"></i>
        <span><?php echo htmlspecialchars($success_message); ?></span>
    </div>
</div>
<?php endif; ?>

<?php if ($error_message): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6 alert-auto-hide">
    <div class="flex items-center">
        <i class="fas fa-exclamation-circle mr-2"></i>
        <span><?php echo htmlspecialchars($error_message); ?></span>
    </div>
</div>
<?php endif; ?>

<!-- Tab Navigation -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 mb-6">
    <div class="border-b border-gray-200">
        <nav class="flex space-x-8 px-6" aria-label="Tabs">
            <button id="areasTab" class="tab-button border-b-2 border-cop-blue py-4 px-1 text-sm font-medium text-cop-blue" onclick="showTab('areas')">
                <i class="fas fa-globe-africa mr-2"></i>Areas
            </button>
            <button id="districtsTab" class="tab-button border-b-2 border-transparent py-4 px-1 text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300" onclick="showTab('districts')">
                <i class="fas fa-map-marked-alt mr-2"></i>Districts
            </button>
            <button id="assembliesTab" class="tab-button border-b-2 border-transparent py-4 px-1 text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300" onclick="showTab('assemblies')">
                <i class="fas fa-church mr-2"></i>Assemblies
            </button>
        </nav>
    </div>
</div>

<!-- Areas Tab -->
<div id="areasContent" class="tab-content">
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Areas List -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-semibold text-gray-800">Areas</h3>
                        <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">
                            <?php echo count($areas); ?> areas
                        </span>
                    </div>
                </div>
                
                <div class="overflow-x-auto">
                    <?php if (empty($areas)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-globe-africa text-4xl text-gray-400 mb-4"></i>
                        <h3 class="text-lg font-medium text-gray-600 mb-2">No areas found</h3>
                        <p class="text-gray-500">Create your first area using the form on the right.</p>
                    </div>
                    <?php else: ?>
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Area Name</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Districts</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Assemblies</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created By</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($areas as $area): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($area['name']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo $area['district_count']; ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo $area['assembly_count']; ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    <?php echo htmlspecialchars($area['first_name'] . ' ' . $area['last_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <div class="flex items-center space-x-2">
                                        <button onclick="editArea(<?php echo htmlspecialchars(json_encode($area)); ?>)" 
                                                class="text-blue-600 hover:text-blue-900" title="Edit Area">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <?php if ($area['district_count'] == 0): ?>
                                        <form method="POST" class="inline">
                                            <input type="hidden" name="area_id" value="<?php echo $area['id']; ?>">
                                            <button type="submit" name="delete_area"
                                                    onclick="return confirm('Are you sure you want to delete this area?')"
                                                    class="text-red-600 hover:text-red-900" title="Delete Area">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                        <?php else: ?>
                                        <span class="text-gray-400" title="Cannot delete area with districts">
                                            <i class="fas fa-trash"></i>
                                        </span>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Create Area Form -->
        <div>
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <h3 class="text-lg font-semibold text-gray-800">Create New Area</h3>
                    <p class="text-gray-600 text-sm">Add a new area to the system</p>
                </div>
                <div class="p-6">
                    <form method="POST" class="space-y-4">
                        <div>
                            <label for="area_name" class="block text-sm font-medium text-gray-700 mb-1">
                                Area Name <span class="text-red-500">*</span>
                            </label>
                            <input type="text" id="area_name" name="area_name" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        </div>
                        
                        <button type="submit" name="create_area"
                                class="w-full bg-cop-blue text-white py-2 px-4 rounded-lg hover:bg-cop-light-blue transition duration-200 text-sm">
                            <i class="fas fa-plus mr-2"></i>Create Area
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Districts Tab -->
<div id="districtsContent" class="tab-content hidden">
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Districts List -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-semibold text-gray-800">Districts</h3>
                        <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">
                            <?php echo count($districts); ?> districts
                        </span>
                    </div>
                </div>
                
                <div class="overflow-x-auto">
                    <?php if (empty($districts)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-map-marked-alt text-4xl text-gray-400 mb-4"></i>
                        <h3 class="text-lg font-medium text-gray-600 mb-2">No districts found</h3>
                        <p class="text-gray-500">Create your first district using the form on the right.</p>
                    </div>
                    <?php else: ?>
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">District Name</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Area</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Assemblies</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created By</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($districts as $district): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($district['name']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo htmlspecialchars($district['area_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo $district['assembly_count']; ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    <?php echo htmlspecialchars($district['first_name'] . ' ' . $district['last_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <div class="flex items-center space-x-2">
                                        <button onclick="editDistrict(<?php echo htmlspecialchars(json_encode($district)); ?>)" 
                                                class="text-blue-600 hover:text-blue-900" title="Edit District">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <?php if ($district['assembly_count'] == 0): ?>
                                        <form method="POST" class="inline">
                                            <input type="hidden" name="district_id" value="<?php echo $district['id']; ?>">
                                            <button type="submit" name="delete_district"
                                                    onclick="return confirm('Are you sure you want to delete this district?')"
                                                    class="text-red-600 hover:text-red-900" title="Delete District">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                        <?php else: ?>
                                        <span class="text-gray-400" title="Cannot delete district with assemblies">
                                            <i class="fas fa-trash"></i>
                                        </span>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Create District Form -->
        <div>
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <h3 class="text-lg font-semibold text-gray-800">Create New District</h3>
                    <p class="text-gray-600 text-sm">Add a new district to an area</p>
                </div>
                <div class="p-6">
                    <form method="POST" class="space-y-4">
                        <div>
                            <label for="district_name" class="block text-sm font-medium text-gray-700 mb-1">
                                District Name <span class="text-red-500">*</span>
                            </label>
                            <input type="text" id="district_name" name="district_name" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        </div>
                        
                        <div>
                            <label for="district_area_id" class="block text-sm font-medium text-gray-700 mb-1">
                                Area <span class="text-red-500">*</span>
                            </label>
                            <select id="district_area_id" name="area_id" required
                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                                <option value="">Select Area</option>
                                <?php foreach ($areas as $area): ?>
                                <option value="<?php echo $area['id']; ?>"><?php echo htmlspecialchars($area['name']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <button type="submit" name="create_district"
                                class="w-full bg-cop-blue text-white py-2 px-4 rounded-lg hover:bg-cop-light-blue transition duration-200 text-sm">
                            <i class="fas fa-plus mr-2"></i>Create District
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Assemblies Tab -->
<div id="assembliesContent" class="tab-content hidden">
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Assemblies List -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-semibold text-gray-800">Assemblies</h3>
                        <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">
                            <?php echo count($assemblies); ?> assemblies
                        </span>
                    </div>
                </div>
                
                <div class="overflow-x-auto">
                    <?php if (empty($assemblies)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-church text-4xl text-gray-400 mb-4"></i>
                        <h3 class="text-lg font-medium text-gray-600 mb-2">No assemblies found</h3>
                        <p class="text-gray-500">Create your first assembly using the form on the right.</p>
                    </div>
                    <?php else: ?>
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Assembly Name</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">District</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Area</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created By</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($assemblies as $assembly): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($assembly['name']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo htmlspecialchars($assembly['district_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <?php echo htmlspecialchars($assembly['area_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    <?php echo htmlspecialchars($assembly['first_name'] . ' ' . $assembly['last_name']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <div class="flex items-center space-x-2">
                                        <button onclick="editAssembly(<?php echo htmlspecialchars(json_encode($assembly)); ?>)" 
                                                class="text-blue-600 hover:text-blue-900" title="Edit Assembly">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <form method="POST" class="inline">
                                            <input type="hidden" name="assembly_id" value="<?php echo $assembly['id']; ?>">
                                            <button type="submit" name="delete_assembly"
                                                    onclick="return confirm('Are you sure you want to delete this assembly?')"
                                                    class="text-red-600 hover:text-red-900" title="Delete Assembly">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Create Assembly Form -->
        <div>
            <div class="bg-white rounded-lg shadow-sm">
                <div class="p-6 border-b border-gray-200">
                    <h3 class="text-lg font-semibold text-gray-800">Create New Assembly</h3>
                    <p class="text-gray-600 text-sm">Add a new assembly to a district</p>
                </div>
                <div class="p-6">
                    <form method="POST" class="space-y-4">
                        <div>
                            <label for="assembly_name" class="block text-sm font-medium text-gray-700 mb-1">
                                Assembly Name <span class="text-red-500">*</span>
                            </label>
                            <input type="text" id="assembly_name" name="assembly_name" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        </div>
                        
                        <div>
                            <label for="assembly_area_id" class="block text-sm font-medium text-gray-700 mb-1">
                                Area <span class="text-red-500">*</span>
                            </label>
                            <select id="assembly_area_id" onchange="loadDistrictsForAssembly()" required
                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                                <option value="">Select Area</option>
                                <?php foreach ($areas as $area): ?>
                                <option value="<?php echo $area['id']; ?>"><?php echo htmlspecialchars($area['name']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div>
                            <label for="assembly_district_id" class="block text-sm font-medium text-gray-700 mb-1">
                                District <span class="text-red-500">*</span>
                            </label>
                            <select id="assembly_district_id" name="district_id" required
                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                                <option value="">Select District</option>
                            </select>
                        </div>
                        
                        <button type="submit" name="create_assembly"
                                class="w-full bg-cop-blue text-white py-2 px-4 rounded-lg hover:bg-cop-light-blue transition duration-200 text-sm">
                            <i class="fas fa-plus mr-2"></i>Create Assembly
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Edit Area Modal -->
<div id="editAreaModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden">
    <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
        <div class="mt-3">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-medium text-gray-900">Edit Area</h3>
                <button onclick="closeEditAreaModal()" class="text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form method="POST" id="editAreaForm" class="space-y-4">
                <input type="hidden" name="area_id" id="edit_area_id">
                
                <div>
                    <label for="edit_area_name" class="block text-sm font-medium text-gray-700 mb-1">Area Name</label>
                    <input type="text" id="edit_area_name" name="area_name" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                </div>
                
                <div class="flex justify-end space-x-3 pt-4">
                    <button type="button" onclick="closeEditAreaModal()" 
                            class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition duration-200">
                        Cancel
                    </button>
                    <button type="submit" name="edit_area"
                            class="px-4 py-2 bg-cop-blue text-white rounded-lg hover:bg-cop-light-blue transition duration-200">
                        Update Area
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit District Modal -->
<div id="editDistrictModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden">
    <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
        <div class="mt-3">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-medium text-gray-900">Edit District</h3>
                <button onclick="closeEditDistrictModal()" class="text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form method="POST" id="editDistrictForm" class="space-y-4">
                <input type="hidden" name="district_id" id="edit_district_id">
                
                <div>
                    <label for="edit_district_name" class="block text-sm font-medium text-gray-700 mb-1">District Name</label>
                    <input type="text" id="edit_district_name" name="district_name" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                </div>
                
                <div>
                    <label for="edit_district_area_id" class="block text-sm font-medium text-gray-700 mb-1">Area</label>
                    <select id="edit_district_area_id" name="area_id" required
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        <option value="">Select Area</option>
                        <?php foreach ($areas as $area): ?>
                        <option value="<?php echo $area['id']; ?>"><?php echo htmlspecialchars($area['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="flex justify-end space-x-3 pt-4">
                    <button type="button" onclick="closeEditDistrictModal()" 
                            class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition duration-200">
                        Cancel
                    </button>
                    <button type="submit" name="edit_district"
                            class="px-4 py-2 bg-cop-blue text-white rounded-lg hover:bg-cop-light-blue transition duration-200">
                        Update District
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Assembly Modal -->
<div id="editAssemblyModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden">
    <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
        <div class="mt-3">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-medium text-gray-900">Edit Assembly</h3>
                <button onclick="closeEditAssemblyModal()" class="text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form method="POST" id="editAssemblyForm" class="space-y-4">
                <input type="hidden" name="assembly_id" id="edit_assembly_id">
                
                <div>
                    <label for="edit_assembly_name" class="block text-sm font-medium text-gray-700 mb-1">Assembly Name</label>
                    <input type="text" id="edit_assembly_name" name="assembly_name" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                </div>
                
                <div>
                    <label for="edit_assembly_area_id" class="block text-sm font-medium text-gray-700 mb-1">Area</label>
                    <select id="edit_assembly_area_id" onchange="loadDistrictsForEdit()" required
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        <option value="">Select Area</option>
                        <?php foreach ($areas as $area): ?>
                        <option value="<?php echo $area['id']; ?>"><?php echo htmlspecialchars($area['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div>
                    <label for="edit_assembly_district_id" class="block text-sm font-medium text-gray-700 mb-1">District</label>
                    <select id="edit_assembly_district_id" name="district_id" required
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent text-sm">
                        <option value="">Select District</option>
                    </select>
                </div>
                
                <div class="flex justify-end space-x-3 pt-4">
                    <button type="button" onclick="closeEditAssemblyModal()" 
                            class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition duration-200">
                        Cancel
                    </button>
                    <button type="submit" name="edit_assembly"
                            class="px-4 py-2 bg-cop-blue text-white rounded-lg hover:bg-cop-light-blue transition duration-200">
                        Update Assembly
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Tab switching functionality
function showTab(tabName) {
    // Hide all tab contents
    document.querySelectorAll('.tab-content').forEach(content => {
        content.classList.add('hidden');
    });
    
    // Remove active class from all tab buttons
    document.querySelectorAll('.tab-button').forEach(button => {
        button.classList.remove('border-cop-blue', 'text-cop-blue');
        button.classList.add('border-transparent', 'text-gray-500');
    });
    
    // Show selected tab content
    document.getElementById(tabName + 'Content').classList.remove('hidden');
    
    // Add active class to selected tab button
    const activeButton = document.getElementById(tabName + 'Tab');
    activeButton.classList.remove('border-transparent', 'text-gray-500');
    activeButton.classList.add('border-cop-blue', 'text-cop-blue');
}

// Area edit functions
function editArea(area) {
    document.getElementById('edit_area_id').value = area.id;
    document.getElementById('edit_area_name').value = area.name;
    document.getElementById('editAreaModal').classList.remove('hidden');
}

function closeEditAreaModal() {
    document.getElementById('editAreaModal').classList.add('hidden');
}

// District edit functions
function editDistrict(district) {
    document.getElementById('edit_district_id').value = district.id;
    document.getElementById('edit_district_name').value = district.name;
    document.getElementById('edit_district_area_id').value = district.area_id;
    document.getElementById('editDistrictModal').classList.remove('hidden');
}

function closeEditDistrictModal() {
    document.getElementById('editDistrictModal').classList.add('hidden');
}

// Assembly edit functions
function editAssembly(assembly) {
    document.getElementById('edit_assembly_id').value = assembly.id;
    document.getElementById('edit_assembly_name').value = assembly.name;
    document.getElementById('edit_assembly_area_id').value = assembly.area_id;
    
    // Load districts for the selected area
    loadDistrictsForEdit().then(() => {
        document.getElementById('edit_assembly_district_id').value = assembly.district_id;
    });
    
    document.getElementById('editAssemblyModal').classList.remove('hidden');
}

function closeEditAssemblyModal() {
    document.getElementById('editAssemblyModal').classList.add('hidden');
}

// Load districts for assembly creation
function loadDistrictsForAssembly() {
    const areaId = document.getElementById('assembly_area_id').value;
    const districtSelect = document.getElementById('assembly_district_id');
    
    districtSelect.innerHTML = '<option value="">Select District</option>';
    
    if (areaId) {
        const districts = <?php echo json_encode($districts); ?>;
        districts.forEach(district => {
            if (district.area_id == areaId) {
                const option = document.createElement('option');
                option.value = district.id;
                option.textContent = district.name;
                districtSelect.appendChild(option);
            }
        });
    }
}

// Load districts for assembly editing
function loadDistrictsForEdit() {
    return new Promise((resolve) => {
        const areaId = document.getElementById('edit_assembly_area_id').value;
        const districtSelect = document.getElementById('edit_assembly_district_id');
        
        districtSelect.innerHTML = '<option value="">Select District</option>';
        
        if (areaId) {
            const districts = <?php echo json_encode($districts); ?>;
            districts.forEach(district => {
                if (district.area_id == areaId) {
                    const option = document.createElement('option');
                    option.value = district.id;
                    option.textContent = district.name;
                    districtSelect.appendChild(option);
                }
            });
        }
        resolve();
    });
}

// Close modals when clicking outside
document.addEventListener('click', function(e) {
    if (e.target.id === 'editAreaModal') closeEditAreaModal();
    if (e.target.id === 'editDistrictModal') closeEditDistrictModal();
    if (e.target.id === 'editAssemblyModal') closeEditAssemblyModal();
});
</script>

<?php include __DIR__ . '/../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists