<?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 = 'employees'") 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>Manage Employees | <?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-users fa-lg"></i> MANAGE EMPLOYEES</h4>
                    <p>Manage Employees 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="employees">Manage Employees</a></li>
                </ul>
            </div>
      
            <?php include("models/admin_config.php");?>
            <?php include("models/system_config.php");?>

            <!-- Employees page -->
            <div class="row">
                <div class="col-md-12">
                    <div class="tile">
                        <div class="tile-body">
                            <div class="row">
								<div class="form-group col-md-12">
									<h5 class="pb-2">
										<span><i class="fa fa-list-ul"></i> Employees List</span>
										<span>
											<?php
												if ($_SESSION['userTypeID'] == 'PS0001') {
													?><button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#registerNewEmployeeModal"><i class="fa fa-plus"></i> Register New Employee</button><?php
												} else {
													if ($role_rs[3] == '1') { ?>
														?><button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#registerNewEmployeeModal"><i class="fa fa-plus"></i> Register New Employee</button><?php								
													}
												}
											?>
										</span>
									</h5>
									<hr />
								</div>
							</div>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-sm" id="employeesTable">
                                    <thead>
										<tr class="text-center bg-primary text-light" height="35px">
                                            <th class="align-middle">Serial</th>
                                            <th class="align-middle">Photo</th>
                                            <th class="align-middle">Employee Name</th>
                                            <th class="align-middle">Position</th>
                                            <th class="align-middle">Working Area</th>
                                            <th class="align-middle">Salary</th>
                                            <th class="align-middle">Telephone</th>
                                            <th class="align-middle">Register Date</th>
                                            <th class="align-middle">Status</th>
                                            <?php if (($_SESSION['userTypeID'] != 'PS0001') || ($role_rs[4] == '0' && $role_rs[5] == '0')) { } else {?><th class="align-middle">Action</th><?php } ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $getEmployees = mysqli_query($conn, "SELECT employees.employeeID, employees.employeePhoto, employees.employeeName, employee_positions.positionName, working_areas.workingAreaName, employees.employeeSalary, employees.employeeMobileNo, employees.registerDate, employees.employeeStatus 
                                            FROM employees
                                            JOIN employee_positions ON employees.employeePosition = employee_positions.positionID
                                            JOIN working_areas ON employees.workingAreaID = working_areas.workingAreaID
                                            WHERE employeePosition != 'PS0001'") or die(mysqli_error($conn));
                                            $x = 1;
                                            while ($rs = mysqli_fetch_array($getEmployees)) {?>
                                                <tr>
                                                    <td class="text-center"><?php echo $x; ?></td>
                                                    <td class="align-middle text-center py-1"><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-left"><?php echo $rs[4]; ?></td>
                                                    <td class="align-middle text-center"><?php echo "$ ".number_format($rs[5], 2); ?></td>
                                                    <td class="align-middle text-center"><?php echo $rs[6]; ?></td>
                                                    <td class="align-middle text-center"><?php echo substr($rs[7], 0, 10); ?></td>
                                                    <td class="align-middle text-center"><?php if ($rs[8] == 1) { echo "<span class='badge badge-success'><i class='fa fa-check-circle'></i> Active</span>"; } else if ($rs[8] == 0) { echo "<span class='badge badge-danger'><i class='fa fa-times-circle'></i> Left</span>"; } ?></td>
                                                    <?php
                                                        if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
                                                            ?><td class="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-primary btnUpdateEmployee" id="<?php echo "Update". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>" data-toggle="modal" data-target="#updateEmployeeModal"><i class="fa fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteEmployee" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                                                           </td><?php
                                                        } else if ($role_rs[4] == '1') {
                                                            ?><td class="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-primary btnUpdateEmployee" id="<?php echo "Update". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>" data-toggle="modal" data-target="#updateEmployeeModal"><i class="fa fa-edit"></i> Edit</a>
                                                                    </div>
                                                                </div>                                                            
                                                            </td><?php
                                                        } else if ($role_rs[5] == '1') {
                                                            ?><td class="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 btnDeleteEmployee" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
                                                </tr><?php
                                                $x++;
                                            }
                                        ?>
                                    </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">$('#employeesTable').DataTable({"pageLength": 100, "order": []});</script>
		<script type="text/javascript"> $("#cmbEmployeeSexNewEmployee, #cmbMaritalStatusNewEmployee, #cmbIdentificationDocumentTypeNewEmployee, #cmbEmployeeDepartmentNameNewEmployee, #cmbEmployeePositionNameNewEmployee, #cmbWorkingAreaNameNewEmployee").select2({ tags: true, dropdownParent: $("#registerNewEmployeeModal") });</script>
		<script type="text/javascript"> $("#cmbEmployeeSexUpdateEmployee, #cmbMaritalStatusUpdateEmployee, #cmbIdentificationDocumentTypeUpdateEmployee, #cmbEmployeeDepartmentNameUpdateEmployee, #cmbEmployeePositionNameUpdateEmployee").select2({ tags: true, dropdownParent: $("#updateEmployeeModal") });</script>

    </body>
</html>