Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/demography-edit.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 data edit
if (!checkPermission('dataentry')) {
    header('Location: ' . $_SESSION['user_level'] . '.php?error=access_denied');
    exit();
}

$page_title = 'Demography 1 - Data Edit';
$page_description = 'Edit demographic data for assemblies';

$success_message = '';
$error_message = '';
$current_record = null;

// Get accessible areas, districts, and assemblies
$areas = getAccessibleAreas($_SESSION['user_level'], $_SESSION['area_id']);

// Determine current data type from URL parameter
$data_type = isset($_GET['type']) ? $_GET['type'] : 'membership';
$valid_types = ['membership', 'souls', 'transfers'];
if (!in_array($data_type, $valid_types)) {
    $data_type = 'membership';
}

// Handle record selection based on data type
if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
    $record_id = (int)$_GET['edit'];
    
    // Build query based on data type
    switch ($data_type) {
        case 'membership':
            $query = "SELECT d.*, a.name as area_name, dist.name as district_name, ass.name as assembly_name 
                      FROM demography_data d 
                      JOIN areas a ON d.area_id = a.id 
                      JOIN districts dist ON d.district_id = dist.id 
                      JOIN assemblies ass ON d.assembly_id = ass.id 
                      WHERE d.id = :id";
            break;
        case 'souls':
            $query = "SELECT s.*, a.name as area_name, dist.name as district_name, ass.name as assembly_name 
                      FROM souls_data s 
                      JOIN areas a ON s.area_id = a.id 
                      JOIN districts dist ON s.district_id = dist.id 
                      JOIN assemblies ass ON s.assembly_id = ass.id 
                      WHERE s.id = :id";
            break;
        case 'transfers':
            $query = "SELECT t.*, a.name as area_name, dist.name as district_name, ass.name as assembly_name 
                      FROM transfers_data t 
                      JOIN areas a ON t.area_id = a.id 
                      JOIN districts dist ON t.district_id = dist.id 
                      JOIN assemblies ass ON t.assembly_id = ass.id 
                      WHERE t.id = :id";
            break;
    }
    
    $stmt = $db->prepare($query);
    $stmt->bindParam(':id', $record_id);
    $stmt->execute();
    
    if ($stmt->rowCount() > 0) {
        $current_record = $stmt->fetch(PDO::FETCH_ASSOC);
    } else {
        $error_message = 'Record not found.';
    }
}

// Handle form submissions
if ($_POST && isset($_POST['update_demography'])) {
    $record_id = (int)$_POST['record_id'];
    $children_male = (int)$_POST['children_male'];
    $children_female = (int)$_POST['children_female'];
    $teens_male = (int)$_POST['teens_male'];
    $teens_female = (int)$_POST['teens_female'];
    $young_adults_male = (int)$_POST['young_adults_male'];
    $young_adults_female = (int)$_POST['young_adults_female'];
    $other_adults_male = (int)$_POST['other_adults_male'];
    $other_adults_female = (int)$_POST['other_adults_female'];
    
    try {
        // Get old values for audit
        $old_query = "SELECT * FROM demography_data WHERE id = :id";
        $old_stmt = $db->prepare($old_query);
        $old_stmt->bindParam(':id', $record_id);
        $old_stmt->execute();
        $old_values = $old_stmt->fetch(PDO::FETCH_ASSOC);
        
        // Update record
        $update_query = "UPDATE demography_data SET 
                        children_male = :children_male, children_female = :children_female,
                        teens_male = :teens_male, teens_female = :teens_female,
                        young_adults_male = :young_adults_male, young_adults_female = :young_adults_female,
                        other_adults_male = :other_adults_male, other_adults_female = :other_adults_female,
                        updated_by = :updated_by, updated_at = NOW()
                        WHERE id = :id";
        
        $update_stmt = $db->prepare($update_query);
        $update_stmt->bindParam(':children_male', $children_male);
        $update_stmt->bindParam(':children_female', $children_female);
        $update_stmt->bindParam(':teens_male', $teens_male);
        $update_stmt->bindParam(':teens_female', $teens_female);
        $update_stmt->bindParam(':young_adults_male', $young_adults_male);
        $update_stmt->bindParam(':young_adults_female', $young_adults_female);
        $update_stmt->bindParam(':other_adults_male', $other_adults_male);
        $update_stmt->bindParam(':other_adults_female', $other_adults_female);
        $update_stmt->bindParam(':updated_by', $_SESSION['user_id']);
        $update_stmt->bindParam(':id', $record_id);
        
        if ($update_stmt->execute()) {
            $success_message = 'Demography data updated successfully!';
            logAudit('UPDATE', 'demography_data', $record_id, $old_values, $_POST);
            createNotification($_SESSION['user_id'], 'Data Updated', 'Demography data has been successfully updated.', 'success');
            
            // Refresh current record
            header("Location: demography-edit.php?edit=$record_id&success=1");
            exit();
        } else {
            $error_message = 'Error updating demography data.';
        }
    } catch (Exception $e) {
        $error_message = 'Database error: ' . $e->getMessage();
    }
}

// Handle delete
if (isset($_POST['delete_demography']) && checkPermission('admin')) {
    $record_id = (int)$_POST['record_id'];
    
    try {
        $delete_query = "DELETE FROM demography_data WHERE id = :id";
        $delete_stmt = $db->prepare($delete_query);
        $delete_stmt->bindParam(':id', $record_id);
        
        if ($delete_stmt->execute()) {
            $success_message = 'Record deleted successfully!';
            logAudit('DELETE', 'demography_data', $record_id, $current_record, null);
            createNotification($_SESSION['user_id'], 'Data Deleted', 'Demography record has been deleted.', 'warning');
            $current_record = null;
        } else {
            $error_message = 'Error deleting record.';
        }
    } catch (Exception $e) {
        $error_message = 'Database error: ' . $e->getMessage();
    }
} elseif ($_POST && isset($_POST['update_souls'])) {
    $record_id = (int)$_POST['record_id'];
    
    try {
        // Get old values for audit
        $old_query = "SELECT * FROM souls_data WHERE id = :id";
        $old_stmt = $db->prepare($old_query);
        $old_stmt->bindParam(':id', $record_id);
        $old_stmt->execute();
        $old_values = $old_stmt->fetch(PDO::FETCH_ASSOC);
        
        // Update record
        $update_query = "UPDATE souls_data SET 
                        outreach_program = :outreach_program, adult_souls_won_cop = :adult_souls_won_cop, other_souls_won = :other_souls_won,
                        gospel_sunday_morning = :gospel_sunday_morning, hum = :hum, mpwds = :mpwds, chaplaincy = :chaplaincy,
                        chieftancy = :chieftancy, som = :som, digital_space = :digital_space,
                        baptized_in_water = :baptized_in_water, holy_spirit_baptism = :holy_spirit_baptism, 
                        old_members_now_baptized_holy_spirit = :old_members_baptized,
                        updated_by = :updated_by, updated_at = NOW()
                        WHERE id = :id";
        
        $update_stmt = $db->prepare($update_query);
        $update_stmt->bindValue(':outreach_program', (int)$_POST['outreach_program']);
        $update_stmt->bindValue(':adult_souls_won_cop', (int)$_POST['adult_souls_won_cop']);
        $update_stmt->bindValue(':other_souls_won', (int)$_POST['other_souls_won']);
        $update_stmt->bindValue(':gospel_sunday_morning', (int)$_POST['gospel_sunday_morning']);
        $update_stmt->bindValue(':hum', (int)$_POST['hum']);
        $update_stmt->bindValue(':mpwds', (int)$_POST['mpwds']);
        $update_stmt->bindValue(':chaplaincy', (int)$_POST['chaplaincy']);
        $update_stmt->bindValue(':chieftancy', (int)$_POST['chieftancy']);
        $update_stmt->bindValue(':som', (int)$_POST['som']);
        $update_stmt->bindValue(':digital_space', (int)$_POST['digital_space']);
        $update_stmt->bindValue(':baptized_in_water', (int)$_POST['baptized_in_water']);
        $update_stmt->bindValue(':holy_spirit_baptism', (int)$_POST['holy_spirit_baptism']);
        $update_stmt->bindValue(':old_members_baptized', (int)$_POST['old_members_baptized']);
        $update_stmt->bindValue(':updated_by', $_SESSION['user_id']);
        $update_stmt->bindValue(':id', $record_id);
        
        if ($update_stmt->execute()) {
            $success_message = 'Souls data updated successfully!';
            logAudit('UPDATE', 'souls_data', $record_id, $old_values, $_POST);
            createNotification($_SESSION['user_id'], 'Data Updated', 'Souls data has been successfully updated.', 'success');
            
            // Refresh current record
            header("Location: demography-edit.php?type=souls&edit=$record_id&success=1");
            exit();
        } else {
            $error_message = 'Error updating souls data.';
        }
    } catch (Exception $e) {
        $error_message = 'Database error: ' . $e->getMessage();
    }
} elseif ($_POST && isset($_POST['update_transfers'])) {
    $record_id = (int)$_POST['record_id'];
    
    try {
        // Get old values for audit
        $old_query = "SELECT * FROM transfers_data WHERE id = :id";
        $old_stmt = $db->prepare($old_query);
        $old_stmt->bindParam(':id', $record_id);
        $old_stmt->execute();
        $old_values = $old_stmt->fetch(PDO::FETCH_ASSOC);
        
        // Update record
        $update_query = "UPDATE transfers_data SET 
                        transfers_in_13_19 = :transfers_in_13_19, transfers_in_20_35 = :transfers_in_20_35, transfers_in_above_35 = :transfers_in_above_35,
                        transfers_out_13_19 = :transfers_out_13_19, transfers_out_20_35 = :transfers_out_20_35, transfers_out_above_35 = :transfers_out_above_35,
                        updated_by = :updated_by, updated_at = NOW()
                        WHERE id = :id";
        
        $update_stmt = $db->prepare($update_query);
        $update_stmt->bindValue(':transfers_in_13_19', (int)$_POST['transfers_in_13_19']);
        $update_stmt->bindValue(':transfers_in_20_35', (int)$_POST['transfers_in_20_35']);
        $update_stmt->bindValue(':transfers_in_above_35', (int)$_POST['transfers_in_above_35']);
        $update_stmt->bindValue(':transfers_out_13_19', (int)$_POST['transfers_out_13_19']);
        $update_stmt->bindValue(':transfers_out_20_35', (int)$_POST['transfers_out_20_35']);
        $update_stmt->bindValue(':transfers_out_above_35', (int)$_POST['transfers_out_above_35']);
        $update_stmt->bindValue(':updated_by', $_SESSION['user_id']);
        $update_stmt->bindValue(':id', $record_id);
        
        if ($update_stmt->execute()) {
            $success_message = 'Transfers data updated successfully!';
            logAudit('UPDATE', 'transfers_data', $record_id, $old_values, $_POST);
            createNotification($_SESSION['user_id'], 'Data Updated', 'Transfers data has been successfully updated.', 'success');
            
            // Refresh current record
            header("Location: demography-edit.php?type=transfers&edit=$record_id&success=1");
            exit();
        } else {
            $error_message = 'Error updating transfers data.';
        }
    } catch (Exception $e) {
        $error_message = 'Database error: ' . $e->getMessage();
    }
}

// Get all records for selection based on data type
switch ($data_type) {
    case 'membership':
        $records_query = "SELECT d.id, d.created_at, a.name as area_name, dist.name as district_name, ass.name as assembly_name,
                          d.overall_members as summary_field, u.first_name, u.last_name
                          FROM demography_data d 
                          JOIN areas a ON d.area_id = a.id 
                          JOIN districts dist ON d.district_id = dist.id 
                          JOIN assemblies ass ON d.assembly_id = ass.id 
                          JOIN users u ON d.created_by = u.id
                          ORDER BY d.created_at DESC";
        break;
    case 'souls':
        $records_query = "SELECT s.id, s.created_at, a.name as area_name, dist.name as district_name, ass.name as assembly_name,
                          (s.outreach_program + s.adult_souls_won_cop + s.other_souls_won) as summary_field, u.first_name, u.last_name
                          FROM souls_data s 
                          JOIN areas a ON s.area_id = a.id 
                          JOIN districts dist ON s.district_id = dist.id 
                          JOIN assemblies ass ON s.assembly_id = ass.id 
                          JOIN users u ON s.created_by = u.id
                          ORDER BY s.created_at DESC";
        break;
    case 'transfers':
        $records_query = "SELECT t.id, t.created_at, a.name as area_name, dist.name as district_name, ass.name as assembly_name,
                          t.transfers_in_total as summary_field, u.first_name, u.last_name
                          FROM transfers_data t 
                          JOIN areas a ON t.area_id = a.id 
                          JOIN districts dist ON t.district_id = dist.id 
                          JOIN assemblies ass ON t.assembly_id = ass.id 
                          JOIN users u ON t.created_by = u.id
                          ORDER BY t.created_at DESC";
        break;
}

$records_stmt = $db->prepare($records_query);
$records_stmt->execute();
$all_records = $records_stmt->fetchAll(PDO::FETCH_ASSOC);

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

<div class="bg-white rounded-lg shadow-sm border border-gray-200">
    <!-- Tab Navigation -->
    <div class="border-b border-gray-200">
        <nav class="flex space-x-8 px-6" aria-label="Tabs">
            <a href="demography-edit.php?type=membership" class="border-b-2 <?php echo $data_type == 'membership' ? 'border-cop-blue text-cop-blue' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'; ?> py-4 px-1 text-sm font-medium">
                Membership
            </a>
            <a href="demography-edit.php?type=souls" class="border-b-2 <?php echo $data_type == 'souls' ? 'border-cop-blue text-cop-blue' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'; ?> py-4 px-1 text-sm font-medium">
                Souls
            </a>
            <a href="demography-edit.php?type=transfers" class="border-b-2 <?php echo $data_type == 'transfers' ? 'border-cop-blue text-cop-blue' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'; ?> py-4 px-1 text-sm font-medium">
                Transfers
            </a>
        </nav>
    </div>

    <div class="p-6">
        <?php if (isset($_GET['success'])): ?>
        <div class="mb-6 bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg">
            <i class="fas fa-check-circle mr-2"></i>Record updated successfully!
        </div>
        <?php endif; ?>

        <?php if ($success_message): ?>
        <div class="mb-6 bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg">
            <i class="fas fa-check-circle mr-2"></i><?php echo htmlspecialchars($success_message); ?>
        </div>
        <?php endif; ?>

        <?php if ($error_message): ?>
        <div class="mb-6 bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg">
            <i class="fas fa-exclamation-circle mr-2"></i><?php echo htmlspecialchars($error_message); ?>
        </div>
        <?php endif; ?>

        <!-- Dynamic Content Based on Data Type -->
        <?php if (!$current_record): ?>
        <!-- Record Selection -->
        <div class="mb-8">
            <h3 class="text-lg font-semibold text-gray-800 mb-4">Select Record to Edit</h3>
            <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 tracking-wider">Area</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">Assembly</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                <?php 
                                switch($data_type) {
                                    case 'membership': echo 'Total Members'; break;
                                    case 'souls': echo 'Total Souls Won'; break;
                                    case 'transfers': echo 'Transfers In'; break;
                                }
                                ?>
                            </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">Date</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 ($all_records as $record): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?php echo htmlspecialchars($record['area_name']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?php echo htmlspecialchars($record['district_name']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?php echo htmlspecialchars($record['assembly_name']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?php echo number_format($record['summary_field']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?php echo htmlspecialchars($record['first_name'] . ' ' . $record['last_name']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo formatDateTime($record['created_at']); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <a href="?type=<?php echo $data_type; ?>&edit=<?php echo $record['id']; ?>" class="text-cop-blue hover:text-cop-dark-blue mr-3">
                                    <i class="fas fa-edit mr-1"></i>Edit
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php else: ?>
        <!-- Edit Form -->
        <div class="mb-4">
            <a href="demography-edit.php" class="text-cop-blue hover:text-cop-dark-blue">
                <i class="fas fa-arrow-left mr-2"></i>Back to Record List
            </a>
        </div>

        <div class="bg-blue-50 p-4 rounded-lg mb-6">
            <h3 class="font-semibold text-gray-800">Editing <?php echo ucfirst($data_type); ?> Data: <?php echo htmlspecialchars($current_record['area_name'] . ' > ' . $current_record['district_name'] . ' > ' . $current_record['assembly_name']); ?></h3>
        </div>

        <form method="POST" id="editForm" class="space-y-8">
            <input type="hidden" name="record_id" value="<?php echo $current_record['id']; ?>">
            <input type="hidden" name="data_type" value="<?php echo $data_type; ?>">
            
            <?php if ($data_type == 'membership'): ?>

            <!-- Children's Membership -->
            <div class="bg-blue-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">1. CHILDREN'S MEMBERSHIP (below 13yrs)</h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div>
                        <label for="children_male" class="block text-sm font-medium text-gray-700 mb-2">Male</label>
                        <input type="number" name="children_male" id="children_male" min="0" 
                               value="<?php echo $current_record['children_male']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="children_female" class="block text-sm font-medium text-gray-700 mb-2">Female</label>
                        <input type="number" name="children_female" id="children_female" min="0" 
                               value="<?php echo $current_record['children_female']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Total (1i)</label>
                        <input type="text" id="children_total" readonly 
                               class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                    </div>
                </div>
            </div>

            <!-- Youth Membership -->
            <div class="bg-green-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-6">2. YOUTH MEMBERSHIP (13 to 35 years)</h3>
                
                <!-- Teens -->
                <div class="mb-6">
                    <h4 class="text-md font-medium text-gray-700 mb-4">2.1 TEENS (13-19YRS)</h4>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div>
                            <label for="teens_male" class="block text-sm font-medium text-gray-700 mb-2">Male</label>
                            <input type="number" name="teens_male" id="teens_male" min="0" 
                                   value="<?php echo $current_record['teens_male']; ?>"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                        </div>
                        <div>
                            <label for="teens_female" class="block text-sm font-medium text-gray-700 mb-2">Female</label>
                            <input type="number" name="teens_female" id="teens_female" min="0" 
                                   value="<?php echo $current_record['teens_female']; ?>"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">Total</label>
                            <input type="text" id="teens_total" readonly 
                                   class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                        </div>
                    </div>
                </div>

                <!-- Young Adults -->
                <div class="mb-6">
                    <h4 class="text-md font-medium text-gray-700 mb-4">2.2 YOUNG ADULTS (20-35 YRS)</h4>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div>
                            <label for="young_adults_male" class="block text-sm font-medium text-gray-700 mb-2">Male</label>
                            <input type="number" name="young_adults_male" id="young_adults_male" min="0" 
                                   value="<?php echo $current_record['young_adults_male']; ?>"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                        </div>
                        <div>
                            <label for="young_adults_female" class="block text-sm font-medium text-gray-700 mb-2">Female</label>
                            <input type="number" name="young_adults_female" id="young_adults_female" min="0" 
                                   value="<?php echo $current_record['young_adults_female']; ?>"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">Total</label>
                            <input type="text" id="young_adults_total" readonly 
                                   class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                        </div>
                    </div>
                </div>

                <!-- Youth Total -->
                <div class="bg-white p-4 rounded-lg border-2 border-green-300">
                    <div class="flex justify-between items-center">
                        <span class="font-semibold text-gray-800">Youth Total (a)</span>
                        <input type="text" id="youth_total" readonly 
                               class="px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600 font-semibold">
                    </div>
                </div>
            </div>

            <!-- Other Adults -->
            <div class="bg-yellow-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">3. OTHER ADULTS (above 35 years)</h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
                    <div>
                        <label for="other_adults_male" class="block text-sm font-medium text-gray-700 mb-2">Male</label>
                        <input type="number" name="other_adults_male" id="other_adults_male" min="0" 
                               value="<?php echo $current_record['other_adults_male']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="other_adults_female" class="block text-sm font-medium text-gray-700 mb-2">Female</label>
                        <input type="number" name="other_adults_female" id="other_adults_female" min="0" 
                               value="<?php echo $current_record['other_adults_female']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Total (b)</label>
                        <input type="text" id="other_adults_total" readonly 
                               class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                    </div>
                </div>
            </div>

            <!-- Summary -->
            <div class="bg-gray-800 text-white p-6 rounded-lg">
                <h3 class="text-lg font-semibold mb-6">SUMMARY</h3>
                <div class="space-y-4">
                    <div class="flex justify-between items-center py-2 border-b border-gray-600">
                        <span class="font-medium">Total Adult Members (a)+(b)</span>
                        <input type="text" id="total_adult_members" readonly 
                               class="px-3 py-2 bg-gray-700 border border-gray-600 rounded-lg text-white font-semibold">
                    </div>
                    <div class="flex justify-between items-center py-2">
                        <span class="font-semibold text-lg">Overall Members (Adults & Children)</span>
                        <input type="text" id="overall_members" readonly 
                               class="px-3 py-2 bg-gray-700 border border-gray-600 rounded-lg text-white font-bold text-lg">
                    </div>
                </div>
            </div>

            <!-- Action Buttons -->
            <div class="flex justify-between">
                <div>
                    <?php if (checkPermission('admin')): ?>
                    <button type="button" onclick="confirmDelete()" 
                            class="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition duration-200">
                        <i class="fas fa-trash mr-2"></i>Delete Record
                    </button>
                    <?php endif; ?>
                </div>
                <div class="space-x-4">
                    <a href="demography-edit.php" 
                       class="px-6 py-3 bg-gray-500 text-white rounded-lg hover:bg-gray-600 transition duration-200">
                        <i class="fas fa-times mr-2"></i>Cancel
                    </a>
                    <button type="submit" name="update_demography" 
                            class="px-6 py-3 bg-cop-blue text-white rounded-lg hover:bg-cop-dark-blue transition duration-200">
                        <i class="fas fa-save mr-2"></i>Update Record
                    </button>
                </div>
            </div>
            
            <?php elseif ($data_type == 'souls'): ?>
            
            <!-- 1. Souls Won -->
            <div class="bg-blue-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">1. SOULS WON</h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div>
                        <label for="outreach_program" class="block text-sm font-medium text-gray-700 mb-2">OUT-REACH PROGRAM</label>
                        <input type="number" name="outreach_program" id="outreach_program" min="0" 
                               value="<?php echo $current_record['outreach_program']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="adult_souls_won_cop" class="block text-sm font-medium text-gray-700 mb-2">ADULT SOULS WON(COP)</label>
                        <input type="number" name="adult_souls_won_cop" id="adult_souls_won_cop" min="0" 
                               value="<?php echo $current_record['adult_souls_won_cop']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="other_souls_won" class="block text-sm font-medium text-gray-700 mb-2">OTHER SOULS WON</label>
                        <input type="number" name="other_souls_won" id="other_souls_won" min="0" 
                               value="<?php echo $current_record['other_souls_won']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                </div>
            </div>

            <!-- 2. Souls Won Through -->
            <div class="bg-green-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">2. SOULS WON THROUGH</h3>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                    <div>
                        <label for="gospel_sunday_morning" class="block text-sm font-medium text-gray-700 mb-2">GOSPEL SUNDAY MORNING</label>
                        <input type="number" name="gospel_sunday_morning" id="gospel_sunday_morning" min="0" 
                               value="<?php echo $current_record['gospel_sunday_morning']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="hum" class="block text-sm font-medium text-gray-700 mb-2">HUM</label>
                        <input type="number" name="hum" id="hum" min="0" 
                               value="<?php echo $current_record['hum']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="mpwds" class="block text-sm font-medium text-gray-700 mb-2">MPWDs</label>
                        <input type="number" name="mpwds" id="mpwds" min="0" 
                               value="<?php echo $current_record['mpwds']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="chaplaincy" class="block text-sm font-medium text-gray-700 mb-2">CHAPLAINCY</label>
                        <input type="number" name="chaplaincy" id="chaplaincy" min="0" 
                               value="<?php echo $current_record['chaplaincy']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="chieftancy" class="block text-sm font-medium text-gray-700 mb-2">CHIEFTANCY</label>
                        <input type="number" name="chieftancy" id="chieftancy" min="0" 
                               value="<?php echo $current_record['chieftancy']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="som" class="block text-sm font-medium text-gray-700 mb-2">SOM</label>
                        <input type="number" name="som" id="som" min="0" 
                               value="<?php echo $current_record['som']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="digital_space" class="block text-sm font-medium text-gray-700 mb-2">DIGITAL SPACE</label>
                        <input type="number" name="digital_space" id="digital_space" min="0" 
                               value="<?php echo $current_record['digital_space']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                </div>
            </div>

            <!-- 3. Converts -->
            <div class="bg-yellow-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">3. CONVERTS</h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div>
                        <label for="baptized_in_water" class="block text-sm font-medium text-gray-700 mb-2">BAPTIZED IN WATER</label>
                        <input type="number" name="baptized_in_water" id="baptized_in_water" min="0" 
                               value="<?php echo $current_record['baptized_in_water']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="holy_spirit_baptism" class="block text-sm font-medium text-gray-700 mb-2">HOLY SPIRIT BAPTISM</label>
                        <input type="number" name="holy_spirit_baptism" id="holy_spirit_baptism" min="0" 
                               value="<?php echo $current_record['holy_spirit_baptism']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="old_members_baptized" class="block text-sm font-medium text-gray-700 mb-2">OLD MEMBERS NOW BAPTIZED IN HOLY SPIRIT</label>
                        <input type="number" name="old_members_baptized" id="old_members_baptized" min="0" 
                               value="<?php echo $current_record['old_members_now_baptized_holy_spirit']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="flex justify-end space-x-4">
                <a href="demography-edit.php?type=souls" 
                   class="px-6 py-3 bg-gray-500 text-white rounded-lg hover:bg-gray-600 transition duration-200">
                    <i class="fas fa-times mr-2"></i>Cancel
                </a>
                <button type="submit" name="update_souls" 
                        class="px-6 py-3 bg-cop-blue text-white rounded-lg hover:bg-cop-dark-blue transition duration-200">
                    <i class="fas fa-save mr-2"></i>Update Souls Data
                </button>
            </div>
            
            <?php elseif ($data_type == 'transfers'): ?>
            
            <!-- 1. Transfers In -->
            <div class="bg-blue-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">1. TRANSFERS IN</h3>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                    <div>
                        <label for="transfers_in_13_19" class="block text-sm font-medium text-gray-700 mb-2">13 - 19 YRS</label>
                        <input type="number" name="transfers_in_13_19" id="transfers_in_13_19" min="0" 
                               value="<?php echo $current_record['transfers_in_13_19']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="transfers_in_20_35" class="block text-sm font-medium text-gray-700 mb-2">20-35 YRS</label>
                        <input type="number" name="transfers_in_20_35" id="transfers_in_20_35" min="0" 
                               value="<?php echo $current_record['transfers_in_20_35']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="transfers_in_above_35" class="block text-sm font-medium text-gray-700 mb-2">ABOVE 35YRS</label>
                        <input type="number" name="transfers_in_above_35" id="transfers_in_above_35" min="0" 
                               value="<?php echo $current_record['transfers_in_above_35']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">TOTAL</label>
                        <input type="text" id="transfers_in_total" readonly 
                               value="<?php echo $current_record['transfers_in_total']; ?>"
                               class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                    </div>
                </div>
            </div>

            <!-- 2. Transfers Out -->
            <div class="bg-red-50 p-6 rounded-lg">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">2. TRANSFERS OUT</h3>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                    <div>
                        <label for="transfers_out_13_19" class="block text-sm font-medium text-gray-700 mb-2">13 - 19 YRS</label>
                        <input type="number" name="transfers_out_13_19" id="transfers_out_13_19" min="0" 
                               value="<?php echo $current_record['transfers_out_13_19']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="transfers_out_20_35" class="block text-sm font-medium text-gray-700 mb-2">20-35 YRS</label>
                        <input type="number" name="transfers_out_20_35" id="transfers_out_20_35" min="0" 
                               value="<?php echo $current_record['transfers_out_20_35']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label for="transfers_out_above_35" class="block text-sm font-medium text-gray-700 mb-2">ABOVE 35YRS</label>
                        <input type="number" name="transfers_out_above_35" id="transfers_out_above_35" min="0" 
                               value="<?php echo $current_record['transfers_out_above_35']; ?>"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-cop-blue focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">TOTAL</label>
                        <input type="text" id="transfers_out_total" readonly 
                               value="<?php echo $current_record['transfers_out_total']; ?>"
                               class="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600">
                    </div>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="flex justify-end space-x-4">
                <a href="demography-edit.php?type=transfers" 
                   class="px-6 py-3 bg-gray-500 text-white rounded-lg hover:bg-gray-600 transition duration-200">
                    <i class="fas fa-times mr-2"></i>Cancel
                </a>
                <button type="submit" name="update_transfers" 
                        class="px-6 py-3 bg-cop-blue text-white rounded-lg hover:bg-cop-dark-blue transition duration-200">
                    <i class="fas fa-save mr-2"></i>Update Transfers Data
                </button>
            </div>
            
            <?php endif; ?>
        </form>

        <!-- Hidden Delete Form -->
        <form id="deleteForm" method="POST" style="display: none;">
            <input type="hidden" name="record_id" value="<?php echo $current_record['id']; ?>">
            <input type="hidden" name="delete_demography" value="1">
        </form>
        <?php endif; ?>
        </div>

        <!-- Content is now handled by the data type parameter and single interface -->
    </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');
}

function calculateTotals() {
    const childrenMale = parseInt(document.getElementById('children_male').value) || 0;
    const childrenFemale = parseInt(document.getElementById('children_female').value) || 0;
    document.getElementById('children_total').value = childrenMale + childrenFemale;
    
    const teensMale = parseInt(document.getElementById('teens_male').value) || 0;
    const teensFemale = parseInt(document.getElementById('teens_female').value) || 0;
    const teensTotal = teensMale + teensFemale;
    document.getElementById('teens_total').value = teensTotal;
    
    const youngAdultsMale = parseInt(document.getElementById('young_adults_male').value) || 0;
    const youngAdultsFemale = parseInt(document.getElementById('young_adults_female').value) || 0;
    const youngAdultsTotal = youngAdultsMale + youngAdultsFemale;
    document.getElementById('young_adults_total').value = youngAdultsTotal;
    
    const youthTotal = teensTotal + youngAdultsTotal;
    document.getElementById('youth_total').value = youthTotal;
    
    const otherAdultsMale = parseInt(document.getElementById('other_adults_male').value) || 0;
    const otherAdultsFemale = parseInt(document.getElementById('other_adults_female').value) || 0;
    const otherAdultsTotal = otherAdultsMale + otherAdultsFemale;
    document.getElementById('other_adults_total').value = otherAdultsTotal;
    
    const totalAdultMembers = youthTotal + otherAdultsTotal;
    document.getElementById('total_adult_members').value = totalAdultMembers;
    
    const overallMembers = (childrenMale + childrenFemale) + totalAdultMembers;
    document.getElementById('overall_members').value = overallMembers;
}

function confirmDelete() {
    if (confirm('Are you sure you want to delete this record? This action cannot be undone.')) {
        document.getElementById('deleteForm').submit();
    }
}

// Auto-calculation functions for Transfers tab
function calculateTransfersTotals() {
    // Transfers In total
    const transfersIn13_19 = parseInt(document.getElementById('transfers_in_13_19')?.value) || 0;
    const transfersIn20_35 = parseInt(document.getElementById('transfers_in_20_35')?.value) || 0;
    const transfersInAbove35 = parseInt(document.getElementById('transfers_in_above_35')?.value) || 0;
    const transfersInTotal = transfersIn13_19 + transfersIn20_35 + transfersInAbove35;
    if (document.getElementById('transfers_in_total')) {
        document.getElementById('transfers_in_total').value = transfersInTotal;
    }
    
    // Transfers Out total
    const transfersOut13_19 = parseInt(document.getElementById('transfers_out_13_19')?.value) || 0;
    const transfersOut20_35 = parseInt(document.getElementById('transfers_out_20_35')?.value) || 0;
    const transfersOutAbove35 = parseInt(document.getElementById('transfers_out_above_35')?.value) || 0;
    const transfersOutTotal = transfersOut13_19 + transfersOut20_35 + transfersOutAbove35;
    if (document.getElementById('transfers_out_total')) {
        document.getElementById('transfers_out_total').value = transfersOutTotal;
    }
}

document.addEventListener('DOMContentLoaded', function() {
    const inputs = document.querySelectorAll('input[type="number"]');
    inputs.forEach(input => {
        input.addEventListener('input', function() {
            calculateTotals();
            calculateTransfersTotals();
        });
    });
    
    calculateTotals();
    calculateTransfersTotals();
});
</script>

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

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