<?php
session_start();
include "../../inc/config.php";
    $token = get_token();
    $url_rest = $db->get_service_url('rest');

  $data_req_kelas = [
    'act' => 'GetDetailMataKuliah',
      'token' => $token,
      'filter' => "id_matkul='".$_POST['id_matkul']."'"
  ];

  $data_edit = service_request($data_req_kelas);
  $data_edit = $data_edit->data[0];

  $tanggal_mulai_efektif = $data_edit->tanggal_mulai_efektif;
  if ($tanggal_mulai_efektif!="") {
    $tanggal_mulai_efektif = new DateTime($data_edit->tanggal_mulai_efektif);
    $tanggal_mulai_efektif = $tanggal_mulai_efektif->format('Y-m-d');
  }

    $tanggal_selesai_efektif = $data_edit->tanggal_selesai_efektif;
  if ($tanggal_selesai_efektif!="") {
    $tanggal_selesai_efektif = new DateTime($data_edit->tanggal_selesai_efektif);
    $tanggal_selesai_efektif = $tanggal_selesai_efektif->format('Y-m-d');
  }
  

?>
  <div class="box-body">
<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>
<form id="update" method="post" action="<?=base_admin();?>modul/manage_matakuliah/manage_matakuliah_action.php?act=up_matakuliah">
                     <div class="row">
              <!-- First Column -->
              <div class="col-xs-6">
                <div class="form-group">
                  <label for="kodeMatkul">Kode Mata Kuliah <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" value="<?=$data_edit->kode_mata_kuliah;?>" name="kode_mata_kuliah" maxlength="20">
                </div>
                <div class="form-group">
                  <label for="prodiPengampu">Program Studi Pengampu <span class="text-danger">*</span></label>
                  <select class="form-control" id="prodiPengampu">
                    <option><?=$data_edit->nama_program_studi;?></option>
                  </select>
                </div>
                <input type="hidden" name="id_prodi" value="<?=$data_edit->id_prodi;?>">
                <div class="form-group">
                  <label for="jenisMatkul">Jenis Mata Kuliah</label>
                  <select name="id_jenis_mata_kuliah" id="id_jenis_mata_kuliah" class="form-control" tabindex="2"  >

 <?php
foreach ($db->fetch_custom("select * from jenis_mk") as $key) {
    if ($data_edit->id_jenis_mata_kuliah==$key->id_jns_mk) {
      echo "<option value='$key->id_jns_mk' selected>".$key->nm_jns_mk."</option>";
    } else {
      echo "<option value='$key->id_jns_mk'>".$key->nm_jns_mk."</option>";
    }
 }

?>
</select>
                </div>
                <div class="form-group">
                  <label for="kelompokMatkul">Kelompok Mata Kuliah</label>
                  <select class="form-control" name="id_kelompok_mata_kuliah" id="id_kelompok_mata_kuliah">
                    <option></option>
                     <?php foreach ($db->fetch_all("kelompok_mk") as $isi) {
                  if ($data_edit->id_kelompok_mata_kuliah==$isi->id_kel_mk) {
                    echo "<option value='$isi->id_kel_mk' selected>$isi->nm_kel_mk</option>";
                  } else {
                  echo "<option value='$isi->id_kel_mk'>$isi->nm_kel_mk</option>";
                    }
               } ?>
              </select>
                 
                </div>
                <div class="form-group">
                  <label for="bobotMatkul">Bobot Mata Kuliah</label>
                  <input type="text" class="form-control" value="<?=$data_edit->sks_mata_kuliah;?>" id="sks_mata_kuliah" maxlength="5" readonly>
                  <small>(sks Tatap Muka + sks Praktikum + sks Praktek Lapangan + sks Simulasi)</small>
                </div>
                <div class="form-group">
                  <label for="bobotPraktikum">Bobot Praktikum</label>
                  <input type="text" class="form-control desimal" name="sks_praktek" maxlength="5" value="<?=$data_edit->sks_praktek;?>">
                </div>
                <div class="form-group">
                  <label for="bobotSimulasi">Bobot Simulasi</label>
                  <input type="text" class="form-control desimal" name="sks_simulasi" maxlength="5" value="<?=$data_edit->sks_simulasi;?>">
                </div>
              </div>
              <!-- Second Column -->
              <div class="col-xs-6">
                <div class="form-group">
                  <label for="namaMatkul">Nama Mata Kuliah <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" name="nama_mata_kuliah" maxlength="200" value="<?=$data_edit->nama_mata_kuliah;?>">
                </div>
                <div class="form-group">
                  <label for="bobotTatapMuka">Bobot Tatap Muka</label>
                  <input type="text" class="form-control desimal" name="sks_tatap_muka" maxlength="5" value="<?=$data_edit->sks_tatap_muka;?>">
                </div>
                <div class="form-group">
                  <label for="bobotPraktekLapangan">Bobot Praktek Lapangan</label>
                  <input type="text" class="form-control desimal" name="sks_praktek_lapangan" maxlength="5" value="<?=$data_edit->sks_praktek_lapangan;?>">
                </div>
                <div class="form-group">
                  <label for="metodePembelajaran">Metode Pembelajaran</label>
                  <input type="text" class="form-control" name="metode_kuliah" id="metode_kuliah" maxlength="50" value="<?=$data_edit->metode_kuliah;?>">
                </div>
                <div class="form-group">
                  <label for="tanggalMulai">Tanggal Mulai Efektif</label>
                  <input type="text" class="form-control" id="tgl1" autocomplete="off" name="tanggal_mulai_efektif" value="<?=$tanggal_mulai_efektif;?>">
                </div>
                <div class="form-group">
                  <label for="tanggalAkhir">Tanggal Akhir Efektif</label>
                  <input type="text" class="form-control" id="tgl2" autocomplete="off" name="tanggal_selesai_efektif" value="<?=$tanggal_selesai_efektif;?>">
                </div>
              </div>
            </div>
            <div class="row text-center">
              <div class="col-12">
                      <input type="hidden" name="id_matkul" value="<?=$data_edit->id_matkul;?>">
                      <div class="form-group" style="float:left;padding-left:15px">
                      
                          <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan</button>
                          <span class="btn btn-default btn-flat" data-dismiss="modal"><i class="fa fa-step-backward"></i> Batal</span>
                      </div><!-- /.form-group -->
              </div>
            </div>
                
         
                    </form>
                </div>
<script type="text/javascript">
(function($)
{$.fn.numberField=function(options)
{if(!options)
{options={}}
var defaultOptions={ints:2,floats:6,separator:"."};options=$.extend(defaultOptions,options);var intNumAllow=options.ints;var floatNumAllow=options.floats;var separator=options.separator;$(this).on('keydown keypress keyup paste input',function()
{while((this.value.split(separator).length-1)>1)
{this.value=this.value.slice(0,-1);if((this.value.split(separator).length-1)<=1)
{return!1}}
var re=new RegExp('[^0-9'+options.separator+']','g');this.value=this.value.replace(re,'');var allowedLength;var iof=this.value.indexOf(separator);if((iof!=-1)&&(this.value.substring(0,iof).length>intNumAllow))
{allowedLength=0}
else if(iof!=-1)
{allowedLength=iof+floatNumAllow+1}
else{allowedLength=intNumAllow}
this.value=this.value.substring(0,allowedLength);return!0});return $(this)}})(jQuery)
        $(document).ready(function() {
      $('input.desimal').numberField(
    {
      ints: 1, // digits count to the left from separator
      floats: 2, // digits count to the right from separator
      separator: "."
  }
  );
$(".batal-edit").on("click",function(){
  $("#modal_edit").modal( 'hide' ).data( 'bs.modal', null );
});

$("#tgl1").datepicker( {
    format: "yyyy-mm-dd",
});
$("#tgl2").datepicker( {
    format: "yyyy-mm-dd",
});
    $("#update").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_kelas_jadwal').modal('hide');
                    $(".notif_top_up").fadeOut(1000, function() {
                    dataTable.draw(false);
                    });
                  } else {
                      $(".isi_warning_sks").html(data.error_desc);
                      $(".error_data_sks").focus()
                      $(".error_data_sks").fadeIn();
                  }
                  console.log(data.status);

                }

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