<?php
session_start();
require_once 'config/database.php';
require_once 'includes/site_settings.php';

// Get system name for branding
$system_name = get_system_name($pdo);
// Fetch active academic calendar events with search filters
$sql = "SELECT * FROM academic_calendar WHERE status='active'";
$params = [];

// Apply search filters if provided
if (isset($_GET['search']) && !empty($_GET['search'])) {
    $searchTerm = trim($_GET['search']);
    $sql .= " AND (title LIKE ? OR description LIKE ?)";
    $params[] = "%{$searchTerm}%";
    $params[] = "%{$searchTerm}%";
}

if (isset($_GET['type']) && !empty($_GET['type'])) {
    $eventType = $_GET['type'];
    $sql .= " AND event_type = ?";
    $params[] = $eventType;
}

if (isset($_GET['year']) && !empty($_GET['year'])) {
    $year = $_GET['year'];
    $sql .= " AND YEAR(event_date) = ?";
    $params[] = $year;
}

$sql .= " ORDER BY event_date ASC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$events = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Group events by type
$holidays = [];
$exams = [];
$events_list = [];
$others = [];

foreach ($events as $event) {
    switch ($event['event_type']) {
        case 'holiday':
            $holidays[] = $event;
            break;
        case 'exam':
            $exams[] = $event;
            break;
        case 'event':
            $events_list[] = $event;
            break;
        default:
            $others[] = $event;
            break;
    }
}

function formatDate($date) {
    return date('d F, Y', strtotime($date));
}

function formatDateShort($date) {
    return date('d/m/Y', strtotime($date));
}

function calculateDuration($start_date, $end_date) {
    if (empty($end_date) || $end_date == $start_date) {
        return 1; // Single day event
    }
    
    $start = new DateTime($start_date);
    $end = new DateTime($end_date);
    $interval = $start->diff($end);
    return $interval->days + 1; // +1 to include both start and end dates
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>একাডেমিক ক্যালেন্ডার - <?php echo htmlspecialchars($system_name); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Hind Siliguri', sans-serif;
            background-color: #f8f9fa;
        }
        .calendar-header {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: white;
            padding: 2rem 0;
            margin-bottom: 2rem;
        }
        .event-card {
            border-radius: 10px;
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
            transition: all 0.3s ease;
            border-top: 4px solid;
            margin-bottom: 1rem;
        }
        .event-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }
        .holiday-card {
            border-top-color: #dc3545;
        }
        .exam-card {
            border-top-color: #fd7e14;
        }
        .event-card-type {
            border-top-color: #0d6efd;
        }
        .other-card {
            border-top-color: #6c757d;
        }
        .recurring-badge {
            background-color: #ffc107;
            color: #000;
            font-size: 0.75rem;
        }
        .event-date {
            font-size: 1.2rem;
            font-weight: 600;
            color: #006747;
        }
        .section-title {
            border-bottom: 2px solid #006747;
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="calendar-header">
        <div class="container">
            <div class="row">
                <div class="col-12 text-center">
                    <h1><i class="fas fa-calendar-alt me-2"></i>একাডেমিক ক্যালেন্ডার</h1>
                    <p class="lead mb-0">বিদ্যালয়ের সকল গুরুত্বপূর্ণ তারিখ ও ইভেন্ট</p>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        <!-- Navigation -->
        <div class="row mb-4">
            <div class="col-12">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">হোম</a></li>
                        <li class="breadcrumb-item active" aria-current="page">একাডেমিক ক্যালেন্ডার</li>
                    </ol>
                </nav>
            </div>
        </div>
        
        <!-- Search Section -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <form method="GET" class="row g-3">
                            <div class="col-md-5">
                                <label for="search_term" class="form-label">ইভেন্ট খুঁজুন</label>
                                <input type="text" class="form-control" id="search_term" name="search" placeholder="ইভেন্টের নাম বা বিবরণ দিন" value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
                            </div>
                            <div class="col-md-3">
                                <label for="event_type" class="form-label">ধরন</label>
                                <select class="form-select" id="event_type" name="type">
                                    <option value="">সব ধরন</option>
                                    <option value="holiday" <?php echo (isset($_GET['type']) && $_GET['type'] == 'holiday') ? 'selected' : ''; ?>>ছুটি</option>
                                    <option value="exam" <?php echo (isset($_GET['type']) && $_GET['type'] == 'exam') ? 'selected' : ''; ?>>পরীক্ষা</option>
                                    <option value="event" <?php echo (isset($_GET['type']) && $_GET['type'] == 'event') ? 'selected' : ''; ?>>ইভেন্ট</option>
                                    <option value="other" <?php echo (isset($_GET['type']) && $_GET['type'] == 'other') ? 'selected' : ''; ?>>অন্যান্য</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label for="year" class="form-label">বছর</label>
                                <select class="form-select" id="year" name="year">
                                    <option value="">সব বছর</option>
                                    <?php
                                    // Generate year options based on events
                                    $years = [];
                                    foreach ($events as $event) {
                                        $year = date('Y', strtotime($event['event_date']));
                                        if (!in_array($year, $years)) {
                                            $years[] = $year;
                                        }
                                    }
                                    rsort($years);
                                    foreach ($years as $year) {
                                        $selected = (isset($_GET['year']) && $_GET['year'] == $year) ? 'selected' : '';
                                        echo "<option value=\"{$year}\" {$selected}>{$year}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <div class="btn-group" role="group">
                                    <button type="submit" class="btn btn-primary"><i class="fas fa-search me-1"></i> খুঁজুন</button>
                                    <a href="academic_calendar_view.php" class="btn btn-outline-secondary"><i class="fas fa-redo"></i></a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card text-white bg-danger h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title"><i class="fas fa-umbrella-beach me-2"></i>ছুটি</h5>
                        <h2><?php echo count($holidays); ?></h2>
                        <p class="mb-0">টি ছুটি</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card text-white bg-warning h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title"><i class="fas fa-file-alt me-2"></i>পরীক্ষা</h5>
                        <h2><?php echo count($exams); ?></h2>
                        <p class="mb-0">টি পরীক্ষা</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card text-white bg-primary h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title"><i class="fas fa-calendar-day me-2"></i>ইভেন্ট</h5>
                        <h2><?php echo count($events_list); ?></h2>
                        <p class="mb-0">টি ইভেন্ট</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card text-white bg-secondary h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title"><i class="fas fa-ellipsis-h me-2"></i>অন্যান্য</h5>
                        <h2><?php echo count($others); ?></h2>
                        <p class="mb-0">টি ইভেন্ট</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Holidays Section -->
        <?php if (!empty($holidays)): ?>
        <div class="row">
            <div class="col-12">
                <h3 class="section-title"><i class="fas fa-umbrella-beach me-2"></i>ছুটি তালিকা</h3>
            </div>
            <?php foreach ($holidays as $holiday): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card event-card holiday-card h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start">
                            <h5 class="card-title"><?php echo htmlspecialchars($holiday['title']); ?></h5>
                            <?php if ($holiday['is_recurring']): ?>
                                <span class="badge recurring-badge">পুনরাবৃত্তি</span>
                            <?php endif; ?>
                        </div>
                        <?php if (!empty($holiday['description'])): ?>
                            <p class="card-text"><?php echo htmlspecialchars($holiday['description']); ?></p>
                        <?php endif; ?>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <span class="event-date">
                                <i class="fas fa-calendar me-1"></i>
                                <?php 
                                echo formatDate($holiday['event_date']);
                                if (!empty($holiday['end_date']) && $holiday['end_date'] != $holiday['event_date']) {
                                    echo ' - ' . formatDate($holiday['end_date']);
                                    $duration = calculateDuration($holiday['event_date'], $holiday['end_date']);
                                    echo ' (' . $duration . ' দিন)';
                                } else {
                                    echo ' (1 দিন)';
                                }
                                ?>
                            </span>
                            <span class="badge bg-danger">ছুটি</span>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- Exams Section -->
        <?php if (!empty($exams)): ?>
        <div class="row mt-4">
            <div class="col-12">
                <h3 class="section-title"><i class="fas fa-file-alt me-2"></i>পরীক্ষা তালিকা</h3>
            </div>
            <?php foreach ($exams as $exam): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card event-card exam-card h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start">
                            <h5 class="card-title"><?php echo htmlspecialchars($exam['title']); ?></h5>
                            <?php if ($exam['is_recurring']): ?>
                                <span class="badge recurring-badge">পুনরাবৃত্তি</span>
                            <?php endif; ?>
                        </div>
                        <?php if (!empty($exam['description'])): ?>
                            <p class="card-text"><?php echo htmlspecialchars($exam['description']); ?></p>
                        <?php endif; ?>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <span class="event-date">
                                <i class="fas fa-calendar me-1"></i>
                                <?php 
                                echo formatDate($exam['event_date']);
                                if (!empty($exam['end_date']) && $exam['end_date'] != $exam['event_date']) {
                                    echo ' - ' . formatDate($exam['end_date']);
                                    $duration = calculateDuration($exam['event_date'], $exam['end_date']);
                                    echo ' (' . $duration . ' দিন)';
                                } else {
                                    echo ' (1 দিন)';
                                }
                                ?>
                            </span>
                            <span class="badge bg-warning">পরীক্ষা</span>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- Events Section -->
        <?php if (!empty($events_list)): ?>
        <div class="row mt-4">
            <div class="col-12">
                <h3 class="section-title"><i class="fas fa-calendar-day me-2"></i>ইভেন্ট তালিকা</h3>
            </div>
            <?php foreach ($events_list as $event_item): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card event-card event-card-type h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start">
                            <h5 class="card-title"><?php echo htmlspecialchars($event_item['title']); ?></h5>
                            <?php if ($event_item['is_recurring']): ?>
                                <span class="badge recurring-badge">পুনরাবৃত্তি</span>
                            <?php endif; ?>
                        </div>
                        <?php if (!empty($event_item['description'])): ?>
                            <p class="card-text"><?php echo htmlspecialchars($event_item['description']); ?></p>
                        <?php endif; ?>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <span class="event-date">
                                <i class="fas fa-calendar me-1"></i>
                                <?php 
                                echo formatDate($event_item['event_date']);
                                if (!empty($event_item['end_date']) && $event_item['end_date'] != $event_item['event_date']) {
                                    echo ' - ' . formatDate($event_item['end_date']);
                                    $duration = calculateDuration($event_item['event_date'], $event_item['end_date']);
                                    echo ' (' . $duration . ' দিন)';
                                } else {
                                    echo ' (1 দিন)';
                                }
                                ?>
                            </span>
                            <span class="badge bg-primary">ইভেন্ট</span>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- Other Events Section -->
        <?php if (!empty($others)): ?>
        <div class="row mt-4">
            <div class="col-12">
                <h3 class="section-title"><i class="fas fa-ellipsis-h me-2"></i>অন্যান্য ইভেন্ট</h3>
            </div>
            <?php foreach ($others as $other): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card event-card other-card h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start">
                            <h5 class="card-title"><?php echo htmlspecialchars($other['title']); ?></h5>
                            <?php if ($other['is_recurring']): ?>
                                <span class="badge recurring-badge">পুনরাবৃত্তি</span>
                            <?php endif; ?>
                        </div>
                        <?php if (!empty($other['description'])): ?>
                            <p class="card-text"><?php echo htmlspecialchars($other['description']); ?></p>
                        <?php endif; ?>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <span class="event-date">
                                <i class="fas fa-calendar me-1"></i>
                                <?php 
                                echo formatDate($other['event_date']);
                                if (!empty($other['end_date']) && $other['end_date'] != $other['event_date']) {
                                    echo ' - ' . formatDate($other['end_date']);
                                    $duration = calculateDuration($other['event_date'], $other['end_date']);
                                    echo ' (' . $duration . ' দিন)';
                                } else {
                                    echo ' (1 দিন)';
                                }
                                ?>
                            </span>
                            <span class="badge bg-secondary">অন্যান্য</span>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- No Events Message -->
        <?php if (empty($events)): ?>
        <div class="row">
            <div class="col-12">
                <div class="alert alert-info text-center">
                    <i class="fas fa-info-circle fa-2x mb-3"></i>
                    <h4>কোন একাডেমিক ইভেন্ট খুঁজে পাওয়া যায়নি</h4>
                    <p class="mb-0">পরবর্তীতে নতুন ইভেন্ট যোগ করা হবে</p>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Footer -->
    <footer class="bg-dark text-white mt-5 py-4">
        <div class="container">
            <div class="row">
                <div class="col-12 text-center">
                    <p class="mb-0">&copy; <?php echo date('Y'); ?> <?php echo htmlspecialchars($system_name); ?> | সকল স্বত্ব সংরক্ষিত</p>
                </div>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>