
<?= $this->extend('Page_layout') ?>
<?= $this->section('content') ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Employee Data</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
            
              <li class="breadcrumb-item active">Master Staff</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card card-primary">
        <div class="card-header">
          <h3 class="card-title">Staff Data</h3>

          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>
        <div class="card-body">
         
             <div class="col-12" style="overflow-x:scroll">
             <table id="table-data" style="width:1800px;" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th>Action</th>
                    <th>No</th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>NIK</th>
                    <th>Join Date</th>
                    <th>Termination Date</th>
                    <th>Phone number</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Birthdate</th>
                    <th>Bank Name</th>
                    <th>Account No</th>
                    
                  </tr>
                  </thead>
                  <tbody>
                  
                </table>
                 </div>
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
           <button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#form-add">Add new</button>
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  </div>
  <?php include('Staff_add.php');?>
  <?php include('Staff_edit.php');?>


   <?= $this->endSection() ?> 

  <?=$this->section("scripts")?>
<script src="<?=base_url('public/plugins/jquery-ui/jquery-ui.js')?>"></script>  
<script src="<?=base_url('public/plugins/datatables/jquery.dataTables.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-responsive/js/dataTables.responsive.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-responsive/js/responsive.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/dataTables.buttons.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/jszip/jszip.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/pdfmake.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/vfs_fonts.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.html5.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.print.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.colVis.min.js')?>"></script>
<script src="<?=base_url('public/plugins/moment/moment.min.js')?>"></script>

<script>
  $(function () {
   

  $( "#contract_start_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});
  $( "#contract_end_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});
  $( "#birthdate" ).datepicker({ dateFormat: 'dd/mm/yy'});
  });


  function loaduser(param){
  
    
      if(param == "new")
      {
        var ddl_username = $("#username");
        var $data = {
               'KONDISI' : ' ( USER_LEVEL LIKE "STAFF_%" or  USER_LEVEL = "CEO" ) and USER_NAME not in (select USER_NAME from ref_staff )'
            }  
      }else
      {
        var ddl_username = $("#eusername");  
        var $data = {
               'KONDISI' : '  ( USER_LEVEL LIKE "STAFF_%" or  USER_LEVEL = "CEO" ) '
            }
      }
        
     $.ajax({
            type: "POST",
            url: "<?= site_url('user/fetch/where')?>",
            data: $data,
        
            success: function (response) {
             ddl_username.empty().append('<option selected="selected" value="">Please select</option>');
          
             $.each(response.data, function (key, value) { 
               ddl_username.append($("<option></option>").val(value['USER_NAME']).html(value['USER_NAME']));
               
             });
             
            }
          });

       
  }
  

  function loadstaff(){
   
     $.ajax({
            type: "GET",
            url: "<?= site_url('staf/fetch')?>",
        
            success: function (response) {
            //  console.log(response.data);
             $.each(response.data, function (key, value) { 

            var $TERMINATION_DT;

            if(value['TERMINATION_DT'] != "0000-00-00 00:00:00"){
              $TERMINATION_DT = moment(value['TERMINATION_DT']).format("DD/MM/YYYY");
            }else{
              $TERMINATION_DT = "";
            }
             
              var body = "<tr>";
              body += "<td><div class=\"btn-group\"><a  class=\"btn btn-info btn-sm edit-btn\">Edit</a>";
              body += "</td>";
              body += "<td class=\"data-id\">"+value['STAFF_ID']+"</td>";
              body += "<td >"+value['XNAME']+"</td>";
              body += "<td>"+value['POSITION']+"</td>";
              body += "<td>"+value['NIK']+"</td>";
              body += "<td>"+moment(value['JOIN_DT']).format("DD/MM/YYYY")+"</td>";
              body += "<td>"+$TERMINATION_DT+"</td>";
              body += "<td>"+value['PHONE_NUMBER']+"</td>";
              body += "<td>"+value['EMAIL']+"</td>";
              body += "<td>"+value['ADDRESS']+"</td>";
              body += "<td>"+moment(value['BIRTHDATE']).format("DD/MM/YYYY")+"</td>";  
              body += "<td>"+value['BANK_NAME']+"</td>";           
              body += "<td>"+value['ACCOUNT_NUMBER']+"</td>";
            

              
              body += "</tr>";
              $("#table-data tbody").append(body);
               
             });
              $("#table-data").DataTable();
            }
          });

       
  }

  $(document).ready(function () {

    loaduser("new");
    loadstaff();
    $(document).on('click','.save-data', function () {
       if( $("#username").val() == "" || $("#name").val() == "" || $("#position").val() == "" || $("#nik").val() == "" || $("#contract_start_dt").val() == ""  || $("#phone").val() == "" || $("#email").val() == "" || $("#address").val() == "-" || $("#bankname").val() == "" || $("#accountnum").val() == "" || $("#birthdate").val() == "")
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
          return false;
       }
       else
       { // jika sudah di isi semua.
         
          var $data = {
            'XNAME' : $("#name").val(),
            'POSITION' : $("#position").val(),
            'NIK' : $("#nik").val(),
            'JOIN_DT' : $("#contract_start_dt").val(),
            'TERMINATION_DT' : $("#contract_end_dt").val(),
            'PHONE_NUMBER' : $("#phone").val(),
            'EMAIL' : $("#email").val(),
            'ADDRESS' : $("#address").val(),
            'BANK_NAME' : $("#bankname").val(),
            'ACCOUNT_NUMBER' : $("#accountnum").val(),
            'BIRTHDATE' : $("#birthdate").val(),
            'USER_NAME' : $("#username").children("option").filter(":selected").text()
          
           
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('staf/simpan')?>",
            data: $data,
           
            success: function (response) {
              $('#form-add').modal('hide');
              $('#form-add').find('input').val('');
              loadstaff();
           
           

               $("#table-data tbody").html("");
               
              toastr.success(response.status);
               loaduser("new");
           
            }
          });
         

       }
    });

    $(document).on('click','.edit-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
      loaduser("edit");
     
    $.ajax({
      type: "POST",
      url: "<?= site_url('staf/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
    
       $.each(response, function (key, value) { 

            var $TERMINATION_DT;

            if(value['TERMINATION_DT'] == ""){
              $TERMINATION_DT = moment(value['TERMINATION_DT']).format("DD/MM/YYYY");
            }
       
            $('#form-edit').find('input').val('');
            $("#ename").val(value['XNAME']),
            $("#eposition").val(value['POSITION']),
            $("#enik").val(value['NIK']),
            $("#econtract_start_dt").val(moment(value['JOIN_DT']).format("DD/MM/YYYY")),
            $("#econtract_end_dt").val($TERMINATION_DT),
            $("#ephone").val(value['PHONE_NUMBER']),
            $("#eemail").val(value['EMAIL']),
            $("#eaddress").val(value['ADDRESS']),
            $("#ebankname").val(value['BANK_NAME']),
            $("#eaccountnum").val(value['ACCOUNT_NUMBER']),
            $("#ebirthdate").val(moment(value['BIRTHDATE']).format("DD/MM/YYYY")),
            $("#eusername").val(value['USER_NAME']).change(),
             $("#estaff_id").val(id),
           $("#form-edit").modal("show");
           
          
       });
      }
    });
  });
  });
// confirm Update data
  $(document).on('click','.edit-data', function () {
        if( $("#eusername").val() == "" || $("#ename").val() == "" || $("#eposition").val() == "" || $("#enik").val() == "" || $("#econtract_start_dt").val() == ""  || $("#ephone").val() == "" || $("#eemail").val() == "" || $("#eaddress").val() == "-" || $("#ebankname").val() == "" || $("#eaccountnum").val() == "" || $("#ebirthdate").val() == "")
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
          return false;
       }
       else
       { // jika sudah di isi semua.

        alert($("#econtract_end_dt").val());

        var $data = {
            'XNAME' : $("#ename").val(),
            'POSITION' : $("#eposition").val(),
            'NIK' : $("#enik").val(),
            'JOIN_DT' : $("#econtract_start_dt").val(),
            'TERMINATION_DT' : $("#econtract_end_dt").val(),
            'PHONE_NUMBER' : $("#ephone").val(),
            'EMAIL' : $("#eemail").val(),
            'ADDRESS' : $("#eaddress").val(),
            'BANK_NAME' : $("#ebankname").val(),
            'ACCOUNT_NUMBER' : $("#eaccountnum").val(),
            'BIRTHDATE' : $("#ebirthdate").val(),
            'USER_NAME' : $("#eusername").children("option").filter(":selected").text(),
            'STAFF_ID' : $("#estaff_id").val(),
           
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('staf/update')?>",
            data: $data,
           
            success: function (response) {
              $('#form-edit').modal('hide');
              $('#form-edit').find('input').val('');
              
              loadstaff();
           

               $("#table-data tbody").html("");
               
              toastr.success(response.status);
              
           
            }
          });
         

       }
    });


</script>

<?=$this->endSection()?>
