<?php
/**
 * Parent Portal - Events
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/database.php';

// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in as parent
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'parent') {
    header('Location: ../parent_login.php');
    exit;
}

// Get parent information
$stmt = $pdo->prepare("SELECT * FROM parents WHERE user_id = ?");
$stmt->execute([$_SESSION['user_id']]);
$parent = $stmt->fetch();

if (!$parent) {
    header('Location: ../parent_register.php');
    exit;
}

// Get associated students
try {
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name
        FROM students s
        LEFT JOIN classes c ON s.class_id = c.id
        WHERE s.id IN (
            SELECT student_id FROM parent_students WHERE parent_id = ?
        ) AND s.status = 'active'
    ");
    $stmt->execute([$parent['id']]);
    $students = $stmt->fetchAll();
} catch (Exception $e) {
    $students = [];
}

// Get upcoming events
try {
    $stmt = $pdo->prepare("
        SELECT * FROM events 
        WHERE event_date >= CURDATE() 
        ORDER BY event_date ASC
        LIMIT 10
    ");
    $stmt->execute();
    $upcoming_events = $stmt->fetchAll();
} catch (Exception $e) {
    $upcoming_events = [];
}

// Get recent events
try {
    $stmt = $pdo->prepare("
        SELECT * FROM events 
        WHERE event_date < CURDATE() 
        ORDER BY event_date DESC
        LIMIT 10
    ");
    $stmt->execute();
    $recent_events = $stmt->fetchAll();
} catch (Exception $e) {
    $recent_events = [];
}

// Get student-specific events (class-wise)
$student_events = [];
if (count($students) > 0) {
    $student_class_ids = array_column($students, 'class_id');
    $placeholders = str_repeat('?,', count($student_class_ids) - 1) . '?';
    
    try {
        $stmt = $pdo->prepare("
            SELECT e.*, c.name as class_name
            FROM events e
            LEFT JOIN classes c ON e.class_id = c.id
            WHERE e.class_id IN ($placeholders) OR e.class_id IS NULL
            ORDER BY e.event_date DESC
            LIMIT 15
        ");
        $stmt->execute($student_class_ids);
        $student_events = $stmt->fetchAll();
    } catch (Exception $e) {
        $student_events = [];
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ইভেন্ট - অভিভাবক পোর্টাল</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .parent-sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        .parent-sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 15px 20px;
            border-radius: 8px;
            margin: 5px 0;
            text-decoration: none;
            display: block;
        }
        .parent-sidebar .nav-link:hover, .parent-sidebar .nav-link.active {
            background: rgba(255,255,255,0.2);
            color: white;
        }
        .event-card {
            border-left: 4px solid #007bff;
            transition: transform 0.2s;
        }
        .event-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .event-date {
            background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
            color: white;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
        }
        .upcoming-event {
            border-left-color: #28a745;
        }
        .recent-event {
            border-left-color: #6c757d;
        }
        .student-event {
            border-left-color: #ffc107;
        }
    </style>
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Parent Sidebar -->
            <nav class="col-md-2 d-none d-md-block parent-sidebar p-3">
                <div class="text-center mb-4">
                    <div class="bg-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width:80px;height:80px;">
                        <i class="fas fa-user-tie fa-2x text-primary"></i>
                    </div>
                    <h6 class="mb-0"><?= htmlspecialchars($parent['father_name']) ?></h6>
                    <small>অভিভাবক</small>
                </div>
                
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">
                            <i class="fas fa-home me-2"></i> ড্যাশবোর্ড
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="results.php">
                            <i class="fas fa-chart-line me-2"></i> ফলাফল
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="subjects.php">
                            <i class="fas fa-book me-2"></i> বিষয়সমূহ
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="fees.php">
                            <i class="fas fa-money-bill-wave me-2"></i> ফি পেমেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="events.php">
                            <i class="fas fa-calendar-alt me-2"></i> ইভেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="attendance.php">
                            <i class="fas fa-clipboard-check me-2"></i> উপস্থিতি
                        </a>
                    </li>
                    <li class="nav-item mt-4">
                        <a class="nav-link text-warning" href="../parent_logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i> লগআউট
                        </a>
                    </li>
                </ul>
            </nav>
            
            <!-- Main Content -->
            <main class="col-md-10 ms-sm-auto px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3"><i class="fas fa-calendar-alt text-primary"></i> স্কুল ইভেন্ট</h2>
                    <div class="text-muted">
                        <?= date('l, d F Y', strtotime('now')) ?>
                    </div>
                </div>
                
                <!-- Event Summary Cards -->
                <div class="row mb-4">
                    <div class="col-md-4 mb-3">
                        <div class="card text-center">
                            <div class="card-body">
                                <i class="fas fa-calendar-plus fa-2x text-success mb-2"></i>
                                <h3 class="text-success"><?= count($upcoming_events) ?></h3>
                                <p class="text-muted mb-0">আসন্ন ইভেন্ট</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <div class="card text-center">
                            <div class="card-body">
                                <i class="fas fa-history fa-2x text-info mb-2"></i>
                                <h3 class="text-info"><?= count($recent_events) ?></h3>
                                <p class="text-muted mb-0">সাম্প্রতিক ইভেন্ট</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <div class="card text-center">
                            <div class="card-body">
                                <i class="fas fa-users fa-2x text-warning mb-2"></i>
                                <h3 class="text-warning"><?= count($student_events) ?></h3>
                                <p class="text-muted mb-0">আপনার শিক্ষার্থীর ইভেন্ট</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <!-- Upcoming Events -->
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header bg-success text-white">
                                <h5 class="mb-0"><i class="fas fa-calendar-check"></i> আসন্ন ইভেন্ট</h5>
                            </div>
                            <div class="card-body">
                                <?php if (count($upcoming_events) > 0): ?>
                                <?php foreach ($upcoming_events as $event): ?>
                                <div class="card event-card upcoming-event mb-3">
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-3">
                                                <div class="event-date">
                                                    <div class="h5 mb-0"><?= date('d', strtotime($event['event_date'])) ?></div>
                                                    <div class="small"><?= date('M Y', strtotime($event['event_date'])) ?></div>
                                                    <div class="small"><?= date('l', strtotime($event['event_date'])) ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-9">
                                                <h5 class="card-title"><?= htmlspecialchars($event['title']) ?></h5>
                                                <p class="card-text text-muted small">
                                                    <?= htmlspecialchars(substr($event['description'] ?? '', 0, 100)) ?>...
                                                </p>
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <span class="badge bg-<?= getEventTypeClass($event['event_type']) ?>">
                                                        <?= htmlspecialchars($event['event_type']) ?>
                                                    </span>
                                                    <?php if (!empty($event['location'])): ?>
                                                    <small class="text-muted">
                                                        <i class="fas fa-map-marker-alt"></i> 
                                                        <?= htmlspecialchars($event['location']) ?>
                                                    </small>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                                <?php else: ?>
                                <div class="text-center py-4">
                                    <i class="fas fa-calendar-times fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">কোনো আসন্ন ইভেন্ট নেই</h5>
                                    <p class="text-muted">নতুন ইভেন্ট ঘোষণা করা হলে এখানে দেখাবে</p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Recent Events -->
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header bg-secondary text-white">
                                <h5 class="mb-0"><i class="fas fa-history"></i> সাম্প্রতিক ইভেন্ট</h5>
                            </div>
                            <div class="card-body">
                                <?php if (count($recent_events) > 0): ?>
                                <?php foreach ($recent_events as $event): ?>
                                <div class="card event-card recent-event mb-3">
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-3">
                                                <div class="event-date bg-secondary">
                                                    <div class="h5 mb-0"><?= date('d', strtotime($event['event_date'])) ?></div>
                                                    <div class="small"><?= date('M Y', strtotime($event['event_date'])) ?></div>
                                                    <div class="small"><?= date('l', strtotime($event['event_date'])) ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-9">
                                                <h5 class="card-title"><?= htmlspecialchars($event['title']) ?></h5>
                                                <p class="card-text text-muted small">
                                                    <?= htmlspecialchars(substr($event['description'] ?? '', 0, 100)) ?>...
                                                </p>
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <span class="badge bg-<?= getEventTypeClass($event['event_type']) ?>">
                                                        <?= htmlspecialchars($event['event_type']) ?>
                                                    </span>
                                                    <small class="text-muted">
                                                        <i class="fas fa-clock"></i> 
                                                        <?= dateDiffInDays($event['event_date']) ?> দিন আগে
                                                    </small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                                <?php else: ?>
                                <div class="text-center py-4">
                                    <i class="fas fa-history fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">কোনো সাম্প্রতিক ইভেন্ট নেই</h5>
                                    <p class="text-muted">সম্প্রতি কোনো ইভেন্ট অনুষ্ঠিত হয়নি</p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Student-Specific Events -->
                <?php if (count($student_events) > 0): ?>
                <div class="card mt-4">
                    <div class="card-header bg-warning text-dark">
                        <h5 class="mb-0"><i class="fas fa-user-graduate"></i> আপনার শিক্ষার্থীদের জন্য ইভেন্ট</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <?php foreach ($student_events as $event): ?>
                            <div class="col-md-6 mb-3">
                                <div class="card event-card student-event h-100">
                                    <div class="card-body">
                                        <div class="d-flex justify-content-between align-items-start mb-2">
                                            <h6 class="card-title mb-0"><?= htmlspecialchars($event['title']) ?></h6>
                                            <span class="badge bg-<?= getEventTypeClass($event['event_type']) ?>">
                                                <?= htmlspecialchars($event['event_type']) ?>
                                            </span>
                                        </div>
                                        
                                        <?php if (!empty($event['class_name'])): ?>
                                        <p class="text-muted small mb-2">
                                            <i class="fas fa-school"></i> 
                                            <?= htmlspecialchars($event['class_name']) ?>
                                        </p>
                                        <?php endif; ?>
                                        
                                        <p class="card-text small">
                                            <?= htmlspecialchars(substr($event['description'] ?? '', 0, 120)) ?>...
                                        </p>
                                        
                                        <div class="d-flex justify-content-between align-items-center">
                                            <small class="text-muted">
                                                <i class="fas fa-calendar"></i> 
                                                <?= date('d M Y', strtotime($event['event_date'])) ?>
                                            </small>
                                            <?php if (!empty($event['location'])): ?>
                                            <small class="text-muted">
                                                <i class="fas fa-map-marker-alt"></i> 
                                                <?= htmlspecialchars($event['location']) ?>
                                            </small>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                
                <!-- Important Notice -->
                <div class="alert alert-info mt-4">
                    <h5><i class="fas fa-info-circle"></i> গুরুত্বপূর্ণ তথ্য</h5>
                    <ul class="mb-0">
                        <li>ইভেন্টের সময় ও স্থান পরিবর্তিত হতে পারে</li>
                        <li>আপনার শিক্ষার্থীদের জন্য কোনো ইভেন্ট থাকলে স্কুল থেকে আলাদা নোটিফিকেশন পাবেন</li>
                        <li>কোনো প্রশ্ন থাকলে স্কুল অফিসে যোগাযোগ করুন</li>
                    </ul>
                </div>
            </main>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

<?php
function getEventTypeClass($event_type) {
    $types = [
        'exam' => 'danger',
        'holiday' => 'warning',
        'meeting' => 'info',
        'sports' => 'success',
        'cultural' => 'primary',
        'academic' => 'secondary'
    ];
    
    $event_type = strtolower($event_type);
    return $types[$event_type] ?? 'secondary';
}

function dateDiffInDays($date) {
    $now = new DateTime();
    $event_date = new DateTime($date);
    $interval = $now->diff($event_date);
    return $interval->days;
}
?>