<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

include "../../config/master.php";
include "../../config/image_upload.php";

allowRoles([1,5]);

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

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

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

/* FETCH DATA */
$q = mysqli_query($conn,"SELECT * FROM village_temples WHERE id='$id'");
$data = mysqli_fetch_assoc($q);

if(!$data){
    echo "Temple not found";
    exit;
}

/* UPDATE */
if(isset($_POST['update']))
{

$temple_name = mysqli_real_escape_string($conn,$_POST['temple_name']);
$god_name = mysqli_real_escape_string($conn,$_POST['god_name']);
$description = mysqli_real_escape_string($conn,$_POST['description']);

$image = $data['image'];

/* IMAGE UPDATE */
if(!empty($_FILES['image']['name']))
{
    $newImage = uploadImage($_FILES['image'], "uploads/temples");

    if($newImage){
        $image = $newImage;
    }
}

/* UPDATE */
mysqli_query($conn,"
UPDATE village_temples SET
temple_name='$temple_name',
god_name='$god_name',
description='$description',
image='$image'
WHERE id='$id'
");

echo "<script>
alert('Temple Updated Successfully');
window.location='temples_list.php';
</script>";
exit;

}
?>

<div class="app-content">

<!-- HEADER -->
<div class="card-box">
    <h4>Edit Temple</h4>
    <p style="font-size:12px;color:#777;">Update temple details</p>
</div>

<!-- FORM -->
<div class="card-box">

<form method="POST" enctype="multipart/form-data">

<label>Temple Name</label>
<input type="text" name="temple_name" class="form-control"
value="<?php echo htmlspecialchars($data['temple_name']); ?>" required>

<label>God Name</label>
<input type="text" name="god_name" class="form-control"
value="<?php echo htmlspecialchars($data['god_name']); ?>">

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

<!-- NEW IMAGE -->
<label>Change Image</label>

<label style="
display:block;
text-align:center;
padding:14px;
background:#3498db;
color:white;
border-radius:10px;
cursor:pointer;
margin-top:8px;
">
<i class="fa fa-upload"></i> Select New Image
<input type="file" name="image" id="fileInput" accept="image/*" style="display:none;">
</label>

<img id="preview" style="
width:100%;
height:160px;
object-fit:cover;
border-radius:10px;
margin-top:10px;
display:none;
">

<label>Description</label>
<textarea name="description" class="form-control"><?php echo htmlspecialchars($data['description']); ?></textarea>

<br>

<button type="submit" name="update" class="btn-mobile" style="background:#f39c12;color:#fff;">
<i class="fa fa-save"></i> Update Temple
</button>

</form>

</div>

</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"; ?>

<script>

/* IMAGE PREVIEW */
document.getElementById("fileInput")?.addEventListener("change", function(){
    if(this.files && this.files[0]){
        document.getElementById("preview").src = URL.createObjectURL(this.files[0]);
        document.getElementById("preview").style.display = "block";
    }
});

</script>