<?php
    session_start();
    include("library/conn.php");
    
    $role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'system_users'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php include("library/head.php");?>
        <title>System Users | <?php echo $_SESSION['systemName']; ?></title>
    </head>
    <body class="app sidebar-mini">

        <!-- Navbar-->
        <?php include("library/header.php");?>
        <!-- Sidebar menu-->
        <?php include("library/sidebar.php");?>
        <main class="app-content">
            <div class="app-title">
                <div>
					<h4 class="text-primary"><i class="fa fa-user fa-lg"></i> SYSTEM USERS</h4>
                    <p>System Users Page</p>
                </div>
                <ul class="app-breadcrumb breadcrumb">
                    <li class="breadcrumb-item"><i class="fa fa-home"></i></li>
                    <li class="breadcrumb-item"><a href="system-users">System Users</a></li>
                </ul>
            </div>

            <?php include("models/admin_config.php");?>
            <?php include("models/system_config.php");?>

            <!-- Users page -->
            <div class="row">
                
                <div class="col-md-4">
					<div class="tile">
						<div class="tile-body">
							<div class="row">
								<div class="col-md-12">
                                    <h5 class="text-primary"><i class="fa fa-user-plus"></i> Register User</h5>									
                                    <hr />
								</div>
    							<div class="col-md-12">
                                    <form action="#" method="POST" id="registerUserForm">
                                        <div class="row">
                                            <div class="form-group col-md-12">
                                                <label class="control-label">Full Name</label>
                                                <select class="form-control" name="cmbFullNameRegisterUser" id="cmbFullNameRegisterUser" style="font-size: 16px; width: 100%;" required>
                
                                                </select>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <label class="control-label">User Name</label>
                                                <input class="form-control" type="text" name="txtUsernameRegisterUser" id="txtUsernameRegisterUser" placeholder="Example: Nasiimpc " required>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <label class="control-label">Passcode</label>
                                                <input class="form-control" type="text" name="txtPasscodeRegisterUser" id="txtPasscodeRegisterUser" placeholder="Enter password..." required>
                                            </div>
                                            <div class="form-group col-md-12">
                								<label class="control-label">2-Step Verification</label>
                								<select class="form-control" style="width: 100%;" name="cmbEnableDisable2StepVerificationRegisterUser" id="cmbEnableDisable2StepVerificationRegisterUser" required>
                									<option value="">Select Mode</option>
                									<option value="1">Enabled</option>
                									<option value="0">Disabled</option>
                								</select>
                	                            <input class="form-control" type="hidden" name="txtRegisteredByRegisterUser" id="txtRegisteredByRegisterUser" value="<?php echo $_SESSION['userIdd']; ?>" required>
                								<input class="form-control" type="hidden" name="txtRegisterDateRegisterUser" id="txtRegisterDateRegisterUser" value="<?php echo date("Y-m-d H:i:s"); ?>" required>							
                							</div>
                                        </div>                
                    					<div class="form-group col-md-12 mt-3" style="text-align: right;">
                                            <button type="submit" class="btn btn-primary" name="btnRegisterUser" id="btnRegisterUser"><i class="fa fa-fw fa-lg fa-save"></i>Register User</button>&nbsp;&nbsp;&nbsp;
                        				    <button type="reset" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-fw fa-lg fa-times"></i>Close</button>
                                        </div>
                                    </form>
					            </div>
						    </div>
					    </div>
				    </div>
			    </div>
                
                <div class="col-md-8">
                    <div class="tile">
                        <div class="tile-body">
                             <div class="row">
								<div class="form-group col-md-12">
									<h5><span><i class="fa fa-list-ul"></i> System Users List</span></h5>
									<hr />
								</div>
							</div>
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover table-sm" id="systemUsersTable">
                                    <thead>
										<tr class="text-center bg-primary text-light" height="40px">
                                            <th class="align-middle">Photo</th>
                                            <th class="align-middle">Full Name</th>
                                            <th class="align-middle">Username</th>
                                            <th class="align-middle">2-Step Verify</th>
                                            <th class="align-middle">Login Status</th>
                                            <?php if ($role_rs[4] == '0' && $role_rs[5] == '0') { } else {?><th class="align-middle">Action</th><?php } ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $getUsers = mysqli_query($conn, "SELECT system_users.userID, employees.employeePhoto, employees.employeeName, system_users.Username, system_users.twoStepVerification, system_users.verificationCode, system_users.loginStatus 
                                            FROM employees 
                                            JOIN system_users ON employees.employeeID = system_users.employeeID
                                            WHERE system_users.userID NOT IN (SELECT userID FROM system_users WHERE userID = '". $_SESSION['userIdd'] ."') AND employees.employeePosition != 'PS0001'") or die(mysqli_error($conn));
                                            while ($rs = mysqli_fetch_array($getUsers)) { ?>
                                                <tr>
                                                    <td class="py-1 align-middle  text-center"><img class="img-fluid rounded-circle" src="<?php echo $rs[1]; ?>" width="35px" height="35px"/></td>
                                                    <td class="align-middle text-left"><?php echo $rs[2]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $rs[3]; ?></td>
                                                    <td class="align-middle text-center">
                                                        <?php 
                                                            if ($rs[4] == 1) {
                                                                ?><div class="toggle lg">
                                                                    <label><input type="checkbox" id="cbxStepVerificationStatus" name="cbxStepVerificationStatus" checked disabled /><span class="button-indecator"></span></label>
                                                                </div><?php 
                                                            } else if ($rs[4] == 0) { 
                                                                ?><div class="toggle lg">
                                                                    <label><input type="checkbox" id="cbxStepVerificationStatus" name="cbxStepVerificationStatus" disabled /><span class="button-indecator"></span></label>
                                                                </div><?php
                                                            }
                                                        ?>
                                                    </td>
                                                    <td class="align-middle text-center"><?php if ($rs[6] == 1) { echo "<span class='badge badge-success p-2'><i class='fa fa-check'></i> Online</span>"; } else if ($rs[6] == 0) { echo "<span class='badge badge-warning p-2'><i class='fa fa-minus'></i> Offline</span>"; } ?></td>
                                                    
                                                    <?php
                                                        if ($_SESSION['userTypeID'] == 'PS0001') {
                                                            ?><td class="align-middle text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary">Action</button>
                                                                    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                        <span class="caret"></span>
                                                                    </button>
                                                                    <div class="dropdown-menu" style="min-width: 8rem;">
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteUser" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                                                            </td><?php
                                                        } else {
                                                            if ($role_rs[5] == 1) {
                                                                ?><td class="align-middle text-center">
                                                                    <div class="btn-group col-md-12">
                                                                        <button type="button" class="btn btn-primary">Action</button>
                                                                        <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                            <span class="caret"></span>
                                                                        </button>
                                                                        <div class="dropdown-menu" style="min-width: 8rem;">
                                                                            <a href="#" class="dropdown-item text-danger btnDeleteUser" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                        </div>
                                                                    </div>   
                                                                </td><?php
                                                            }
                                                        }
                                                    ?>
                                                </tr><?php
                                            }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        
    	<!-- Essential javascripts for application to work-->
    	<?php include("library/footer.php");?>	
        <?php include("library/scripts.php");?>
        
        <script type="text/javascript">
			$('#systemUsersTable').DataTable({"pageLength": 100, "order": []})
			$("#cmbFullNameRegisterUser, #cmbEnableDisable2StepVerificationRegisterUser").select2({tags: false})
			$("#cmbSexUpdateTenant, #cmbMaritalStatusUpdateTenant, #cmbContactPersonRelationshipUpdateTenant, #cmbTenantStatusUpdateTenant").select2({ tags: false, dropdownParent: $("#updateTenantModal") })
		</script>
		
    </body>
</html>