<?php

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

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

$id = $_GET['id'];

$q = mysqli_query($conn,"SELECT * FROM gallery_categories WHERE id='$id'");
$data = mysqli_fetch_assoc($q);

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

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

$image = $data['image'];

/* NEW IMAGE UPLOAD */

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,"
UPDATE gallery_categories
SET category_name='$category_name', image='$image'
WHERE id='$id'
");

echo "<script>alert('Category Updated');location.href='gallery_categories.php';</script>";

}

?>

<div class="main">

<h2>Edit Gallery Category</h2>

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

<label>Category Name</label>
<input type="text" name="category_name" value="<?php echo $data['category_name']; ?>" class="form-control" required>

<label>Current Image</label><br>

<?php if($data['image']){ ?>

<img src="../../uploads/gallery/<?php echo $data['image']; ?>" width="100">

<?php } ?>

<br><br>

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

<br>

<button type="submit" name="update" class="btn btn-success">
Update
</button>

</form>

</div>

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