<!-- Page Heading -->
          <!-- <h1 class="h3 mb-2 text-gray-800">Tables</h1>
          <p class="mb-4">DataTables is a third party plugin that is used to generate the demo table below. For more information about DataTables, please visit the <a target="_blank" href="https://datatables.net">official DataTables documentation</a>.</p> -->
          <!-- DataTales Example -->
          <div class="card shadow mb-4">
            <div class="card-header py-3">
              <h6 class="m-0 font-weight-bold text-primary">Data Evaluasi PKA Triwulan</h6>
            </div>
            <div class="card-body">
              <!-- <a href='javascript:void(0)' onclick="getcontens('page/tambah_pka');" class="btn btn-primary btn-icon-split btn-sm mb-4">
                    <span class="icon text-white-50">
                      <i class="fas fa-plus"></i>
                    </span>
                    <span class="text">Tambah Data PKA</span>
              </a> -->
              <div class="row">
                <div class="form-group  col-md-6">
                      <label for="exampleFormControlSelect1">Periode PKA</label>
                      <select class="form-control" id="periode_thn" name="periode_thn" required>
                      </select>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="table-responsive">
                    <table class="table table-bordered" id="tabel_data_pka" width="100%" cellspacing="0">
                      <thead>
                        <tr>
                          <th>No</th>
                            <th>Periode</th>
                            <th>Unit Misioner</th>
                            <th>Email</th>
                            <th>Jumlah Kegiatan</th>
                            <th>Tanggal Pembuatan</th>
                            <th>Dibuat Oleh</th>
                            <th style="width: 130px;">Status</th>
                            <th>Aksi</th>
                        </tr>
                      </thead>
                      <!-- <tfoot>
                        <tr>
                          <th>Name</th>
                          <th>Position</th>
                          <th>Office</th>
                          <th>Age</th>
                          <th>Start date</th>
                          <th>Salary</th>
                        </tr>
                      </tfoot> -->
                      <tbody>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
<script type="text/javascript">
$(document).ready(function(){
    var base_url = "<?=base_url()?>";
    periode_thn = function(param){
      jQuery.ajax({
        url: base_url+"pka/select_periode_thn",
        dataType:'json',
        success:function(response){
          var data = response;
          var $id = $('#periode_thn');
          $id.empty();
          $id.append('<option value="all">- Semua Periode -</option>');
          for (var i = 0; i < data.length; i++) {
            $id.append('<option value="'+data[i].periode_thn+'">' + data[i].periode_thn + '</option>');
          }        
        }            
      });
    }
    periode_thn();
    $('#periode_thn').change(function(){
      var periode_thn = $('#periode_thn').val(); 
      data_pka(periode_thn.replace('/','-'));
    })
    data_pka = function(periode_thn){
        $('#tabel_produk').hide();
        $.ajax({ 
            url: base_url + 'pka_ev_tri/data_ev_pka_tri_pka/'+periode_thn,
            type: "post",
            // data:{<?php echo $this->security->get_csrf_token_name(); ?>:'<?php echo $this->security->get_csrf_hash(); ?>'},
            // data:{},
            dataType: "json",
            async : 'false',
            success: function(result){
            //   alert(JSON.stringify(result))
              var data = [];
                for ( var i=0 ; i<result.length ; i++ ) {
                  // alert(result[i].kode_pka)
                    var no = i+1;
                    var link_detail = `<a href="javascript:void(0)" onclick="getcontens('page/data_ev_pka_tri','`+result[i].no_pka+`');" class="btn btn-primary btn-icon-split btn-sm" title="Detail">
                                        <span class="icon text-white-50">
                                          <i class="fas fa-eye"></i>
                                        </span>
                                      </a>`;
                    if(result[i].status_ev_tri == 0 || result[i].status_ev_tri == 3){
                      var link_status_ev_tri = `<a href="javascript:void(0)" onclick="ajukan_ev_tri('`+result[i].no_pka+`');" class="btn btn-info btn-icon-split btn-sm" title="Evaluasi">
                                        <span class="icon text-white-50">
                                          <i class="fas fa-file-upload"> Ajukan evaluasi</i>
                                        </span>
                                      </a>`;
                    }else{
                      var link_status_ev_tri ="";
                    }
                    if(result[i].status_ev_tri == 0){
                      var stt = "<span class='text-primary'>Belum diajukan<span>"
                    }else if(result[i].status_ev_tri == 1){
                      var stt = "<span class='text-success'>Approve<span>"
                    }else if(result[i].status_ev_tri == 2){
                      var stt = "<span class='text-warning'>Evaluasi Diajukan<span>"
                    }else{
                      var stt = "<span class='text-danger'>Evaluasi Ditolak<span>"
                    }
                    var d = `<div class="btn-group" role="group" aria-label="Basic example">`;
                    var dd = `</div>`;
                    data.push([no,result[i].periode_thn,result[i].nm_um,result[i].email,result[i].jml+" Kegiatan",result[i].tgl_buat+"<br>"+result[i].waktu_buat,result[i].username,stt,link_detail+" "+link_status_ev_tri+dd]);
                }
                $('#tabel_data_pka').DataTable({
                    //"bJQueryUI"     : true,
                    data                : data,
                    deferRender         : true,
                    processing          : true,
                    ordering            : true,
                    retrieve            : false,
                    paging              : true,
                    deferLoading        : 57,
                    bDestroy            : true,
                    autoWidth           : false,
                    bFilter             : true,
                    iDisplayLength      : 10,
                    responsive: true,
                    language: {
                      searchPlaceholder: 'Cari',
                      sSearch: '',
                      lengthMenu: '_MENU_',
                    },
                });
            },
                error: function(jqXHR, textStatus, errorThrown) {
                  $('#loading').modal('show');
                        Command: toastr["error"](errorThrown, "Error");
                },
                beforeSend: function () {
                  $('#loading').modal('show');
                },
                complete: function () {
                    $('#loading').modal('hide');
                }
        });
    }
    data_pka("all");
    ajukan_ev_tri = function(no_pka){
      bootbox.confirm("Apakah Anda Yakin Akan Mengirim Evaluasi Triwulan?", function(result){ 
        if (result==true) {
          $.ajax({ 
            url: base_url + 'pka_ev_tri/ajukan_ev_tri',
            type: "post",
            data:{no_pka:no_pka,message:result},
            dataType: "json",
            async : 'false',
            success: function(result){
              alert(JSON.stringify(result))
              if (result === true) {
                Command: toastr["success"]("Selamat Anda Berhasil Mengirim Evaluasi", "Berhasil");
                getcontens('page/data_ev_pka_tri_pka');
              }else{
                Command: toastr["info"]("Gagal Input Data", "Info");
              }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                  $('#loading').modal('show');
                    Command: toastr["error"](errorThrown, "Error");
                },
                beforeSend: function () {
                  $('#loading').modal('show');
                },
                complete: function () {
                    $('#loading').modal('hide');
                }
          })
        }
      });
    }
  });
</script>
