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

// Check if user is logged in and is admin
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true || $_SESSION['role'] !== 'admin') {
    header('Location: ../auth/login.php');
    exit();
}

$title = "View Activity";

// Get activity ID
$activity_id = $_GET['id'] ?? 0;

if ($activity_id <= 0) {
    $_SESSION['message'] = 'Invalid activity ID!';
    $_SESSION['message_type'] = 'danger';
    header('Location: manage_activities.php');
    exit();
}

// Get activity data with related information
$sql = "SELECT a.*, u.full_name as created_by_name, 
               c.code as course_code, c.name as course_name,
               d.code as department_code, d.name as department_name
        FROM activities a
        LEFT JOIN users u ON a.created_by = u.id
        LEFT JOIN courses c ON a.course_id = c.id
        LEFT JOIN departments d ON a.department_id = d.id
        WHERE a.id = $activity_id";
$result = query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
    $_SESSION['message'] = 'Activity not found!';
    $_SESSION['message_type'] = 'danger';
    header('Location: manage_activities.php');
    exit();
}

$activity = mysqli_fetch_assoc($result);

// Get attendance stats
$attendance_sql = "SELECT 
                    COUNT(*) as total,
                    SUM(CASE WHEN status = 'present' THEN 1 ELSE 0 END) as present,
                    SUM(CASE WHEN status = 'late' THEN 1 ELSE 0 END) as late,
                    SUM(CASE WHEN status = 'absent' THEN 1 ELSE 0 END) as absent
                  FROM attendance 
                  WHERE activity_id = " . $activity['id'];
$attendance_result = query($conn, $attendance_sql);
$stats = mysqli_fetch_assoc($attendance_result);

$today = date('Y-m-d');
$current_time = date('H:i:s');

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

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-2">Activity Details</h1>
        <p class="text-muted">View detailed information about the activity.</p>
    </div>
    <div>
        <a href="manage_activities.php" class="btn btn-secondary">
            <i class="bi bi-arrow-left me-2"></i> Back to Activities
        </a>
    </div>
</div>

<!-- Activity Details -->
<div class="row">
    <div class="col-lg-8">
        <div class="card shadow mb-4">
            <div class="card-header">
                <h5 class="mb-0"><?php echo htmlspecialchars($activity['name']); ?></h5>
            </div>
            <div class="card-body">
                <div class="mb-4">
                    <h6>Description</h6>
                    <p><?php echo nl2br(htmlspecialchars($activity['description'] ?: 'No description provided')); ?></p>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <h6><i class="bi bi-calendar me-2"></i> Date</h6>
                        <p><?php echo date('l, F j, Y', strtotime($activity['date'])); ?></p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h6><i class="bi bi-clock me-2"></i> Time</h6>
                        <p>
                            <?php echo date('h:i A', strtotime($activity['time_in'])); ?> - 
                            <?php echo date('h:i A', strtotime($activity['time_out'])); ?>
                        </p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6><i class="bi bi-geo-alt me-2"></i> Location</h6>
                        <p><?php echo htmlspecialchars($activity['location']); ?></p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h6><i class="bi bi-people me-2"></i> Participants</h6>
                        <p>
                            <?php 
                            switch($activity['required_students']) {
                                case 'all': 
                                    echo '<span class="badge bg-info">All Students</span>';
                                    break;
                                case 'specific_course': 
                                    echo '<span class="badge bg-warning">' . ($activity['course_name'] ?? 'Specific Course') . '</span>';
                                    break;
                                case 'specific_department': 
                                    echo '<span class="badge bg-primary">' . ($activity['department_name'] ?? 'Specific Department') . '</span>';
                                    break;
                            }
                            ?>
                        </p>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <h6>Status</h6>
                        <p>
                            <?php if ($activity['status'] == 1): ?>
                                <span class="badge bg-success">Active</span>
                            <?php else: ?>
                                <span class="badge bg-danger">Inactive</span>
                            <?php endif; ?>
                            
                            <?php 
                            $activity_date = $activity['date'];
                            $activity_start = $activity['time_in'];
                            $activity_end = $activity['time_out'];
                            
                            if ($activity_date > $today) {
                                echo '<span class="badge bg-primary ms-2">Upcoming</span>';
                            } elseif ($activity_date == $today && $current_time >= $activity_start && $current_time <= $activity_end) {
                                echo '<span class="badge bg-success ms-2">Ongoing</span>';
                            } else {
                                echo '<span class="badge bg-secondary ms-2">Past</span>';
                            }
                            ?>
                        </p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6>Created By</h6>
                        <p><?php echo htmlspecialchars($activity['created_by_name']); ?></p>
                        <small class="text-muted">
                            Created: <?php echo date('F j, Y', strtotime($activity['created_at'])); ?><br>
                            <?php if ($activity['updated_at'] != $activity['created_at']): ?>
                                Updated: <?php echo date('F j, Y', strtotime($activity['updated_at'])); ?>
                            <?php endif; ?>
                        </small>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <div class="btn-group">
                    <a href="edit_activity.php?id=<?php echo $activity['id']; ?>" class="btn btn-warning">
                        <i class="bi bi-pencil me-1"></i> Edit
                    </a>
                    <a href="reports.php?activity_id=<?php echo $activity['id']; ?>" class="btn btn-primary">
                        <i class="bi bi-file-earmark-text me-1"></i> View Report
                    </a>
                    <?php if ($activity['date'] == $today && $current_time >= $activity['time_in'] && $current_time <= $activity['time_out']): ?>
                        <a href="../admin/attendance.php" class="btn btn-success">
                            <i class="bi bi-qr-code-scan me-1"></i> Take Attendance
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-lg-4">
        <!-- Attendance Stats Card -->
        <div class="card shadow mb-4">
            <div class="card-header">
                <h6 class="mb-0">Attendance Statistics</h6>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <div class="d-flex justify-content-between mb-1">
                        <span>Attendance Rate</span>
                        <strong><?php echo $stats['total'] > 0 ? round(($stats['present'] / $stats['total']) * 100, 1) : 0; ?>%</strong>
                    </div>
                    <div class="progress" style="height: 8px;">
                        <div class="progress-bar" role="progressbar" 
                             style="width: <?php echo $stats['total'] > 0 ? ($stats['present'] / $stats['total'] * 100) : 0; ?>%">
                        </div>
                    </div>
                </div>
                
                <div class="mb-2">
                    <small class="d-flex justify-content-between">
                        <span>Present:</span>
                        <strong><?php echo $stats['present'] ?: 0; ?></strong>
                    </small>
                </div>
                <div class="mb-2">
                    <small class="d-flex justify-content-between">
                        <span>Late:</span>
                        <strong><?php echo $stats['late'] ?: 0; ?></strong>
                    </small>
                </div>
                <div class="mb-2">
                    <small class="d-flex justify-content-between">
                        <span>Absent:</span>
                        <strong><?php echo $stats['absent'] ?: 0; ?></strong>
                    </small>
                </div>
                <div class="mt-3 pt-3 border-top">
                    <small class="d-flex justify-content-between">
                        <span>Total:</span>
                        <strong><?php echo $stats['total'] ?: 0; ?></strong>
                    </small>
                </div>
            </div>
        </div>
        
        <!-- Quick Actions -->
        <div class="card shadow">
            <div class="card-header">
                <h6 class="mb-0">Quick Actions</h6>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="reports.php?activity_id=<?php echo $activity['id']; ?>" 
                       class="btn btn-primary">
                        <i class="bi bi-file-earmark-text me-1"></i> View Full Report
                    </a>
                    <a href="../admin/attendance.php" class="btn btn-success">
                        <i class="bi bi-qr-code-scan me-1"></i> Take Attendance
                    </a>
                    <a href="edit_activity.php?id=<?php echo $activity['id']; ?>" class="btn btn-warning">
                        <i class="bi bi-pencil me-1"></i> Edit Activity
                    </a>
                    <a href="manage_activities.php" class="btn btn-secondary">
                        <i class="bi bi-list-ul me-1"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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