<?php

include "../../config/master.php";

/* Only Super Admin */
allowRoles([1]);

$id = intval($_GET['id']);

/* FETCH DATA */
$data = mysqli_query($conn,"SELECT * FROM districts WHERE id='$id'");
$row = mysqli_fetch_assoc($data);

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

/* UPDATE */
if(isset($_POST['update']))
{

$district_name = mysqli_real_escape_string($conn, trim($_POST['district_name']));
$district_name_hi = mysqli_real_escape_string($conn, trim($_POST['district_name_hi']));

/* CHECK DUPLICATE */
$check = mysqli_query($conn,"
SELECT id FROM districts 
WHERE LOWER(district_name)=LOWER('$district_name') 
AND id != '$id'
");

if(mysqli_num_rows($check) > 0)
{
echo "<script>alert('District already exists');</script>";
}
else
{

mysqli_query($conn,"UPDATE districts SET
district_name='$district_name',
district_name_hi='$district_name_hi'
WHERE id='$id'");

echo "<script>alert('District Updated Successfully');</script>";
echo "<script>window.location='../villages/village_data.php';</script>";

}

}
?>

<div class="main">

<h2>Edit District</h2>

<form method="POST" class="form-box">

<label>District Name (English)</label>
<input type="text" name="district_name"
value="<?php echo htmlspecialchars($row['district_name']); ?>" required>

<label>District Name (Hindi)</label>
<input type="text" name="district_name_hi"
value="<?php echo htmlspecialchars($row['district_name_hi']); ?>">

<button name="update" class="btn-save">Update District</button>

</form>

</div>

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