<?php
    session_start();
    include("library/conn.php");

    // ========================== Our Encrytion Function =======================================
	$ereyFure = 'qkwjdiw239&&jdafweihbrhnan&^%$ggdnawhd4njshjwuuO';
	function bedelXogta($xogta, $ereyFure) {
		$encryption_key = base64_decode($ereyFure);
		$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
		$encrypted = openssl_encrypt($xogta, 'aes-256-cbc', $encryption_key, 0, $iv);
		return base64_encode($encrypted . '::' . $iv);
	}

	function kuceliAsalka($xogta, $ereyFure) {
		$encryption_key = base64_decode($ereyFure);
		list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($xogta), 2), 2, null);
		return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
	}
	//=========================================================================================

    $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>Users Passcode | <?php echo $_SESSION['systemName']; ?></title>
</head>

<body class="app sidebar-mini">

    <!-- Navbar-->
    <?php include("library/header.php"); ?>
    <!-- Sidebar menu-->
    <div class="app-sidebar__overlay" data-toggle="sidebar"></div>
    <?php include("library/sidebar.php"); ?>

    <main class="app-content">
        <div class="app-title">
            <div>
                <h4 class="text-primary"><i class="fa fa-lock fa-lg"></i> USERS PASSCODE</h4>
            </div>
        </div>

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

        <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-edit"></i> Change User Passcode</h5>									
                                <hr />
							</div>
							<div class="col-md-12">
                                <form action="#" method="POST" id="changeUserPasscodeAdminForm" changing-user-type="administrator">
            						<div class="row">
            							<div class="form-group col-md-12">
            								<label class="control-label">Employee Information</label>
            								<input type="hidden" name="txtUserIddChangeUserPasscodeAdmin" id="txtUserIddChangeUserPasscodeAdmin" required readonly />
            								<input class="form-control" type="text" name="txtUserInformationChangeUserPasscodeAdmin" id="txtUserInformationChangeUserPasscodeAdmin" required readonly />
            							</div>
            		                    <div class="form-group col-md-12">
            								<label class="control-label">Username</label>
            								<input class="form-control" type="text" name="txtUserNameChangeUserPasscodeAdmin" id="txtUserNameChangeUserPasscodeAdmin" required />
            							</div>
            							<div class="form-group col-md-12">
            								<label class="control-label">New Passcode</label>
            								<input type="text" class="form-control" name="txtNewPasscodeChangeUserPasscodeAdmin" id="txtNewPasscodeChangeUserPasscodeAdmin" placeholder="Enter new passcode" required>
            							</div>
            						</div>
            
            						<div class="mt-2" style="text-align: right;">
            							<button type="submit" class="btn btn-primary" name="btnChangeUserPasscodeAdmin" id="btnChangeUserPasscodeAdmin"><i class="fa fa-fw fa-lg fa-pencil"></i>Change Passcode</button>&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> Users Passcode List</span></h5>
								<hr />
							</div>
						</div>
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover table-sm" id="usersPasscodeTable">
                                <thead>
									<tr class="text-center bg-primary text-light" height="40px">
                                        <th class="align-middle">Photo</th>
                                        <th class="align-middle">Fullname</th>
                                        <th class="align-middle">Position</th>
                                        <th class="align-middle">Username</th>
                                        <th class="align-middle">Password</th>
                                        <?php if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1') || ($role_rs[5] == '1')) {?><th class="align-middle"><center>Action</center></th><?php } ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        if ($_SESSION['userTypeID'] == "PS0001" ) $fetch_parameter = "";
                                        else if ($_SESSION['userTypeID'] == "PS0002" ) $fetch_parameter = "WHERE employees.employeePosition != 'PS0001'";
                                        else $fetch_parameter = "WHERE employees.employeePosition != 'PS0001' AND employees.employeePosition != 'PS0002'";
                                        $qry = mysqli_query($conn, "SELECT system_users.userID, employees.employeePhoto, employees.employeeName, employee_positions.positionName, system_users.Username, system_users.Passcode
                                        FROM system_users 
                                        JOIN employees ON system_users.employeeID = employees.employeeID
                                        JOIN employee_positions ON employees.employeePosition = employee_positions.positionID
                                        ". $fetch_parameter ." ORDER BY system_users.Username ASC") or die(mysqli_error($conn));
                                        while ($result = mysqli_fetch_array($qry)) {?>
                                            <tr>
                                                <td class="py-1 align-middle text-center"><img class="img-fluid rounded-circle" src="<?php echo $result[1]; ?>" width="35px" height="35px"/></td>
                                                <td class="text-left align-middle"><?php echo ($result[0] == $_SESSION['userIdd']) ? $result[2]. " (<strong>You</strong>)" : $result[2]; ?></td>
                                                <td class="text-left align-middle"> <?php echo $result[3]; ?></td>
                                                <td class="text-left text-primary align-middle font-weight-bold"> <?php echo $result[4]; ?></td>
                                                <td class="text-center align-middle"> <?php echo kuceliAsalka($result[5], $ereyFure); ?></td>
                                                <?php
                                                    if ($_SESSION['userTypeID'] == "PS0001") {
                                                        ?><td class="text-center align-middle">
                                                            <span class="btn btn-outline-primary btnChangeUserPasscodeAdmin" id="<?php echo "Update" . $result[0]; ?>" data-id="<?php echo $result[0]; ?>"><i class="fa fa-fw fa-lg fa-edit"></i> Change Passcode</span>
                                                        </td><?php
                                                    } else {
                                                        if ($role_rs[4] == '1') {
                                                            ?><td class="text-center align-middle">
                                                                <span class="btn btn-outline-primary btnChangeUserPasscodeAdmin" id="<?php echo "Update" . $result[0]; ?>" data-id="<?php echo $result[0]; ?>"><i class="fa fa-fw fa-lg fa-edit"></i> Change Passcode</span>
                                                            </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">
        $("#usersPasscodeTable").DataTable({"pageLength": 100, "order": []})
    </script>


</body>

</html>