<?php

include "../config/db.php";
include "header.php";

/* GET ID SAFELY */
$scheme_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if($scheme_id <= 0){
    echo "<div class='container'><h3>Invalid Scheme ID</h3></div>";
    include "footer.php";
    exit;
}

/* FETCH DATA (FIXED education_id) */
$q = mysqli_query($conn,"
SELECT s.*, sc.category_name, e.education_name
FROM schemes s
LEFT JOIN scheme_categories sc ON s.category_id = sc.id
LEFT JOIN education_master e ON s.education_id = e.id
WHERE s.id = '$scheme_id'
LIMIT 1
");

$scheme = mysqli_fetch_assoc($q);

/* SAFE CHECK */
if(!$scheme){
    echo "<div class='container'><h3>Scheme not found</h3></div>";
    include "footer.php";
    exit;
}

$is_general = ($scheme['scheme_mode'] == 'general');

?>

<div class="container">

<div class="scheme-card-main">

<!-- TITLE -->
<h2 class="scheme-title">
<?php echo htmlspecialchars($scheme['scheme_name']); ?>
</h2>

<!-- IMAGE -->
<div class="scheme-img-box">
<?php if(!empty($scheme['image'])){ ?>
<img src="/digital_panchayat/uploads/schemes/<?php echo htmlspecialchars($scheme['image']); ?>" 
onerror="this.src='/digital_panchayat/assets/no-image.png';">
<?php } else { ?>
<img src="/digital_panchayat/assets/no-image.png">
<?php } ?>
</div>

<!-- INFO -->
<?php if(!$is_general){ ?>
<div class="scheme-info-box">

<div><b>📂 Category:</b> <?php echo $scheme['category_name'] ?? 'All'; ?></div>
<div><b>🏢 Department:</b> <?php echo htmlspecialchars($scheme['department']); ?></div>
<div><b>👤 Gender:</b> <?php echo htmlspecialchars($scheme['gender']); ?></div>
<div><b>🎓 Education:</b> <?php echo $scheme['education_name'] ?? 'Any'; ?></div>
<div><b>🏠 Family:</b> <?php echo htmlspecialchars($scheme['family_type']); ?></div>
<div><b>🎂 Age:</b> <?php echo $scheme['age_min']." - ".$scheme['age_max']; ?></div>

</div>
<?php } ?>

<!-- DESCRIPTION -->
<?php if(!empty($scheme['description'])){ ?>
<div class="section-box desc-box">
<h3>📄 Description</h3>
<div class="content">
<?php echo $scheme['description']; ?>
</div>
</div>
<?php } ?>

<!-- BENEFITS -->
<?php if(!empty($scheme['benefits'])){ ?>
<div class="section-box benefit-box">
<h3>🎁 Benefits</h3>
<div class="content">
<?php echo $scheme['benefits']; ?>
</div>
</div>
<?php } ?>

<!-- ELIGIBILITY -->
<?php if(!empty($scheme['eligibility'])){ ?>
<div class="section-box eligibility-box">
<h3>✅ Eligibility</h3>
<div class="content">
<?php echo $scheme['eligibility']; ?>
</div>
</div>
<?php } ?>

<!-- ONLINE PROCESS -->
<?php if(!empty($scheme['apply_online'])){ ?>
<div class="section-box desc-box">
<h3>🌐 Apply Online</h3>
<div class="content">
<?php echo $scheme['apply_online']; ?>
</div>
</div>
<?php } ?>

<!-- OFFLINE PROCESS -->
<?php if(!empty($scheme['apply_offline'])){ ?>
<div class="section-box benefit-box">
<h3>🏢 Apply Offline</h3>
<div class="content">
<?php echo $scheme['apply_offline']; ?>
</div>
</div>
<?php } ?> 

<!-- DOCUMENTS -->
<?php if(!empty($scheme['documents'])){ ?>
<div class="section-box doc-box">
<h3>📑 Required Documents</h3>
<div class="content">
<?php echo $scheme['documents']; ?>
</div>
</div>
<?php } ?>

</div>

</div>

<?php include "footer.php"; ?>