Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/check_status.php

<?php
require_once 'config/config.php';

$record = null;
$error_message = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
        $error_message = 'Invalid security token. Please try again.';
    } else {
        $tracking_code = sanitizeInput($_POST['tracking_code'] ?? '');
        
        if (empty($tracking_code)) {
            $error_message = 'Please enter a tracking code.';
        } else {
            // Get attendance record details
            $db = new Database();
            $conn = $db->getConnection();

            $query = "SELECT ar.*, p.name as program_name, p.description as program_description,
                             l.name as location_name, l.address as location_address,
                             ld.name as district_name_full, la.name as assembly_name_full
                      FROM attendance_records ar 
                      JOIN programs p ON ar.program_id = p.id 
                      LEFT JOIN locations l ON p.location_id = l.id 
                      LEFT JOIN locations ld ON ar.district_id = ld.id 
                      LEFT JOIN locations la ON ar.assembly_id = la.id 
                      WHERE ar.tracking_code = ?";
            $stmt = $conn->prepare($query);
            $stmt->execute([$tracking_code]);
            $record = $stmt->fetch();
            
            if (!$record) {
                $error_message = 'No attendance record found with this tracking code. Please check your code and try again.';
            } else {
                // Get program details including duration
                $program_query = "SELECT *, 
                                         CASE 
                                             WHEN end_date IS NULL OR end_date >= CURDATE() THEN 1 
                                             ELSE 0 
                                         END as is_active
                                  FROM programs WHERE id = ?";
                $program_stmt = $conn->prepare($program_query);
                $program_stmt->execute([$record['program_id']]);
                $program_details = $program_stmt->fetch();
                
                // Get all attendance records for this person and program
                $attendance_query = "SELECT DATE(submitted_at) as attendance_date, 
                                           submitted_at,
                                           tracking_code
                                    FROM attendance_records 
                                    WHERE full_name = ? 
                                    AND program_id = ? 
                                    AND email = ?
                                    ORDER BY submitted_at ASC";
                $attendance_stmt = $conn->prepare($attendance_query);
                $attendance_stmt->execute([
                    $record['full_name'], 
                    $record['program_id'], 
                    $record['email']
                ]);
                $all_attendance = $attendance_stmt->fetchAll();
                
                // Check if user is registered for today
                $today = date('Y-m-d');
                $registered_today = false;
                foreach ($all_attendance as $attendance) {
                    if ($attendance['attendance_date'] === $today) {
                        $registered_today = true;
                        break;
                    }
                }
                
                // Store attendance data in record for display
                $record['attendance_count'] = count($all_attendance);
                $record['attendance_dates'] = $all_attendance;
                $record['program_is_active'] = $program_details['is_active'];
                $record['program_end_date'] = $program_details['end_date'];
                $record['registered_today'] = $registered_today;
                $record['can_register_today'] = $program_details['is_active'] && !$registered_today;
            }
        }
    }
}

// Handle direct access with code parameter
if (!$record && isset($_GET['code']) && !empty($_GET['code'])) {
    $tracking_code = sanitizeInput($_GET['code']);
    
    $db = new Database();
    $conn = $db->getConnection();

    $query = "SELECT ar.*, p.name as program_name, p.description as program_description,
                     l.name as location_name, l.address as location_address,
                     ld.name as district_name_full, la.name as assembly_name_full
              FROM attendance_records ar 
              JOIN programs p ON ar.program_id = p.id 
              LEFT JOIN locations l ON p.location_id = l.id 
              LEFT JOIN locations ld ON ar.district_id = ld.id 
              LEFT JOIN locations la ON ar.assembly_id = la.id 
              WHERE ar.tracking_code = ?";
    $stmt = $conn->prepare($query);
    $stmt->execute([$tracking_code]);
    $record = $stmt->fetch();
    
    if ($record) {
        // Get program details including duration
        $program_query = "SELECT *, 
                                 CASE 
                                     WHEN end_date IS NULL OR end_date >= CURDATE() THEN 1 
                                     ELSE 0 
                                 END as is_active
                          FROM programs WHERE id = ?";
        $program_stmt = $conn->prepare($program_query);
        $program_stmt->execute([$record['program_id']]);
        $program_details = $program_stmt->fetch();
        
        // Get all attendance records for this person and program
        $attendance_query = "SELECT DATE(submitted_at) as attendance_date, 
                                   submitted_at,
                                   tracking_code
                            FROM attendance_records 
                            WHERE full_name = ? 
                            AND program_id = ? 
                            AND email = ?
                            ORDER BY submitted_at ASC";
        $attendance_stmt = $conn->prepare($attendance_query);
        $attendance_stmt->execute([
            $record['full_name'], 
            $record['program_id'], 
            $record['email']
        ]);
        $all_attendance = $attendance_stmt->fetchAll();
        
        // Check if user is registered for today
        $today = date('Y-m-d');
        $registered_today = false;
        foreach ($all_attendance as $attendance) {
            if ($attendance['attendance_date'] === $today) {
                $registered_today = true;
                break;
            }
        }
        
        // Store attendance data in record for display
        $record['attendance_count'] = count($all_attendance);
        $record['attendance_dates'] = $all_attendance;
        $record['program_is_active'] = $program_details['is_active'];
        $record['program_end_date'] = $program_details['end_date'];
        $record['registered_today'] = $registered_today;
        $record['can_register_today'] = $program_details['is_active'] && !$registered_today;
    }
}

// Get site settings
$db = new Database();
$conn = $db->getConnection();
$query = "SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('site_title', 'site_logo')";
$stmt = $conn->prepare($query);
$stmt->execute();
$settings = [];
while ($row = $stmt->fetch()) {
    $settings[$row['setting_key']] = $row['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Check Status - <?php echo $settings['site_title'] ?? SITE_TITLE; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#F59E0B',
                        accent: '#6B7280'
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #3B82F6 0%, #F59E0B 50%, #6B7280 100%);
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <!-- Header -->
    <header class="gradient-bg text-white py-6">
        <div class="container mx-auto px-4">
            <div class="flex items-center justify-between">
                <div class="flex items-center">
                    <img src="<?php echo $settings['site_logo'] ?? SITE_LOGO; ?>" alt="Logo" class="h-12 w-12 mr-4">
                    <h1 class="text-2xl font-bold"><?php echo $settings['site_title'] ?? SITE_TITLE; ?></h1>
                </div>
                <nav class="hidden md:flex space-x-6">
                    <a href="index.php" class="hover:text-yellow-300 transition duration-300">
                        <i class="fas fa-home mr-2"></i>Home
                    </a>
                    <a href="check_status.php" class="text-yellow-300">
                        <i class="fas fa-search mr-2"></i>Check Status
                    </a>
                </nav>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto px-4 py-12">
        <div class="max-w-4xl mx-auto">
            <!-- Page Title -->
            <div class="text-center mb-8">
                <h2 class="text-3xl font-bold text-gray-900 mb-4">Check Attendance Status</h2>
                <p class="text-lg text-gray-600">Enter your tracking code to view your attendance details</p>
            </div>

            <!-- Search Form -->
            <div class="bg-white rounded-lg shadow-lg p-8 mb-8">
                <form method="POST" class="max-w-md mx-auto">
                    <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                    
                    <div class="mb-6">
                        <label for="tracking_code" class="block text-sm font-medium text-gray-700 mb-2">
                            <i class="fas fa-barcode mr-2"></i>Tracking Code
                        </label>
                        <input type="text" 
                               id="tracking_code" 
                               name="tracking_code" 
                               value="<?php echo htmlspecialchars($_POST['tracking_code'] ?? $_GET['code'] ?? ''); ?>"
                               class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent text-center text-lg font-mono uppercase"
                               placeholder="Enter your tracking code"
                               maxlength="20"
                               required>
                        <p class="text-sm text-gray-500 mt-2">
                            Example: ABC12345
                        </p>
                    </div>

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

                    <button type="submit" class="w-full bg-primary text-white py-3 px-6 rounded-lg hover:bg-blue-700 transition duration-300 font-semibold">
                        <i class="fas fa-search mr-2"></i>Check Status
                    </button>
                </form>
            </div>

            <!-- Results -->
            <?php if ($record): ?>
                <div class="bg-white rounded-lg shadow-lg p-8">
                    <!-- Success Header -->
                    <div class="text-center mb-8">
                        <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-check-circle text-green-600 text-2xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-900 mb-2">Attendance Record Found</h3>
                        <p class="text-gray-600">Here are your attendance details</p>
                    </div>

                    <!-- Tracking Code Display -->
                    <div class="bg-blue-50 border-2 border-blue-200 rounded-lg p-6 mb-8 text-center">
                        <h4 class="text-lg font-semibold text-gray-900 mb-2">Tracking Code</h4>
                        <div class="text-2xl font-mono font-bold text-primary">
                            <?php echo htmlspecialchars($record['tracking_code']); ?>
                        </div>
                    </div>

                    <!-- Attendance Details -->
                    <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                        <!-- Personal Information -->
                        <div class="bg-gray-50 rounded-lg p-6">
                            <h4 class="text-lg font-semibold text-gray-900 mb-4">
                                <i class="fas fa-user mr-2 text-primary"></i>Personal Information
                            </h4>
                            <div class="space-y-3">
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Full Name</label>
                                    <p class="text-gray-900 font-medium"><?php echo htmlspecialchars($record['full_name']); ?></p>
                                </div>
                                <?php if ($record['email']): ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Email</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['email']); ?></p>
                                </div>
                                <?php endif; ?>
                                <?php if ($record['telephone']): ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Phone</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['telephone']); ?></p>
                                </div>
                                <?php endif; ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">District</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['district_name_full'] ?: $record['district_name']); ?></p>
                                </div>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Assembly</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['assembly_name_full'] ?: $record['assembly_name']); ?></p>
                                </div>
                            </div>
                        </div>

                        <!-- Program Information -->
                        <div class="bg-gray-50 rounded-lg p-6">
                            <h4 class="text-lg font-semibold text-gray-900 mb-4">
                                <i class="fas fa-calendar mr-2 text-primary"></i>Program Information
                            </h4>
                            <div class="space-y-3">
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Program Name</label>
                                    <p class="text-gray-900 font-medium"><?php echo htmlspecialchars($record['program_name']); ?></p>
                                </div>
                                <?php if ($record['program_description']): ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Description</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['program_description']); ?></p>
                                </div>
                                <?php endif; ?>
                                <?php if ($record['location_name']): ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Location</label>
                                    <p class="text-gray-900"><?php echo htmlspecialchars($record['location_name']); ?></p>
                                </div>
                                <?php endif; ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Submission Date</label>
                                    <p class="text-gray-900"><?php echo date('F j, Y g:i A', strtotime($record['submitted_at'])); ?></p>
                                </div>
                                <?php if ($record['program_end_date']): ?>
                                <div>
                                    <label class="text-sm font-medium text-gray-500">Program End Date</label>
                                    <p class="text-gray-900 <?php echo $record['program_is_active'] ? 'text-green-600' : 'text-red-600'; ?>">
                                        <?php echo date('F j, Y', strtotime($record['program_end_date'])); ?>
                                        <?php if ($record['program_is_active']): ?>
                                            <span class="text-xs bg-green-100 text-green-800 px-2 py-1 rounded-full ml-2">Active</span>
                                        <?php else: ?>
                                            <span class="text-xs bg-red-100 text-red-800 px-2 py-1 rounded-full ml-2">Expired</span>
                                        <?php endif; ?>
                                    </p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                    <!-- Today's Registration Status -->
                    <div class="mt-8 bg-gradient-to-r from-orange-50 to-yellow-50 rounded-lg p-6 border border-orange-200">
                        <h4 class="text-lg font-semibold text-gray-900 mb-4">
                            <i class="fas fa-calendar-day mr-2 text-orange-600"></i>Today's Registration Status
                        </h4>
                        
                        <div class="flex items-center justify-between">
                            <div class="flex items-center">
                                <div class="w-12 h-12 <?php echo $record['registered_today'] ? 'bg-green-100' : 'bg-gray-100'; ?> rounded-full flex items-center justify-center mr-4">
                                    <i class="fas <?php echo $record['registered_today'] ? 'fa-check-circle text-green-600' : 'fa-clock text-gray-600'; ?> text-xl"></i>
                                </div>
                                <div>
                                    <h5 class="text-lg font-semibold text-gray-900">
                                        <?php if ($record['registered_today']): ?>
                                            Already Registered for Today
                                        <?php else: ?>
                                            Not Registered for Today
                                        <?php endif; ?>
                                    </h5>
                                    <p class="text-sm text-gray-600">
                                        <?php if ($record['registered_today']): ?>
                                            You have already submitted your attendance for <?php echo date('F j, Y'); ?>
                                        <?php else: ?>
                                            You haven't registered for today's program yet
                                        <?php endif; ?>
                                    </p>
                                </div>
                            </div>
                            
                            <?php if ($record['can_register_today']): ?>
                                <div class="text-right">
                                    <button onclick="registerForToday()" class="bg-orange-600 text-white px-6 py-3 rounded-lg hover:bg-orange-700 transition duration-300 font-semibold">
                                        <i class="fas fa-plus mr-2"></i>Register for Today
                                    </button>
                                    <p class="text-xs text-gray-500 mt-1">Uses your previous information</p>
                                </div>
                            <?php elseif (!$record['program_is_active']): ?>
                                <div class="text-right">
                                    <div class="bg-red-100 text-red-800 px-4 py-2 rounded-lg text-sm">
                                        <i class="fas fa-exclamation-triangle mr-2"></i>Program has ended
                                    </div>
                                    <p class="text-xs text-gray-500 mt-1">Registration no longer available</p>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- Attendance History -->
                    <?php if (isset($record['attendance_count']) && $record['attendance_count'] > 0): ?>
                        <div class="mt-8 bg-gradient-to-r from-blue-50 to-indigo-50 rounded-lg p-6 border border-blue-200">
                            <h4 class="text-lg font-semibold text-gray-900 mb-4">
                                <i class="fas fa-history mr-2 text-primary"></i>Attendance History for This Program
                            </h4>
                            
                            <!-- Attendance Summary -->
                            <div class="bg-white rounded-lg p-4 mb-6 border border-blue-100">
                                <div class="flex items-center justify-between">
                                    <div class="flex items-center">
                                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                                            <i class="fas fa-calendar-check text-blue-600 text-xl"></i>
                                        </div>
                                        <div>
                                            <h5 class="text-lg font-semibold text-gray-900">Total Attendance</h5>
                                            <p class="text-sm text-gray-600">Times attended this program</p>
                                        </div>
                                    </div>
                                    <div class="text-right">
                                        <div class="text-3xl font-bold text-blue-600">
                                            <?php echo $record['attendance_count']; ?>
                                        </div>
                                        <div class="text-sm text-gray-500">
                                            <?php echo $record['attendance_count'] == 1 ? 'time' : 'times'; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Individual Attendance Dates -->
                            <div class="bg-white rounded-lg p-4 border border-blue-100">
                                <h5 class="text-md font-semibold text-gray-900 mb-3">
                                    <i class="fas fa-list mr-2 text-blue-600"></i>Individual Attendance Dates
                                </h5>
                                <div class="space-y-3 max-h-64 overflow-y-auto">
                                    <?php foreach ($record['attendance_dates'] as $index => $attendance): ?>
                                        <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg border <?php echo $attendance['tracking_code'] === $record['tracking_code'] ? 'border-blue-300 bg-blue-50' : 'border-gray-200'; ?>">
                                            <div class="flex items-center">
                                                <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center mr-3">
                                                    <span class="text-sm font-semibold text-blue-600"><?php echo $index + 1; ?></span>
                                                </div>
                                                <div>
                                                    <div class="font-medium text-gray-900">
                                                        <?php echo date('F j, Y', strtotime($attendance['attendance_date'])); ?>
                                                    </div>
                                                    <div class="text-sm text-gray-500">
                                                        <?php echo date('g:i A', strtotime($attendance['submitted_at'])); ?>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="flex items-center">
                                                <?php if ($attendance['tracking_code'] === $record['tracking_code']): ?>
                                                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded-full mr-2">
                                                        Current Record
                                                    </span>
                                                <?php endif; ?>
                                                <div class="text-xs font-mono text-gray-500 bg-gray-100 px-2 py-1 rounded">
                                                    <?php echo htmlspecialchars($attendance['tracking_code']); ?>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                                
                                <?php if ($record['attendance_count'] > 5): ?>
                                    <div class="mt-3 text-center">
                                        <p class="text-sm text-gray-500">
                                            <i class="fas fa-info-circle mr-1"></i>
                                            Showing all <?php echo $record['attendance_count']; ?> attendance records. Scroll to view more.
                                        </p>
                                    </div>
                                <?php endif; ?>
                            </div>

                            <!-- Attendance Statistics -->
                            <?php if ($record['attendance_count'] > 1): ?>
                                <?php 
                                $first_date = strtotime($record['attendance_dates'][0]['submitted_at']);
                                $last_date = strtotime($record['attendance_dates'][count($record['attendance_dates']) - 1]['submitted_at']);
                                $days_span = ceil(($last_date - $first_date) / (60 * 60 * 24));
                                ?>
                                <div class="mt-4 bg-white rounded-lg p-4 border border-blue-100">
                                    <h5 class="text-md font-semibold text-gray-900 mb-3">
                                        <i class="fas fa-chart-line mr-2 text-blue-600"></i>Attendance Statistics
                                    </h5>
                                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                                        <div class="text-center">
                                            <div class="text-lg font-semibold text-gray-900">
                                                <?php echo date('M j, Y', $first_date); ?>
                                            </div>
                                            <div class="text-sm text-gray-500">First Attendance</div>
                                        </div>
                                        <div class="text-center">
                                            <div class="text-lg font-semibold text-gray-900">
                                                <?php echo date('M j, Y', $last_date); ?>
                                            </div>
                                            <div class="text-sm text-gray-500">Latest Attendance</div>
                                        </div>
                                        <div class="text-center">
                                            <div class="text-lg font-semibold text-gray-900">
                                                <?php echo $days_span; ?> days
                                            </div>
                                            <div class="text-sm text-gray-500">Attendance Span</div>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>

                    <!-- Additional Data -->
                    <?php if ($record['additional_data'] && $record['additional_data'] !== 'null'): ?>
                        <?php $additional_data = json_decode($record['additional_data'], true); ?>
                        <?php if ($additional_data && is_array($additional_data) && !empty($additional_data)): ?>
                            <div class="mt-8 bg-gray-50 rounded-lg p-6">
                                <h4 class="text-lg font-semibold text-gray-900 mb-4">
                                    <i class="fas fa-info-circle mr-2 text-primary"></i>Additional Information
                                </h4>
                                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                    <?php foreach ($additional_data as $key => $value): ?>
                                        <?php if (!empty($value)): ?>
                                            <div>
                                                <label class="text-sm font-medium text-gray-500"><?php echo ucfirst(str_replace('_', ' ', $key)); ?></label>
                                                <p class="text-gray-900"><?php echo htmlspecialchars($value); ?></p>
                                            </div>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>

                    <!-- Actions -->
                    <div class="mt-8 text-center space-x-4">
                        <button onclick="window.print()" class="bg-gray-600 text-white px-6 py-2 rounded-lg hover:bg-gray-700 transition duration-300">
                            <i class="fas fa-print mr-2"></i>Print Details
                        </button>
                        <a href="index.php" class="bg-primary text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition duration-300 inline-block">
                            <i class="fas fa-home mr-2"></i>Back to Home
                        </a>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-800 text-white py-8 mt-12">
        <div class="container mx-auto px-4 text-center">
            <p>&copy; 2024 <?php echo $settings['site_title'] ?? SITE_TITLE; ?>. All rights reserved.</p>
        </div>
    </footer>

    <script>
        // Auto-uppercase tracking code input
        document.getElementById('tracking_code').addEventListener('input', function(e) {
            e.target.value = e.target.value.toUpperCase();
        });

        // Function to register for today using previous data
        function registerForToday() {
            if (!confirm('Register for today\'s program using your previous information?\n\nThis will create a new attendance record for today.')) {
                return;
            }

            // Show loading state
            const button = document.querySelector('button[onclick="registerForToday()"]');
            const originalText = button.innerHTML;
            button.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>Registering...';
            button.disabled = true;

            // Create form data with previous user information
            const formData = new FormData();
            formData.append('csrf_token', '<?php echo generateCSRFToken(); ?>');
            formData.append('program_id', '<?php echo $record['program_id'] ?? ''; ?>');
            formData.append('district_id', '<?php echo $record['district_id'] ?? ''; ?>');
            formData.append('assembly_id', '<?php echo $record['assembly_id'] ?? ''; ?>');
            formData.append('officer_type', '<?php echo htmlspecialchars($record['officer_type'] ?? ''); ?>');
            formData.append('full_name', '<?php echo htmlspecialchars($record['full_name'] ?? ''); ?>');
            formData.append('email', '<?php echo htmlspecialchars($record['email'] ?? ''); ?>');
            formData.append('telephone', '<?php echo htmlspecialchars($record['telephone'] ?? ''); ?>');
            
            // Add additional data if available
            <?php if (!empty($record['additional_data']) && $record['additional_data'] !== 'null'): ?>
                <?php $additional_data = json_decode($record['additional_data'], true); ?>
                <?php if ($additional_data && is_array($additional_data)): ?>
                    <?php foreach ($additional_data as $key => $value): ?>
                        <?php if (!empty($value)): ?>
                            formData.append('<?php echo addslashes($key); ?>', '<?php echo addslashes($value); ?>');
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            <?php endif; ?>

            // Submit to attendance form
            fetch('attendance/form.php?program=<?php echo $record['program_id']; ?>', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                if (response.redirected) {
                    // If redirected to thank you page, registration was successful
                    if (response.url.includes('thank_you.php')) {
                        alert('Registration successful! You will be redirected to the confirmation page.');
                        window.location.href = response.url;
                    } else {
                        // Handle other redirects
                        window.location.href = response.url;
                    }
                } else {
                    return response.text();
                }
            })
            .then(html => {
                if (html) {
                    // Check if there's an error in the response
                    if (html.includes('already registered')) {
                        alert('You are already registered for today\'s program.');
                        location.reload();
                    } else if (html.includes('error')) {
                        alert('An error occurred during registration. Please try again.');
                        button.innerHTML = originalText;
                        button.disabled = false;
                    } else {
                        // Registration might have been successful, reload to check
                        location.reload();
                    }
                }
            })
            .catch(error => {
                console.error('Registration error:', error);
                alert('An error occurred during registration. Please try again.');
                button.innerHTML = originalText;
                button.disabled = false;
            });
        }
    </script>
</body>
</html>

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