<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>

<!-- Content Header (Page header) -->
<div class="content-header ">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Judul Atas</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<?php
error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);
?>
<!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Hapus Angsuran </h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>
              <!-- /.card-header -->
        <div class="card-body">

         

<script language="javascript" type="text/javascript">
/*https://www.diskusiweb.com/discussion/31709/entry-pake-key-enter-kok-gak-berhasil*/
var fromEnter = false ;
function changeFocus (event,nextFocus)
{
if (event.keyCode==13)
{
var obj = document.getElementsByName( nextFocus )[ 0 ] ;
if (obj)
obj.focus() ;
}
fromEnter = true ;
}
function checkSubmit ()
{
fromEnter = !fromEnter ;
return fromEnter ;
}
</script>
        
<?php
include 'koneksi.php'; 

       if(isset($_POST['no_bukti'])){                  
        $no_bukti=$_POST['no_bukti'];
        $query1 = "SELECT c.nama as nama,c.alamat as alamat,a.rekening AS rekening,a.plafond as plafond, a.margin as margin,a.jangkawaktu as jangkawaktu,a.tgl as tgl,((a.plafond)/(a.jangkawaktu/15)) as byrpokok,SUM(b.plafond-b.pokok)*a.margin/100 AS byrmargin,a.plafond-SUM(b.pokok) AS bakidebet,(((a.plafond*a.margin/100)*(a.jangkawaktu/15))-SUM(b.margin)) AS sisamargin,sum(b.pokok)as pokokdibyar,((a.plafond*a.margin/100)*(a.jangkawaktu/15)) AS totalmargin,sum(b.margin) AS margindibayar,b.margin AS bayarujroh,b.pokok AS bayarpokok,b.tgl AS tgla,b.keterangan AS keterangan FROM pinjaman a left join angsuran b on a.rekening=b.rekening left join registrasi c on c.id=substr(a.register,1,10) WHERE b.no_bukti='".$no_bukti."' GROUP BY a.rekening "; 
          $hasil2 = mysqli_query($koneksi, $query1);
          $reg = mysqli_fetch_assoc($hasil2); 

             }

         $byrpokok= $reg['byrpokok'];
         $byrmargin= $reg['byrmargin'];
         $jumlah = $byrpokok+$byrmargin;

        function Rp($angka) { 
        $jadi = number_format($angka,2,'.',',');
        return $jadi;
        }

         ?>
            
            <script type="text/javascript">
              /*https://app.sko.dev/post/cara-membuat-hitung-total-pada-javascript-1587615897*/
    function hitungPaket2() {
      var nilai_paket_1 = document.getElementById('pokok').value.replaceAll(",","");
      var nilai_paket_2 = document.getElementById('margin').value.replaceAll(",","");

      /*untuk menghilangkan angka kosong*/
      /*if (nilai_paket_2 == ""){
        document.getElementById('jumlah').value ="Ketik Angka 0 di kolum Ujroh";
      }else if(nilai_paket_1 == ""){
        document.getElementById('jumlah').value ="Ketik Angka 0 di kolum Pokok";
      }else{*/
        var jumlah = Number(nilai_paket_2) + Number(nilai_paket_1);
       /* klo parseInt maka hanya untuk angka bulat
       var jumlah = parseInt(nilai_paket_2) + parseInt(nilai_paket_1);*/

/*https://app.sko.dev/post/cara-mengatasi-penjumlahan-yang-dianggap-sebagai-string-pada-prompt-javascript-1553270328*/

        document.getElementById('jumlah').value = jumlah.toLocaleString();
        /*https://www.dumetschool.com/blog/Penjumlahan-Dalam-JavaScript*/
      /*}*/
    }
           
            </script>  

            <form action="" method="POST">
          <div class="row">
            <div class="col-sm-6">
              
             <div class="form-group row ">
              <label for="nama" class=" col-sm-4">No Bukti </label>
        
              <input type="text" name="no_bukti" id="no_bukti" required="no_bukti" class="form-control col-sm-6" value="<?php echo $_POST['no_bukti'];?>" onchange="this.form.submit();" onKeyDown="changeFocus(event,'tgl')"/>
              
                    
                 <button type="submit" class="  btn btn-info"  ><i class="fas fa-search"></i></button>
               </div>
              </form>

              

              <form name="myform" action="" method="POST"  class="form-simpanangsuran" id="form-simpanangsuran">

                <input type="hidden" name="no_bukti" id="no_bukti"  class="form-control col-sm-7" value="<?php if(isset($_POST['no_bukti'])){echo $_POST['no_bukti']; }?>"/>
              
                <input type="hidden" name="rekening" id="rekening"  class="form-control col-sm-7" value="<?php echo  $reg['rekening'];?>"/>
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Tanggal</label>
                <input type="text" name="tgl" id="tgl" class="tgl form-control col-sm-8" value="<?php echo date('d-m-Y',strtotime($reg['tgla']));?>" onKeyDown="changeFocus(event,'pokok')" autofocus />
              </div>


        
              
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Pokok</label>
                <input type="text" name="pokok" id="pokok" class="form-control col-sm-8"onkeyup="hitungPaket2()" value="<?php echo Rp($reg['bayarpokok']);?>" onKeyDown="changeFocus(event,'margin')"/> 
              </div>             

            <div class="form-group row">
                <label for="nama" class="col-sm-4">Ujroh</label>
                <input type="text" name="margin" id="margin" class="form-control col-sm-8"onkeyup="hitungPaket2()" value="<?php echo Rp($reg['bayarujroh']);?>" onKeyDown="changeFocus(event,'jumlah')"/>      
            </div>   
            
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Jumlah</label>
                <input type="text" name="jumlah" id="jumlah" class="form-control col-sm-8" value="<?php echo Rp($reg['bayarpokok']+$reg['bayarujroh']);?>"  onKeyDown="changeFocus(event,'carabayar')"/>
            </div>

            <div class="form-group row">
                <label for="nama" class="col-sm-4">Metode Bayar</label>
                <div class="custom-control custom-radio col-sm-2 ">
                          <input type="radio"oninput="this.className = ''" class="custom-control-input"  id="customRadio1" name="carabayar" value="j" onKeyDown="changeFocus(event,'keterangan')" />
                          <label for="customRadio1" class="custom-control-label">Jual </label>
                </div>
                <div class="custom-control custom-radio col-sm-2">
                          <input class="custom-control-input " type="radio"oninput="this.className = ''" id="customRadio2" name="carabayar" value="t" onKeyDown="changeFocus(event,'keterangan')"/>
                          <label for="customRadio2" class="custom-control-label">Tebus</label>
                 </div>
                 <div class="custom-control custom-radio col-sm-2">
                          <input class="custom-control-input " type="radio"oninput="this.className = ''" id="customRadio3" name="carabayar" value="a" onKeyDown="changeFocus(event,'keterangan')" checked/>
                          <label for="customRadio3" class="custom-control-label">Angsuran</label>
                 </div>
              </div>
              
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Keterangan</label>
                <input type="text" name="keterangan" id="keterangan" class="form-control col-sm-8" required="" value="<?php echo $reg['keterangan']; ?>"  onKeyDown="changeFocus(event,'simpanangsuran')"/>
            </div>
      
        </div>


        <div class="col-sm-6">
           <div class="form-group row"> 
            <label for="nama" class="col-sm-3" style="margin-left: 12px;">Plafond</label> 
             <input type="text"name="plafond" id="plafond" class="form-control col-sm-8" readonly="plafond" value="<?php echo number_format($reg['plafond'],0)." [Margin ".$reg['margin']."% ]"." [jangka Waktu ".$reg['jangkawaktu']."]"?>" />
           </div> 

           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Bayar Pokok</label> 
             <input type="text"name="bayarpokok" id="bayarpokok" class="form-control col-sm-8" readonly="bayarpokok" value="<?php echo Rp($reg['pokokdibyar']);?>"/>
           </div>

           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Bakidebet</label> 
             <input type="text"name="bakidebet" id="bakidebet" class="form-control col-sm-8" readonly="bakidebet" value="<?php echo Rp($reg['bakidebet']);?>"/>
           </div> 

           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Total Margin</label> 
             <input type="text"name="totalmargin" id="totalmargin" class="form-control col-sm-8" readonly="totalmargin" value="<?php echo Rp($reg['totalmargin']);?>"/>
           </div>
           

           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Bayar Margin</label> 
             <input type="text"name="margindibayar" id="margindibayar" class="form-control col-sm-8" readonly="margindibayar" value="<?php echo Rp($reg['margindibayar']);?>"/>
           </div>
           
           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Sisa Margin</label> 
             <input type="text"name="sisamargin" id="sisamargin" class="form-control col-sm-8" readonly="plafond" value="<?php echo Rp($reg['sisamargin']);?>"/>
           </div>
           <div class="form-group row"> 
            <label for="nama" class="col-sm-3"style="margin-left: 12px;">Nama/Alamat</label> 
             <input type="text"name="sisamargin" id="sisamargin" class="form-control col-sm-8" readonly="plafond" value="<?php echo $reg['nama']." ||".$reg['alamat'];?>"/>
           </div>

         </div> 
            
            <!-- /.card-body -->
            <div class="card-footer ">
              <input type="button" name="simpanangsuran" id="simpanangsuran" 
              class="btn btn-primary"  value="Simpan" onclick='document.geElementById("myform").submit()'>
              <a href="home.php?home=<?php echo md5("angsuran");?>" id="tambah" class="btn btn-success">
                Tambah
              </a>

              <a href="home.php?home=<?php echo md5("editangsuran");?>" class="btn btn-warning">
                Edit
              </a>
              <?php if($_SESSION['level']<0){ ?>
              <a href="home.php?home=<?php echo md5("deleteangsuran");?>" class="btn btn-danger">
                Hapus
              </a>
              <?php }?>
              <a class="btn btn-secondary" type="button" href="pdf/cetak_validasi_angsuran_pdf.php?rekening=<?php echo $_POST['rekening'];?>" target="_bank">Print
              </a>
              <a class="btn btn-secondary" type="button" href="pdf/cetak_nobukti_angsuran_pdf.php?no_bukti=<?php echo $_POST['no_bukti'];?>" target="_bank">Cetak
              </a>

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


<!-- <style>
input:out-of-range {
  border:2px solid red;
}
</style> -->
<!-- <script type="text/javascript" src="jquery.js"></script>  -->
<script type="text/javascript">

 $(document).ready(function(){
    $("#simpanangsuran").click(function(){
      var data       = $('#form-simpanangsuran').serialize(); 
      var no_bukti   = $('#no_bukti');
      var rekening   = $('#rekening');
      var tgl        = $('#tgl').val()
      var pokok      = $('#pokok').val().replace(/,(?=\d{3})/g, '');
      
      var bakidebet  = $('#bakidebet').val().replace(/,(?=\d{3})/g, '');
      

      if(rekening== "0"|| pokok=="0" ||  bakidebet=="Angs. []"){ 
Swal.fire({
  icon: 'error',
  title: 'Hati-hati...',
  text: 'Data masih ada yg belum diisi!',
  footer: '<a href="">Anda Perlu teliti sedikit lagi?</a>'
});
      
    } else if(rekening== ""|| pokok==""|| bakidebet==""){ 

      Swal.fire(
  'Data Masih ada yang Kosong !',
  'Mohon data dilengkapi dlu !',
  'question'
);
   
               
    }else{

      $.ajax({
        type: 'POST',
        url: 'simpan/delete_angsuran.php',
         data: data,
        success: function(data) {
         console.log(data)
         let timerInterval
         alert("Data Sudah Dihapus");
        window.location.href="home.php?home=dbecb5ad09705de4976c6bdccf1e1971"; 

         Swal.fire({
  title: 'Mau Print?',
  text: "Pilih Tombol Print!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  
  confirmButtonText: '<a href="faktur/cetak_faktur_angsuran.php" target="_bank" class="text-white">Print</a>'  
}).then((result) => {
  if (result.value) {

    /*
    Swal.fire(
      'Deleted!',
      '<a class="fa fa-print" href="laporan/cetak_faktur2.php" target="_blank">Print</a>',
      'Clik Tombol di atas'
    )*/
  }
})

        

        },
        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
 $('#pokok').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> 
<script type="text/javascript">
 $('#margin').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>

 
</head>
</html>