Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/assemblies.php

<?php
require_once '../includes/functions.php';

// Check if user is logged in and has admin privileges
if (!isLoggedIn()) {
    header('Location: ' . BASE_URL . 'login.php');
    exit();
}

$user = getCurrentUser();
if (!in_array($user['role'], ['superuser', 'area_admin', 'district_admin', 'assembly_admin'])) {
    header('Location: ' . BASE_URL . 'dashboard.php');
    exit();
}

$db = new CopMadinaDB();
$conn = $db->getConnection();

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'create') {
        $name = trim($_POST['name']);
        $district_id = $_POST['district_id'];
        $location = trim($_POST['location']);
        $description = trim($_POST['description']);
        $contact_person = trim($_POST['contact_person']);
        $contact_email = trim($_POST['contact_email']);
        $contact_phone = trim($_POST['contact_phone']);
        
        $stmt = executeQuery(
            "INSERT INTO assemblies (name, district_id, location, description, contact_person, contact_email, contact_phone, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
            [$name, $district_id, $location, $description, $contact_person, $contact_email, $contact_phone, $user['id']]
        );
        
        if ($stmt) {
            logAudit('create', 'assemblies', $conn->lastInsertId());
            addNotification('success', 'Assembly created successfully!', $user['id']);
        } else {
            addNotification('error', 'Failed to create assembly.', $user['id']);
        }
    } elseif ($action === 'update') {
        $id = $_POST['id'];
        $name = trim($_POST['name']);
        $district_id = $_POST['district_id'];
        $location = trim($_POST['location']);
        $description = trim($_POST['description']);
        $contact_person = trim($_POST['contact_person']);
        $contact_email = trim($_POST['contact_email']);
        $contact_phone = trim($_POST['contact_phone']);
        $status = $_POST['status'];
        
        $stmt = executeQuery(
            "UPDATE assemblies SET name = ?, district_id = ?, location = ?, description = ?, contact_person = ?, contact_email = ?, contact_phone = ?, status = ? WHERE id = ?",
            [$name, $district_id, $location, $description, $contact_person, $contact_email, $contact_phone, $status, $id]
        );
        
        if ($stmt) {
            logAudit('update', 'assemblies', $id);
            addNotification('success', 'Assembly updated successfully!', $user['id']);
        } else {
            addNotification('error', 'Failed to update assembly.', $user['id']);
        }
    } elseif ($action === 'delete') {
        $id = $_POST['id'];
        
        $stmt = executeQuery("DELETE FROM assemblies WHERE id = ?", [$id]);
        
        if ($stmt) {
            logAudit('delete', 'assemblies', $id);
            addNotification('success', 'Assembly deleted successfully!', $user['id']);
        } else {
            addNotification('error', 'Failed to delete assembly.', $user['id']);
        }
    }
    
    header('Location: assemblies.php');
    exit();
}

// Get assemblies with statistics based on user role
$assemblies_query = "
    SELECT ass.*, d.name as district_name, a.name as area_name,
           COUNT(DISTINCT e.id) as event_count
    FROM assemblies ass
    LEFT JOIN districts d ON ass.district_id = d.id
    LEFT JOIN areas a ON d.area_id = a.id
    LEFT JOIN events e ON ass.id = e.assembly_id AND e.status = 'active'
";

$params = [];
if ($user['role'] === 'area_admin') {
    $assemblies_query .= " WHERE d.area_id = ?";
    $params[] = $user['area_id'];
} elseif ($user['role'] === 'district_admin') {
    $assemblies_query .= " WHERE ass.district_id = ?";
    $params[] = $user['district_id'];
} elseif ($user['role'] === 'assembly_admin') {
    $assemblies_query .= " WHERE ass.id = ?";
    $params[] = $user['assembly_id'];
}

$assemblies_query .= " GROUP BY ass.id ORDER BY a.name, d.name, ass.name";

$stmt = executeQuery($assemblies_query, $params);
$assemblies = $stmt ? $stmt->fetchAll() : [];

// Get districts for dropdown (based on user role)
$districts_query = "
    SELECT d.id, d.name, a.name as area_name 
    FROM districts d 
    LEFT JOIN areas a ON d.area_id = a.id 
    WHERE d.status = 'active'
";
$districts_params = [];
if ($user['role'] === 'area_admin') {
    $districts_query .= " AND d.area_id = ?";
    $districts_params[] = $user['area_id'];
} elseif ($user['role'] === 'district_admin') {
    $districts_query .= " AND d.id = ?";
    $districts_params[] = $user['district_id'];
}
$districts_query .= " ORDER BY a.name, d.name";

$stmt = executeQuery($districts_query, $districts_params);
$districts = $stmt ? $stmt->fetchAll() : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Assemblies Management - COP Madina Conference</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    animation: {
                        'fade-in': 'fadeIn 0.5s ease-in-out',
                        'slide-up': 'slideUp 0.6s ease-out',
                        'scale-in': 'scaleIn 0.3s ease-out',
                    },
                    keyframes: {
                        fadeIn: {
                            '0%': { opacity: '0', transform: 'translateY(10px)' },
                            '100%': { opacity: '1', transform: 'translateY(0)' }
                        },
                        slideUp: {
                            '0%': { opacity: '0', transform: 'translateY(20px)' },
                            '100%': { opacity: '1', transform: 'translateY(0)' }
                        },
                        scaleIn: {
                            '0%': { opacity: '0', transform: 'scale(0.95)' },
                            '100%': { opacity: '1', transform: 'scale(1)' }
                        }
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gradient-to-br from-slate-50 to-blue-50 min-h-screen">
    <div id="app" class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/admin_sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden ml-72">
            <!-- Header -->
            <header class="bg-white/80 backdrop-blur-sm shadow-lg border-b border-slate-200/50">
                <div class="flex items-center justify-between px-8 py-6">
                    <div class="animate-fade-in">
                        <h1 class="text-3xl font-bold bg-gradient-to-r from-purple-600 to-pink-600 bg-clip-text text-transparent flex items-center">
                            <div class="p-2 rounded-xl bg-gradient-to-br from-purple-500 to-pink-600 mr-3">
                                <i class="fas fa-church text-white"></i>
                            </div>
                            Assemblies Management
                        </h1>
                        <p class="text-slate-600 mt-1">Manage local church assemblies</p>
                    </div>
                    <button @click="showCreateModal = true" 
                            class="px-6 py-3 bg-gradient-to-r from-purple-600 to-pink-600 hover:from-purple-700 hover:to-pink-700 text-white font-medium rounded-xl transition-all duration-200 flex items-center space-x-2 shadow-lg hover:shadow-xl animate-scale-in">
                        <i class="fas fa-plus"></i>
                        <span>Add New Assembly</span>
                    </button>
                </div>
            </header>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-8">
                <!-- Assemblies Grid -->
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <?php foreach ($assemblies as $index => $assembly): ?>
                    <div class="group bg-white/70 backdrop-blur-sm rounded-2xl shadow-lg hover:shadow-xl transition-all duration-300 p-6 border border-slate-200/50 hover:border-purple-300/50 animate-slide-up" 
                         style="animation-delay: <?php echo $index * 0.1; ?>s">
                        <!-- Assembly Header -->
                        <div class="flex items-start justify-between mb-4">
                            <div class="flex-1">
                                <h3 class="text-xl font-bold text-slate-800 mb-1"><?php echo htmlspecialchars($assembly['name']); ?></h3>
                                <p class="text-sm text-purple-600 font-medium mb-1"><?php echo htmlspecialchars($assembly['district_name']); ?></p>
                                <p class="text-xs text-slate-500 mb-1"><?php echo htmlspecialchars($assembly['area_name']); ?></p>
                                <?php if ($assembly['location']): ?>
                                <p class="text-sm text-slate-600 flex items-center">
                                    <i class="fas fa-map-marker-alt mr-2 text-purple-500"></i>
                                    <?php echo htmlspecialchars($assembly['location']); ?>
                                </p>
                                <?php endif; ?>
                            </div>
                            <span class="px-3 py-1 text-xs font-semibold rounded-full <?php echo $assembly['status'] === 'active' ? 'bg-emerald-100 text-emerald-800' : 'bg-red-100 text-red-800'; ?>">
                                <?php echo ucfirst($assembly['status']); ?>
                            </span>
                        </div>

                        <!-- Description -->
                        <?php if ($assembly['description']): ?>
                        <p class="text-sm text-slate-600 mb-4 line-clamp-2"><?php echo htmlspecialchars($assembly['description']); ?></p>
                        <?php endif; ?>

                        <!-- Statistics -->
                        <div class="text-center p-3 bg-purple-50 rounded-xl mb-4">
                            <div class="text-lg font-bold text-purple-600"><?php echo $assembly['event_count']; ?></div>
                            <div class="text-xs text-slate-600">Active Events</div>
                        </div>

                        <!-- Contact Info -->
                        <?php if ($assembly['contact_person'] || $assembly['contact_email'] || $assembly['contact_phone']): ?>
                        <div class="border-t border-slate-200 pt-4 mb-4">
                            <h4 class="text-sm font-semibold text-slate-700 mb-2">Contact Information</h4>
                            <?php if ($assembly['contact_person']): ?>
                            <p class="text-xs text-slate-600 flex items-center mb-1">
                                <i class="fas fa-user mr-2 text-slate-400"></i>
                                <?php echo htmlspecialchars($assembly['contact_person']); ?>
                            </p>
                            <?php endif; ?>
                            <?php if ($assembly['contact_email']): ?>
                            <p class="text-xs text-slate-600 flex items-center mb-1">
                                <i class="fas fa-envelope mr-2 text-slate-400"></i>
                                <?php echo htmlspecialchars($assembly['contact_email']); ?>
                            </p>
                            <?php endif; ?>
                            <?php if ($assembly['contact_phone']): ?>
                            <p class="text-xs text-slate-600 flex items-center">
                                <i class="fas fa-phone mr-2 text-slate-400"></i>
                                <?php echo htmlspecialchars($assembly['contact_phone']); ?>
                            </p>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>

                        <!-- Actions -->
                        <div class="flex space-x-2">
                            <button @click="editAssembly(<?php echo htmlspecialchars(json_encode($assembly)); ?>)"
                                    class="flex-1 px-3 py-2 bg-purple-100 hover:bg-purple-200 text-purple-700 font-medium rounded-lg transition-colors text-sm">
                                <i class="fas fa-edit mr-1"></i>
                                Edit
                            </button>
                            <button @click="confirmDelete(<?php echo $assembly['id']; ?>, '<?php echo htmlspecialchars($assembly['name']); ?>')"
                                    class="flex-1 px-3 py-2 bg-red-100 hover:bg-red-200 text-red-700 font-medium rounded-lg transition-colors text-sm">
                                <i class="fas fa-trash mr-1"></i>
                                Delete
                            </button>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>

                <!-- Empty State -->
                <?php if (empty($assemblies)): ?>
                <div class="text-center py-16">
                    <div class="mx-auto w-24 h-24 bg-slate-100 rounded-full flex items-center justify-center mb-6">
                        <i class="fas fa-church text-3xl text-slate-400"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-slate-700 mb-2">No Assemblies Found</h3>
                    <p class="text-slate-500 mb-6">Get started by creating your first assembly.</p>
                    <button @click="showCreateModal = true" 
                            class="px-6 py-3 bg-gradient-to-r from-purple-600 to-pink-600 hover:from-purple-700 hover:to-pink-700 text-white font-medium rounded-xl transition-all duration-200">
                        <i class="fas fa-plus mr-2"></i>
                        Create First Assembly
                    </button>
                </div>
                <?php endif; ?>
            </main>
        </div>

        <!-- Create/Edit Modal -->
        <div v-if="showCreateModal || showEditModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4">
            <div class="bg-white/95 backdrop-blur-sm rounded-2xl shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto border border-slate-200/50 animate-scale-in">
                <div class="sticky top-0 bg-white/95 backdrop-blur-sm border-b border-slate-200/50 px-6 py-4 rounded-t-2xl">
                    <div class="flex items-center justify-between">
                        <h3 class="text-xl font-bold text-slate-800">
                            {{ showCreateModal ? 'Create New Assembly' : 'Edit Assembly' }}
                        </h3>
                        <button @click="closeModal()" class="p-2 rounded-lg hover:bg-slate-100 transition-colors">
                            <i class="fas fa-times text-slate-500"></i>
                        </button>
                    </div>
                </div>
                
                <form method="POST" class="p-6 space-y-6">
                    <input type="hidden" name="action" :value="showCreateModal ? 'create' : 'update'">
                    <input v-if="showEditModal" type="hidden" name="id" :value="editingAssembly.id">
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Assembly Name *</label>
                            <input type="text" name="name" :value="editingAssembly.name" required
                                   class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200"
                                   placeholder="Enter assembly name">
                        </div>
                        
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">District *</label>
                            <select name="district_id" :value="editingAssembly.district_id" required
                                    class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200">
                                <option value="">Select District</option>
                                <?php foreach ($districts as $district): ?>
                                <option value="<?php echo $district['id']; ?>">
                                    <?php echo htmlspecialchars($district['name'] . ' (' . $district['area_name'] . ')'); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Location</label>
                        <input type="text" name="location" :value="editingAssembly.location"
                               class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200"
                               placeholder="Enter location">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Description</label>
                        <textarea name="description" rows="3" :value="editingAssembly.description"
                                  class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200 resize-none"
                                  placeholder="Enter assembly description"></textarea>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Contact Person</label>
                            <input type="text" name="contact_person" :value="editingAssembly.contact_person"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200"
                                   placeholder="Contact person">
                        </div>
                        
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Contact Email</label>
                            <input type="email" name="contact_email" :value="editingAssembly.contact_email"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200"
                                   placeholder="Email address">
                        </div>
                        
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Contact Phone</label>
                            <input type="tel" name="contact_phone" :value="editingAssembly.contact_phone"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200"
                                   placeholder="Phone number">
                        </div>
                    </div>
                    
                    <div v-if="showEditModal">
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Status</label>
                        <select name="status" :value="editingAssembly.status"
                                class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200">
                            <option value="active">Active</option>
                            <option value="inactive">Inactive</option>
                        </select>
                    </div>
                    
                    <div class="flex justify-end space-x-3 pt-4 border-t border-slate-200">
                        <button type="button" @click="closeModal()" 
                                class="px-6 py-3 bg-slate-100 hover:bg-slate-200 text-slate-700 font-medium rounded-xl transition-colors">
                            Cancel
                        </button>
                        <button type="submit" 
                                class="px-6 py-3 bg-gradient-to-r from-purple-600 to-pink-600 hover:from-purple-700 hover:to-pink-700 text-white font-medium rounded-xl transition-all duration-200 shadow-lg">
                            {{ showCreateModal ? 'Create Assembly' : 'Update Assembly' }}
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Delete Confirmation Modal -->
        <div v-if="showDeleteModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4">
            <div class="bg-white/95 backdrop-blur-sm rounded-2xl shadow-2xl w-full max-w-md border border-slate-200/50 animate-scale-in">
                <div class="p-6 text-center">
                    <div class="mx-auto w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-exclamation-triangle text-red-600 text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-slate-800 mb-2">Delete Assembly</h3>
                    <p class="text-slate-600 mb-6">
                        Are you sure you want to delete <strong>"{{ deletingAssemblyName }}"</strong>? This action cannot be undone.
                    </p>
                    <div class="flex justify-center space-x-3">
                        <form method="POST" class="flex space-x-3">
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="id" :value="deletingAssemblyId">
                            <button type="button" @click="showDeleteModal = false" 
                                    class="px-6 py-3 bg-slate-100 hover:bg-slate-200 text-slate-700 font-medium rounded-xl transition-colors">
                                Cancel
                            </button>
                            <button type="submit" 
                                    class="px-6 py-3 bg-red-600 hover:bg-red-700 text-white font-medium rounded-xl transition-colors">
                                Delete
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp } = Vue;
        
        createApp({
            data() {
                return {
                    showCreateModal: false,
                    showEditModal: false,
                    showDeleteModal: false,
                    editingAssembly: {},
                    deletingAssemblyId: null,
                    deletingAssemblyName: ''
                }
            },
            methods: {
                editAssembly(assembly) {
                    this.editingAssembly = { ...assembly };
                    this.showEditModal = true;
                },
                confirmDelete(id, name) {
                    this.deletingAssemblyId = id;
                    this.deletingAssemblyName = name;
                    this.showDeleteModal = true;
                },
                closeModal() {
                    this.showCreateModal = false;
                    this.showEditModal = false;
                    this.editingAssembly = {};
                }
            }
        }).mount('#app');
    </script>
</body>
</html>

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