<?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);
	}
	//=========================================================================================
?>
<!DOCTYPE html>
<html lang="en">
	<head>
        <?php include("library/head.php");?>
        <title>Users List Report | <?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-file-text fa-lg"></i> USERS LIST REPORT</h4>
					<p>Users List Report 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="users-list-report">Users List Report</a></li>
				</ul>
			</div>
      
			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>

			<div class="row">
				<div class="col-md-12">
                <div class="tile">
                    <div class="tile-body">
                        <div class="row" style="margin-left: 0px; margin-right: 0px;">
                            <div class="col-md-12">
                                <div class="row">
                                    <div class="col-md-12" style="margin: 0px;">
                                        <img src="img/reportHeader.png" width="100%" height="150px" /><br />
                                        <h2 class="text-center text-primary mb-5">Users List Report</h2>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-12" style="background-image: url('img/reportBackground.png'); background-size : 90% 800px; background-repeat : repeat-y; background-position: center">
                                        <div class="table-responsive">
                                            <table class="table table-bordered">
                                                <thead>
                                                    <tr class="bg-light text-center">
                                                        <th>Serial</th>
                                                        <th>User ID</th>
                                                        <th>Fullname</th>
                                                        <th>Username</th>
                                                        <th>Password</th>
                                                        <th>Status</th>
                                                        <th>Register Date</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                        $i = 1;
                                                        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.employeeName, system_users.Username, system_users.Passcode, system_users.loginStatus, system_users.registerDate 
                                                        FROM system_users 
                                                        JOIN employees ON system_users.employeeID = employees.employeeID
                                                        ". $fetch_parameter ." ORDER BY system_users.Username ASC") or die(mysqli_error($conn));
                                                        while ($result = mysqli_fetch_array($qry)) { ?>
                                                            <tr>
                                                                <td class="text-center"> <?php echo $i; ?></td>
                                                                <td class="text-center"> <?php echo $result[0]; ?></td>
                                                                <td class="text-left"><?php if ($result[0] == $_SESSION['userIdd']) { echo $result[1]. " (<strong>You</strong>)"; } else { echo $result[1]; } ?></td>
                                                                <td class="text-left"> <?php echo $result[2]; ?></td>
                                                                <td class="text-center"> <?php echo kuceliAsalka($result[3], $ereyFure); ?></td>
                                                                <td class="text-center"><?php if ($result[4] == 1) { echo "<span class='badge badge-success p-2'><i class='fa fa-check'></i> Online</span>"; } else if ($result[4] == 0) { echo "<span class='badge badge-warning p-2'><i class='fa fa-minus'></i> Offline</span>"; } ?></td>
                                                                <td class="text-center"> <?php echo substr($result[5], 0, 10); ?></td>
                                                            </tr> <?php
                                                            $i++;
                                                        }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <div class="row" style="margin-top: 40px;">
                                    <div class="col-md-8">&nbsp;</div>
                                    <div class="col-md-4">
                                        <h5 class="text-black text-center" style="font-weight: bold; padding: 10px;"><?php echo $_SESSION['fullName']; ?><br /><b style="font-weight: normal">(Signature)</b></h5>
                                        <h5 style="font-weight: normal; padding-left: 10px;"><hr style="border-top: 1px solid black;"></h5>
                                    </div>
                                </div>
                            </div>
                        </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">
			$('#tenantsTable').DataTable({"pageLength": 100, "order": []})
			$("#cmbSexNewTenant, #cmbMaritalStatusNewTenant, #cmbContactPersonRelationshipNewTenant").select2({ tags: false, dropdownParent: $("#registerNewTenantModal") })
			$("#cmbSexUpdateTenant, #cmbMaritalStatusUpdateTenant, #cmbContactPersonRelationshipUpdateTenant, #cmbTenantStatusUpdateTenant").select2({ tags: false, dropdownParent: $("#updateTenantModal") })
		</script>

	</body>
</html>