<?php

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

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

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

/* HIDE SIDEBAR */
echo "<style>
.sidebar{display:none !important;}
.topbar{margin-left:0 !important;}
.main{margin-left:0 !important;padding-bottom:80px;}
</style>";

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

$name = mysqli_real_escape_string($conn,$_POST['name']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$role_id = intval($_POST['role_id']);
$district_id = $_POST['district_id'] ?: NULL;
$block_id = $_POST['block_id'] ?: NULL;
$village_id = $_POST['village_id'] ?: NULL;

/* ROLE LOGIC */

if($role_id==1){
$district_id=NULL;
$block_id=NULL;
$village_id=NULL;
}
elseif($role_id==3 || $role_id==7){
$block_id=NULL;
$village_id=NULL;
}
elseif($role_id==4 || $role_id==8){
$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>";

}

/* FETCH DATA */
$roles = mysqli_query($conn,"SELECT * FROM roles");
$districts = mysqli_query($conn,"SELECT * FROM districts ORDER BY district_name");
$blocks = mysqli_query($conn,"SELECT * FROM blocks ORDER BY block_name");
$villages = mysqli_query($conn,"SELECT * FROM villages ORDER BY village_name");

?>

<div class="app-content">

<!-- HEADER -->
<div class="card-box">
<h4>Create User</h4>
<p style="font-size:12px;color:#777;">Add new system user</p>
</div>

<!-- FORM -->
<div class="card-box">

<form method="post">

<label>Name</label>
<input type="text" name="name" class="form-control" required>

<label style="margin-top:10px;">Email</label>
<input type="email" name="email" class="form-control">

<label style="margin-top:10px;">Password</label>
<input type="text" name="password" class="form-control">

<label style="margin-top:10px;">Role</label>
<select name="role_id" class="form-control">
<?php while($r=mysqli_fetch_assoc($roles)){ ?>
<option value="<?php echo $r['id']; ?>">
<?php echo htmlspecialchars($r['role_name']); ?>
</option>
<?php } ?>
</select>

<label style="margin-top:10px;">District</label>
<select name="district_id" class="form-control">
<option value="">Select District</option>
<?php while($d=mysqli_fetch_assoc($districts)){ ?>
<option value="<?php echo $d['id']; ?>">
<?php echo htmlspecialchars($d['district_name']); ?>
</option>
<?php } ?>
</select>

<label style="margin-top:10px;">Block</label>
<select name="block_id" class="form-control">
<option value="">Select Block</option>
<?php while($b=mysqli_fetch_assoc($blocks)){ ?>
<option value="<?php echo $b['id']; ?>">
<?php echo htmlspecialchars($b['block_name']); ?>
</option>
<?php } ?>
</select>

<label style="margin-top:10px;">Village</label>
<select name="village_id" class="form-control">
<option value="">Select Village</option>
<?php while($v=mysqli_fetch_assoc($villages)){ ?>
<option value="<?php echo $v['id']; ?>">
<?php echo htmlspecialchars($v['village_name']); ?>
</option>
<?php } ?>
</select>

<br>

<button type="submit" name="save" class="btn-mobile"
style="background:#2c7be5;color:#fff;">
Create User
</button>

</form>

</div>

</div>

<!-- ===== FOOTER ===== -->

<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-house"></i>
        Home
    </a>

    <a href="../villages/village_data.php">
        <i class="fa fa-map"></i>
        Villages
    </a>

    <a href="../families/view_families.php">
        <i class="fa fa-users"></i>
        Families
    </a>

    <a href="../schemes/view_schemes.php">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

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