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

function bn_date($d){ return $d ? date('d-m-Y', strtotime($d)) : ''; }

// Fetch upcoming and past events
$upcoming = [];
$past = [];
try {
  $upcoming = $pdo->query("SELECT * FROM events WHERE (status='upcoming' AND event_date >= CURDATE()) ORDER BY event_date ASC LIMIT 50")->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {}
try {
  $past = $pdo->query("SELECT * FROM events WHERE (status='past' OR event_date < CURDATE()) ORDER BY event_date DESC LIMIT 50")->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {}
?>
<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 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;800&display=swap" rel="stylesheet">
  <style>
    body { 
      font-family: 'Hind Siliguri', sans-serif;
      background: #f5f7fa; 
    }
    
    .jb-navbar {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      box-shadow: 0 4px 20px rgba(0, 103, 71, 0.3);
      border-bottom: 3px solid #F42A41;
    }
    
    .jb-navbar .navbar-brand {
      color: #fff !important;
      font-weight: 800;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    .card { 
      border: 0; 
      border-radius: 14px; 
      box-shadow: 0 10px 25px rgba(0,103,71,0.1);
      border-top: 4px solid #006747;
      transition: all 0.3s ease;
    }
    
    .card:hover {
      transform: translateY(-8px);
      box-shadow: 0 15px 35px rgba(0,103,71,0.2);
      border-top-color: #F42A41;
    }
    
    .btn-primary {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      border: none;
      font-weight: 600;
    }
    
    .btn-primary:hover {
      background: linear-gradient(135deg, #004d35 0%, #006747 100%);
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(0, 103, 71, 0.4);
    }
    
    h3 {
      color: #006747;
      font-weight: 700;
      border-bottom: 3px solid #F42A41;
      padding-bottom: 10px;
      margin-bottom: 30px;
    }
    
    h5 {
      color: #006747;
      font-weight: 700;
    }
    
    .badge.bg-success {
      background: linear-gradient(135deg, #28a745 0%, #20c997 100%) !important;
    }
    
    .event-date-badge {
      background: linear-gradient(135deg, #006747, #004d35);
      color: #fff;
      padding: 15px;
      border-radius: 10px;
      text-align: center;
      border: 2px solid #F42A41;
    }
    
    /* Mobile Responsive */
    @media (max-width: 991px) {
      .navbar-brand {
        font-size: 1.2rem;
      }
      
      h3 {
        font-size: 1.5rem;
      }
      
      h5 {
        font-size: 1.2rem;
      }
    }
    
    @media (max-width: 576px) {
      .navbar-brand {
        font-size: 1rem;
      }
      
      h3 {
        font-size: 1.3rem;
      }
      
      .btn {
        font-size: 0.9rem;
        padding: 8px 15px;
      }
    }
  </style>
</head>
<body>
  <nav class="navbar navbar-expand-lg jb-navbar">
    <div class="container">
      <a class="navbar-brand" href="index.php"><i class="fas fa-graduation-cap"></i> লিদ্যা নিকেফন উচ্চ লিদ্যালয়</a>
      <div class="ms-auto d-flex gap-2">
        <a class="btn btn-outline-secondary" href="student_portal.php"><i class="fas fa-user-graduate"></i> শিক্ষার্থী পোর্টাল</a>
        <a class="btn btn-primary" href="login.php"><i class="fas fa-sign-in-alt"></i> লগইন</a>
      </div>
    </div>
  </nav>

  <div class="container my-4">
    <h3 class="mb-3"><i class="fas fa-calendar"></i> ইভেন্টসমূহ</h3>

    <div class="mb-4">
      <h5 class="mb-2 text-success">আসন্ন ইভেন্ট</h5>
      <div class="row g-3">
        <?php if (empty($upcoming)): ?>
          <div class="col-12"><div class="card"><div class="card-body text-center text-muted">কোন আসন্ন ইভেন্ট নেই</div></div></div>
        <?php else: foreach ($upcoming as $e): ?>
          <div class="col-md-6 col-lg-4">
            <div class="card h-100">
              <?php if (!empty($e['cover_image'])): ?><img src="<?php echo htmlspecialchars($e['cover_image']); ?>" class="card-img-top" alt=""><?php endif; ?>
              <div class="card-body">
                <span class="badge bg-success mb-2"><i class="far fa-calendar"></i> <?php echo bn_date($e['event_date']); ?></span>
                <h6 class="mb-2"><a class="text-decoration-none" href="event.php?id=<?php echo (int)$e['id']; ?>"><?php echo htmlspecialchars($e['title']); ?></a></h6>
                <p class="text-muted small mb-0"><?php echo htmlspecialchars(mb_strimwidth(strip_tags($e['description'] ?? ''),0,120,'...','UTF-8')); ?></p>
              </div>
            </div>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>

    <div>
      <h5 class="mb-2 text-secondary">পূর্ববর্তী ইভেন্ট</h5>
      <div class="row g-3">
        <?php if (empty($past)): ?>
          <div class="col-12"><div class="card"><div class="card-body text-center text-muted">কোন ইভেন্ট নেই</div></div></div>
        <?php else: foreach ($past as $e): ?>
          <div class="col-md-6 col-lg-4">
            <div class="card h-100">
              <?php if (!empty($e['cover_image'])): ?><img src="<?php echo htmlspecialchars($e['cover_image']); ?>" class="card-img-top" alt=""><?php endif; ?>
              <div class="card-body">
                <span class="badge bg-secondary mb-2"><i class="far fa-calendar"></i> <?php echo bn_date($e['event_date']); ?></span>
                <h6 class="mb-2"><a class="text-decoration-none" href="event.php?id=<?php echo (int)$e['id']; ?>"><?php echo htmlspecialchars($e['title']); ?></a></h6>
                <p class="text-muted small mb-0"><?php echo htmlspecialchars(mb_strimwidth(strip_tags($e['description'] ?? ''),0,120,'...','UTF-8')); ?></p>
              </div>
            </div>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>
  </div>
</body>
</html>