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

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

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

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

$image="";

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

$dir="../../uploads/gallery/";

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

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

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

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

}

mysqli_query($conn,"
INSERT INTO gallery_images
(village_id,category_id,title,image)
VALUES
('$village_id','$category_id','$title','$image')
");

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

}

?>

<div class="main">

<h2>Add Gallery Image</h2>

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

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">
<option value="">Select Village</option>
</select>

<?php } ?>


<label>Category</label>

<select name="category_id" id="category" class="form-control">

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

<?php

if($role_id != 1){

$c=mysqli_query($conn,"
SELECT * FROM gallery_categories
WHERE village_id='$village_id'
");

while($row=mysqli_fetch_assoc($c)){
?>

<option value="<?php echo $row['id']; ?>">
<?php echo $row['category_name']; ?>
</option>

<?php }} ?>

</select>


<label>Title</label>
<input type="text" name="title" class="form-control">


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

<br>

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

</form>

</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");
const category = document.getElementById("category");

/* DISTRICT → BLOCK */

if(district){

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

fetch("get_blocks.php?district_id="+this.value)
.then(res=>res.text())
.then(data=>{
block.innerHTML=data;
});

});

}

/* BLOCK → VILLAGE */

if(block){

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

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

});

}

/* VILLAGE → CATEGORY */

if(village){

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

fetch("get_gallery_categories.php?village_id="+this.value)
.then(res=>res.text())
.then(data=>{
category.innerHTML=data;
});

});

}

});

</script>