<?php

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

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

$id = $_GET['id'];

$districts = mysqli_query($conn,"SELECT * FROM districts");

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

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

$district_id = $_POST['district_id'];
$department_name = $_POST['department_name'];
$description = $_POST['description'];

mysqli_query($conn,"
UPDATE departments
SET district_id='$district_id',
department_name='$department_name',
description='$description'
WHERE id='$id'
");

echo "<p style='color:green;'>Department Updated</p>";

}

?>

<div class="main">

<h2>Edit Department</h2>

<form method="POST">

<label>Select District</label>

<select name="district_id">

<?php while($d=mysqli_fetch_assoc($districts)){ ?>

<option value="<?php echo $d['id']; ?>"
<?php if($d['id']==$row['district_id']) echo "selected"; ?>>

<?php echo $d['district_name']; ?>

</option>

<?php } ?>

</select>

<br><br>

<label>Department Name</label>

<input type="text" name="department_name"
value="<?php echo $row['department_name']; ?>">

<br><br>

<label>Description</label>

<textarea name="description" id="editor">
<?php echo $row['description']; ?>
</textarea>

<br><br>

<button type="submit" name="update">Update</button>

</form>

</div>

<script src="https://cdn.ckeditor.com/4.21.0/standard/ckeditor.js"></script>

<script>
CKEDITOR.replace('editor');
</script>

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