<?php
	require ('my_functions.php');
 
	$pagetitle = "System Users";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$results = DB::query("SELECT personnel_details.id, personnel_details.display_name, personnel_details.employee_role,  personnel_details.phone, personnel_details.email, personnel_details.login_name, personnel_details.status, peronnel_role.role_name, shop.full_name, personnel_details.is_loggedin, personnel_details.is_locked, personnel_details.version FROM personnel_details  LEFT JOIN shop ON personnel_details.branch = shop.id LEFT JOIN peronnel_role ON personnel_details.role = peronnel_role.id WHERE    personnel_details.role > 1 ORDER BY   personnel_details.employee_role, shop.full_name, peronnel_role.role_name, personnel_details.display_name ");
	#$results = DB::query("SELECT personnel_details.id, personnel_details.display_name, personnel_details.employee_role,  personnel_details.phone, personnel_details.email, personnel_details.login_name, personnel_details.status, peronnel_role.role_name, shop.full_name, personnel_details.is_loggedin, personnel_details.is_locked, personnel_details.version FROM personnel_details  LEFT JOIN shop ON personnel_details.branch = shop.id LEFT JOIN peronnel_role ON personnel_details.role = peronnel_role.id  ORDER BY   personnel_details.employee_role, shop.full_name, peronnel_role.role_name, personnel_details.display_name ");
	
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>System Users</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1)
							{
								 
						?>
						<div class="card-header with-border text-center">
							<h3 class="card-title">System Users List</h3>
						</div>
						
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							
							<div class="card-body table-responsive no-padding">	
							
								<form class="form" method="get" action="employee_commissions.php">
													
													<div class="row">
														 
														<div class="col-lg-3">
															From<br />
															<input type="text" class="form-control" name="start_date" placeholder="YYYY-MM-DD" id="start_date" >
														</div>
														<div class="col-lg-3">
															To<br />
															<input type="text" class="form-control" name="end_date" placeholder="YYYY-MM-DD" id="end_date" >
														</div>
														<div class="col-lg-3">
															&nbsp;<br />
															<input type="submit" class="form-control btn-primary" value="Search" />
														</div>
													</div>
														  
													</form>
													
													<?php 
													if(isset($_GET['start_date'])){
													
													 
													$start_date = $end_date =  false;
													if(isset($_GET['start_date'])){
															$start_date = $_GET['start_date'];
													}else{
														#$start_date = date('Y-m-d');
													}
													
													if(isset($_GET['end_date'])){
															$end_date = $_GET['end_date'];
													}else{
														#$end_date = date('Y-m-d');
													}
													
													 echo $start_date.'Start Dayte <br />'; 
													 echo $end_date.'End date';
												 
													 $_SESSION['start_date'] = $start_date;
													 $_SESSION['end_date'] = $end_date;
														
													?>
							 
								 
								
								<table class="table table-hover">
									<thead>
										<tr>
											<th>Name</th>
											<th>Role</th>
											 
											<th>Phone</th>
											 
											<th class="text-center"> Category</th>
											<th class="text-center">Status</th>
											 
											<th>Branch</th>
											<th>Value</th>
											<th>Rate</th>
											<th>Commission</th>
											<th class="text-center">Action</th>
										</tr>
									</thead>
									<tfoot>
										<tr>
											<th>Name</th>
											<th>Role</th>
											 
											<th>Phone</th>
										 
											<th class="text-center"> Category</th>
											<th class="text-center">Status</th>
											 
											<th>Branch</th>
											<th>Value</th>
											 
											<th>Commission</th>
											<th class="text-center">Action</th>
										</tr>
									</tfoot>
									<tbody>
									<?php
									 
										foreach($results as $user)
										{
									?>
										<tr>
											<td><?php echo $user['display_name']; ?></td>
											<td><?php echo $user['role_name']; ?></td>
										 
											<td><?php echo $user['phone']; ?></td>
											 
											<td class="text-center">
											
											<?php
											$role_name = 'N/A';
												$employee_roledata = DB::query("SELECT * FROM employee_category  ");
												
												foreach($employee_roledata as $useemployee_role)
												{
													if($useemployee_role['id'] == $user['employee_role'] ){
														$role_name =  $useemployee_role['name'];
													}
											 
											 
												}
												echo $role_name ;
												 
												$commission = 0;
												$tech = $sales = $user['id'];
												$rate = 0;
												$money = 0;
												
												 
												if($user['employee_role'] ==1){
													 
													 
													  $commission = Sum_Repair_com( $tech, $start_date , $end_date);
													  $money = Sum_tech_bill($tech, $start_date , $end_date);
												}else if($user['employee_role'] ==2){
													 
													 
												  $commission = Sum_sales_com( $tech, $start_date , $end_date);
												  $money = Sum_bill($tech, $start_date , $end_date);
												  
												 
												}
												 
												 
											 
												
											?>
											 
											
											</td>
											
											<td class="text-center"><?php echo ($user['status'] == 1) ? '<label class="label label-success" >Active</label>' : '<label class="label label-danger" >Inactive</label>'; ?></td>
											 <td><?php echo $user['full_name']; ?></td>
											 <td> <?php  //echo $tech.$start_date .$end_date; 
											 echo number_format($money, 2);?> </td>
											 
											 <td> <?php   echo number_format($commission , 2);?> </td>
											<td class="text-center">
												<a href="javascript:void(0);" onclick="gendawano('commission_details', 'userfocus', <?php echo $user['id'] ; ?>)" data-toggle="tooltip" title="View Details" class="tooltips"><span class="label label-primary" ><i class="fa fa-book"></i></span></a>
												 
											</td>
										</tr>
									<?php
										}
									?>
									</tbody>
								</table>
							</div>
						</div>
						<?php
													}
													else echo 'Select period for your analysis';
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
		<div class="modal" id="waitdialogmodal"  data-backdrop="static">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                  <div class="modal-body">
					<div class="text-center">
						<img src="<?php echo $base_url . $ajax_loading_image; ?>" />
					</div>
				  </div>
              </div>
            </div>
        </div>

		<?php include ('includes/general_footer.php')?>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/systemusers.js" type="text/javascript"></script>
	<!-- bootstrap datepicker -->
		<script src="<?php echo $base_url; ?>plugins/datepicker/bootstrap-datepicker.js"></script>

		<script type="text/javascript">
		//Date picker
    $('#start_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
	
	$('#end_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
		</script>
	</body>
</html>