<?php
include "include/db.php";
include "include/adminauth.php";
include "header.php";
$message = "";
$successmessage = "";
if($conn){    
    
    if(isset($_REQUEST['insert']) && isset($_REQUEST['pay_type']) && isset($_REQUEST['staff_date']) && isset($_REQUEST['staff_amount'])){
        if(strlen($_REQUEST['staff_date']) >0 && strlen($_REQUEST['pay_type']) > 0 && strlen($_REQUEST['staff_amount']) > 0){
            $staff_date = $_REQUEST['staff_date'];
            $staff_date = mysqli_real_escape_string($conn, $staff_date);
            $staff_date = trim($staff_date);
            $pay_type = $_REQUEST['pay_type'];
            $pay_type = mysqli_real_escape_string($conn, $pay_type);
            $pay_type = trim($pay_type);
            $staff_amount = $_REQUEST['staff_amount'];     
            $staff_amount = mysqli_real_escape_string($conn, $staff_amount);
            $staff_amount = trim($staff_amount);       
            $time = date("Y-m-d H:i:s");

            $query1 = "INSERT INTO staff_pay SET pay_date ='$staff_date', pay_type = '$pay_type', pay_amount = $staff_amount,admin_id = $admin_id, create_time ='$time'";
            
            $run1 = mysqli_query($conn, $query1);
            if(mysqli_affected_rows($conn)== 1){
                $successmessage = "Payroll Total Added";
            }else{
                $message = "Failed.";
            } 
        }       
    } 
    

    if(isset($_REQUEST['spe'])){

        $id2 = $_REQUEST['spe'];

        $query2 = "SELECT * FROM staff_pay WHERE id = '$id2'";
        $run2 = mysqli_query($conn, $query2);

        if($run2){
            if(mysqli_num_rows($run2) == 1){
                while($row2 = mysqli_fetch_array($run2)){
                    $speID2 = $row2['id'];
                    $pay_date = $row2['pay_date'];
                    $pay_amount = $row2['pay_amount'];     
                    $type = $row2['pay_type'];             
                }
            }else{
                header("Location:payroll.php");
            }            
        }else{
            header("Location:payroll.php");
        }

    }

    if(isset($_REQUEST['edit']) && isset($_REQUEST['pay_type'])  && isset($_REQUEST['staff_date']) && isset($_REQUEST['staff_amount'])){
        if(strlen($_REQUEST['edit']) > 0 && strlen($_REQUEST['pay_type']) > 0 && strlen($_REQUEST['staff_date']) >0 && strlen($_REQUEST['staff_amount']) > 0){
            $id2 = $_REQUEST['edit'];
            $staff_date = $_REQUEST['staff_date'];
            $staff_date = mysqli_real_escape_string($conn, $staff_date);
            $staff_date = trim($staff_date);
            $pay_type = $_REQUEST['pay_type'];
            $pay_type = mysqli_real_escape_string($conn, $pay_type);
            $pay_type = trim($pay_type);
            $staff_amount = $_REQUEST['staff_amount'];     
            $staff_amount = mysqli_real_escape_string($conn, $staff_amount);
            $staff_amount = trim($staff_amount);       
            $time = date("Y-m-d H:i:s");
            
            $query1 = "UPDATE staff_pay SET pay_date = '$staff_date', pay_type = '$pay_type', pay_amount = $staff_amount, edit_time ='$time' WHERE id = $id2";
            $run1 = mysqli_query($conn, $query1);
            if($run1){
                $successmessage = "Payroll Total Edited";
            }else{
                $message = "Failed.";
            }
        }
    }

    if(isset($_REQUEST['delete'])){

        $id3 = $_REQUEST['delete'];
        $time = date("Y-m-d H:i:s");
        $query3 = "UPDATE staff_pay SET is_delete = 1, edit_time = '$time' WHERE id = $id3";
        $run2 = mysqli_query($conn, $query3);

        if(mysqli_affected_rows($conn)==1){
            $successmessage = "Payroll Total deleted.";
        }else{
            $message = "Some Error.";
        }
    }
}
?>

<div id="page-wrapper">
<h3>Payroll</h3>

<br>   
<?php 
                    
                    
                    if(strlen($successmessage)>0){

                    echo '<div class="alert alert-success">
                    <strong>'. $successmessage .'</strong></div>';
                } 

             

                if(strlen($message)>0){
                    echo '<div class="alert alert-danger">
                    <strong>'. $message .'</strong>
                  </div>';
                }
                    ?>              
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                <h4>
        <?php 
        
        if(isset($_REQUEST['spe'])){             
            echo "Edit Payroll Total<a style = 'float:right;' class='btn btn-warning' href='payroll.php'>Cancel</a>";        
        }else{
            echo "Add Payroll Total";
        } ?> </h4>
        </div>
              <!--   </div> -->
                                                                  

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

               <?php 
               if(isset($_REQUEST['spe'])){
                echo "<form action = 'payroll.php?edit=$speID2' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
               }else{
                echo '<form action = "payroll.php?insert=" method = "POST" class = "form-group" enctype = "multipart/form-data">';
               }

               ?>
               
<div class="row">
      <div class="form-group col-lg-6">
              <label for="staff_date">Pay Date</label>
      <?php    
       if(isset($_REQUEST['spe'])){
            echo "<input type='date' class='form-control' name='staff_date' id='staff_date' value='$pay_date' required='required' autofocus>";
        }else{
            echo '<input type="date" class="form-control" name="staff_date" id="staff_date" required="required" autofocus>';
        }       
        ?>    
    </div> 
    <div class="form-group col-lg-6">
                <label for="staff_amount">Amount</label>
        <?php    
        if(isset($_REQUEST['spe'])){
            echo "<input type='text' class='form-control' name='staff_amount' id='staff_amount' value='$pay_amount' required='required'>";
        }else{
            echo '<input type="text" class="form-control" name="staff_amount" id="staff_amount" required="required">';
        }  
        ?>    
    </div> 
</div>
<div class = "row">
     
<div class="form-group col-lg-6">
        <?php    
        if(isset($_REQUEST['spe'])){
            ?>
            &nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" <?php if($type == 'vendor'){echo "checked";} ?> required value="vendor"> <b>Vendor Payroll</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="radio" name="pay_type" <?php if($type == 'staff'){echo "checked";} ?> required value="staff"> <b>Staff Payroll</b>

            <?php
        }else{
           ?>
            &nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" required value="vendor"> <b>Vendor Payroll</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="radio" name="pay_type" required value="staff"> <b>Staff Payroll</b>

            <?php
        }  
        ?>    
    </div> 
    <div class="form-group col-lg-6">
                
    </div> 
</div>
<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 -->
                            
     <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">
                                    <thead>
                                        <tr>    
                                                <th>
                                                Pay Date
                                                </th>
                                                <th>
                                                Pay Type
                                                </th>
                                                <th>
                                               Amount
                                                </th>
                                            <th>Edit</th>
                                            <th>Delete</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM staff_pay WHERE is_delete IS NULL ORDER BY pay_date DESC";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $id = $row['id'];
                $pay_date = $row['pay_date']; 
                $pay_type = $row['pay_type']; 
                if(!isset($pay_type)){
                    $pay_type = '';
                }
                $pay_type = ucfirst($pay_type);
                $pay_date =strtotime($pay_date);
                $pay_date = date( "m-d-Y",$pay_date);

                $pay_amount = $row['pay_amount']; 
                $pay_amount = number_format($pay_amount, 2, '.', '');  
                $pay_amount = '$'.$pay_amount;
    
                echo '<tr class="odd gradeX">';               
                echo '<td>' .$pay_date. '</td>';
                echo '<td>' .$pay_type. '</td>';
                echo '<td>' .$pay_amount. '</td>';
                
                echo "<td><a href='payroll.php?spe=$id'>Edit</a> </td>";            
                
                echo "<td><a href='payroll.php?delete=$id' class=\"delete22\" data-confirm=\"Are you sure you want to delete this Payroll Total?\">Delete</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";   ?>