<?php

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


// GET ID
$id = $_GET['id'] ?? 0;

// FETCH NEWS
$news = mysqli_query($conn,"
SELECT n.*, 
d.district_name,
b.block_name,
v.village_name
FROM village_news n
LEFT JOIN districts d ON n.district_id = d.id
LEFT JOIN blocks b ON n.block_id = b.id
LEFT JOIN villages v ON n.village_id = v.id
WHERE n.id='$id'
");

$data = mysqli_fetch_assoc($news);

if(!$data){
echo "News not found";
exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title><?php echo $data['title']; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
body{font-family:Arial;background:#f5f5f5;margin:0;}
.container{max-width:800px;margin:auto;background:#fff;padding:15px;}
img{width:100%;border-radius:10px;margin-bottom:15px;}
.title{font-size:22px;font-weight:bold;}
.meta{font-size:13px;color:#777;margin:8px 0;}
.badge{display:inline-block;padding:4px 8px;border-radius:5px;font-size:11px;color:#fff;margin-right:5px;}
.district{ background:#007bff; }
.block{ background:#28a745; }
.village{ background:#ff9800; }
.desc{font-size:15px;line-height:1.6;margin-top:15px;}
.back{margin-bottom:10px;display:inline-block;color:#007bff;text-decoration:none;}
</style>

</head>

<body>

<div class="container">

<a href="index.php" class="back">← Back</a>

<?php if(!empty($data['image'])){ ?>
<img src="/digital_panchayat/uploads/news/<?php echo $data['image']; ?>">
<?php } ?>

<div class="title"><?php echo $data['title']; ?></div>

<div class="meta">🗓 <?php echo $data['news_date']; ?></div>

<div class="meta">

<?php if(!empty($data['district_name'])){ ?>
<span class="badge district"><?php echo $data['district_name']; ?></span>
<?php } ?>

<?php if(!empty($data['block_name'])){ ?>
<span class="badge block"><?php echo $data['block_name']; ?></span>
<?php } ?>

<?php if(!empty($data['village_name'])){ ?>
<span class="badge village"><?php echo $data['village_name']; ?></span>
<?php } ?>

</div>

<div class="desc">
<?php echo nl2br($data['description']); ?>
</div>

</div>

</body>
</html>