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

// Student Portal: quick tools and important links
function bn_date($d){ return $d ? date('d-m-Y', strtotime($d)) : ''; }

$isLoggedIn = isset($_SESSION['user_id']);
$role = $_SESSION['user_role'] ?? 'guest';

// Resolve destination links based on login/role
$fee_link = !$isLoggedIn ? 'login.php' : ($role === 'student' ? 'fees/my_fees.php' : 'fees/dashboard.php');
$result_link = !$isLoggedIn ? 'login.php' : ($role === 'student' ? 'reports/my_results.php' : 'reports/index.php');
$subject_link = !$isLoggedIn ? 'login.php' : 'subjects/subject_selection.php';

// Pull a few public-facing pieces: latest notices, upcoming events
$notices = [];
try {
  $stmt = $pdo->query("SELECT id, title, publish_date, priority FROM notices WHERE status='active' AND publish_date <= CURDATE() AND (expiry_date IS NULL OR expiry_date >= CURDATE()) ORDER BY priority DESC, created_at DESC LIMIT 6");
  $notices = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {}

$events = [];
try {
  $events = $pdo->query("SELECT id, title, event_date, location FROM events WHERE (status='upcoming' AND event_date >= CURDATE()) ORDER BY event_date ASC LIMIT 6")->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; 
    }
    
    /* Jessoreboard Navigation */
    .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(-5px);
      box-shadow: 0 15px 35px rgba(0,103,71,0.2);
      border-top-color: #F42A41;
    }
    
    .card-header {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      color: #fff;
      font-weight: 600;
      border-bottom: 2px solid #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);
    }
    
    .btn-outline-primary {
      border-color: #006747;
      color: #006747;
      font-weight: 600;
    }
    
    .btn-outline-primary:hover {
      background: #006747;
      border-color: #006747;
    }
    
    h3 {
      color: #006747;
      font-weight: 700;
      border-bottom: 3px solid #F42A41;
      padding-bottom: 10px;
      margin-bottom: 30px;
    }
    
    .list-group-item {
      border-left: 3px solid transparent;
      transition: all 0.3s ease;
    }
    
    .list-group-item:hover {
    }
    
    .form-control:focus {
      border-color: #006747;
      box-shadow: 0 0 0 0.25rem rgba(0, 103, 71, 0.15);
    }
    
    /* Mobile Responsive */
    @media (max-width: 991px) {
      .navbar-brand {
        font-size: 1.2rem;
      }
      
      h3 {
        font-size: 1.5rem;
      }
      
      .btn {
        font-size: 0.9rem;
        padding: 8px 15px;
      }
    }
    
    @media (max-width: 576px) {
      .navbar-brand {
        font-size: 1rem;
      }
      
      h3 {
        font-size: 1.3rem;
      }
      
      .card-header {
        font-size: 0.95rem;
      }
      
      .list-group-item {
        font-size: 0.9rem;
      }
    }
  </style>
</head>
<body>
  <nav class="navbar navbar-expand-lg jb-navbar">
    <div class="container">
      <div class="ms-auto d-flex gap-2">
        <a class="btn btn-outline-secondary" href="events.php"><i class="fas fa-calendar"></i> ইভেন্ট</a>
        <?php if ($isLoggedIn): ?>
          <a class="btn btn-outline-primary" href="logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
        <?php else: ?>
          <a class="btn btn-primary" href="login.php"><i class="fas fa-sign-in-alt"></i> লগইন</a>
        <?php endif; ?>
      </div>
    </div>
  </nav>

  <div class="container my-4">
    <h3 class="mb-3"><i class="fas fa-user-graduate"></i> শিক্ষার্থী পোর্টাল</h3>

    <div class="row g-3">
      <div class="col-lg-6">
        <div class="card h-100">
          <div class="card-header"><strong>গুরুত্বপূর্ণ</strong></div>
          <div class="card-body">
            <ul class="list-group list-group-flush">
              <li class="list-group-item d-flex justify-content-between align-items-center">
                <span><i class="fas fa-credit-card"></i> ফি/বিল সিস্টেম</span>
                <a class="btn btn-sm btn-outline-primary" href="<?php echo htmlspecialchars($fee_link); ?>"><?php echo $isLoggedIn ? 'ওপেন' : 'লগইন করুন'; ?></a>
              </li>
              <li class="list-group-item d-flex justify-content-between align-items-center">
                <span><i class="fas fa-poll-h"></i> রেজাল্ট দেখুন</span>
                <a class="btn btn-sm btn-outline-primary" href="<?php echo htmlspecialchars($result_link); ?>"><?php echo $isLoggedIn ? 'ওপেন' : 'লগইন করুন'; ?></a>
              </li>
              <li class="list-group-item d-flex justify-content-between align-items-center">
                <span><i class="fas fa-book"></i> বিষয় নির্বাচন</span>
                <a class="btn btn-sm btn-outline-primary" href="<?php echo htmlspecialchars($subject_link); ?>"><?php echo $isLoggedIn ? 'ওপেন' : 'লগইন করুন'; ?></a>
              </li>
              <?php if ($isLoggedIn && $role === 'student'): ?>
              <li class="list-group-item d-flex justify-content-between align-items-center">
                <span><i class="fas fa-mobile-alt"></i> অনলাইন পেমেন্ট</span>
                <a class="btn btn-sm btn-outline-primary" href="fees/demo_payment.php">ডেমো</a>
              </li>
              <?php endif; ?>
            </ul>
          </div>
        </div>
      </div>

      <div class="col-lg-6">
        <div class="card h-100">
          <div class="card-header"><strong>দ্রুত লগইন</strong></div>
          <div class="card-body">
            <form method="POST" action="login.php">
              <div class="mb-2">
                <label class="form-label">ইউজারনেম</label>
                <input type="text" class="form-control" name="username" required>
              </div>
              <div class="mb-3">
                <label class="form-label">পাসওয়ার্ড</label>
                <input type="password" class="form-control" name="password" required>
              </div>
              <div class="d-grid">
                <button class="btn btn-primary"><i class="fas fa-sign-in-alt"></i> লগইন</button>
              </div>
              <div class="form-text mt-2">লগইন করলে বিল/ফি, রেজাল্ট, প্রোফাইল ইত্যাদি দেখা যাবে।</div>
            </form>
          </div>
        </div>
      </div>
    </div>

    <div class="row g-3 mt-1">
      <div class="col-lg-6">
        <div class="card h-100">
          <div class="card-header"><strong><i class="fas fa-bullhorn"></i> সর্বশেষ নোটিশ</strong></div>
          <div class="card-body">
            <?php if (empty($notices)): ?>
              <div class="text-center text-muted">কোন নোটিশ নেই</div>
            <?php else: ?>
              <ul class="list-group list-group-flush">
                <?php foreach ($notices as $n): ?>
                  <li class="list-group-item d-flex justify-content-between align-items-center">
                    <a class="text-decoration-none" href="notice.php?id=<?php echo (int)$n['id']; ?>"><?php echo htmlspecialchars($n['title']); ?></a>
                    <small class="text-muted"><?php echo bn_date($n['publish_date']); ?></small>
                  </li>
                <?php endforeach; ?>
              </ul>
            <?php endif; ?>
          </div>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="card h-100">
          <div class="card-header"><strong><i class="fas fa-calendar"></i> আসন্ন ইভেন্ট</strong></div>
          <div class="card-body">
            <?php if (empty($events)): ?>
              <div class="text-center text-muted">কোন ইভেন্ট নেই</div>
            <?php else: ?>
              <ul class="list-group list-group-flush">
                <?php foreach ($events as $e): ?>
                  <li class="list-group-item">
                    <div class="d-flex justify-content-between">
                      <a class="text-decoration-none" href="event.php?id=<?php echo (int)$e['id']; ?>"><?php echo htmlspecialchars($e['title']); ?></a>
                      <small class="text-muted"><?php echo bn_date($e['event_date']); ?></small>
                    </div>
                    <?php if (!empty($e['location'])): ?><div class="small text-muted"><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($e['location']); ?></div><?php endif; ?>
                  </li>
                <?php endforeach; ?>
              </ul>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>