<?php

include "../../config/master.php";
allowRoles([1,5]);

include "../layout/header.php";

/* HIDE SIDEBAR FOR MOBILE APP UI */
echo "<style>
.sidebar{display:none !important;}
.topbar{margin-left:0 !important;}
.main{margin-left:0 !important;padding-bottom:80px;}
</style>";

$role_id = $_SESSION['role_id'] ?? 0;

if($role_id == 1){

$temples=mysqli_query($conn,"
SELECT t.*,v.village_name
FROM village_temples t
LEFT JOIN villages v ON t.village_id=v.id
ORDER BY t.id DESC
");

}else{

$village_id=$_SESSION['village_id'];

$temples=mysqli_query($conn,"
SELECT t.*,v.village_name
FROM village_temples t
LEFT JOIN villages v ON t.village_id=v.id
WHERE t.village_id='$village_id'
ORDER BY t.id DESC
");

}

?>

<div class="app-content">

<!-- HEADER CARD -->
<div class="card-box" style="display:flex;justify-content:space-between;align-items:center;">

<div>
<h4 style="margin:0;">Temples</h4>
<p style="font-size:12px;color:#777;margin:0;">Village religious places</p>
</div>

<a href="add_temple.php" class="btn-mobile btn-add">
<i class="fa fa-plus"></i>
</a>

</div>

<!-- LIST -->

<?php if(mysqli_num_rows($temples)>0){ ?>

<?php while($row=mysqli_fetch_assoc($temples)){ ?>

<div class="card-box">

<!-- IMAGE -->
<?php if(!empty($row['image'])){ ?>
<img src="../../uploads/temples/<?php echo $row['image']; ?>"
style="width:100%;height:160px;object-fit:cover;border-radius:10px;margin-bottom:10px;">
<?php } ?>

<!-- TITLE -->
<h5 style="margin:0;"><?php echo $row['temple_name']; ?></h5>

<!-- DETAILS -->
<p style="font-size:12px;color:#666;margin:4px 0;">
🙏 <?php echo $row['god_name']; ?>
</p>

<p style="font-size:12px;color:#999;margin:0;">
📍 <?php echo $row['village_name']; ?>
</p>

<!-- ACTION ICONS -->
<div style="display:flex;gap:10px;margin-top:12px;">

<a href="edit_temple.php?id=<?php echo $row['id']; ?>"
style="
flex:1;
text-align:center;
padding:10px;
border-radius:8px;
background:#3498db;
color:white;
text-decoration:none;
font-size:13px;
">
<i class="fa fa-edit"></i>
</a>

<a href="delete_temple.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Delete temple?')"
style="
flex:1;
text-align:center;
padding:10px;
border-radius:8px;
background:#e74c3c;
color:white;
text-decoration:none;
font-size:13px;
">
<i class="fa fa-trash"></i>
</a>

</div>

</div>

<?php } ?>

<?php } else { ?>

<div class="card-box" style="text-align:center;">
No temples found
</div>

<?php } ?>

</div>

<!-- BOTTOM NAV -->
<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-home"></i>
        Home
    </a>

    <a href="../villages/village_data.php">
        <i class="fa fa-map"></i>
        Villages
    </a>

    <a href="../families/view_families.php">
        <i class="fa fa-users"></i>
        Families
    </a>

    <a href="../schemes/view_schemes.php">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

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