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

$name = $_GET['name'] ?? '';
$roll = $_GET['roll'] ?? '';
$class = $_GET['class'] ?? '';

// Build query
$sql = "SELECT s.id, s.first_name, s.last_name, s.roll_number, s.photo, s.gender, s.date_of_birth,
        c.name as class_name, c.section
        FROM students s
        LEFT JOIN classes c ON s.class_id = c.id
        WHERE s.status = 'active'";

$params = [];

if (!empty($name)) {
    $sql .= " AND (s.first_name LIKE ? OR s.last_name LIKE ?)";
    $params[] = "%$name%";
    $params[] = "%$name%";
}

if (!empty($roll)) {
    $sql .= " AND s.roll_number LIKE ?";
    $params[] = "%$roll%";
}

if (!empty($class)) {
    $sql .= " AND s.class_id = ?";
    $params[] = $class;
}

$sql .= " ORDER BY s.roll_number ASC";

try {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    $students = [];
}

function bn_date($date) {
    if (!$date) return '';
    return date('d-m-Y', strtotime($date));
}
?>
<!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 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;
      padding: 15px 20px;
    }
    .search-header {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      color: #fff;
      padding: 30px;
      border-radius: 15px;
      margin: 30px 0;
      border-bottom: 4px solid #F42A41;
    }
    .student-card {
      background: #fff;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0, 103, 71, 0.1);
      border-top: 3px solid #006747;
      transition: all 0.3s ease;
      margin-bottom: 20px;
    }
    .student-card:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 25px rgba(0, 103, 71, 0.2);
      border-top-color: #F42A41;
    }
    .student-photo {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      object-fit: cover;
      border: 3px solid #006747;
    }
    .student-avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      background: linear-gradient(135deg, #006747, #004d35);
      color: #fff;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 2rem;
      font-weight: 700;
      border: 3px solid #F42A41;
    }
    .btn-print {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      color: #fff;
      border: none;
      font-weight: 600;
    }
    .btn-print:hover {
      background: linear-gradient(135deg, #004d35 0%, #006747 100%);
      color: #fff;
    }
    .table thead th {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      color: #fff;
      border: none;
    }
    .table tbody tr:hover {
      background: #e8f5e9;
    }
    @media print {
      .no-print { display: none !important; }
      .card-view { display: none !important; }
      body { background: #fff; }
      .table { font-size: 12px; }
      .table thead th {
        background: #006747 !important;
        color: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
      }
      .search-header {
        background: #006747 !important;
        color: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        page-break-after: avoid;
      }
      .card {
        box-shadow: none;
        border: 1px solid #ddd;
        page-break-inside: avoid;
      }
      h5 {
        page-break-after: avoid;
      }
      .table-responsive {
        page-break-inside: auto;
      }
      .table tbody tr {
        page-break-inside: avoid;
        page-break-after: auto;
      }
    }
  </style>
</head>
<body>
  <nav class="navbar jb-navbar no-print">
    <div class="container">
      <a class="navbar-brand" href="index.php">
        <i class="fas fa-graduation-cap"></i> কুড়ুলগাছি মাধ্যমিক বিদ্যালয়
      </a>
      <div class="d-flex gap-2">
        <button onclick="window.print()" class="btn btn-light">
          <i class="fas fa-print"></i> প্রিন্ট
        </button>
        <a href="index.php#students" class="btn btn-outline-light">
          <i class="fas fa-arrow-left"></i> ফিরে যান
        </a>
      </div>
    </div>
  </nav>

  <div class="container">
    <div class="search-header text-center">
      <h2><i class="fas fa-graduation-cap"></i> কুড়ুলগাছি মাধ্যমিক বিদ্যালয়</h2>
      <h4><i class="fas fa-list"></i> শিক্ষার্থী তালিকা</h4>
      <p class="mb-0">মোট শিক্ষার্থী: <strong><?php echo count($students); ?></strong> জন | তারিখ: <strong><?php echo date('d-m-Y'); ?></strong></p>
    </div>

    <?php if (empty($students)): ?>
      <div class="alert alert-warning">
        <i class="fas fa-exclamation-triangle"></i> কোন শিক্ষার্থী পাওয়া যায়নি। অনুগ্রহ করে অন্য তথ্য দিয়ে সার্চ করুন।
        <br><br>
        <a href="index.php#students" class="btn btn-primary">
          <i class="fas fa-arrow-left"></i> আবার সার্চ করুন
        </a>
      </div>
    <?php else: ?>
      
      <!-- Card View -->
      <div class="row mb-4 card-view">
        <?php foreach ($students as $student): ?>
          <div class="col-lg-4 col-md-6 mb-3">
            <div class="student-card">
              <div class="card-body">
                <div class="d-flex align-items-center">
                  <div class="me-3">
                    <?php if (!empty($student['photo']) && file_exists($student['photo'])): ?>
                      <img src="<?php echo htmlspecialchars($student['photo']); ?>" 
                           alt="<?php echo htmlspecialchars($student['first_name']); ?>" 
                           class="student-photo">
                    <?php else: ?>
                      <div class="student-avatar">
                        <?php echo strtoupper(substr($student['first_name'], 0, 1)); ?>
                      </div>
                    <?php endif; ?>
                  </div>
                  <div class="flex-grow-1">
                    <h5 class="mb-1" style="color: #006747;">
                      <?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?>
                    </h5>
                    <p class="mb-1">
                      <i class="fas fa-id-card text-primary"></i> 
                      রোল: <strong><?php echo htmlspecialchars($student['roll_number']); ?></strong>
                    </p>
                    <p class="mb-1">
                      <i class="fas fa-school text-success"></i> 
                      <?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['section']); ?>
                    </p>
                    <p class="mb-0 text-muted">
                      <i class="fas fa-venus-mars"></i> 
                      <?php echo $student['gender'] == 'male' ? 'ছেলে' : 'মেয়ে'; ?>
                      <?php if ($student['date_of_birth']): ?>
                        | <i class="fas fa-birthday-cake"></i> <?php echo bn_date($student['date_of_birth']); ?>
                      <?php endif; ?>
                    </p>
                  </div>
                </div>
              </div>
            </div>
          </div>
        <?php endforeach; ?>
      </div>

      <!-- Table View -->
      <div class="card mt-4">
        <div class="card-body">
          <h5 class="mb-3 no-print" style="color: #006747;">
            <i class="fas fa-table"></i> বিস্তারিত তালিকা
          </h5>
          <div class="table-responsive">
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th style="width: 50px;">ক্রমিক</th>
                  <th>শিক্ষার্থীর নাম</th>
                  <th style="width: 100px;">রোল নম্বর</th>
                  <th style="width: 150px;">ক্লাস ও সেকশন</th>
                  <th style="width: 80px;">লিঙ্গ</th>
                  <th style="width: 120px;">জন্ম তারিখ</th>
                </tr>
              </thead>
              <tbody>
                <?php $serial = 1; ?>
                <?php foreach ($students as $student): ?>
                  <tr>
                    <td class="text-center"><?php echo $serial++; ?></td>
                    <td><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></td>
                    <td class="text-center"><strong><?php echo htmlspecialchars($student['roll_number']); ?></strong></td>
                    <td><?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['section']); ?></td>
                    <td class="text-center"><?php echo $student['gender'] == 'male' ? 'ছেলে' : 'মেয়ে'; ?></td>
                    <td class="text-center"><?php echo bn_date($student['date_of_birth']); ?></td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
              <tfoot>
                <tr class="table-active">
                  <td colspan="6" class="text-end">
                    <strong>মোট শিক্ষার্থী: <?php echo count($students); ?> জন</strong>
                  </td>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      </div>

      <div class="text-center my-4 no-print">
        <button onclick="window.print()" class="btn btn-print btn-lg me-2">
          <i class="fas fa-print"></i> প্রিন্ট করুন
        </button>
        <a href="index.php#students" class="btn btn-secondary btn-lg">
          <i class="fas fa-arrow-left"></i> ফিরে যান
        </a>
      </div>

    <?php endif; ?>
  </div>

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