<?php
include "include/db.php";
include "include/adminauth.php";
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

function clean_input_data($cellVal){	
    include "include/db.php";
	$cellVal = trim($cellVal);
	$cellVal = mysqli_real_escape_string($conn, $cellVal);
	$cellVal = strtolower($cellVal);
    $cellVal = str_replace(' ', '', $cellVal);
    $cellVal = str_replace('*', '', $cellVal);
	return $cellVal;
}

function clean_cell_input_data($cellVal1){	
    include "include/db.php";
	$cellVal1 = trim($cellVal1);
	$cellVal1 = mysqli_real_escape_string($conn, $cellVal1);
	return $cellVal1;
}
    $successmessage = '';
    $message = '';
if(isset($_REQUEST['mess'])){
    $message = $_REQUEST['mess'];
}
if($conn){      
    if(isset($_REQUEST['pass'])){
        $passID = $_REQUEST['pass'];
        $pass_email = '';
        $query00 = "SELECT employee_email FROM employee WHERE employee_id = $passID";
        $run00 = mysqli_query($conn,$query00);
        while($row00 = mysqli_fetch_array($run00)){
            $pass_email = $row00['employee_email'];
        }
    }
    
    if(isset($_REQUEST['repass']) && isset($_REQUEST['password']) && isset($_REQUEST['con_password'])){       
        $passID =$_REQUEST['repass'];
        $pass_email = '';
        $query00 = "SELECT employee_email FROM employee WHERE employee_id = $passID";
        $run00 = mysqli_query($conn,$query00);
        while($row00 = mysqli_fetch_array($run00)){
            $pass_email = $row00['employee_email'];
        }
        $password = $_REQUEST['password'];
        $con_password = $_REQUEST['con_password'];
        if(strlen($password) < 8){
           $message = "The pasword is too short. Set a password longer than 6 characters for $pass_email.";
        }else{
            if($password !== $con_password){
                $message = "The passwords don't Match Enter Again for $pass_email";
            }else{
             
            $emp_pass_hash = password_hash($password,PASSWORD_DEFAULT);
            if(strlen($emp_pass_hash) > 0){    
                $time = date("Y-m-d H:i:s");
                $query1 = "UPDATE employee SET status = 'active', password_hash = '$emp_pass_hash' , active_time ='$time' WHERE employee_id= $passID";

                
                $run1 = mysqli_query($conn, $query1);
                if(mysqli_affected_rows($conn)==1){
                    $successmessage = "Employee Password Changed for $pass_email";
                }else{
                    $message = "Employee Password Could Not be changed for $pass_email";
                }
            }else{
                $message = "Employee Password Could Not be changed for $pass_email";
            }    

        }
     }

    }
    

    if(isset($_REQUEST['delete'])){
        $id3 = $_REQUEST['delete'];
        $time = date("Y-m-d H:i:s");
        $query3 = "UPDATE employee SET status = 'delete', deact_time = '$time' WHERE employee_id = '$id3'";
        $run2 = mysqli_query($conn, $query3);
        if(mysqli_affected_rows($conn)==1){
            $successmessage = "Employee Deleted";
        }
    }
}

include "header.php";

?>

<div id="page-wrapper">

<h3>Set Employee Password and Activate</h3>

<br>   
<?php 
                    
                    
                    if(isset($successmessage)){
                        if(strlen($successmessage) > 0){   
                            echo '<div class="alert alert-success">
                            <strong>'. $successmessage .'</strong></div>';
                        } 
                    }

                   if(isset($message)){
                        if(strlen($message) > 0){
                            echo '<div class="alert alert-danger">
                                <strong>'. $message .'</strong>
                            </div>';
                        }
                   }
                    ?>  
                    <?php 
if(isset($_REQUEST['pass']) || isset($_REQUEST['repass'])){

?>            
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
               
                <h3>
        <?php if(isset($_REQUEST['pass']) || isset($_REQUEST['repass'])){
              echo "Set Password for $pass_email <a style = 'float:right;' class='btn btn-warning' href='set-up-employee.php'>Cancel</a>";
        } ?> </h3>
        </div>
              <!--   </div> -->
                                                                  

             <div class="panel-body">
                      
             <div class = "row">                   
      
                  
<div class="col-lg-12">


               <?php 
}
               if(isset($_REQUEST['pass']) || isset($_REQUEST['repass'])){
                echo "<form action = 'set-up-employee.php?repass=$passID' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
               }
               ?>   


<?php
   if(isset($_REQUEST['pass']) || isset($_REQUEST['repass'])){ ?>

     
   
      <div class="row">

 
        <div class = "form-group col-lg-6">
        <label for="password">Password</label>
        <input type="password" class="form-control" name="password" id="password" required="required">

        </div>
        <div class="form-group col-lg-6">
   
        <label for="con_password">Confirm Password</label>
        
        <input type="password" class="form-control" name="con_password" id="con_password" required="required">
        </div>
        </div>
   
   <?php
    }   
    
    ?> 		
  
 
<?php 
if(isset($_REQUEST['pass']) || isset($_REQUEST['repass'])){

?>
    <br>
 <div class = "row">
     
<div class = "form-group col-lg-6">
<input type="submit" name = "submit" value = "Submit" class="btn btn-success">
</div>


<div class = "form-group col-lg-6">
</div>
</div>
    

 
</form>

 <br>
 
</div>
</div>
                </div><!-- panel-body  -->
            </div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row -->
   <?php 
   }

?>
                            
     <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-emp">
                                    <thead>
                                        <tr>
                                                <th>
                                                Staff ID
                                                </th>
                                                <th>
                                                First Name
                                                </th>
                                                <th>
                                                Last Name
                                                </th>
                                                <th>
                                                Email
                                                </th>                                               
                                            <th>Delete User</th>
                                            <th>Set Password</th>
                                            
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM employee WHERE status = 'deactive' ORDER BY employee_id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $id = $row['employee_id'];
                $name = $row['employee_fname']; 
                $code = $row['employee_lname'];  
                $email = $row['employee_email'];  
                $password_hash = $row['password_hash']; 
                $status = $row['status'];      
     
               if($password_hash == 0){    
                echo '<tr class="odd gradeX">';
                echo '<td>' .$id. '</td>'; 
                echo '<td>' .$name. '</td>';
                echo '<td>' .$code. '</td>';
                echo '<td>' .$email. '</td>';
                echo "<td><a href='set-up-employee.php?delete=$id' class=\"delete22\" data-confirm=\"Are you sure to Delete $name $code?\">Delete</a> </td>";
                echo "<td><a href='set-up-employee.php?pass=$id'>Set password and activate</a> </td>";
                echo '</tr>';
               } 
            }
        }
    }

?>
                                            
                                            </tbody>
                                    
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
    </div> <!--  page wrapper -->
                              
       
        <script>
    var deleteLinks = document.querySelectorAll('.delete22');

for (var i = 0; i < deleteLinks.length; i++) {
  deleteLinks[i].addEventListener('click', function(event) {
	  event.preventDefault();

	  var choice = confirm(this.getAttribute('data-confirm'));

	  if (choice) {
	    window.location.href = this.getAttribute('href');
	  }
  });
}
    </script>
    
<?php  include "footer.php";   ?>