Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/administration/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/administration/assemblies.php

<?php
require_once '../../config/config.php';
checkLogin();
require_once '../../classes/AuditLog.php';

// Assembly management requires district level access or higher
checkAccess('assembly');

$pageTitle = "Assemblies Management - " . APP_NAME;
$db = Database::getInstance()->getConnection();

$success = '';
$error = '';

// Handle PDF Export
if (isset($_GET['action']) && $_GET['action'] == 'export_pdf') {
    require_once '../../vendor/autoload.php';
    
    // Get all assemblies with area and district names
    $assembliesStmt = $db->query("
        SELECT asm.*, a.area_name, a.area_code, d.district_name, d.district_code,
               (SELECT COUNT(*) FROM members WHERE assembly_id = asm.id) as member_count
        FROM assemblies asm
        JOIN areas a ON asm.area_id = a.id
        JOIN districts d ON asm.district_id = d.id
        ORDER BY a.area_name, d.district_name, asm.assembly_name
    ");
    $assemblies = $assembliesStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Create PDF
    $pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Landscape for more columns
    
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor(APP_NAME);
    $pdf->SetTitle('Assemblies Reference List');
    $pdf->SetSubject('Complete list of assemblies with reference codes');
    
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetMargins(15, 15, 15);
    $pdf->SetAutoPageBreak(TRUE, 15);
    $pdf->AddPage();
    
    // Title
    $pdf->SetFont('helvetica', 'B', 16);
    $pdf->Cell(0, 10, APP_NAME, 0, 1, 'C');
    $pdf->SetFont('helvetica', 'B', 14);
    $pdf->Cell(0, 10, 'Assemblies Reference List', 0, 1, 'C');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->Cell(0, 5, 'Generated: ' . date('F d, Y - h:i A'), 0, 1, 'C');
    $pdf->Ln(5);
    
    // Summary
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 7, 'Total Assemblies: ' . count($assemblies), 0, 1);
    $pdf->Ln(3);
    
    // Table header
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->SetFillColor(30, 64, 175);
    $pdf->SetTextColor(255, 255, 255);
    
    $pdf->Cell(10, 7, '#', 1, 0, 'C', true);
    $pdf->Cell(25, 7, 'Area Code', 1, 0, 'C', true);
    $pdf->Cell(30, 7, 'District Code', 1, 0, 'C', true);
    $pdf->Cell(35, 7, 'Assembly Code', 1, 0, 'C', true);
    $pdf->Cell(70, 7, 'Assembly Name', 1, 0, 'C', true);
    $pdf->Cell(30, 7, 'Pastor Name', 1, 0, 'C', true);
    $pdf->Cell(20, 7, 'Members', 1, 0, 'C', true);
    $pdf->Cell(30, 7, 'City', 1, 0, 'C', true);
    $pdf->Cell(15, 7, 'Active', 1, 1, 'C', true);
    
    // Table content
    $pdf->SetFont('helvetica', '', 7);
    $pdf->SetTextColor(0, 0, 0);
    
    $num = 1;
    foreach ($assemblies as $assembly) {
        $fillColor = $assembly['is_active'] ? [240, 253, 244] : [254, 242, 242];
        $pdf->SetFillColor($fillColor[0], $fillColor[1], $fillColor[2]);
        
        $pdf->Cell(10, 6, $num++, 1, 0, 'C', true);
        $pdf->Cell(25, 6, $assembly['area_code'], 1, 0, 'C', true);
        $pdf->Cell(30, 6, $assembly['district_code'], 1, 0, 'C', true);
        $pdf->Cell(35, 6, $assembly['assembly_code'], 1, 0, 'C', true);
        $pdf->Cell(70, 6, substr($assembly['assembly_name'], 0, 40), 1, 0, 'L', true);
        $pdf->Cell(30, 6, substr($assembly['pastor_name'] ?? '-', 0, 18), 1, 0, 'L', true);
        $pdf->Cell(20, 6, $assembly['member_count'], 1, 0, 'C', true);
        $pdf->Cell(30, 6, substr($assembly['city'] ?? '-', 0, 18), 1, 0, 'L', true);
        $pdf->Cell(15, 6, $assembly['is_active'] ? 'Yes' : 'No', 1, 1, 'C', true);
    }

// Handle ACTIVATE/DEACTIVATE with cascading
if (isset($_GET['toggle_active']) && isset($_GET['id'])) {
    $assemblyId = (int)$_GET['id'];
    $targetStatus = (int)$_GET['toggle_active']; // 1 = activate, 0 = deactivate

    try {
        $db->beginTransaction();

        // Update assembly status
        $stmt = $db->prepare("UPDATE assemblies SET is_active = :status WHERE id = :id");
        $stmt->execute(['status' => $targetStatus, 'id' => $assemblyId]);

        if ($targetStatus === 0) {
            // Deactivate all members in this assembly
            $stmt = $db->prepare("UPDATE members SET is_active = 0 WHERE assembly_id = :assembly_id");
            $stmt->execute(['assembly_id' => $assemblyId]);
        }

        $db->commit();

        $auditLog = new AuditLog();
        $auditLog->log($_SESSION['user_id'], $targetStatus === 1 ? 'activate' : 'deactivate', 'assemblies', $assemblyId);

        $success = $targetStatus === 1
            ? "Assembly activated successfully!"
            : "Assembly and all related members deactivated successfully!";
    } catch (PDOException $e) {
        $db->rollBack();
        $error = "Error updating assembly status: " . $e->getMessage();
    }
}
    
    $filename = 'assemblies_reference_' . date('Ymd_His') . '.pdf';
    $pdf->Output($filename, 'D');
    exit;
}

// Handle Admin Assignment
if (isset($_POST['assign_admin'])) {
    try {
        $stmt = $db->prepare("
            INSERT IGNORE INTO admin_location_assignments 
            (user_id, location_type, assembly_id, assigned_by) 
            VALUES (:user_id, 'assembly', :assembly_id, :assigned_by)
        ");
        $stmt->execute([
            'user_id' => $_POST['user_id'],
            'assembly_id' => $_POST['location_id'],
            'assigned_by' => $_SESSION['user_id']
        ]);
        $success = "Admin assigned successfully!";
    } catch (PDOException $e) {
        $error = "Error assigning admin: " . $e->getMessage();
    }
}

// Handle Remove Assignment
if (isset($_GET['remove_assignment'])) {
    try {
        $stmt = $db->prepare("DELETE FROM admin_location_assignments WHERE id = :id");
        $stmt->execute(['id' => $_GET['remove_assignment']]);
        $success = "Assignment removed successfully!";
    } catch (PDOException $e) {
        $error = "Error removing assignment";
    }
}

// Handle DELETE
if (isset($_GET['delete'])) {
    try {
        $stmt = $db->prepare("DELETE FROM assemblies WHERE id = :id");
        $stmt->execute(['id' => $_GET['delete']]);
        $success = "Assembly deleted successfully!";
        
        $auditLog = new AuditLog();
        $auditLog->log($_SESSION['user_id'], 'delete', 'assemblies', $_GET['delete']);
    } catch (PDOException $e) {
        $error = "Cannot delete assembly: This assembly may have associated members.";
    }
}

// Handle UPDATE
if (isset($_POST['update_assembly'])) {
    try {
        $stmt = $db->prepare("
            UPDATE assemblies 
            SET area_id = :area_id, district_id = :district_id, assembly_name = :name, assembly_code = :code, 
                description = :description, pastor_name = :pastor, phone = :phone, email = :email, 
                address = :address, gps_address = :gps, city = :city
            WHERE id = :id
        ");
        
        $stmt->execute([
            'area_id' => $_POST['area_id'],
            'district_id' => $_POST['district_id'],
            'name' => $_POST['assembly_name'],
            'code' => strtoupper($_POST['assembly_code']),
            'description' => $_POST['description'] ?: null,
            'pastor' => $_POST['pastor_name'] ?: null,
            'phone' => $_POST['phone'] ?: null,
            'email' => $_POST['email'] ?: null,
            'address' => $_POST['address'] ?: null,
            'gps' => $_POST['gps_address'] ?: null,
            'city' => $_POST['city'] ?: null,
            'id' => $_POST['assembly_id']
        ]);
        
        $auditLog = new AuditLog();
        $auditLog->log($_SESSION['user_id'], 'update', 'assemblies', $_POST['assembly_id']);
        
        $success = "Assembly updated successfully!";
    } catch (PDOException $e) {
        $error = "Error: " . $e->getMessage();
    }
}

// Handle ADD
if (isset($_POST['add_assembly'])) {
    try {
        // Check if assembly_code already exists
        $checkStmt = $db->prepare("SELECT asm.id, asm.assembly_name, d.district_name, a.area_name 
                                    FROM assemblies asm 
                                    JOIN districts d ON asm.district_id = d.id 
                                    JOIN areas a ON asm.area_id = a.id 
                                    WHERE asm.assembly_code = :code");
        $checkStmt->execute(['code' => strtoupper($_POST['assembly_code'])]);
        $existing = $checkStmt->fetch();
        
        if ($existing) {
            $error = "⚠️ Assembly with code '" . htmlspecialchars(strtoupper($_POST['assembly_code'])) . "' already exists (Name: " . htmlspecialchars($existing['assembly_name']) . " in " . htmlspecialchars($existing['district_name']) . ", " . htmlspecialchars($existing['area_name']) . "). Please use a different code.";
        } else {
            // Check if assembly_name already exists in the same district
            $checkNameStmt = $db->prepare("SELECT asm.id, asm.assembly_code FROM assemblies asm WHERE asm.assembly_name = :name AND asm.district_id = :district_id");
            $checkNameStmt->execute(['name' => $_POST['assembly_name'], 'district_id' => $_POST['district_id']]);
            $existingName = $checkNameStmt->fetch();
            
            if ($existingName) {
                $error = "⚠️ Assembly with name '" . htmlspecialchars($_POST['assembly_name']) . "' already exists in this district (Code: " . htmlspecialchars($existingName['assembly_code']) . "). Please use a different name.";
            } else {
                $stmt = $db->prepare("
                    INSERT INTO assemblies (area_id, district_id, assembly_name, assembly_code, description, pastor_name, phone, email, address, gps_address, city)
                    VALUES (:area_id, :district_id, :name, :code, :description, :pastor, :phone, :email, :address, :gps, :city)
                ");
                
                $stmt->execute([
                    'area_id' => $_POST['area_id'],
                    'district_id' => $_POST['district_id'],
                    'name' => $_POST['assembly_name'],
                    'code' => strtoupper($_POST['assembly_code']),
                    'description' => $_POST['description'] ?: null,
                    'pastor' => $_POST['pastor_name'] ?: null,
                    'phone' => $_POST['phone'] ?: null,
                    'email' => $_POST['email'] ?: null,
                    'address' => $_POST['address'] ?: null,
                    'gps' => $_POST['gps_address'] ?: null,
                    'city' => $_POST['city'] ?: null
                ]);
                
                $auditLog = new AuditLog();
                $auditLog->log($_SESSION['user_id'], 'create', 'assemblies', $db->lastInsertId());
                
                $success = "✅ Assembly added successfully!";
            }
        }
    } catch (PDOException $e) {
        $error = "Error: " . $e->getMessage();
    }
}

// Get all assemblies with area and district names, plus member count
$assembliesStmt = $db->query("
    SELECT asm.*, a.area_name, d.district_name,
           (SELECT COUNT(*) FROM members WHERE assembly_id = asm.id) as member_count
    FROM assemblies asm
    JOIN areas a ON asm.area_id = a.id
    JOIN districts d ON asm.district_id = d.id
    ORDER BY a.area_name, d.district_name, asm.assembly_name
");
$assemblies = $assembliesStmt->fetchAll();

// Get all areas for dropdowns
$areasStmt = $db->query("SELECT id, area_name, area_code FROM areas WHERE is_active = 1 ORDER BY area_name");
$areas = $areasStmt->fetchAll();

// Get all districts for dropdowns
$districtsStmt = $db->query("SELECT id, district_name, area_id FROM districts WHERE is_active = 1 ORDER BY district_name");
$districts = $districtsStmt->fetchAll();

// Get all users for assignment
$usersStmt = $db->query("SELECT id, full_name, email, access_level FROM users WHERE is_active = 1 ORDER BY full_name");
$users = $usersStmt->fetchAll();

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

<?php include '../../includes/sidebar.php'; ?>

<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
    <!-- Header -->
    <div class="gradient-bg rounded-2xl shadow-2xl p-8 mb-8 text-white">
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-4xl font-bold mb-2">
                    <i class="fas fa-church mr-3"></i>Assemblies Management
                </h1>
                <p class="text-white/90 text-lg">Manage local assemblies and assign administrators</p>
            </div>
            <div class="flex flex-wrap gap-3">
                <button onclick="document.getElementById('addModal').classList.remove('hidden')" 
                        class="btn-gradient-orange text-white px-8 py-3 rounded-full font-bold shadow-lg hover:shadow-xl transition">
                    <i class="fas fa-plus mr-2"></i>Add New Assembly
                </button>
                <a href="upload-locations.php" 
                   class="btn-gradient text-white px-8 py-3 rounded-full font-bold shadow-lg hover:shadow-xl transition inline-block">
                    <i class="fas fa-upload mr-2"></i>Bulk Upload
                </a>
                <a href="?action=export_pdf" 
                   class="bg-red-600 hover:bg-red-700 text-white px-8 py-3 rounded-full font-bold shadow-lg hover:shadow-xl transition inline-block">
                    <i class="fas fa-file-pdf mr-2"></i>Export PDF
                </a>
            </div>
        </div>
    </div>
    
    <?php if ($success): ?>
        <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-6 rounded-lg mb-6 animate-fadeIn">
            <i class="fas fa-check-circle mr-2"></i><?php echo $success; ?>
        </div>
    <?php endif; ?>
    
    <?php if ($error): ?>
        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-6 rounded-lg mb-6">
            <i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?>
        </div>
    <?php endif; ?>
    
    <!-- Assemblies Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <?php foreach ($assemblies as $assembly): 
            // Get assigned admins
            $adminStmt = $db->prepare("
                SELECT ala.id as assignment_id, u.full_name, u.email 
                FROM admin_location_assignments ala
                JOIN users u ON ala.user_id = u.id
                WHERE ala.location_type = 'assembly' AND ala.assembly_id = ? AND ala.is_active = 1
            ");
            $adminStmt->execute([$assembly['id']]);
            $assignedAdmins = $adminStmt->fetchAll();
        ?>
            <div class="bg-white rounded-2xl shadow-lg p-6 hover:shadow-2xl transition border-t-4" style="border-top-color: #9333EA;">
                <div class="flex items-start justify-between mb-4">
                    <div class="flex items-center">
                        <div class="rounded-full p-3 mr-3 gradient-tertiary">
                            <i class="fas fa-church text-white text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-lg text-gray-800"><?php echo htmlspecialchars($assembly['assembly_name']); ?></h3>
                            <span class="text-sm bg-purple-100 text-purple-800 px-2 py-1 rounded font-mono">
                                <?php echo htmlspecialchars($assembly['assembly_code']); ?>
                            </span>
                        </div>
                    </div>
                    <span class="px-3 py-1 text-xs font-semibold rounded-full <?php echo $assembly['is_active'] ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                        <?php echo $assembly['is_active'] ? 'Active' : 'Inactive'; ?>
                    </span>
                </div>
                
                <!-- Area & District Badges -->
                <div class="mb-4 space-y-2">
                    <span class="inline-flex items-center text-xs bg-blue-100 text-blue-800 px-2 py-1 rounded-full mr-2">
                        <i class="fas fa-map-marked-alt mr-1"></i><?php echo htmlspecialchars($assembly['area_name']); ?>
                    </span>
                    <span class="inline-flex items-center text-xs bg-orange-100 text-orange-800 px-2 py-1 rounded-full">
                        <i class="fas fa-map-marker-alt mr-1"></i><?php echo htmlspecialchars($assembly['district_name']); ?>
                    </span>
                </div>
                
                <?php if ($assembly['description']): ?>
                    <p class="text-gray-600 text-sm mb-4"><?php echo htmlspecialchars($assembly['description']); ?></p>
                <?php endif; ?>
                
                <div class="space-y-2 text-sm text-gray-600 mb-4">
                    <?php if ($assembly['pastor_name']): ?>
                        <div><i class="fas fa-user-tie mr-2 text-purple-600"></i><?php echo htmlspecialchars($assembly['pastor_name']); ?></div>
                    <?php endif; ?>
                    <?php if ($assembly['phone']): ?>
                        <div><i class="fas fa-phone mr-2 text-green-600"></i><?php echo htmlspecialchars($assembly['phone']); ?></div>
                    <?php endif; ?>
                    <?php if ($assembly['email']): ?>
                        <div><i class="fas fa-envelope mr-2 text-orange-600"></i><?php echo htmlspecialchars($assembly['email']); ?></div>
                    <?php endif; ?>
                    <?php if ($assembly['city']): ?>
                        <div><i class="fas fa-map-pin mr-2 text-blue-600"></i><?php echo htmlspecialchars($assembly['city']); ?></div>
                    <?php endif; ?>
                </div>
                
                <!-- Stats -->
                <div class="mb-4 p-3 bg-gray-50 rounded-lg text-center">
                    <div class="text-2xl font-bold text-purple-600"><?php echo $assembly['member_count']; ?></div>
                    <div class="text-xs text-gray-600">Members</div>
                </div>
                
                <!-- Assigned Admins -->
                <div class="mb-4">
                    <div class="text-sm font-semibold text-gray-700 mb-2">
                        <i class="fas fa-users-cog mr-1"></i>Assigned Admins (<?php echo count($assignedAdmins); ?>)
                    </div>
                    <?php if (count($assignedAdmins) > 0): ?>
                        <div class="space-y-1">
                            <?php foreach ($assignedAdmins as $admin): ?>
                                <div class="flex items-center justify-between text-xs bg-purple-50 p-2 rounded">
                                    <span class="font-medium"><?php echo htmlspecialchars($admin['full_name']); ?></span>
                                    <a href="?remove_assignment=<?php echo $admin['assignment_id']; ?>" 
                                       onclick="return confirm('Remove this admin assignment?')"
                                       class="text-red-600 hover:text-red-800">
                                        <i class="fas fa-times"></i>
                                    </a>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <p class="text-xs text-gray-500 italic">No admins assigned</p>
                    <?php endif; ?>
                </div>
                
                <!-- Actions -->
                <div class="grid grid-cols-3 gap-2 pt-4 border-t border-gray-200">
                    <button onclick='editAssembly(<?php echo json_encode($assembly); ?>)' 
                            class="btn-gradient text-white px-3 py-2 rounded-lg hover:shadow-lg transition text-sm">
                        <i class="fas fa-edit"></i>
                    </button>
                    <button onclick="assignAdmin(<?php echo $assembly['id']; ?>, '<?php echo addslashes($assembly['assembly_name']); ?>')" 
                            class="bg-green-600 text-white px-3 py-2 rounded-lg hover:bg-green-700 transition text-sm">
                        <i class="fas fa-user-plus"></i>
                    </button>
                    <div class="flex space-x-1">
                        <button onclick="toggleAssemblyStatus(<?php echo $assembly['id']; ?>, <?php echo $assembly['is_active'] ? '0' : '1'; ?>)" 
                                class="<?php echo $assembly['is_active'] ? 'bg-yellow-500 hover:bg-yellow-600' : 'bg-blue-600 hover:bg-blue-700'; ?> text-white px-3 py-2 rounded-lg transition text-xs">
                            <i class="fas <?php echo $assembly['is_active'] ? 'fa-ban' : 'fa-check'; ?> mr-1"></i>
                            <?php echo $assembly['is_active'] ? 'Deactivate' : 'Activate'; ?>
                        </button>
                        <button onclick="deleteAssembly(<?php echo $assembly['id']; ?>)" 
                                class="bg-red-600 text-white px-3 py-2 rounded-lg hover:bg-red-700 transition text-sm">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- Add Modal -->
<div id="addModal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
    <div class="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-screen overflow-y-auto">
        <div class="p-6 border-b border-gray-200 flex justify-between items-center gradient-bg text-white rounded-t-2xl">
            <h2 class="text-2xl font-bold">Add New Assembly</h2>
            <button onclick="document.getElementById('addModal').classList.add('hidden')" class="text-white hover:text-gray-200">
                <i class="fas fa-times text-xl"></i>
            </button>
        </div>
        
        <form method="POST" class="p-6">
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Select Area *</label>
                    <select name="area_id" id="add_area_id" required onchange="loadDistrictsForAdd(this.value)" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">-- Select Area --</option>
                        <?php foreach ($areas as $area): ?>
                            <option value="<?php echo $area['id']; ?>">
                                <?php echo htmlspecialchars($area['area_name']); ?> (<?php echo $area['area_code']; ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Select District *</label>
                    <select name="district_id" id="add_district_id" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">-- Select Area First --</option>
                    </select>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly Name *</label>
                        <input type="text" name="assembly_name" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly Code *</label>
                        <input type="text" name="assembly_code" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="e.g., AS001">
                    </div>
                </div>
                
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Description</label>
                    <textarea name="description" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Pastor Name</label>
                        <input type="text" name="pastor_name" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Phone</label>
                        <input type="tel" name="phone" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Email</label>
                    <input type="email" name="email" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">GPS Address</label>
                        <input type="text" name="gps_address" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="e.g., GA-123-4567">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">City</label>
                        <input type="text" name="city" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Address</label>
                    <textarea name="address" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
                </div>
            </div>
            
            <div class="mt-6 flex justify-end space-x-4">
                <button type="button" onclick="document.getElementById('addModal').classList.add('hidden')" 
                        class="px-6 py-2 border border-gray-300 rounded-lg hover:bg-gray-100 transition">
                    Cancel
                </button>
                <button type="submit" name="add_assembly" 
                        class="btn-gradient text-white px-6 py-2 rounded-lg hover:shadow-lg transition">
                    <i class="fas fa-save mr-2"></i>Save Assembly
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Edit Modal -->
<div id="editModal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
    <div class="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-screen overflow-y-auto">
        <div class="p-6 border-b border-gray-200 flex justify-between items-center gradient-bg text-white rounded-t-2xl">
            <h2 class="text-2xl font-bold">Edit Assembly</h2>
            <button onclick="closeEditModal()" class="text-white hover:text-gray-200">
                <i class="fas fa-times text-xl"></i>
            </button>
        </div>
        
        <form method="POST" class="p-6">
            <input type="hidden" name="assembly_id" id="edit_assembly_id">
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Select Area *</label>
                    <select name="area_id" id="edit_area_id" required onchange="loadDistrictsForEdit(this.value)" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">-- Select Area --</option>
                        <?php foreach ($areas as $area): ?>
                            <option value="<?php echo $area['id']; ?>">
                                <?php echo htmlspecialchars($area['area_name']); ?> (<?php echo $area['area_code']; ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Select District *</label>
                    <select name="district_id" id="edit_district_id" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">-- Select Area First --</option>
                        <?php foreach ($districts as $district): ?>
                            <option value="<?php echo $district['id']; ?>" data-area-id="<?php echo $district['area_id']; ?>">
                                <?php echo htmlspecialchars($district['district_name']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly Name *</label>
                        <input type="text" name="assembly_name" id="edit_assembly_name" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly Code *</label>
                        <input type="text" name="assembly_code" id="edit_assembly_code" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Description</label>
                    <textarea name="description" id="edit_description" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Pastor Name</label>
                        <input type="text" name="pastor_name" id="edit_pastor" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Phone</label>
                        <input type="tel" name="phone" id="edit_phone" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Email</label>
                    <input type="email" name="email" id="edit_email" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">GPS Address</label>
                        <input type="text" name="gps_address" id="edit_gps" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">City</label>
                        <input type="text" name="city" id="edit_city" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-2">Address</label>
                    <textarea name="address" id="edit_address" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
                </div>
            </div>
            <div class="mt-6 flex justify-end space-x-4">
                <button type="button" onclick="closeEditModal()" class="px-6 py-2 border border-gray-300 rounded-lg hover:bg-gray-100 transition">Cancel</button>
                <button type="submit" name="update_assembly" class="btn-gradient text-white px-6 py-2 rounded-lg hover:shadow-lg transition">
                    <i class="fas fa-save mr-2"></i>Update Assembly
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Assign Admin Modal -->
<div id="assignModal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
    <div class="bg-white rounded-2xl shadow-2xl max-w-md w-full">
        <div class="p-6 border-b border-gray-200 flex justify-between items-center gradient-bg text-white rounded-t-2xl">
            <h2 class="text-2xl font-bold"><i class="fas fa-user-plus mr-2"></i>Assign Admin</h2>
            <button onclick="closeAssignModal()" class="text-white hover:text-gray-200">
                <i class="fas fa-times text-xl"></i>
            </button>
        </div>
        
        <form method="POST" class="p-6">
            <input type="hidden" name="location_id" id="assign_location_id">
            
            <div class="mb-4">
                <p class="text-gray-700 mb-2">Assigning admin to:</p>
                <p class="font-bold text-lg text-gray-800" id="assign_location_name"></p>
            </div>
            
            <div class="mb-4">
                <label class="block text-sm font-semibold text-gray-700 mb-2">Select User *</label>
                <select name="user_id" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    <option value="">-- Select User --</option>
                    <?php foreach ($users as $user): ?>
                        <option value="<?php echo $user['id']; ?>">
                            <?php echo htmlspecialchars($user['full_name']); ?> 
                            (<?php echo htmlspecialchars($user['email']); ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="flex justify-end space-x-4">
                <button type="button" onclick="closeAssignModal()" class="px-6 py-2 border border-gray-300 rounded-lg hover:bg-gray-100 transition">Cancel</button>
                <button type="submit" name="assign_admin" class="btn-gradient text-white px-6 py-2 rounded-lg hover:shadow-lg transition">
                    <i class="fas fa-check mr-2"></i>Assign
                </button>
            </div>
        </form>
    </div>
</div>
</main>

<script>
function editAssembly(assembly) {
    document.getElementById('edit_assembly_id').value = assembly.id;
    document.getElementById('edit_area_id').value = assembly.area_id;
    document.getElementById('edit_assembly_name').value = assembly.assembly_name;
    document.getElementById('edit_assembly_code').value = assembly.assembly_code;
    document.getElementById('edit_description').value = assembly.description || '';
    document.getElementById('edit_pastor').value = assembly.pastor_name || '';
    document.getElementById('edit_phone').value = assembly.phone || '';
    document.getElementById('edit_email').value = assembly.email || '';
    document.getElementById('edit_gps').value = assembly.gps_address || '';
    document.getElementById('edit_city').value = assembly.city || '';
    document.getElementById('edit_address').value = assembly.address || '';
    
    // Load districts for the selected area, then set the district value
    fetch('get-districts.php?area_id=' + assembly.area_id)
        .then(response => response.json())
        .then(data => {
            const districtSelect = document.getElementById('edit_district_id');
            if (data.success && data.districts) {
                districtSelect.innerHTML = '<option value="">-- Select District --</option>';
                data.districts.forEach(district => {
                    const option = document.createElement('option');
                    option.value = district.id;
                    option.textContent = district.district_name;
                    if (district.id == assembly.district_id) {
                        option.selected = true;
                    }
                    districtSelect.appendChild(option);
                });
            }
        });
    
    document.getElementById('editModal').classList.remove('hidden');
}

function closeEditModal() {
    document.getElementById('editModal').classList.add('hidden');
}

function deleteAssembly(id) {
    if (confirm('Are you sure you want to delete this assembly? This will also delete all associated member records.')) {
        window.location.href = 'assemblies.php?delete=' + id;
    }
}

function toggleAssemblyStatus(id, targetStatus) {
    var msg = targetStatus === 0
        ? 'Deactivate this assembly and ALL its members?'
        : 'Activate this assembly?';
    if (confirm(msg)) {
        window.location.href = 'assemblies.php?toggle_active=' + targetStatus + '&id=' + id;
    }
}

function assignAdmin(id, name) {
    document.getElementById('assign_location_id').value = id;
    document.getElementById('assign_location_name').textContent = name;
    document.getElementById('assignModal').classList.remove('hidden');
}

function closeAssignModal() {
    document.getElementById('assignModal').classList.add('hidden');
}

// Cascading Dropdown Functions
function loadDistrictsForAdd(areaId) {
    const districtSelect = document.getElementById('add_district_id');
    
    if (!areaId) {
        districtSelect.innerHTML = '<option value="">-- Select Area First --</option>';
        return;
    }
    
    // Show loading state
    districtSelect.innerHTML = '<option value="">Loading districts...</option>';
    districtSelect.disabled = true;
    
    fetch('get-districts.php?area_id=' + areaId)
        .then(response => response.json())
        .then(data => {
            if (data.success && data.districts) {
                districtSelect.innerHTML = '<option value="">-- Select District --</option>';
                data.districts.forEach(district => {
                    const option = document.createElement('option');
                    option.value = district.id;
                    option.textContent = district.district_name;
                    districtSelect.appendChild(option);
                });
            } else {
                districtSelect.innerHTML = '<option value="">No districts found</option>';
            }
            districtSelect.disabled = false;
        })
        .catch(error => {
            console.error('Error loading districts:', error);
            districtSelect.innerHTML = '<option value="">Error loading districts</option>';
            districtSelect.disabled = false;
        });
}

function loadDistrictsForEdit(areaId) {
    const districtSelect = document.getElementById('edit_district_id');
    const currentDistrictId = districtSelect.value; // Save current value
    
    if (!areaId) {
        districtSelect.innerHTML = '<option value="">-- Select Area First --</option>';
        return;
    }
    
    // Show loading state
    districtSelect.innerHTML = '<option value="">Loading districts...</option>';
    districtSelect.disabled = true;
    
    fetch('get-districts.php?area_id=' + areaId)
        .then(response => response.json())
        .then(data => {
            if (data.success && data.districts) {
                districtSelect.innerHTML = '<option value="">-- Select District --</option>';
                data.districts.forEach(district => {
                    const option = document.createElement('option');
                    option.value = district.id;
                    option.textContent = district.district_name;
                    districtSelect.appendChild(option);
                });
                // Restore previous selection if it exists in the new list
                if (currentDistrictId) {
                    districtSelect.value = currentDistrictId;
                }
            } else {
                districtSelect.innerHTML = '<option value="">No districts found</option>';
            }
            districtSelect.disabled = false;
        })
        .catch(error => {
            console.error('Error loading districts:', error);
            districtSelect.innerHTML = '<option value="">Error loading districts</option>';
            districtSelect.disabled = false;
        });
}
</script>

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

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