<!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 -->

<!-- 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 ">Buat Buku Tabungan</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">
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
    $kode         = $_SESSION['kode'];
    error_reporting(E_ALL^(E_NOTICE|E_WARNING));
    ini_set('display_errors',1);
    error_reporting(0);

   include 'koneksi.php'; 

       if(isset($_POST['rekening3'])){ 
        $register= $_POST['rekening1'].".".sprintf("%06d",$_POST['rekening3']);
        $rektab= $_POST['rekening1'].".".$_POST['rekening2'].".".sprintf("%06d",$_POST['rekening3']);

        $rekening= $_POST['rekening1'].".".$_POST['rekening2'].".".sprintf("%06d",$_POST['rekening3']).".".sprintf("%02d",$_POST['rekening4']);
        
        $query1 = "SELECT nama as nama,alamat as alamat,rtrw as rtrw,ibu as ibu FROM registrasi WHERE id='".$register."'  "; 
          $hasil2 = mysqli_query($koneksi, $query1);
          $reg = mysqli_fetch_assoc($hasil2); 
     }
      $rekak = mysqli_query($koneksi,"SELECT count(rektab) as kodea FROM bukutabungan where rektab='$rektab' ");
      $rekakhir = mysqli_fetch_row($rekak) ;

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

         ?>   
         <style>
.custom-input1 {
    width: 50px;
    margin-right: 10px;
}
.custom-input2 {
    width: 70px;
    margin-right: 10px;
}
.custom-input3 {
    width: 110px;
    margin-right: 10px;
}
.custom-input4 {
    width: 40px;
    margin-right: 10px;
}
.cariid{
  width: 20px;
}
</style>         

            <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. Rekening</label>

                <input type="text" name="rekening1" id="rekening1" class="form-control custom-input1" value="<?php if(isset($_POST['rekening1'])){echo $_POST['rekening1']; }else{ echo $kode;}?>" onKeyDown="changeFocus(event,'rekening2')" />

                 <input type="text" name="rekening2" id="rekening2" class="form-control custom-input2" list="optionsList" value="<?php if(isset($_POST['rekening2'])){ echo $_POST['rekening2'];} ?>"onChange="updateRekening2()" onKeyDown="changeFocus(event,'rekening3')"/>
                    <datalist id="optionsList">
                        <!-- Opsi akan ditambahkan di sini -->
                    </datalist>
                
                <input type="text" name="rekening3" id="rekening3" class="form-control custom-input3" value="<?php if(isset($_POST['rekening3'])){echo sprintf("%06d",$_POST['rekening3']); }?>" onKeyDown="changeFocus(event,'rekening4')" onchange="this.form.submit();"/> 

                <input type="text" name="rekening4" id="rekening4" class="form-control custom-input4" value="<?php if(isset($_POST['rekening4'])){echo sprintf("%02d",$_POST['rekening4']); }?>" onKeyDown="changeFocus(event,'cariid')"  />
                

                <button type="submit" id="cariid" name="cariid" style="width:40px;" ><i class="fas fa-search"></i></button>
            </div>
        
</form>
<script>
function changeFocus(event, nextFieldId) {
    if (event.key === 'Enter') {
        event.preventDefault(); // Mencegah pengiriman form saat Enter ditekan
        const nextField = document.getElementById(nextFieldId);
        if (nextField) {
            nextField.focus(); // Pindah fokus ke field berikutnya
        }
    }
}
</script>
<script>
        // Data pilihan untuk dimasukkan ke dalam datalist
        const optionsData = ["11. Wadiah", "12. Idul Fitri", "13. Qurban", "40. Deposito"];

        // Mengisi datalist dengan pilihan data
        const dataList = document.getElementById('optionsList');
        optionsData.forEach(option => {
            const opt = document.createElement('option');
            opt.value = option; // Menyimpan nilai asli
            dataList.appendChild(opt);
        });

        // Menampilkan array optionsData di samping input
       // const optionsDisplay = document.getElementById('optionsDisplay');
        //optionsDisplay.innerText = "Pilihan: " + optionsData.join(", "); // Menggabungkan array menjadi string

        // Fungsi untuk memperbarui input rekening2 dengan dua karakter pertama dari pilihan
        function updateRekening2() {
            const dataInput = document.getElementById('rekening2');
            
            // Dapatkan nilai yang dipilih dari input rekening2
            const selectedValue = dataInput.value;

            // Ambil dua karakter pertama dari selectedValue
            const twoChars = selectedValue.substring(0, 2);
            // Set nilai rekening2 dengan dua karakter pertama
            dataInput.value = twoChars; // Memperbarui nilai input rekening2
        }
    

        function changeFocus(event, nextFieldId) {
            if (event.key === 'Enter') {
                event.preventDefault(); // Mencegah pengiriman form
                const nextField = document.getElementById(nextFieldId);
                if (nextField) {
                    nextField.focus(); // Pindah fokus
                }
            }
        }
    </script>

              <form name="myform" action=".../../simpan/simpan_bukutabungan.php" method="POST"  class="form-simpanangsuran" id="form-simpanangsuran">
                
                <input type="hidden" name="rekening" id="rekening"  value="<?php echo $rektab.".".sprintf("%02d",$rekakhir['0']+1); ?>" />
                <input type="hidden" name="rektab" id="rektab"  value="<?php echo $rektab; ?>" />
                
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Tanggal</label>
                <input type="date" name="tgl" id="tgl" class="tgl form-control col-sm-8" value="<?php echo date('Y-m-d');?>" max="<?=date('Y-m-d')?>" required="" onKeyDown="changeFocus(event,'kodetab')" />
              </div>

              <div class="form-group row">
                <label for="nama" class="col-sm-4">Jenis Tabungan</label>
                <select id="kodetab" name="kodetab" class="form-control col-sm-8 select2" onKeyDown="changeFocus(event,'asuransi')" readonly>
    <option value="11" <?php echo ($_POST['rekening2']=='11')?"selected":""; ?>>TABUNGAN WADIAH</option>
    <option value="12" <?php echo ($_POST['rekening2']=='12')?"selected":""; ?>>TABUNGAN IDUL FITRI</option>
    <option value="13" <?php echo ($_POST['rekening2']=='13')?"selected":""; ?>>QURBAN</option>
    <option value="40" <?php echo ($_POST['rekening2']=='40')?"selected":""; ?>>DEPOSITO</option>
</select>

            
                  
            </div>
              <div class="form-group row">
                <label for="nama" class="col-sm-4">Ikut Asuransi</label>
                <div class="custom-control custom-radio col-sm-2 ">
                          <input type="radio"oninput="this.className = ''" class="custom-control-input"  id="customRadio1" name="asuransi" value="Y" onKeyDown="changeFocus(event,'customRadio2')" checked/>
                          <label for="customRadio1" class="custom-control-label">Ya </label>
                </div>
                <div class="custom-control custom-radio col-sm-2">
                          <input class="custom-control-input " type="radio"oninput="this.className = ''" id="customRadio2" name="asuransi" value="T" onKeyDown="changeFocus(event,'simpanatabungan')"/>
                          <label for="customRadio2" class="custom-control-label">Tidak</label>
                 </div>
                 <div class="custom-control custom-radio col-sm-2">
                          
                 </div>
              </div>
              
                          
              <!-- <div class="form-group row">
                <label for="nama" class="col-sm-4">Keterangan</label>
                <input type="text" name="keterangan"value="<?php echo "an.". $reg["nama"];?>" id="keterangan" class="form-control col-sm-8" required="" 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;">Nama</label> 
             <input type="text"name="nama" id="nama" class="form-control col-sm-8" readonly="nama" value="<?php echo $reg["nama"];?>" />
           </div> 

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

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

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

           

         </div> 
            
            <!-- /.card-body -->
            <div class="card-footer ">
              <input type="submit" name="simpanatabungan" id="simpanatabungan" 
              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>
              <a href="" class="btn btn-danger">
                Hapus
              </a>
              <a class="btn btn-secondary" type="button" href="faktur/cetak_faktur_angsuran.php" target="_bank">Print</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 rekening   = $('#rekening');
      var tgl        = $('#tgl').val()
      var pokok      = $('#pokok').val().replace(/,(?=\d{3})/g, '');
      var margin     = $('#margin').val();
      var keterangan = $('#keterangan').val();
      var bakidebet  = $('#bakidebet').val().replace(/,(?=\d{3})/g, '');
      

      if(rekening== "0"|| tgl=="0" ||  keterangan=="0"){ 
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== ""|| tgl==""|| keterangan==""){ 

      Swal.fire(
  'Data Masih ada yang Kosong !',
  'Mohon data dilengkapi dlu !',
  'question'
);
    }else if(parseInt(pokok)>parseInt(bakidebet)){
      Swal.fire(
  'Pembayaran Pokok Kelebihan !',
  'Mohon dilakukan Pengurangan !',
  'question'
);
               
    }else{

      $.ajax({
        type: 'POST',
        url: 'simpan/simpan_angsuran.php',
         data: data,
        success: function(data) {
         console.log(data)
         let timerInterval
         /* alert("Data Sudah Tersimpan");
        alert("Cetak Validasi?");


        window.location.href="laporan/cetak_faktur2.php" ;*/ 

         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
 $('#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>  
 
</head>
</html>