<?php
require_once($_SERVER['DOCUMENT_ROOT']."/hrm/config/config.php");
require_once($_SERVER['DOCUMENT_ROOT']."/hrm/base/payroll.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/coms/stat.php");

if(isset($_POST['add'])){
    addJobGroup($_POST['name'],$_POST['department'],$_POST['disp'], $_POST['payType'],$_POST['basic'], $_POST['rate'],$_POST['commission']);
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <link href="logo.png" rel="icon">
  <title>HRM - application</title>
  <link href="/hrm/views/vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="/hrm/views/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <link href="/hrm/views/css/ruang-admin.min.css" rel="stylesheet">
  <link href="/hrm/views/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
</head>

<body id="page-top">
  <div id="wrapper">
    <!-- Sidebar -->
    <?php
      include( $_SERVER['DOCUMENT_ROOT'].'/hrm/views/admin/sidebar.php');
    ?>
    <!-- Sidebar -->
    <div id="content-wrapper" class="d-flex flex-column">
      <div id="content">
        <!-- TopBar -->
        <?php
          include($_SERVER['DOCUMENT_ROOT'].'/hrm/views/admin/topbar.php');
        ?>
        <!-- Topbar -->

        <!-- Container Fluid-->
        <div class="container-fluid" id="container-wrapper">
          <div class="d-sm-flex align-items-center justify-content-between mb-4">
            <h1 class="h3 mb-0 font-weight-bold text-primary">JOB GROUPS</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Home</a></li>
              <li class="breadcrumb-item active" aria-current="page">Dashboard</li>
            </ol>
          </div>

          <?php
            errorsP($errors);
            success($succcess);

          ?>

          <div class="row mb-3">
            <!-- Start Here -->
           

                     <!-- tab 2 -->
            <div  style="display: block;" id="accademic" class="col-lg-12">

                    <!-- Datatables -->
                    <div class="col-lg-12">
                    <div class="card col-xl-12 col-md-12 mb-4 p-3">
                    <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Job Groups</h6>
                    </div>

                    <!-- inputs -->

                    <div class="form-group col-lg-4" style = "align-items:right" id="add">
                        <button type="button" class="btn btn-primary btn-block" name="add"  onclick="add()">+ ADD</button>
                    </div>
                    <div id="form1" style="display:none">
                    <form action="" method="POST" class = "row" >
                    <!-- <div style="display: flex;"> -->
                    <div class="form-group col-lg-3 mb-4">
                        <label>Job Group Name</label>
                        <input type="text" class="form-control" id="exampleInputLastName"
                        placeholder="" name="name">
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>Department</label>
                        <select name="department" class="form-control" id="channel" >

                        <?php
                            $departments = getDepartments();
                            foreach ($departments as $department){
                                print '<option value="'.$department['name'].'">'.$department['name'].'</option>';
                            }

                        ?>
                            
                        </select>
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>Job Group Description</label>
                        <input type="text" class="form-control" id="exampleInputEmail" aria-describedby="emailHelp"
                        placeholder="" name = "disp" >
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>Pay Type</label>

                        <select name="payType" class="form-control" id="type" onchange="switchs()">
                        <option value="basic">Basic</option>
                        <option value="timeSheet">Time Sheet</option>
                        <option value="commission">Commission</option>

                        </select>
                    </div>

                    

                    <div class="form-group col-lg-3 mb-4" id="basic">
                        <label>BASIC</label>
                        <input type="text" class="form-control" id="basicAmt"
                        placeholder="" name="basic" oninput="calc()">
                    </div>

                    <div class="form-group col-lg-3 mb-4" style="display:none" id="rate">
                        <label>RATE PER HOUR</label>
                        <input type="text" class="form-control" id="rateAmt"
                        placeholder="" name="rate" oninput="calc1()">
                    </div>

                    <div class="form-group col-lg-3 mb-4" style="display:none" id="hours">
                        <label>TOTAL HOURS</label>
                        <input type="text" class="form-control" id="hourVal"
                        placeholder="" name="hours" oninput="calc1()">
                    </div>

                    <div class="form-group col-lg-3 mb-4" style="display:none" id="comm">
                        <label>COMMISSION %</label>
                        <input type="text" class="form-control" id="commAmt"
                        placeholder="" name="commission" oninput="calc2()">
                    </div>

                    <div class="form-group col-lg-3 mb-4" style="display:none" id="sales">
                        <label>TOTAL SALES</label>
                        <input type="text" class="form-control" id="saleAmt"
                        placeholder="" name="sales" oninput="calc2()">
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>PAYEE</label>
                        <input type="text" class="form-control" id="payee"
                        placeholder="" name="payee" readonly>
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>NHIF</label>
                        <input type="text" class="form-control" id="nhif"
                        placeholder="" name="nhif" readonly>
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>NSSF</label>
                        <input type="text" class="form-control" id="nssf"
                        placeholder="" name="nssf" readonly>
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>NITA</label>
                        <input type="text" class="form-control" id="nita"
                        placeholder="" name="nita" readonly>
                    </div>

                    <div class="form-group col-lg-3 mb-4">
                        <label>NET</label>
                        <input type="text" class="form-control" id="net"
                        placeholder="" name="net" >
                    </div>

                

                    <div class="form-group col-lg-4" >
                        <button type="submit" class="btn btn-primary btn-block" name="add" style="margin-top: 25px">SAVE</button>
                    </div>
                    
                    

                    <hr> 
                    </form>
                    </div>
          


                     <!-- inputs -->
                <div class="table-responsive p-3">
                  <table class="table align-items-center table-flush" id="dataTable">
                    <thead class="thead-dark tfoot-dark">
                      <tr>
                        <th>#</th>
                        <th> JOB GROUP NAME</th>
                        <th>DESCRIPTION</th>
                        <th>DEPARTMENT</th>
                        <th>PAY TYPE</th>
                        <th>BASIC</th>
                        <th>RATE</th>
                        <th>COMMISSION</th>
                        <th>EDIT</th>
                        
                      </tr>
                    </thead>
                    <tfoot>
                      <tr>
                      <th>#</th>
                      <th> JOB GROUP NAME</th>
                        <th>DESCRIPTION</th>
                        <th>DEPARTMENT</th>
                        <th>PAY TYPE</th>
                        <th>BASIC</th>
                        <th>RATE</th>
                        <th>COMMISSION</th>
                        <th>EDIT</th>
                        
                      </tr>
                    </tfoot>
                    <tbody>
                         <?php
                            $positions = getJobGroup();
                              foreach($positions as $row){
                                                      
                                print " <tr> ";
                                print "<td>" . $row['id'] . "</td>";
                                print "<td>" . $row['name']. "</td>";
                                print "<td>" . $row['description']. "</td>";
                                print "<td>" . $row['department']. "</td>";
                                print "<td>" . $row['payType']. "</td>";
                                print "<td>" . $row['basic']. "</td>";
                                print "<td>" . $row['rate']. "</td>";
                                print "<td>" . $row['commision']. "</td>";
                                print("<td>");
                                print('<a class="btn btn-danger" href="/hrm/views/candidate/editAcc.php?accid='.$row['id'].'"><i class="fa fa-edit"></i></a>');
                                print("</td>");
                                
                            }
                        ?>

                     
                    </tbody>
                  </table>
                </div>

                </div>
              </div>
            

          <!-- end Here -->

          <!-- <div class="row">
            <div class="col-lg-12 text-center">
              <p>Do you like this template ? you can download from <a href="https://github.com/indrijunanda/RuangAdmin"
                  class="btn btn-primary btn-sm" target="_blank"><i class="fab fa-fw fa-github"></i>&nbsp;GitHub</a></p>
            </div>
          </div> -->

          <!-- Modal Logout -->
          <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelLogout"
            aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabelLogout">Ohh No!</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                  <p>Are you sure you want to logout?</p>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-outline-primary" data-dismiss="modal">Cancel</button>
                  <a href="login.html" class="btn btn-primary">Logout</a>
                </div>
              </div>
            </div>
          </div>

        </div>
        <!---Container Fluid-->
      </div>
      <!-- Footer -->
      <footer class="sticky-footer bg-white">
        <div class="container my-auto">
          <div class="copyright text-center my-auto">
            <span>copyright &copy; <script> document.write(new Date().getFullYear()); </script> - developed by
              <b><a href="https://wintec.co.ke" target="_blank">wintec</a></b>
            </span>
          </div>
        </div>
      </footer>
      <!-- Footer -->
    </div>
  </div>

  <!-- Scroll to top -->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <script src="/hrm/views/vendor/jquery/jquery.min.js"></script>
  <script src="/hrm/views/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="/hrm/views/vendor/jquery-easing/jquery.easing.min.js"></script>
  <script src="/hrm/views/js/ruang-admin.min.js"></script>
  <script src="/hrm/views/vendor/chart.js/Chart.min.js"></script>
  <script src="/hrm/views/js/demo/chart-area-demo.js"></script>  
  <script type="text/javascript" src="/hrm/views/ckeditor/ckeditor.js"></script>
  <script src="/hrm/views/vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="/hrm/views/vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <script>
    $(document).ready(function () {
      $('#dataTable').DataTable(); // ID From dataTable 
      $('#dataTableHover').DataTable(); // ID From dataTable with Hover
    });

    function sSustomer(){
      var target = document.getElementById('target').value;
      if( target == "specify"){
        document.getElementById("customer").style.display = "block";
        //alert("nimefikiwa");
      }else{
        document.getElementById("customer").style.display = "none";
      }
    }

    function add(){
      document.getElementById("form1").style.display = "block";
      document.getElementById("add").style.display = "none";

    }

    function tab(){
      document.getElementById("create").style.display = "none";
      document.getElementById("publish").style.display = "";
    }

    function tab1(){
      document.getElementById("create").style.display = "";
      document.getElementById("publish").style.display = "none";
    }


    function calc(){
        var basic = document.getElementById("basicAmt").value;
        var tax =0;
        var amount = 0;
        var net = basic;
        var nhif = 0;
        var nssf = 200;
        
        if (basic >= 24000){
          amount = basic -24000;
          
          tax = 0.10*24000; 
          
          if(amount>=8333){
              amount = amount - 8333;
              tax = tax + (0.25*8333);
              if(amount>0){
                  tax = tax + (0.30*amount);
                  net= basic - (tax-2400);
                  tax = tax - 2400;
                 
              }
          }else{
            tax = tax + (0.25*amount);
            net= basic - (tax-2400);
            tax = tax - 2400;
          }
        }

        
        if(basic>0 && basic<6000){
            nhif = 150;
        }
        if(basic>=6000 && basic<8000){
            nhif = 300;
        }
        if(basic>=8000 && basic<12000){
            nhif = 400;
        }
        if(basic>=12000 && basic<15000){
            nhif = 500;
        }
        if(basic>=15000 && basic<20000){
            nhif = 600;
        }
        if(basic>=20000 && basic<25000){
            nhif = 750;
        }
        if(basic>=25000 && basic<30000){
            nhif = 850;
        }
        if(basic>=30000 && basic<35000){
            nhif = 900;
        }
        if(basic>=35000 && basic<40000){
            nhif = 950;
        }
        if(basic>=40000 && basic<45000){
            nhif = 1000;
        }
        if(basic>=45000 && basic<50000){
            nhif = 1100;
        }
        if(basic>=50000 && basic<60000){
            nhif = 1200;
        }
        if(basic>=60000 && basic<70000){
            nhif = 1300;
        }
        if(basic>=70000 && basic<80000){
            nhif = 1400;
        }
        if(basic>=80000 && basic<90000){
            nhif = 1500;
        }
        if(basic>=90000 && basic<100000){
            nhif = 1600;
        }
        if(basic>=100000){
            nhif = 1700;
        }
        net = net - nhif;
        net = net - nssf;

        //  alert(net);
        document.getElementById("payee").value = tax;
        document.getElementById("net").value = net;
        document.getElementById("nhif").value = nhif;
        document.getElementById("nssf").value = nssf;


       
    }


    function calc1(){
        var rate= document.getElementById("rateAmt").value;
        var hours = 1;
        hours = document.getElementById("hourVal").value;
        var basic = rate*hours;
        var tax =0;
        var amount = 0;
        var net = basic;
        var nhif = 0;
        var nssf = 200;
        
        if (basic >= 24000){
          amount = basic -24000;
          
          tax = 0.10*24000; 
          
          if(amount>=8333){
              amount = amount - 8333;
              tax = tax + (0.25*8333);
              if(amount>0){
                  tax = tax + (0.30*amount);
                  net= basic - (tax-2400);
                  tax = tax - 2400;
                 
              }
          }else{
            tax = tax + (0.25*amount);
            net= basic - (tax-2400);
            tax = tax - 2400;
          }
        }

        
        if(basic>0 && basic<6000){
            nhif = 150;
        }
        if(basic>=6000 && basic<8000){
            nhif = 300;
        }
        if(basic>=8000 && basic<12000){
            nhif = 400;
        }
        if(basic>=12000 && basic<15000){
            nhif = 500;
        }
        if(basic>=15000 && basic<20000){
            nhif = 600;
        }
        if(basic>=20000 && basic<25000){
            nhif = 750;
        }
        if(basic>=25000 && basic<30000){
            nhif = 850;
        }
        if(basic>=30000 && basic<35000){
            nhif = 900;
        }
        if(basic>=35000 && basic<40000){
            nhif = 950;
        }
        if(basic>=40000 && basic<45000){
            nhif = 1000;
        }
        if(basic>=45000 && basic<50000){
            nhif = 1100;
        }
        if(basic>=50000 && basic<60000){
            nhif = 1200;
        }
        if(basic>=60000 && basic<70000){
            nhif = 1300;
        }
        if(basic>=70000 && basic<80000){
            nhif = 1400;
        }
        if(basic>=80000 && basic<90000){
            nhif = 1500;
        }
        if(basic>=90000 && basic<100000){
            nhif = 1600;
        }
        if(basic>=100000){
            nhif = 1700;
        }
        net = net - nhif;
        net = net - nssf;

        //  alert(net);
        document.getElementById("payee").value = tax;
        document.getElementById("net").value = net;
        document.getElementById("nhif").value = nhif;
        document.getElementById("nssf").value = nssf;


       
    }

    function calc2(){
        var commission= document.getElementById("commAmt").value/100;
        var sales = document.getElementById("saleAmt").value;
        var basic = commission*sales;
        var tax =0;
        var amount = 0;
        var net = basic;
        var nhif = 0;
        var nssf = 200;
        
        if (basic >= 24000){
          amount = basic -24000;
          
          tax = 0.10*24000; 
          
          if(amount>=8333){
              amount = amount - 8333;
              tax = tax + (0.25*8333);
              if(amount>0){
                  tax = tax + (0.30*amount);
                  net= basic - (tax-2400);
                  tax = tax - 2400;
                 
              }
          }else{
            tax = tax + (0.25*amount);
            net= basic - (tax-2400);
            tax = tax - 2400;
          }
        }

        
        if(basic>0 && basic<6000){
            nhif = 150;
        }
        if(basic>=6000 && basic<8000){
            nhif = 300;
        }
        if(basic>=8000 && basic<12000){
            nhif = 400;
        }
        if(basic>=12000 && basic<15000){
            nhif = 500;
        }
        if(basic>=15000 && basic<20000){
            nhif = 600;
        }
        if(basic>=20000 && basic<25000){
            nhif = 750;
        }
        if(basic>=25000 && basic<30000){
            nhif = 850;
        }
        if(basic>=30000 && basic<35000){
            nhif = 900;
        }
        if(basic>=35000 && basic<40000){
            nhif = 950;
        }
        if(basic>=40000 && basic<45000){
            nhif = 1000;
        }
        if(basic>=45000 && basic<50000){
            nhif = 1100;
        }
        if(basic>=50000 && basic<60000){
            nhif = 1200;
        }
        if(basic>=60000 && basic<70000){
            nhif = 1300;
        }
        if(basic>=70000 && basic<80000){
            nhif = 1400;
        }
        if(basic>=80000 && basic<90000){
            nhif = 1500;
        }
        if(basic>=90000 && basic<100000){
            nhif = 1600;
        }
        if(basic>=100000){
            nhif = 1700;
        }
        net = net - nhif;
        net = net - nssf;

        //  alert(net);
        document.getElementById("payee").value = tax;
        document.getElementById("net").value = net;
        document.getElementById("nhif").value = nhif;
        document.getElementById("nssf").value = nssf;


       
    }


    function switchs(){
       var type =document.getElementById("type").value ;
       if(type=="basic"){
        document.getElementById("basic").style.display = "block";
        document.getElementById("rate").style.display = "none";
        document.getElementById("comm").style.display = "none";
        document.getElementById("hours").style.display ="none";
        document.getElementById("sales").style.display ="none";
       }
       if(type=="timeSheet"){
           document.getElementById("basic").style.display = "none";
           document.getElementById("rate").style.display = "block";
           document.getElementById("comm").style.display = "none";
           document.getElementById("hours").style.display ="block";
           document.getElementById("sales").style.display ="none";
       }
       if(type=="commission"){
        document.getElementById("rate").style.display = "none";
        document.getElementById("basic").style.display ="none";
        document.getElementById("comm").style.display = "block";
        document.getElementById("hours").style.display ="none";
        document.getElementById("sales").style.display ="block";
           
       }
    }

    CKEDITOR.replace( 'superA' );
    CKEDITOR.replace( 'superB' );
    CKEDITOR.replace( 'superC' );

    

    
  </script>
</body>

</html>