<?php

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

/* Only Super Admin can access */

allowRoles([1]);

$id=$_GET['id'];

/* FETCH USER DATA */

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


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

$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$role_id=$_POST['role_id'];
$district_id=$_POST['district_id'];
$block_id=$_POST['block_id'];
$village_id=$_POST['village_id'];

/* Role Based Location Logic */

if($role_id==1) // Super Admin
{
$district_id=NULL;
$block_id=NULL;
$village_id=NULL;
}

elseif($role_id==3 || $role_id==7) // District Admin or DC
{
$block_id=NULL;
$village_id=NULL;
}

elseif($role_id==4 || $role_id==8) // Block Admin or BDPO
{
$village_id=NULL;
}

mysqli_query($conn,"
UPDATE users SET
name='$name',
email='$email',
password='$password',
role_id='$role_id',
district_id='$district_id',
block_id='$block_id',
village_id='$village_id'
WHERE id='$id'
");

echo "<script>alert('User Updated Successfully');</script>";
echo "<script>window.location='view_users.php'</script>";

}

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

<div class="main">

<h2>Edit User</h2>

<div style="background:#fff;padding:25px;border-radius:6px;width:500px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">

<form method="post">

<div style="margin-bottom:15px;">
<label>Name</label>
<input type="text" name="name" value="<?php echo $user['name']; ?>" required style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Email</label>
<input type="email" name="email" value="<?php echo $user['email']; ?>" style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Password</label>
<input type="text" name="password" value="<?php echo $user['password']; ?>" style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Role</label>

<select name="role_id" style="width:100%;padding:8px;">

<?php
$roles=mysqli_query($conn,"SELECT * FROM roles");

while($r=mysqli_fetch_assoc($roles))
{
?>

<option value="<?php echo $r['id']; ?>" <?php if($r['id']==$user['role_id']) echo "selected"; ?>>
<?php echo $r['role_name']; ?>
</option>

<?php
}
?>

</select>
</div>

<div style="margin-bottom:15px;">
<label>District</label>

<select name="district_id" style="width:100%;padding:8px;">

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

<?php
$d=mysqli_query($conn,"SELECT * FROM districts");

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

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

<?php
}
?>

</select>
</div>

<div style="margin-bottom:15px;">
<label>Block</label>

<select name="block_id" style="width:100%;padding:8px;">

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

<?php
$b=mysqli_query($conn,"SELECT * FROM blocks");

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

<option value="<?php echo $row['id']; ?>" <?php if($row['id']==$user['block_id']) echo "selected"; ?>>
<?php echo $row['block_name']; ?>
</option>

<?php
}
?>

</select>
</div>

<div style="margin-bottom:20px;">
<label>Village</label>

<select name="village_id" style="width:100%;padding:8px;">

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

<?php
$v=mysqli_query($conn,"SELECT * FROM villages");

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

<option value="<?php echo $row['id']; ?>" <?php if($row['id']==$user['village_id']) echo "selected"; ?>>
<?php echo $row['village_name']; ?>
</option>

<?php
}
?>

</select>
</div>

<button type="submit" name="update" style="background:#2c7be5;color:#fff;padding:10px 18px;border:none;border-radius:4px;cursor:pointer;">
Update User
</button>

</form>

</div>

</div>

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