<?php

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

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

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

/* GET VILLAGE */

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'];
}

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

$image="";

/* IMAGE UPLOAD */

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

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

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

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

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

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

}

mysqli_query($conn,"
INSERT INTO village_sarpanches
(village_id,name,image,duration,description)
VALUES
('$village_id','$name','$image','$duration','$description')
");

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

}

?>

<div class="main">

<div class="container-fluid">

<h2 class="mb-4">Add Sarpanch</h2>

<div class="form-box">

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

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

<!-- DISTRICT -->

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


<!-- BLOCK -->

<label>Block</label>

<select id="block" class="form-control">

<option value="">Select Block</option>

</select>


<!-- VILLAGE -->

<label>Village</label>

<select name="village_id" id="village" class="form-control" required>

<option value="">Select Village</option>

</select>

<?php } ?>


<label>Sarpanch Name</label>

<input 
type="text"
name="name"
class="form-control"
required>


<label>Duration</label>

<input 
type="text"
name="duration"
class="form-control"
placeholder="Example: 2020 - 2025">


<label>Image</label>

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


<label>Description</label>

<textarea 
name="description"
class="form-control"
rows="5"></textarea>


<br>

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

</form>

</div>

</div>

</div>

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


<script>

/* LOAD BLOCKS */

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

var district_id=this.value;

fetch("get_blocks.php?district_id="+district_id)

.then(res=>res.text())

.then(data=>{

document.getElementById("block").innerHTML=data;

});

});


/* LOAD VILLAGES */

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

var block_id=this.value;

fetch("get_villages.php?block_id="+block_id)

.then(res=>res.text())

.then(data=>{

document.getElementById("village").innerHTML=data;

});

});

</script>