Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/modules/ministries/
Upload File :
Current File : /home/copmadinaarea/.trash/modules/ministries/executives.php

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

$pageTitle = "Ministry Executives - " . APP_NAME;
$db = Database::getInstance()->getConnection();
$success = '';
$error = '';

$ministryId = $_GET['ministry_id'] ?? null;

// Handle executive assignment
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (isset($_POST['assign_executive'])) {
            $stmt = $db->prepare("
                INSERT INTO ministry_executives (
                    ministry_id, member_id, position_id, start_date, end_date,
                    appointment_type, appointed_by, notes
                ) VALUES (
                    :ministry_id, :member_id, :position_id, :start_date, :end_date,
                    :appointment_type, :appointed_by, :notes
                )
            ");
            
            $stmt->execute([
                'ministry_id' => $_POST['ministry_id'],
                'member_id' => $_POST['member_id'],
                'position_id' => $_POST['position_id'],
                'start_date' => $_POST['start_date'],
                'end_date' => $_POST['end_date'] ?? null,
                'appointment_type' => $_POST['appointment_type'],
                'appointed_by' => $_SESSION['user_id'],
                'notes' => $_POST['notes'] ?? null
            ]);
            
            // Log to history
            $execId = $db->lastInsertId();
            $historyStmt = $db->prepare("
                INSERT INTO executive_history (
                    executive_id, ministry_id, member_id, position_id,
                    action_type, action_date, reason, performed_by
                ) VALUES (:exec_id, :ministry_id, :member_id, :position_id, 'appointed', :action_date, :reason, :performed_by)
            ");
            $historyStmt->execute([
                'exec_id' => $execId,
                'ministry_id' => $_POST['ministry_id'],
                'member_id' => $_POST['member_id'],
                'position_id' => $_POST['position_id'],
                'action_date' => $_POST['start_date'],
                'reason' => 'Initial appointment',
                'performed_by' => $_SESSION['user_id']
            ]);
            
            $success = "Executive assigned successfully!";
        }
        
        if (isset($_POST['end_assignment'])) {
            $stmt = $db->prepare("
                UPDATE ministry_executives 
                SET status = 'completed', end_date = CURDATE()
                WHERE id = :id
            ");
            $stmt->execute(['id' => $_POST['executive_id']]);
            
            $success = "Executive assignment ended!";
        }
        
    } catch (Exception $e) {
        $error = "Error: " . $e->getMessage();
    }
}

// Get all ministries for dropdown
$ministries = $db->query("SELECT id, ministry_name FROM ministries WHERE is_active = 1 ORDER BY ministry_name")->fetchAll();

// Get active executives
$query = "
    SELECT me.*, 
           m.first_name, m.last_name, m.membershipcard_id,
           mp.position_name, mp.level,
           min.ministry_name, min.ministry_type
    FROM ministry_executives me
    JOIN members m ON me.member_id = m.id
    JOIN ministry_positions mp ON me.position_id = mp.id
    JOIN ministries min ON me.ministry_id = min.id
    WHERE me.status = 'active'
";

$params = [];
if ($ministryId) {
    $query .= " AND me.ministry_id = :ministry_id";
    $params['ministry_id'] = $ministryId;
}

$query .= " ORDER BY min.ministry_name, mp.level, me.start_date DESC";

$stmt = $db->prepare($query);
$stmt->execute($params);
$executives = $stmt->fetchAll();

// Get executive history
$historyStmt = $db->query("
    SELECT eh.*, 
           m.first_name, m.last_name,
           mp.position_name,
           min.ministry_name
    FROM executive_history eh
    JOIN members m ON eh.member_id = m.id
    JOIN ministry_positions mp ON eh.position_id = mp.id
    JOIN ministries min ON eh.ministry_id = min.id
    ORDER BY eh.action_date DESC
    LIMIT 50
");
$history = $historyStmt->fetchAll();

// Get members for dropdown
$members = $db->query("SELECT id, first_name, last_name FROM members WHERE is_active = 1 ORDER BY first_name")->fetchAll();

// Get positions
$positions = $db->query("SELECT * FROM ministry_positions WHERE is_active = 1 ORDER BY level, position_name")->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">
    <div class="max-w-7xl mx-auto">
        <div class="mb-6 flex justify-between items-center">
            <div>
                <h1 class="text-3xl font-bold text-gray-800">
                    <i class="fas fa-user-tie mr-2 text-blue-500"></i>Ministry Executives
                </h1>
                <p class="text-gray-600 mt-2">Manage ministry leadership positions</p>
            </div>
            <div class="flex gap-3">
                <a href="index.php" class="bg-gray-500 text-white px-4 py-2 rounded-lg hover:bg-gray-600 transition">
                    <i class="fas fa-arrow-left mr-2"></i>Back to Ministries
                </a>
                <button onclick="document.getElementById('assignModal').classList.remove('hidden')" 
                        class="btn-gradient text-white px-6 py-3 rounded-lg transition">
                    <i class="fas fa-user-plus mr-2"></i>Assign Executive
                </button>
            </div>
        </div>
        
        <?php if ($success): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6">
                <i class="fas fa-check-circle mr-2"></i><?php echo $success; ?>
            </div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6">
                <i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?>
            </div>
        <?php endif; ?>
        
        <!-- Statistics -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-blue-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">Active Executives</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo count($executives); ?></p>
                    </div>
                    <i class="fas fa-users text-3xl text-blue-500"></i>
                </div>
            </div>
            
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-green-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">Leadership Positions</p>
                        <p class="text-2xl font-bold text-gray-800">
                            <?php echo count(array_filter($executives, fn($e) => $e['level'] == 1)); ?>
                        </p>
                    </div>
                    <i class="fas fa-crown text-3xl text-green-500"></i>
                </div>
            </div>
            
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-purple-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">Core Team</p>
                        <p class="text-2xl font-bold text-gray-800">
                            <?php echo count(array_filter($executives, fn($e) => $e['level'] == 2)); ?>
                        </p>
                    </div>
                    <i class="fas fa-users-cog text-3xl text-purple-500"></i>
                </div>
            </div>
            
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-orange-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">Recent Changes</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo min(count($history), 10); ?></p>
                    </div>
                    <i class="fas fa-history text-3xl text-orange-500"></i>
                </div>
            </div>
        </div>
        
        <!-- Tabs -->
        <div class="bg-white rounded-xl shadow-lg mb-6">
            <div class="border-b border-gray-200">
                <nav class="flex">
                    <button onclick="switchTab('active')" 
                            class="exec-tab px-6 py-4 font-medium border-b-2 border-blue-500 text-blue-600">
                        <i class="fas fa-users mr-2"></i>Active Executives
                    </button>
                    <button onclick="switchTab('history')" 
                            class="exec-tab px-6 py-4 font-medium text-gray-600 hover:text-blue-600 border-b-2 border-transparent">
                        <i class="fas fa-history mr-2"></i>History
                    </button>
                </nav>
            </div>
            
            <!-- Active Executives Tab -->
            <div id="activeTab" class="exec-tab-content p-6">
                <div class="overflow-x-auto">
                    <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">Ministry</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Position</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Executive</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Start Date</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($executives as $exec): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4">
                                    <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($exec['ministry_name']); ?></div>
                                    <div class="text-sm text-gray-500"><?php echo ucfirst($exec['ministry_type']); ?></div>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="px-3 py-1 text-xs font-medium rounded-full <?php 
                                        echo $exec['level'] == 1 ? 'bg-green-100 text-green-800' : 
                                            ($exec['level'] == 2 ? 'bg-blue-100 text-blue-800' : 'bg-gray-100 text-gray-800');
                                    ?>">
                                        <?php echo htmlspecialchars($exec['position_name']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900">
                                        <?php echo htmlspecialchars($exec['first_name'] . ' ' . $exec['last_name']); ?>
                                    </div>
                                    <div class="text-sm text-gray-500"><?php echo htmlspecialchars($exec['membershipcard_id']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    <?php echo date('M j, Y', strtotime($exec['start_date'])); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm">
                                    <span class="px-2 py-1 rounded-full bg-purple-100 text-purple-800 text-xs">
                                        <?php echo ucfirst($exec['appointment_type']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <form method="POST" class="inline" onsubmit="return confirm('End this assignment?')">
                                        <input type="hidden" name="executive_id" value="<?php echo $exec['id']; ?>">
                                        <button type="submit" name="end_assignment" 
                                                class="text-red-600 hover:text-red-900">
                                            <i class="fas fa-user-times mr-1"></i>End
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    
                    <?php if (empty($executives)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-user-tie text-6xl text-gray-300 mb-4"></i>
                        <p class="text-gray-600">No active executives</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- History Tab -->
            <div id="historyTab" class="exec-tab-content hidden p-6">
                <div class="space-y-4">
                    <?php foreach ($history as $item): ?>
                    <div class="bg-gray-50 rounded-lg p-4 border-l-4 <?php 
                        echo $item['action_type'] === 'appointed' ? 'border-green-500' : 
                            ($item['action_type'] === 'resigned' ? 'border-red-500' : 'border-gray-500');
                    ?>">
                        <div class="flex justify-between items-start">
                            <div>
                                <div class="flex items-center gap-2 mb-2">
                                    <span class="px-3 py-1 rounded-full text-xs font-medium <?php 
                                        echo $item['action_type'] === 'appointed' ? 'bg-green-100 text-green-800' : 
                                            ($item['action_type'] === 'resigned' ? 'bg-red-100 text-red-800' : 'bg-gray-100 text-gray-800');
                                    ?>">
                                        <?php echo ucfirst($item['action_type']); ?>
                                    </span>
                                    <span class="text-sm text-gray-500"><?php echo date('M j, Y', strtotime($item['action_date'])); ?></span>
                                </div>
                                <p class="text-sm font-medium text-gray-900">
                                    <?php echo htmlspecialchars($item['first_name'] . ' ' . $item['last_name']); ?> - 
                                    <?php echo htmlspecialchars($item['position_name']); ?>
                                </p>
                                <p class="text-sm text-gray-600"><?php echo htmlspecialchars($item['ministry_name']); ?></p>
                                <?php if ($item['reason']): ?>
                                    <p class="text-sm text-gray-500 mt-2"><?php echo htmlspecialchars($item['reason']); ?></p>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</main>

<!-- Assign Executive Modal -->
<div id="assignModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50">
    <div class="flex items-center justify-center min-h-screen p-4">
        <div class="bg-white rounded-lg max-w-2xl w-full p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-semibold">Assign Executive</h3>
                <button onclick="document.getElementById('assignModal').classList.add('hidden')" 
                        class="text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form method="POST" class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Ministry*</label>
                        <select name="ministry_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 Ministry</option>
                            <?php foreach ($ministries as $ministry): ?>
                                <option value="<?php echo $ministry['id']; ?>" <?php echo $ministryId == $ministry['id'] ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($ministry['ministry_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Member*</label>
                        <select name="member_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 Member</option>
                            <?php foreach ($members as $member): ?>
                                <option value="<?php echo $member['id']; ?>">
                                    <?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Position*</label>
                        <select name="position_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 Position</option>
                            <?php foreach ($positions as $position): ?>
                                <option value="<?php echo $position['id']; ?>">
                                    <?php echo htmlspecialchars($position['position_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Start Date*</label>
                        <input type="date" name="start_date" required value="<?php echo date('Y-m-d'); ?>"
                               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-medium text-gray-700 mb-2">End Date</label>
                        <input type="date" name="end_date"
                               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-medium text-gray-700 mb-2">Appointment Type*</label>
                        <select name="appointment_type" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                            <option value="appointed">Appointed</option>
                            <option value="elected">Elected</option>
                            <option value="interim">Interim</option>
                        </select>
                    </div>
                    
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Notes</label>
                        <textarea name="notes" 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>
                
                <div class="flex justify-end space-x-3 pt-4">
                    <button type="button" 
                            onclick="document.getElementById('assignModal').classList.add('hidden')"
                            class="px-4 py-2 text-gray-600 hover:text-gray-800">
                        Cancel
                    </button>
                    <button type="submit" name="assign_executive" 
                            class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600">
                        <i class="fas fa-user-plus mr-2"></i>Assign Executive
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function switchTab(tabName) {
    // Hide all tabs
    document.querySelectorAll('.exec-tab-content').forEach(content => {
        content.classList.add('hidden');
    });
    
    // Reset all tab buttons
    document.querySelectorAll('.exec-tab').forEach(tab => {
        tab.classList.remove('border-blue-500', 'text-blue-600');
        tab.classList.add('border-transparent', 'text-gray-600', 'hover:text-blue-600');
    });
    
    // Show selected tab
    document.getElementById(tabName + 'Tab').classList.remove('hidden');
    
    // Activate selected tab button
    event.target.classList.remove('border-transparent', 'text-gray-600', 'hover:text-blue-600');
    event.target.classList.add('border-blue-500', 'text-blue-600');
}
</script>

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

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