 
  <?php
    /*error_reporting(E_ALL^(E_NOTICE|E_WARNING));*/
    include "koneksi.php";
    
    if(isset($_POST['j_bukti'])){
    $no_bukti=$_POST['j_bukti'];
    
    $data=mysqli_fetch_assoc(mysqli_query($koneksi,"SELECT min(id_jurnal) as id2,(id_jurnal-1)as id1,no_bukti AS no_bukti,tanggal,keterangan AS keterangan,kode_akun AS kode2,kredit AS kredit,bukubesar2.kode2 AS kode1,bukubesar2.debet2 AS debet FROM bukubesar LEFT JOIN (SELECT id_jurnal AS id2,kode_akun AS kode2,no_bukti AS bukti2,kredit AS debet2 FROM bukubesar WHERE no_bukti='".$no_bukti."' GROUP BY no_bukti) AS bukubesar2 ON bukubesar.no_bukti=bukubesar2.bukti2 WHERE bukubesar.no_bukti='".$no_bukti."' AND id_jurnal>bukubesar2.id2   GROUP BY no_bukti ")); 
}
?>

<!-- /.content-header -->
<!-- Main content -->
<section class="content" style="margin-top: 10px;">
  <div class="container-fluid">
    <div class="row">
      <!-- left column -->
      <div class="col-md-12">
        <!-- jquery validation -->
        <div class="card card-info">
          <div class="card-header">
            <h3 class="card-title">Edit Jurnal</h3>
          </div>
          <!-- /.card-header -->
          <!-- form start -->
            <?php 
            /*function klik($post){

              if(isset($_POST['j_bukti'])){
                $proses= $post;

              }else{
              echo "";                
              }
              return ($proses);
            }
            echo klik();*/
            ?>
          

            <div class="card-body">

                <form action="" method="POST">
              <div class="form-group row">
              <label for="nama" class=" col-sm-2">No. Bukti </label>
        
              <input type="text" name="j_bukti" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $_POST['j_bukti'];}else{echo "";} ?>" onchange="this.form.submit();" onkeypress="nextfield('r_tgl')"/>
                    
                <button type="submit" class="btn btn-info"><i class="fas fa-search"></i></button>
              </div>
            </form>
          <form role="form" class="form-updatejurnaledit" id="form-updatejurnaledit" method="post">

              <!-- <div class="form-group row"> -->
                <!-- <label for="nama" class="col-sm-2">No Transaksi</label> -->
               
                <input type="hidden" name="j_id1" id="j_id1" class="form-control col-sm-9" value="<?php echo $data['id1']?>" >
              
              
                <input type="hidden" name="j_id2" id="j_id2" class="form-control col-sm-9" value="<?php echo $data['id2']?>" >
              
                <input hidden="text" name="j_faktur" id="j_faktur" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $data['no_bukti'];}else{echo "";} ?>" placeholder="No Transaksi" >


              <div class="form-group row">
                <label for="nama" class="col-sm-2">Tanggal</label>
                <input type="text" name="j_tgl" id="j_tgl" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $data['tanggal'];}else{echo "";} ?>" required="">
              </div>

              <div class="form-group row">
                <label for="nama" class="col-sm-2">Akun Debet</label>
               <input type="text" name="j_debet" id="j_debet" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $data['kode1'];}else{echo "";} ?>" required="">
                </div>
                
              
            <div class="form-group row">
                <label for="nama" class="col-sm-2">Akun Kredit</label>
               <input type="text" name="j_kredit" id="j_kredit" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $data['kode2'];}else{echo "";} ?>" required="">      </div>

                <div class="form-group row">
                <label for="nama" class="col-sm-2">Nominal</label>
                <input type="text" name="j_nominal" id="j_nominal" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){ if ($data['debet']==0) echo number_format($data['kredit']); else echo number_format($data['debet']);}else{echo "";} ?>" required="">
              </div>

               <div class="form-group row">
                <label for="nama" class="col-sm-2">Keterangan</label>
                <input type="text" name="j_keterangan" id="j_keterangan" class="form-control col-sm-9" value="<?php if(isset($_POST['j_bukti'])){echo $data['keterangan'];}else{echo "";} ?>" required="">
              </div> 

             </div>
            <!-- /.card-body -->
            <div class="card-footer bg-info">
              <input type="button" name="updatejurnaledit" id="updatejurnaledit" 
              class="btn btn-primary" value="Simpan">
              <a href="home.php?home=inputjurnal" class="btn btn-secondary">
                Tambah
              </a>
              <a href="home.php?home=editjurnal" class="btn btn-warning">
                Edit
              </a>
              <a href="" class="btn btn-danger">
                Hapus
              </a>
            </div>

          </form>
        </div>
        <!-- /.card -->
      </div>
      <!--/.col (left) -->
    </div>
    <!-- /.row -->
  </div><!-- /.container-fluid -->
</section>
<!-- /.content -->

<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">

 $(document).ready(function(){
    $("#updatejurnaledit").click(function(){
      var data = $('#form-updatejurnaledit').serialize();
      var j_id1= $('#j_id1').val();
      var j_id2= $('#j_id2').val();
      var j_faktur= $('#j_faktur').val();
      var j_tgl= $('#j_tgl').val();
      var j_debet= $('#j_debet').val();
      var j_kredit= $('#j_kredit').val();
      var j_nominal= $('#j_nominal').val();
      var j_keterangan= $('#j_keterangan').val();
      

      if(j_faktur== "0"|| j_tgl=="0" || j_debet=="0" ||j_kredit=="0"||j_nominal=="0"){ 
        alert ("Data Masih ada yang kosong 1");
        //window.location.href='/rakerugt/home.php?page=cap_realita';
      
    } else if(j_faktur== ""|| j_tgl=="" || j_debet=="" ||j_kredit==""||j_nominal==""){ 
        alert ("Data Masih ada yang kosong 2");
       
    }else{
      $.ajax({
        type: 'POST',
        url: 'simpan/simpan_edit_jurnal.php',
        data: data,
        success: function(data) {
         console.log(data)
         let timerInterval
Swal.fire({
  title: 'Alhamdulillah..! Edit Data Sukses.',
  html: 'Silahkan Cek <b></b> Sendiri.',
  timer: 3000,
  timerProgressBar: true,
  didOpen: () => {
    Swal.showLoading()
    const b = Swal.getHtmlContainer().querySelector('b')
    timerInterval = setInterval(() => {
      b.textContent = Swal.getTimerLeft()
    }, 100)
  },
  willClose: () => {
    clearInterval(timerInterval)
  }
}).then((result) => {
  /* Read more about handling dismissals below */
  if (result.dismiss === Swal.DismissReason.timer) {
    console.log('I was closed by the timer')
  }
})

         /*alert("Data Sukses Disimpan");
         window.location.href='home.php?home=registrasi'; */
        },
        error: function(error){
          console.log(`error ${error}`)
         
        }
      });
    }
    });
  });
  
</script>

<script type="text/javascript">
  // untuk format koma
  //https://cobangoding.com/tips-dan-trick/memberi-titik-otomatis-pada-angka-saat-menginput-dengan-javascript/#comment-757
  //https://codepen.io/cobangoding/pen/QWOoLXN
 $('#j_nominal').on('keyup',function(){
  var angka = $(this).val();
 
  var hasilAngka = formatRibuan(angka);
 
  $(this).val(hasilAngka);
  });

  function formatRibuan(angka) {
  var number_string = angka.replace(/[^\.\d]/g, '').toString(),
    split = number_string.split('.'),
    sisa = split[0].length % 3,
    angka_hasil = 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 ? ',' : '';
    angka_hasil += separator + ribuan.join(',');
  }

  angka_hasil =
    split[1] != undefined ? angka_hasil + '.' + split[1] : angka_hasil;
  return angka_hasil;
}
</script> 