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

/* SAVE ANGANWADI */

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

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

$anganwadi_name = mysqli_real_escape_string($conn,$_POST['anganwadi_name']);
$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/anganwadi/";

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

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

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

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

}

mysqli_query($conn,"
INSERT INTO village_anganwadi
(village_id,anganwadi_name,location,image,description)
VALUES
('$village_id','$anganwadi_name','$location','$image','$description')
");

echo "<script>alert('Anganwadi Added Successfully');</script>";

}

?>

<div class="main">

<div class="container-fluid">

<h2 class="mb-4">Add Anganwadi</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>Anganwadi Name</label>
<input type="text" name="anganwadi_name" class="form-control" required>


<label>Location</label>
<input type="text" name="location" class="form-control" placeholder="Example: Near School, Main Road">


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

/* DISTRICT → BLOCK */

if(district){

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

let district_id = this.value;

fetch("get_blocks.php?district_id="+district_id)
.then(res => res.text())
.then(data => {

block.innerHTML = data;
village.innerHTML = "<option value=''>Select Village</option>";

});

});

}

/* BLOCK → VILLAGE */

if(block){

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

let block_id = this.value;

fetch("get_villages.php?block_id="+block_id)
.then(res => res.text())
.then(data => {

village.innerHTML = data;

});

});

}

});

</script>