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

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

/* SAVE */

if(isset($_POST['save']))
{
    if($role_id == 1){
        $village_id = intval($_POST['village_id']);
    } else {
        $village_id = $_SESSION['village_id'];
    }

    $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 = uploadImage($_FILES['image'], "uploads/temples");

    if($image === false){
        exit;
    }

    mysqli_query($conn,"
    INSERT INTO village_temples
    (village_id,temple_name,god_name,image,description)
    VALUES
    ('$village_id','$temple_name','$god_name','$image','$description')
    ");

    echo "<script>
    alert('Temple Added Successfully');
    window.location='temples_list.php';
    </script>";
}
?>

<div class="app-content">

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

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

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

<?php if($role_id == 1){ ?>

<label>District</label>
<select id="district" class="form-control">
<option value="">Select District</option>
<?php
$dq=mysqli_query($conn,"SELECT * FROM districts ORDER BY district_name");
while($d=mysqli_fetch_assoc($dq)){
?>
<option value="<?php echo $d['id']; ?>">
<?php echo $d['district_name']; ?>
</option>
<?php } ?>
</select>

<label>Block</label>
<select id="block" class="form-control">
<option value="">Select Block</option>
</select>

<label>Village</label>
<select name="village_id" id="village" class="form-control" required>
<option value="">Select Village</option>
</select>

<?php } ?>

<label>Temple Name</label>
<input type="text" name="temple_name" class="form-control" required>

<label>God Name</label>
<input type="text" name="god_name" class="form-control">

<label>Upload Image</label>

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

<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"></textarea>

<br>

<button type="submit" name="save" class="btn-mobile" style="background:#27ae60;color:#fff;">
<i class="fa fa-save"></i> Save 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";
    }
});

/* DISTRICT → BLOCK */
document.getElementById("district")?.addEventListener("change", function(){

fetch("get_blocks.php?district_id="+this.value)
.then(res=>res.text())
.then(data=>{
document.getElementById("block").innerHTML=data;
document.getElementById("village").innerHTML="<option>Select Village</option>";
});

});

/* BLOCK → VILLAGE */
document.getElementById("block")?.addEventListener("change", function(){

fetch("get_villages.php?block_id="+this.value)
.then(res=>res.text())
.then(data=>{
document.getElementById("village").innerHTML=data;
});

});

</script>