<?php
session_start();
include "../../inc/config.php";
$id_mhs = "";
$id_reg = "";
$id_mhs_reg = explode(",", $_POST['data_ids']);
    foreach ($id_mhs_reg as $reg) {
      $exp = explode("#", $reg);
      $id_mhs = $exp[0];
      $id_reg = $exp[1];
    }

if ($_POST['aksi']=='edit_history') {
  $table = 'GetListRiwayatPendidikanMahasiswa';
  $filter = "id_registrasi_mahasiswa='$id_reg'";
  if ($id_reg=="") {
    echo "<h2>Maaf, Mahasiswa ini tidak punya history Pendidikan</h2>";
    exit();
  }
} else {
  $table = 'GetBiodataMahasiswa';
  $filter = "id_mahasiswa='$id_mhs'";
}
$token = get_token();
  $get_mhs = [
    'act' => $table,
      'token' => $token,
      'filter' => $filter
  ];

  $data_mhs = service_request($get_mhs);
  $mhs = $data_mhs->data[0];

//check if periode is aktif
$semester_lokal = $db->fetch_custom("select * from semester");
foreach ($semester_lokal as $sem) {
  $array_semester[$sem->semester] = $sem->nama_semester;
}
$array_periode_tipe1 = array();
  $get_periode = [
    'act' => 'GetPeriode',
      'token' => $token,
      'filter' => "id_prodi='".$mhs->id_prodi."' and tipe_periode='1'"
  ];
$periode_current = service_request($get_periode);
$get_periode_lampau = [
  'act' => 'GetPeriodeLampau',
    'token' => $token,
    'filter' => "id_program_studi='".$mhs->id_prodi."'and tipe_periode='1'"
];
$periode_lampau = service_request($get_periode_lampau);

if (!empty($periode_current->data)) {
  foreach ($periode_current->data as $current) {
      $array_periode_tipe1[$current->periode_pelaporan] = $current->periode_pelaporan;
  }
}
if (!empty($periode_lampau->data)) {
  foreach ($periode_lampau->data as $lampau) {
      $array_periode_tipe1[$lampau->id_semester] = $lampau->id_semester;
  }
}

$tgl_masuk = $mhs->tanggal_daftar;
$tanggal = substr($tgl_masuk, 0,2);

$bulan = substr($tgl_masuk, 3,2);

$tahun = substr($tgl_masuk, 6,4);

$tanggal_masuk_kuliah = "$tahun-$bulan-$tanggal";


$readonly = "readonly";
if (in_array($mhs->id_periode_masuk, $array_periode_tipe1)) {
  $readonly = "";
}

?>

<div class="alert alert-success alert-dismissible sukses-alert" style="display: none">
<h4><i class="icon fa fa-check"></i> Berhasil!</h4>
Data History Pendidikan berhasil di Update.
</div>
<?php
if ($readonly=="") {
  ?>
<div class="alert alert-danger error_data_history" style="display:none">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <span class="isi_warning_history"></span>
</div>
  <?php
} else {
  ?>
<div class="alert alert-danger error_data_history">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <span class="isi_warning_history">Maaf, tidak bisa Edit History Karena Periode Bukan Tipe 1</span>
</div>
  <?php
}
?>


<form method="post" id="save_history" action="<?=base_admin();?>modul/hapus_mahasiswa/hapus.php?act=edit_action">
  <div class="row">
    <div class="col-md-6">
      <input type="hidden" name="id_registrasi_mahasiswa" value="<?=$mhs->id_registrasi_mahasiswa;?>">
      <input type="hidden" name="id_mahasiswa" value="<?=$mhs->id_mahasiswa;?>">
      <input type="hidden" name="id_prodi" value="<?=$mhs->id_prodi;?>">
            <div class="form-group">
              <label for="nim" class="text-weight-bolder">NIM <span class="text-danger">*</span></label>
              <input type="text" value="<?=$mhs->nim;?>" class="form-control" name="nim">
            </div>
            <div class="form-group">
              <label for="jalur_pendaftaran" class="text-weight-bolder">Jalur Pendaftaran <span class="text-danger">*</span></label>
              <select name="id_jalur_daftar" data-placeholder="Pilih Jalur Pendaftaran..." class="form-control chzn-select" tabindex="2" >
               <option value=""></option>
               <?php foreach ($db->fetch_all("jalur_masuk") as $isi) {

                  if ($mhs->id_jalur_daftar==$isi->id) {
                    echo "<option value='$isi->id' selected>$isi->jalur</option>";
                  } else {
                  echo "<option value='$isi->id'>$isi->jalur</option>";
                    }
               } ?>
              </select>
            </div>
            <div class="form-group">
              <label for="tanggal_masuk" class="text-weight-bolder">Tanggal Masuk<span class="text-danger">*</span></label>
          <div class="input-group date tgl_picker_history" data-target="tgl_mulai">
              <input type="text" class="form-control tgl_picker_input" name="tgl_mulai1" readonly  required value="<?=tgl_indo($tanggal_masuk_kuliah);?>">
              <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
              
              </div>
              <input type="hidden" name="tgl_mulai" value="<?=$tanggal_masuk_kuliah;?>">
            </div>
            <div class="form-group">
              <label for="biaya_masuk" class="text-weight-bolder">Biaya Masuk <span class="text-danger">*</span></label>
              <input type="number" value="<?=$mhs->biaya_masuk;?>" class="form-control" name="biaya_masuk">
            </div>

            <div class="form-group">
              <label for="fakultas_program_studi" class="text-weight-bolder">Fakultas / Program Studi <span class="text-danger">*</span></label>
              <select class="form-control chzn-select" name="fakultas_program_studi">
               <?php 
                 $jur = $db->fetch_custom("select * from jurusan");
               foreach ($jur as $isi) {
                  if ($mhs->id_prodi==$isi->id_sms) {
                    echo "<option value='$isi->id_sms' selected>$isi->jenjang $isi->nama_jurusan</option>";
                  } else {
                    echo "<option value='$isi->id_sms'>$isi->jenjang $isi->nama_jurusan</option>";
                  }
               } ?>
              </select>
            </div>
    </div>
    <div class="col-md-6">
            <div class="form-group">
              <label for="jenis_pendaftaran" class="text-weight-bolder">Jenis Pendaftaran <span class="text-danger">*</span></label>
              <select class="form-control chzn-select" name="id_jenis_daftar" id="id_jns_daftar_edit">
               <option value=""></option>
               <?php foreach ($db->fetch_custom("select * from jenis_pendaftaran") as $isi) {
                if ($mhs->id_jenis_daftar==$isi->id_jns_daftar) {
                  echo "<option value='$isi->id_jns_daftar' selected>$isi->nm_jns_daftar</option>";
                } else {
                  echo "<option value='$isi->id_jns_daftar'>$isi->nm_jns_daftar</option>";
                }
               } ?>
              </select>
            </div>
            <div class="form-group">
              <label for="periode_pendaftaran" class="text-weight-bolder">Periode Pendaftaran <span class="text-danger">*</span></label>
              <select class="form-control chzn-select" name="id_periode_masuk">
               <?php
               foreach ($array_periode_tipe1 as $mulai_smt => $val_mulai) {
                 if ($mhs->id_periode_masuk==$val_mulai) {
                    echo "<option value='$val_mulai' selected>".$array_semester[$val_mulai]."</option>";
                 } else {
                  echo "<option value='$val_mulai'>".$array_semester[$val_mulai]."</option>";
                 }
               }
               ?>
             </select>
            </div>
            <div class="form-group">
              <label for="pembiayaan_awal" class="text-weight-bolder">Pembiayaan Awal <span class="text-danger">*</span></label>
              <select class="form-control chzn-select" name="pembiayaan_awal">
              <option value=""></option>
               <?php foreach ($db->fetch_all("pembiayaan") as $isi) {
                  if ($mhs->id_pembiayaan==$isi->id_pembiayaan) {
                    echo "<option value='$isi->id_pembiayaan' selected>$isi->nm_pembiayaan</option>";
                  } else {
                  echo "<option value='$isi->id_pembiayaan'>$isi->nm_pembiayaan</option>";
                    }
               } ?>
              </select>
            </div>
<?php
if ($mhs->id_jenis_daftar!='1') {
  $show = "";
} else {
  $show = "style='display:none'";
}
?>
            <div class="form-group" id="show_asal_pt" <?=$show?>>
              <label for="fakultas_program_studi" class="text-weight-bolder">Asal Perguruan Tinggi<span class="text-danger">*</span></label>
               <select name="id_perguruan_tinggi_asal" id="kode_pt_asal" data-placeholder="Pilih Asal Perguruan Tinggi ..." class="form-control" tabindex="2" >
                <?php
                if ($mhs->id_jalur_daftar!='1' && $mhs->id_perguruan_tinggi_asal!="") {
                  $kampus = $db->fetch_single_row("satuan_pendidikan","id_sp",$mhs->id_perguruan_tinggi_asal);
                   echo "<option value='$kampus->id_sp'>$kampus->nm_lemb</option>";
                }
                ?>
              </select>
            </div>
            <div class="form-group" id="show_asal_prodi" <?=$show?>>
              <label for="fakultas_program_studi" class="text-weight-bolder">Asal Program Studi<span class="text-danger">*</span></label>
              <select class="form-control chzn-select" name="id_prodi_asal" id="kode_prodi_asal">
   <option value=""></option>
                          <?php
                          $npsn = "";
                          if ($mhs->id_jalur_daftar!='1' && $mhs->id_prodi_asal!="") {
                            $prodis = $db->fetch_custom("SELECT concat(nm_jenj_didik,' ',nm_lemb) as nama_jurusan,kode_prodi,id_sms from jenjang_pendidikan INNER join sms USING(id_jenj_didik) where id_sp=?",array('id_sp' => $mhs->id_perguruan_tinggi_asal));
                            foreach ($prodis as $prodi) {
                              if ($mhs->id_prodi_asal==$prodi->id_sms) {
                                 echo "<option value='$prodi->id_sms' selected>$prodi->nama_jurusan</option>";
                              } else {
                                echo "<option value='$prodi->id_sms'>$prodi->nama_jurusan</option>";
                              }
                          }
                        }
                          ?>
              </select>
            </div>
            <input type="hidden" name="id_perguruan_tinggi" value="<?=$mhs->id_perguruan_tinggi;?>">
         
      </div>
  </div>
<div class="box box-primary">
    <div class="box-header with-border" style="height: 50px">
          <div class="box-tools">
  <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i> Close</button>
  <?php
  if ($readonly=="") {
    ?>


      <button type="submit" class="btn btn-success btn-sm">
        <i class="fa fa-save"></i> Simpan Data
      </button>
    

   <?php 
  }
  ?>
    </div>
  </div>

</div>
 </form>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/locales/bootstrap-datepicker.id.min.js" charset="UTF-8"></script>
<script type="text/javascript">
	    $(document).ready(function() {
    //var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
    $(".tgl_picker_history").datepicker({
        format: "dd MM yyyy",
        autoclose: true,
        language: "id",
        locale: "id",
        //container: container,
        locale: "id",
        todayHighlight: true,
        forceParse : false
        }).on("change",function(){
          var val = $(this).datepicker('getDate');
          var formatted = moment(val).format('YYYY-MM-DD');
          var target = $(this).data('target');
          //$(`[name="${target}"]`).val(formatted);
          $("input[name='"+target+"']").val(formatted);
          console.log(target);
          $(":input",this).valid();
    });
      $( "#kode_pt_asal" ).select2({
    ajax: {
      url: '<?=base_admin();?>modul/hapus_mahasiswa/get_kampus.php',
      dataType: 'json'
      // Additional AJAX parameters go here; see the end of this chapter for the full code of this example
    },
    formatInputTooShort: "Cari Nama Kampus",
    width: "100%",
  });
$("#kode_pt_asal").change(function(){
      $.ajax({
      type : "post",
      url : "<?=base_admin();?>modul/hapus_mahasiswa/get_prodi_asal.php",
      data : {id_sp:this.value},
      success : function(data) {
          $("#kode_prodi_asal").html(data);
          $("#kode_prodi_asal").trigger("chosen:updated");
      }
  });
});
        $(".chzn-select").chosen();

  $("#id_jns_daftar_edit").change(function(){
    if ($(this).val()!=1) {
        //$("#show_sks_diakui").show();
        $("#show_asal_pt").show();
        $("#show_asal_prodi").show();
        $("#kode_pt_asal").prop('required',true);
     
    } else {
        //$("#show_sks_diakui").hide();
        $("#show_asal_pt").hide();
        $("#show_asal_prodi").hide();
        $("#kode_pt_asal").prop('required',false);
    }
});
});
    $("#save_history").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_history").html(data.responseText);
                  $(".error_data_history").focus()
                  $(".error_data_history").fadeIn();
                },
                success: function(data) {
                  $("#loadnya").hide();
                  if (data.status=='good') {
                    $(".isi_warning_history").html('');
                    $(".error_data_history").hide();
                    $(".sukses-alert").fadeIn(1000);
                    $(".sukses-alert").fadeOut(1000);
                    table_mhs.draw(false);
                  } else {
                    $(".isi_warning_history").html(data.error_desc);
                    $(".error_data_history").focus()
                    $(".error_data_history").fadeIn();
                    $(".sukses-alert").hide();
                  }
                  console.log(data.status);

                }

            });
        }
    });
</script>
