<?php

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

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

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

/* DEFAULT */
$village_id = 0;
$data = [];

/* ROLE BASE */
if($role_id == 5){
    $village_id = $_SESSION['village_id'];
}

/* LOAD DATA */
if(isset($_POST['village_id'])){
    $village_id = $_POST['village_id'];
}

/* FETCH DATA */
if($village_id){
    $res = mysqli_query($conn,"SELECT * FROM village_about WHERE village_id='$village_id'");
    $data = mysqli_fetch_assoc($res);
}

/* SAVE */
if(isset($_POST['save']))
{
$village_id = $_POST['village_id'] ?? $village_id;

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

$image = $data['image'] ?? "";

/* IMAGE UPLOAD */
if(!empty($_FILES['image']['name']))
{
    $target_dir = "../../uploads/villages/";

    if(!is_dir($target_dir)){
        mkdir($target_dir,0777,true);
    }

    $ext = pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);
    $image = "village_".time().rand(100,999).".".$ext;

    move_uploaded_file($_FILES['image']['tmp_name'],$target_dir.$image);
}

/* CHECK EXIST */
$check = mysqli_query($conn,"SELECT id FROM village_about WHERE village_id='$village_id'");
$exist = mysqli_fetch_assoc($check);

if($exist){
    mysqli_query($conn,"
    UPDATE village_about SET
    title='$title',
    description='$description',
    image='$image'
    WHERE village_id='$village_id'
    ");
}else{
    mysqli_query($conn,"
    INSERT INTO village_about (village_id,title,description,image)
    VALUES ('$village_id','$title','$description','$image')
    ");
}

echo "<script>alert('Saved Successfully'); window.location='about_village.php';</script>";
}
?>

<!-- ===== PAGE TITLE ===== -->

<h4>About Village</h4>
<p style="font-size:13px;color:#666;">Manage village details</p>

<!-- ===== FORM CARD ===== -->

<div class="card-box">

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

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

<!-- LOCATION -->

<label>District</label>
<select id="district" class="input">
<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="input">
<option value="">Select Block</option>
</select>

<label>Village</label>
<select name="village_id" id="village" class="input" required onchange="this.form.submit()">
<option value="">Select Village</option>
</select>

<?php } else { ?>

<input type="hidden" name="village_id" value="<?php echo $village_id; ?>">

<?php } ?>

<!-- TITLE -->

<label>Title</label>
<input type="text" name="title" class="input"
placeholder="Enter village title"
value="<?php echo htmlspecialchars($data['title'] ?? ''); ?>">

<!-- IMAGE -->

<label>Upload Image</label>
<input type="file" name="image" class="input">

<?php if(!empty($data['image'])){ ?>
<div style="margin-top:10px;">
<img src="../../uploads/villages/<?php echo $data['image']; ?>"
style="width:100%;border-radius:10px;">
</div>
<?php } ?>

<!-- DESCRIPTION -->

<label>Description</label>
<textarea name="description" rows="5" class="input"
placeholder="Write about village..."><?php echo htmlspecialchars($data['description'] ?? ''); ?></textarea>

<br>

<button type="submit" name="save" class="btn-mobile btn-save">
    Save Details
</button>

</form>

</div>

<!-- ===== BOTTOM NAV ===== -->

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

    <a href="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>

/* 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>