<?php
session_start();
include "../../inc/config.php";
$token = get_token();
  $data_req_dosen = [
    'act' => 'GetDosenPengajarKelasKuliah',
      'token' => $token,
      'filter' => "id_kelas_kuliah='".$_POST['id_kelas']."'"
  ];

  $get_dosen = service_request($data_req_dosen);
  $total_pertemuan = 0;
  foreach ($get_dosen->data as $dos) {
  	$total_pertemuan+=$dos->rencana_minggu_pertemuan;
  }
  $data_dosen = $get_dosen;

  $data_req_kelas = [
    'act' => 'GetListKelasKuliah',
      'token' => $token,
      'filter' => "id_kelas_kuliah='".$_POST['id_kelas']."'"
  ];

  $get_kelas = service_request($data_req_kelas);
  //dump($get_kelas);
?>
<div class="alert alert-danger error_data_sks" style="display:none">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <span class="isi_warning_sks"></span>
</div>
<div class="box box-primary">

<form method="post" id="save_sks" action="<?=base_admin();?>modul/rekapitulasi/rekapitulasi_action.php?act=save_sks">
  <div class="box-header with-border">
    <h3 class="box-title"><?=$get_kelas->data[0]->kode_mata_kuliah.' - '.$get_kelas->data[0]->nama_mata_kuliah.' - (Kelas '.$get_kelas->data[0]->nama_kelas_kuliah;?>)</h3>
    <div class="box-tools">
      <button type="submit" class="btn btn-success btn-sm">
        <i class="fa fa-save"></i> Simpan Perhitungan
      </button>
    
    </div>
  </div>
<input type="hidden" name="id_prodi" value="<?=$get_kelas->data[0]->id_prodi;?>">
<input type="hidden" name="id_semester" value="<?=$get_kelas->data[0]->id_semester;?>">
<input type="hidden" name="id_kelas" value="<?=$get_kelas->data[0]->id_kelas_kuliah;?>">
  <div class="box-body">
    <div class="table-responsive">
      <table class="table table-bordered table-hover">
        <thead>
          <tr>
            <th class="text-center">#</th>
            <th class="text-center">NIDN</th>
            <th class="text-center">Dosen</th>
            <th style="width: 150px;">Rencana Pertemuan</th>
            <th style="width: 100px;">Perhitungan sks</th>
          </tr>
        </thead>
        <tbody>
        	<?php
        	if (!empty($get_dosen->data)) {
        		$no=1;
        		
        		foreach ($data_dosen->data as $data) {
        			$new_sks = round(($data->rencana_minggu_pertemuan/$total_pertemuan)*(float)$get_kelas->data[0]->sks,2);
        			?>
          <tr>
            <td class="text-center"><?=$no;?></td>
            <td class="text-left"><?=$data->nidn;?></td>
            <td class="text-left"><?=$data->nama_dosen;?></td>
             <td class="text-center"><input type="number" name="rencana[<?=$data->id_aktivitas_mengajar;?>]" class="form-control" required=""  value="<?=$data->rencana_minggu_pertemuan;?>"></td>
            <td class="text-center"><input type="number" name="sks[<?=$data->id_aktivitas_mengajar;?>]" class="form-control" required=""  value="<?=$new_sks;?>"></td>
          </tr>
        			<?php
        			$no++;
        		}
        	}
        	?>

          <tr>
            <th colspan="3" class="text-right">Total</th>
            <th><input type="text" name="pertemuan_total" class="form-control" style="width: 100px;" value="<?=$total_pertemuan;?>" readonly></th>
            <th><input type="text" name="sks_total" class="form-control" style="width: 100px;" value="<?=$get_kelas->data[0]->sks;?>" readonly></th>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="card-view bg-cyan-1" style="border-radius: 5px; padding: 10px; margin-top: 10px;">
      <strong>Keterangan :</strong>
      <ul>
        <li>Fasilitas hitung sks disediakan untuk membantu perhitungan sks dosen otomatis.</li>
        <li>Rumus Perhitungan = ( Rencana Pertemuan / Jumlah Seluruh Rencana Pertemuan Seluruh Dosen ) x sks Matakuliah.</li>
        <li>Jumlah sks dosen harus sama dengan sks Kelas</li>
      </ul>
    </div>
  </div>
  </form>
</div>
<script type="text/javascript">
	    $(document).ready(function() {
    

    
    $("#save_sks").validate({
        errorClass: "help-block",
        errorElement: "span",
        highlight: function(element, errorClass, validClass) {
            $(element).parents(".form-group").removeClass(
                "has-success").addClass("has-error");
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents(".form-group").removeClass(
                "has-error").addClass("has-success");
        },
        errorPlacement: function(error, element) {
            if (element.hasClass("chzn-select")) {
                var id = element.attr("id");
                error.insertAfter("#" + id + "_chosen");
            } else if (element.attr("accept") == "image/*") {
                element.parent().parent().parent().append(error);
            } else if (element.hasClass("tgl_picker_input")) {
               element.parent().parent().append(error);
            } else if (element.hasClass("file-upload-data")) {
               element.parent().parent().parent().append(error);
            } else if (element.attr("type") == "checkbox") {
                element.parent().parent().append(error);
            } else if (element.attr("type") == "radio") {
                element.parent().parent().append(error);
            } else {
                error.insertAfter(element);
            }
        },
        submitHandler: function(form) {
            $("#loadnya").show();
            $(form).ajaxSubmit({
                url : $(this).attr("action"),
                dataType: "json",
                type : "post",
                error: function(data ) { 
                  $("#loadnya").hide();
                  console.log(data); 
                  $(".isi_warning_sks").html(data.responseText);
                  $(".error_data_sks").focus()
                  $(".error_data_sks").fadeIn();
                },
                success: function(data) {
                  $("#loadnya").hide();
                  if (data.status=='good') {
                  	$(".isi_warning_sks").html('');
                  	$(".error_data_sks").hide();
	                $(".notif_top_up").fadeIn(1000);
	                $('#modal_perhitungan').modal('hide');
	                $(".notif_top_up").fadeOut(1000, function() {
	                    $('#filter_kelas_dosen').trigger('click');
	                });
                  } else {
	                  $(".isi_warning_sks").html(data.error_desc);
	                  $(".error_data_sks").focus()
	                  $(".error_data_sks").fadeIn();
                  }
                  console.log(data.status);

                }

            });
        }
    });
});
</script>