<div class="modal" tabindex="-1" role="dialog" id="<?=$id_modal?>">
  <div class="modal-dialog  modal-dialog-centered  modal-lg" role="document" style="overflow-y: initial !important">
    <div class="modal-content">
      <form id="edit">
      <div class="modal-header bg-info text-light">
        <h5 class="modal-title"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" style="height: 70vh;overflow-y: auto;">
          <div class="row">
            <div class="form-group col-md-6">
              <label for="exampleInputEmail1">KMA</label>
              <input type="hidden" id="status" name="status">
              <input type="text" class="form-control" id="kma" name="kma" readonly value="<?=$id_row?>">
              <!-- <small id="emailHelp" class="form-text text-muted">Data Akan terisi Otomatis.</small> -->
            </div>
            <div class="form-group  col-md-6">

                      <label for="bulan_kegiatan">Bulan Kegiatan </label>
                    <input type="hidden" id="tampung_bulan_kegiatan">
                      <select class="form-control" id="bulan_kegiatan" name="bulan_kegiatan[]" multiple="multiple" required>
                      </select>

                    </div>
            <div class="form-group  col-md-6">
              <label for="exampleFormControlTextarea1">Pencapaian</label>
              <textarea class="form-control" id="pencapaian" name="pencapaian" rows="3" ></textarea>
            </div>
            <div class="form-group  col-md-6">
              <label for="exampleFormControlTextarea1">Analisa Terhadap Pencapaian</label>
              <textarea class="form-control" id="atp" name="atp" rows="3" ></textarea>
            </div>
            <div class="form-group  col-md-12">
              <label for="exampleFormControlTextarea1">Realisasi Kualitatif</label>
              <textarea class="form-control" id="realisasi_kualitatif" name="realisasi_kualitatif" rows="3" ></textarea>
            </div>
            <div class="form-group col-md-6">
              <label for="exampleInputEmail1">Realisasi Penerimaan</label>
                      <input type="text" class="form-control timepicker" id="realisasi_penerimaan" name="realisasi_penerimaan" aria-describedby="emailHelp" placeholder="" required>
                      <!-- <small id="emailHelp" class="form-text text-muted">kod</small> -->
            </div>
            <div class="form-group col-md-6">
              <label for="exampleInputEmail1">Realisasi Pengeluaran</label>
              <input type="text" class="form-control timepicker" id="realisasi_pengeluaran" name="realisasi_pengeluaran" aria-describedby="emailHelp" placeholder="" required>
              <!-- <small id="emailHelp" class="form-text text-muted">kod</small> -->
            </div>
            <div class="form-group col-md-6">
              <label for="exampleInputEmail1">Analisa Keuangan</label>
              <textarea class="form-control" id="analisa_keuangan" name="analisa_keuangan" rows="3" ></textarea>
              <!-- <small id="emailHelp" class="form-text text-muted">kod</small> -->
            </div>
            <div class="form-group col-md-6">
              <label for="exampleFormControlSelect1">Terlaksana</label>
              <select class="form-control" id="terlaksana" name="terlaksana">
                <option value="1">Ya</option>
                <option value="0">Tidak</option>
              </select>
            </div>

          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
        <button type="submit" class="btn btn-info" id="btn-submit"><i class="fas fa-save"></i> Update</button>
      </div>
    </form>
    </div>
  </div>
</div>
<script type="text/javascript">
  $(document).ready(function(){
    var base_url = "<?=base_url()?>";
    var id_modal = "<?=$id_modal?>";
    // var time = function(){
    //   $('.timepicker').datepicker({
    //       format: 'dd MM yyyy',
    //       todayHighlight:true,
    //       toggleActive:true,
    //       daysOfWeekDisabled:'0',
    //       autoclose:true,
    //       language:'id'
    //   });
    // }
    // time();
    $.ajax({
        url             : base_url + 'pka_ev_tri/row_ev_tri', 
        type            : "POST",
        dataType        : 'json',
        data            : {kma:'<?=$id_row?>'},
        success         : function(data){

            $("#tampung_bulan_kegiatan").val(data.bulan_kegiatan)
            $("#pencapaian").val(data.pencapaian)
            $("#atp").val(data.atp)
            $("#realisasi_kualitatif").val(data.realisasi_kualitatif)
            $("#realisasi_penerimaan").val(formatRupiah(data.realisasi_penerimaan, 'Rp. '))
            $("#realisasi_pengeluaran").val(formatRupiah(data.realisasi_pengeluaran, 'Rp. '))
            $("#analisa_keuangan").val(data.analisa_keuangan)
            $("#terlaksana").val(data.terlaksana)
            $("#status").val(data.status)

            if(data.status == "0"){
                $(".modal-header").addClass("bg-info");
                $(".modal-title").html("Edit Evaluasi Triwulan");
                $("#btn-submit").html("Update");
                $("#btn-submit").addClass("btn-info");
            }else{
                $(".modal-header").addClass("bg-warning");
                $(".modal-title").html("Ajukan Ulang Evaluasi Triwulan");
                $("#btn-submit").html("Ajukan Ulang");
                $("#btn-submit").addClass("btn-warning");
            }



            
        }
    })
    $("#edit").submit(function(){
      var form_data = new FormData($('#edit')[0]);
      bootbox.confirm("Apakah Anda Yakin Akan Menyimpan Data?", function(result){ 
        if (result==true) {
          $.ajax({
                url             : base_url + 'pka_ev_tri/update_ev_tri', 
                type            : "POST",
                dataType        : 'json',
                mimeType        : 'multipart/form-data',
                data            : form_data,
                contentType     : false,
                cache           : false,
                processData     : false,
                success         : function(response){
                    if(response == true){
                        $('#'+id_modal).modal('hide');  
                        Command: toastr["success"]("Data Evaluasi Triwulan Berhasil Di-Update", "Berhasil");
                        getcontens('page/data_ev_pka_tri');
                    }else{
                        Command: toastr["error"]("Simpan error, data tidak berhasil Update", "Error");
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                  $('#loading').modal('hide');
                  Command: toastr["error"](errorThrown, "Error");
                },
                beforeSend: function () {
                  $('#loading').modal('show');
                },
                complete: function () {
                    $('#loading').modal('hide');
                }
          });
        }
      });
        return false;
    })
    $.ajax({
        url: base_url+"Pka_ev_tri/ambil_bulan_kegiatan",
        dataType:'json',
        success:function(response){
          // alert(response)
          var data = response;
          var $id = $('#bulan_kegiatan');
          $id.empty();
          // $id.append('<option value="">- Pilih Bulan Kegiatan -</option>');
          const tbk = $("#tampung_bulan_kegiatan").val()
          if(tbk==""){
            setTimeout(function(){
              const tbk0 = $("#tampung_bulan_kegiatan").val()
              var Arraytbk = JSON.parse(tbk0);
              
              for (var i = 0; i < data.length; i++) {
                  if(Arraytbk.includes(data[i].triwulan)==true){
                      var sel = "selected";
                  }else{
                      var sel = "";
                  }
                $id.append('<option value="'+data[i].triwulan+'" title="'+data[i].triwulan+'" '+sel+'>' + data[i].triwulan + '</option>');
              }  
            }, 100);
          }else{
            var Arraytbk = JSON.parse(tbk);
              
              for (var i = 0; i < data.length; i++) {
                  if(Arraytbk.includes(data[i].triwulan)==true){
                      var sel = "selected";
                  }else{
                      var sel = "";
                  }
                $id.append('<option value="'+data[i].triwulan+'" title="'+data[i].triwulan+'" '+sel+'>' + data[i].triwulan + '</option>');
              }  
          }
          
        } ,
        error: function(jqXHR, textStatus, errorThrown) {
          // Command: toastr["error"](errorThrown, "Error");
          alert(errorThrown)
        }  
      });
  })

 
  $("#bulan_kegiatan").select2({

    placeholder: " Silahkan Pilih Bulan Kegiatan"

  });
  $('#<?=$id_modal?>').on('shown.bs.modal', function () {
    $('#bulan_kegiatan').select2({
        dropdownParent: $('#<?=$id_modal?>')
    });
});
 
</script>
<script type="text/javascript">
  $("#realisasi_penerimaan").keyup(function(){
    $("#realisasi_penerimaan").val(formatRupiah(this.value, 'Rp. '))
  })
  $("#realisasi_pengeluaran").keyup(function(){
    $("#realisasi_pengeluaran").val(formatRupiah(this.value, 'Rp. '))
  })
 
		/* Fungsi formatRupiah */
		function formatRupiah(angka, prefix){
			var number_string = angka.replace(/[^,\d]/g, '').toString(),
			split   		= number_string.split(','),
			sisa     		= split[0].length % 3,
			rupiah     		= split[0].substr(0, sisa),
			ribuan     		= split[0].substr(sisa).match(/\d{3}/gi);
 
			// tambahkan titik jika yang di input sudah menjadi angka ribuan
			if(ribuan){
				separator = sisa ? '.' : '';
				rupiah += separator + ribuan.join('.');
			}
 
			rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
			return prefix == undefined ? rupiah : (rupiah ? 'Rp. ' + rupiah : '');
		}
	</script>