<?php

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

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

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

if($role_id == 1)
{
$village_id = $_POST['village_id'] ?? 0;
}
else
{
$village_id = $_SESSION['village_id'] ?? 0;
}

if(isset($_POST['save']))
{

if($role_id == 1)
{
$village_id = $_POST['village_id'];
}

$building_name = mysqli_real_escape_string($conn,$_POST['building_name']);
$building_type = mysqli_real_escape_string($conn,$_POST['building_type']);
$location = mysqli_real_escape_string($conn,$_POST['location']);
$description = mysqli_real_escape_string($conn,$_POST['description']);

$image="";

/* IMAGE UPLOAD */

if(!empty($_FILES['image']['name']))
{

$target_dir="../../uploads/buildings/";

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

$ext = pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);

$image="building_".time().rand(100,999).".".$ext;

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

}

mysqli_query($conn,"
INSERT INTO village_govt_buildings
(village_id,building_name,building_type,location,image,description)
VALUES
('$village_id','$building_name','$building_type','$location','$image','$description')
");

echo "<script>alert('Government Building Added');</script>";

}

?>

<div class="main">
<div class="container-fluid">

<h2 class="mb-4">Add Govt Building</h2>

<div class="form-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>Building Name</label>
<input type="text" name="building_name" class="form-control" required>


<label>Building Type</label>
<input type="text" name="building_type" class="form-control" placeholder="Example: School, PHC, Panchayat Bhawan">


<label>Location</label>
<input type="text" name="location" class="form-control">


<label>Image</label>
<input type="file" name="image" class="form-control">


<label>Description</label>
<textarea name="description" class="form-control"></textarea>


<br>

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

</form>

</div>

</div>
</div>

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


<script>

document.addEventListener("DOMContentLoaded", function(){

const district = document.getElementById("district");
const block = document.getElementById("block");
const village = document.getElementById("village");

if(district){

district.addEventListener("change", function(){

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

});

}

if(block){

block.addEventListener("change", function(){

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

});

}

});

</script>