<?php

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

/* Only Super Admin can access */

allowRoles([1]);

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

$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,"INSERT INTO users
(name,email,password,role_id,district_id,block_id,village_id)
VALUES
('$name','$email','$password','$role_id','$district_id','$block_id','$village_id')");

echo "<script>alert('User Created Successfully');</script>";

}

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

<div class="main">

<h2>Create 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" required style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Email</label>
<input type="email" name="email" style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Password</label>
<input type="text" name="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 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 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 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 echo $row['village_name']; ?>
</option>

<?php
}
?>

</select>
</div>

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

</form>

</div>

</div>

include "../layout/footer.php";