<?php 
include 'includes/header.php'; 
include 'includes/db.php'; 

$id = intval($_GET['id']);

// Get department data
$deptResult = $conn->query("SELECT * FROM departments WHERE id=$id");
if(!$deptResult || $deptResult->num_rows === 0){
    echo "<p style='text-align:center; color:red;'>Department not found.</p>";
    exit;
}
$dept = $deptResult->fetch_assoc();

// Get all department images
$imagesResult = $conn->query("SELECT * FROM department_images WHERE department_id=$id ORDER BY id ASC");
$images = [];
if($imagesResult && $imagesResult->num_rows > 0){
    while($img = $imagesResult->fetch_assoc()){
        $images[] = $img['image_path'];
    }
} else {
    $images[] = 'assets/images/no-image.jpg';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($dept['name']); ?></title>

<style>
/* ===== Base ===== */
body {
  background: #f4f6f9;
  font-family: "Poppins", "Segoe UI", serif;
  margin: 0;
}

/* ===== Container ===== */
.department-details {
  width: 85%;
  max-width: 1200px;
  margin: 60px auto;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
  padding: 40px;
  animation: fadeIn 0.8s ease;
}

/* ===== Title ===== */
.department-details h1 {
  font-size: 2.2rem;
  color: #25A9C2;
  margin-bottom: 8px;
  line-height: 1.3;
}

/* ===== Layout ===== */
.department-layout {
  display: grid;
  grid-template-columns: 1.2fr 2fr;
  gap: 35px;
  align-items: start;
}

/* ===== Images ===== */
.department-images {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.department-images img {
  width: 100%;
  border-radius: 10px;
  object-fit: cover;
  animation: fadeUp 0.8s ease;
}

/* First image larger */
.department-images img:first-child {
  height: 360px;
}

/* Other images smaller */
.department-images img:not(:first-child) {
  height: 180px;
}

/* ===== Content ===== */
.department-content {
  font-size: 1.05rem;
  line-height: 1.9;
  color: #222;
  animation: fadeUp 1s ease;
}

.department-content p {
  margin-bottom: 18px;
}

/* Newspaper feel */
.department-content::first-letter {
  font-size: 3.5rem;
  font-weight: bold;
  float: left;
  line-height: 1;
  padding-right: 10px;
  color: #25A9C2;
}

/* ===== Back Button ===== */
.back-btn {
  display: inline-block;
  margin-top: 40px;
  background: #25A9C2;
  color: #fff;
  padding: 12px 26px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  transition: 0.3s;
}

.back-btn:hover {
  background: #a84300;
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(25px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .department-layout {
    grid-template-columns: 1fr;
  }

  .department-images img:first-child {
    height: 260px;
  }

  .department-images img:not(:first-child) {
    height: 160px;
  }
}
</style>
</head>
<body>

<div class="department-details">

  <h1><?= htmlspecialchars($dept['name']); ?></h1>

  <div class="department-layout">

    <!-- Images -->
    <div class="department-images">
      <?php foreach($images as $imgPath): ?>
        <img src="<?= htmlspecialchars($imgPath) ?>" alt="<?= htmlspecialchars($dept['name']); ?>">
      <?php endforeach; ?>
    </div>

    <!-- Content -->
    <div class="department-content">
      <?= nl2br($dept['description']); ?>
    </div>

  </div>


</div>

<?php include 'includes/footer.php'; ?>
</body>
</html>
