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

$role_id = $_SESSION['role_id'] ?? 0;
$village_id = ($role_id == 1) ? ($_POST['village_id'] ?? 0) : $_SESSION['village_id'];

$success = "";

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

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

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

$image="";

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

$filename = time().'_'.$_FILES['image']['name'];

move_uploaded_file($_FILES['image']['tmp_name'],"../../uploads/gallery/".$filename);

$image = $filename;
}

mysqli_query($conn,"INSERT INTO gallery_categories (village_id,category_name,image) VALUES ('$village_id','$category_name','$image')");

$success = "Category created successfully 🎉";
}
?>

<div class="container" style="max-width:500px;margin:auto;">

<h3 style="text-align:center;">📸 Add Category</h3>

<?php if($success){ ?>
<div style="background:#d4edda;padding:10px;border-radius:8px;margin-bottom:10px;">
<?php echo $success; ?>
</div>
<?php } ?>

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

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

<select id="district" class="form-control mb-2">
<option value="">Select District</option>
<?php
$dq=mysqli_query($conn,"SELECT * FROM districts");
while($d=mysqli_fetch_assoc($dq)){
echo "<option value='{$d['id']}'>{$d['district_name']}</option>";
}
?>
</select>

<select id="block" class="form-control mb-2">
<option>Select Block</option>
</select>

<select name="village_id" id="village" class="form-control mb-2" required>
<option>Select Village</option>
</select>

<?php } ?>

<input type="text" name="category_name" placeholder="Category Name" class="form-control mb-2" required>

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

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

</form>

</div>

<script>
// same JS as yours (no change)
</script>

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