<?php
if(basename($_SERVER['PHP_SELF']) == 'users.php') {
  $st=" class='current'";
} else {
  $st="";
}  
include'connection.php';
if(!$_SESSION['Settings'])
Header("location:home.php");
include'header.php';
$perpagevalue=20;
$cond=$condi='';
$ecode=0;
			$conde="AND `Status`='0'";
			
if(isset($_POST['search']))
		{
			$searchkeyword=$_POST['searchkeyword'];
		if($searchkeyword)
			$conde="AND (`Fname` LIKE '%$searchkeyword%' OR `Lname` LIKE '%$searchkeyword%' OR `Address` LIKE '%$searchkeyword%' OR `Idno` LIKE '%$searchkeyword%' OR `Contact1` LIKE '%$searchkeyword%' OR `Contact2` LIKE '%$searchkeyword%' OR `Father` LIKE '%$searchkeyword%' OR `Mother` LIKE '%$searchkeyword%')";
		}
if(isset($_POST['submitfilter']))
		{
			$department=$_POST['department'];
		if($department)
			$condi="AND `Depart`='$department'";
		}

if(isset($_POST['edit_id']))
		{
			$ecode=$_POST['rowid'];
		}
		

if(isset($_POST['changep']))
		{
			$ecode=$_POST['rowid'];
			$passw=md5($_POST['password']);
			$confir=md5($_POST['confirm_password']);
			if($passw==$confir){
			$pto=20;
	$do=mysqli_query($conn, "UPDATE `employees` SET `Password`='$passw' WHERE `Eid`='$ecode' LIMIT 1");
			}
			else{
			    $pto=30;
			}
		}

if(isset($_POST['update']))
		{
			$privilege=$_POST['privilege'];
			$ecode=$_POST['rowid'];
			$box=$_POST['box'];
			$fin=$_POST['fin'];
			$pms=$_POST['pms'];
	
	$do=mysqli_query($conn, "UPDATE `employees` SET `Access`='$privilege', `Cashbox`='$box', `Finance`='$fin', `PMS`='$pms' WHERE `Eid`='$ecode' LIMIT 1");
	
	    $pto=10;
		}


$do=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Eid`!='1001' AND `Password`!='' AND `Email`!='' AND `Access`!='SUSPENDED' $conde $condi $cond ORDER BY `Access` ASC, `Fname` ASC");
$fo=mysqli_num_rows($do);
?>	  
	  
	  <div class="container-fluid main-content">
  <div class="page-title">
           <h1 style='margin-top:-20px; margin-bottom: 5px;'>
         System Users
          </h1>
                </div>
      
        <div class="row">
        <div class="col-md-2">
		<ul class="list-group">
			 			
		    <li class="list-group-item active">
           <a href="users.php">
           <p>
           <i class="lnr lnr-menu-circle"></i>&nbsp;List of Users
           </p></a>
           </li>
                          <li class="list-group-item">
           <a href="new_employee.php">
           <p>
           <i class="lnr lnr-plus-circle"></i>&nbsp;Create New
           </p></a>
           </li>
                       </ul>
		
         
  
            </div>
         
         
         <!-- Search and show per page on top of table -->
         <div class="col-md-10">
             
          <?php
     if($pto==20)
	echo"<center><div class='alert alert-success' style='border-radius:5px; height:30px; padding-top:6px; margin-top:-40px; width:90%;'>
<i class='lnr lnr-checkmark-circle'></i> <button class='close' data-dismiss='alert' type='button'>&times;</button> User has been updated successfull. </div></center>";

     if($pto==20)
	echo"<center><div class='alert alert-success' style='border-radius:5px; height:30px; padding-top:6px; margin-top:-40px; width:90%;'>
<i class='lnr lnr-checkmark-circle'></i> <button class='close' data-dismiss='alert' type='button'>&times;</button> Password has been changed successfull. </div></center>";        

    
     if($pto==30)
	echo"<center><div class='alert alert-danger' style='border-radius:5px; height:30px; padding-top:6px; margin-top:-40px; width:90%;'>
<i class='lnr lnr-warning'></i> <button class='close' data-dismiss='alert' type='button'>&times;</button> Password and confirmation are not matching. </div></center>";     
          
          ?>
             
             
          <div class="row">
           <div class="col-md-4 "><div class="col-md-6 "> 
            
              <?php
			  if(!$ecode)
				print("<span>Total Records Found : $fo </span>");
			?>
          
         </div>    
          
                    <div class="col-md-5 "> 
                    
          </div> 
          
        </div>
          
          
           <div class="col-md-8 ">
           

        <form action="users.php" method="post" class="form-horizontal ">
                  
                       <div class="col-md-6">
                           
                           
                        </div>
                       <div class="col-lg-6 input-group">
                       
                      <input class="form-control" name="searchkeyword" placeholder="search" type="text" id="searche">
                      <input value="" name="department" type="hidden">
                      <span class="input-group-btn">
                        <button class="btn  btn-primary" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                      </span>
                      
                      
                      </div>
         </form>
            </div> 
               
            </div>
            
            <div class="row" style='padding-top:-30px;'>
            <div class="col-md-12">
            <span>&nbsp;&nbsp;&nbsp;</span>  
            <div class="widget-container fluid-height clearfix" style='margin-top:-40px;'>
            <div class="widget-content padded clearfix">



			<?php
			if($ecode){



$do=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Eid`='$ecode' ORDER BY `Eid` ASC LIMIT 1");
while($ro=mysqli_fetch_assoc($do)){
$rowid=$ro['Eid'];
$fname=$ro['Fname'];
$lname=$ro['Lname'];
$depart=$ro['Depart'];
$birth=$ro['Birth'];
$contact1=$ro['Contact1'];
$contact2=$ro['Contact2'];
$idno=$ro['Idno'];
$currentp=$ro['Currentp'];
$address=$ro['Address'];
$gender=$ro['Gender'];
$email=$ro['Email'];
$password=$ro['Password'];
$confirm_password=$ro['Password'];
$bank=$ro['Bank'];
$branch=$ro['Branch'];
$city=$ro['City'];
$account=$ro['Account'];
$remarks=$ro['Remarks'];
$father=$ro['Father'];
$mother=$ro['Mother'];
$rssb=$ro['Rssb'];
$salary=number_format($ro['Salary']);
$qualify=$ro['Qualify'];
$start=$ro['Starting'];
$photo=$ro['Photo'];
$file1=$ro['File1'];
$file2=$ro['File2'];
$file3=$ro['File3'];
$acces=$ro['Access'];
$susp=$ro['Suspend'];
$box=$ro['Cashbox'];
$fin=$ro['Finance'];
$pms=$ro['PMS'];
	if(!$photo)
		$photo="imgs/-text.png";
	else
		$photo="photos/$photo";
}

$de=mysqli_query($conn, "SELECT *FROM `depart` WHERE `Number`='$depart' ORDER BY `Number` ASC");
			  $re=mysqli_fetch_assoc($de);
					$depart=$re['Depart'];


					?>




					
<div class="col-md-3">
            <div class="widget-container fluid-height" style="height:280px;">
              <div class="heading">
                <h3>Profile Picture</h3>
              </div>
              <div class="widget-content padded " style="text-align:center;">
                            	<img src="<?php echo $photo ?>" width="60%"><br><br>
								Gender: <?php echo $gender ?>  <br><div style='padding-top:10px;'><b>
								 <?php echo"&nbsp; $privi" ?> </b></div>
                            </div>
            </div>
          </div>
          <div class="col-md-5">
            <div class="widget-container fluid-height" style="height:280px;">
              <div class="heading">
               <h3>Personal Details</h3>
              </div>
             <div class="widget-content padded">
  <strong>Full Name:</strong>
   <p><?php echo" $fname $lname"; ?></p>
    <strong>Email:</strong>
     <p><?php echo $email ?></p>
     <strong>Phone No:</strong>
    <p><?php echo"$contact1, $contact2" ?></p>
  <strong>Date Of Birth:</strong>                  
 <p><?php echo $birth ?></p>           
 </div>
  </div>
   </div>
          <div class="col-md-4">
            <div class="widget-container fluid-height" style="height:280px;">
              <div class="heading">
                <h3>Official Details</h3>
              </div>
              <div class="widget-content padded">
             <strong>ID/Passport No:</strong>
                  <p><?php echo $idno ?>                 </p>
         <strong>RSSB Number:</strong>
                 <p><?php echo $rssb ?>                  </p>
                  <strong>Employee Code:</strong>
                 <p><?php echo $code ?></p>
                   <strong>Address:</strong>                
 <p><?php echo $address ?></p> 
              </div>
            </div>
          </div>
        </div>
<hr>
 <form action="users.php" method="post" class="form-horizontal">
<div align='right'>
   <div class="col-md-1"> </div>
	<div class="col-md-3 text-left"> &nbsp;&nbsp; User Access
	    <select name="privilege" class="form-control" required>
	<?php
		$n=1;
		$got=mysqli_query($conn, "SELECT `Number`,`Access` FROM `users` ORDER BY `Access` ASC");
			while($rot=mysqli_fetch_assoc($got)){
				$acce=$rot['Access'];
				$numb=$rot['Number'];
				if($acce==$acces)
					$sel='selected';
				else
					$sel='';
				echo"<option $sel value='$acce'>$acce</option>";
			}
			?>
                             
               </select>  </div>
               
        <div class="col-md-4">       
	<div class="col-md-4 text-left"> &nbsp;&nbsp; Cashbox
	<select name="box" class="form-control" required>
	<?php
	
				if($box==1){
					$n='';
					$y='selected';
				}
				else{
					$n='selected';
					$y='';
				}
				echo"<option $n value='0'>NO</option>
				<option $y value='1'>YES</option>";
		?>
                             
               </select>  </div>
               
    <div class="col-md-4 text-left"> &nbsp;&nbsp;Finance
	<select name="fin" class="form-control" required>
	<?php
	
				if($fin==1){
					$n='';
					$y='selected';
				}
				else{
					$n='selected';
					$y='';
				}
				echo"<option $n value='0'>NO</option>
				<option $y value='1'>YES</option>";
		?>
                             
               </select>  </div>
               
               
	<div class="col-md-4 text-left"> &nbsp;&nbsp; APPROVAL
	<select name="pms" class="form-control" required>
	<?php
	
				if($pms==1){
					$n='';
					$y='selected';
				}
				else{
					$n='selected';
					$y='';
				}
		echo"<option $n value='0'>NO</option>
	<option $y value='1'>YES</option>";
		?>
                             
               </select>  </div>
               
                    </div>

	<div class="col-md-2" style='text-align:left;'><br> 
	<?php 
		echo"<input type='hidden' name='rowid' value='$ecode'>";
	?>
		  <button class="btn btn-lg btn-block btn-success" type="submit"  name="update" style='width: 140px; height:34px; padding-top:5px;' onClick='return validatepass(form);'><i class="lnr lnr-checkmark-circle"></i> UPDATE </button> 
		</div><div class="col-md-2 text-center"><br>
		<a href='#' data-placement='top' data-toggle='modal' data-target='#changep'>Change Password</a></div>   
		<div class="row"> </div>    <div class="row"> </div>
</div></form>

<?php
 if($_SESSION['Settings']){
echo"<div class='modal fade' id='changep' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' style='top:120px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content text-left'><div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>CHANGE PASSWORD 
		<label class='text-right pull-right'><b> $fname $lname </b></label></h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:120px; padding-top:30px;'>
      
      
      
      <div class='form-group'><div class='col-md-5 text-center'>
                  New  Password<br>
              <input class='form-control text-center' name='password' id='password' type='password' required>
            </div> 
        <div class='col-md-2'> </div><div class='col-md-5 text-center'>
                  Confirmation New Password</br>
                  
              <input class='form-control text-center' type='password' name='confirm_password' id='confirm_password' required> 
              <span id='message'></span></div> 
                                   </div>
      
      
      
            </div><input type='hidden' name='rowid' value='$ecode'>
      <div class='modal-header' style='height:50px; padding:10px; border:0px solid blue; text-align:right'>
        <button type='button' class='btn btn-sm btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>
        <button type='submit' name='changep' class='btn btn-sm btn-success' style='width:80px;' data-toggle='tooltip' data-placement='top'> SAVE </button>
      </div></form>
    </div>
  </div>
</div>";
}
			}
			else{
				?>
                
    <table class="table table-striped table-hover" style='margin-top:-20px;'>       <thead><tr role="row">
                     <th class="hidden-xs"> No </th> 
                       <th> First&nbsp;Name </th>
                       <th> Last&nbsp;Name</th>
                       <th> Contact</th>
                       <th> Address</th>
					   <th> Cashbox</th>
                       <th> Finance</th>
                       <th> Approval</th>
					   <th> Privilege</th>
					   <th class="hidden-xs"><center> Status</th>
                        <th><center> Actions</th>
                     </tr>
                    </thead>
                                        <tbody>
                                          
				<?php
		$n=1;
	while($ro=mysqli_fetch_assoc($do)){
$code=$ro['Eid'];
$fna=$ro['Fname'];
$lna=$ro['Lname'];
$dep=$ro['Depart'];
$cont=$ro['Contact1'];
$idn=$ro['Idno'];
$pos=$ro['Currentp'];
$adde=$ro['Address'];
$acces=$ro['Access'];
$stn="style='padding:2px;'";

if($acces=='SUSPENDED'){
	$sta='SUSPENDED';
	$clo='color:red';
}
else{
	$sta='ACTIVE';
	$clo='';
}

if($ro['Cashbox'])
$cbx="<font color='blue'>YES</font>";
else
$cbx="NO";

if($ro['Finance'])
$fnc="<font color='blue'>YES</font>";
else
$fnc="NO";


if($ro['PMS'])
$ps="<font color='blue'>YES</font>";
else
$ps="NO";


	print("<tr style='padding:2px; $clo'>
	<td class='hidden-xs' $stn><div align='center'> $n</td>
	<td $stn> $fna</td><td $stn> $lna</td><td $stn align='center'> $cont</td>
	<td $stn> $adde</td><td $stn align='center'> $cbx</td>
	<td $stn align='center'> $fnc</td><td $stn align='center'> $ps</td>
	<td $stn> $acces</td>
	<td $stn> $sta</td>	 

						<form method=post action='users.php'><td align='center' style='width:40px;padding:2px;'>
                        <input type='hidden' name='rowid' value='$code'><button style='background-color:transparent;border:0px solid black; 
						width:40px; margin:0px;' type='submit' name='edit_id'><span style=color:blue;>Edit</span></button></td></form></tr>");
												$n++;
				}
				?>
                    </tbody>   
                  </table>
                                      <div class="row">
                  <div class="col-md-12">
                  <div class="pull-right">
                  <ul class="pagination">
                      <li>
                                            </li>
                      <li class="activei">
                        <a href="#">
                        Next                        </a>
                      </li>
                                             <li>
                                            </li>
                    </ul>
              </div></div></div>
                         
								<?php
			}
								?>
              </div>
            </div>
        </div>
   <?php
   include'footer.php';
   ?>