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

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

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

// Get research images
$imgsResult = $conn->query("SELECT * FROM research_images WHERE research_id=$id ORDER BY id ASC");
$images = [];
if($imgsResult && $imgsResult->num_rows > 0){
    while($img = $imgsResult->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($r['title']); ?></title>

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

/* ===== Container ===== */
.research-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 ===== */
.research-details h1 {
    font-size: 2.2rem;
    color: #25A9C2;
    margin-bottom: 15px;
    line-height: 1.3;
}

/* ===== Meta Info ===== */
.details-meta {
    display: flex;
    gap: 15px;
    font-size: 14px;
    color: #555;
    margin-bottom: 25px;
}

.details-meta span {
    background: #f0f4ff;
    padding: 6px 12px;
    border-radius: 12px;
    font-weight: 500;
}

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

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

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

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

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

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

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

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

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

.back-btn:hover, .btn:hover {
    background: #0d154d;
}

/* ===== 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) {
    .research-layout {
        grid-template-columns: 1fr;
    }

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

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

<div class="research-details">

    <h1><?= htmlspecialchars($r['title']); ?></h1>

    <div class="details-meta">
        <span><?= htmlspecialchars($r['category']); ?></span>
        <span><?= htmlspecialchars($r['status']); ?></span>
        <span><?= date('d M Y', strtotime($r['created_at'])); ?></span>
    </div>

    <div class="research-layout">

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

        <!-- Content -->
        <div class="research-content">
            <h3>Abstract</h3>
            <p><?= nl2br($r['abstract']); ?></p>

            <h3>Full Research</h3>
            <p><?= nl2br($r['content']); ?></p>

            <?php if($r['external_link']): ?>
                <a href="<?= $r['external_link'] ?>" target="_blank" class="btn">View Publication</a>
            <?php endif; ?>
        </div>

    </div>

    <a href="research.php" class="back-btn">← Back to Research</a>

</div>

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

</body>
</html>
