<?php

error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);
?>
<style type="text/css">
  input[type=text], select {
    /*width: 100%;*/ /* Menggunakan 100% dari lebar wadah */
    /*max-width: 300px;*/ /* Lava lebar maksimum untuk input dan select */
    border: 2px solid #ccc;
    border-radius: 3px;
    font-size: 13px;
    background-color: white;
    padding: 4px 10px;
    box-sizing: border-box; /* Menghitung padding dalam lebar elemen */
}
/*membuat modal transparan https://stackoverflow.com/questions/30161453/bootstrap-modal-is-transparent*/
.modal-backdrop, 
.modal-backdrop.fade.in{
opacity: 1 !important;
filter: alpha(opacity=100) !important;
background: transparent;
}
.btn {
    /*width: 100%;*/
    /*max-width: 150px*/; /* Lebar maksimum untuk tombol */
    display: block;
    margin-top: 6px;
}

.modal-body {
    padding: 15px; /* Tambahkan padding untuk menghindari elemen bersentuhan dengan tepi layar */
}

  input[type=text] {
/*  width: 206px;*/
   /*box-sizing: border-box;*/
  border: 2px solid #ccc;
  border-radius: 3px;
  font-size: 13px;
  /*background-color: white;*/
  background-position: 10px 10px; 
  background-repeat: no-repeat;
  padding: 4px 4px 5px 10px;
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;

}

input[type=text]:focus {
  background-color: white;
  padding-top:5px ;
}

.btn{
 float: left;
display: block; 
margin-buttom:13px;
margin-left:9px;
font-family: Calibri;
}
.modal-header {
   background-color: #138d75; /* Warna background */
    font-size: 13px;
    color: white; /* Warna font */
    padding: 12px 0px 0px 0px; /* Padding atas dan bawah dengan padding samping 15px */
    font-family: cambria;
}

.modal-footer{
 /*margin-top:3px;
 margin-bottom:0px;
background-color: white;*/ /*warna background*/ 
}
</style>
<!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-1">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Laporan Neraca []</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 ">Cari Peminjam</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">
          <?php
  function rp($angka){
  $jadi =number_format($angka,0,'.',',');
  return $jadi;
  }
          
          $username=$_SESSION['kode'];
          if(isset($_POST['p_register'])){
            $id = $_POST['p_register'];
            
              $where="WHERE a.kode = '$username' 
          AND (a.rekening LIKE '%$id%'  OR b.nama LIKE '%$id%' OR b.alamat LIKE '%$id%'
    
          )  GROUP BY a.rekening ";

          }else{
            
            $id="";

            $where="where a.register='$id' AND a.kode='$username' ";
           
          }
          ?>
          <form role="form" id="quickForm" action="home.php?home=<?php echo md5("caripeminjam");?>" method="POST">
            <div class="form-group row">
              <label for="nama" class="col-2 m-3">Cari Peminjam</label>
              <input type="text" name="p_register" id="p_register"  class="form-control col-sm-5" placeholder="ketik Nama/Alamat/No Register">
              <div class="form-group row">
                <button type="submit" class="  btn btn-info"  ><i class="fas fa-search"></i></button>
               </div>
              
                     
          </form>                   
          <hr>

          <table class="table table-bordered table-hover" width="148%">
            <tdead>
              <tr align="center">
                <td >Tgl</td>
                <td >Rekening</td>
                <td >Nama</td>
                <td >Alamat</td>
                <td >Plafond</td>
                <td >Bakidebet</td>
               <!-- <td >Ujroh</td> -->
                <!-- <td >Lama</td> -->
                <td >Jaminan</td>
                <!-- <td >Status</td> -->
                <!-- <td >Keterangan</td> -->
                <td colspan="2" >Detail</td>
              </tr>
            </tdead>
            <tbody>
              <?php
              include 'koneksi.php';
              $no=0;
              $queryb = "SELECT a.no as id2,a.tgl as tgl,a.register as id,a.rekening as rekening, b.nama as nama,b.alamat as alamat,b.rtrw as rtrw,a.plafond AS plafond,a.bakidebet as bakidebet,a.jangkawaktu AS jangkawaktu,a.margin AS margin,a.jeniscair AS jeniscair,a.barang AS barang,a.keterangan AS keterangan from pinjaman a left join registrasi b ON a.register=b.id  $where  ";
                $querya       =mysqli_query($koneksi,$queryb);
                if(mysqli_num_rows($querya)){
                
                while ($dataja = mysqli_fetch_assoc($querya)) {
                  extract($dataja);
                  /*https://www.niagahoster.co.id/blog/undefined-index-php/
                  cara mengatasi Undefined variable  php terbaru*/
                  ?>
                  <tr>
                    
                    <td><?php echo date('d-m-Y',strtotime($tgl)); ?></td> 
                    <td><?php echo $rekening; ?></td>
                    <td><?php echo $nama; ?></td>
                    <td><?php echo $alamat ?></td>
                    <!-- <td><?php echo $rtrw; ?></td> -->
                    <td align="right"><?php echo rp($plafond); ?></td>
                    <td align="right"><?php echo rp($bakidebet); ?></td>
                    <!-- <td><?php echo $margin; ?></td>  -->
                    <!-- <td><?php echo $jangkawaktu; ?></td> -->
                    <td><?php echo $barang; ?></td>
                    <!-- <td><?php echo $jeniscair; ?></td> -->
                    <!-- <td><?php echo $keterangan; ?></td> -->

                    <td style=" width: 12px;">
          
<a href="#"class="btn btn-warning" data-toggle="modal" data-target="#tampilangsuran<?php echo $id2; ?>"><i class="far fa-bell"></i></a> 


<!-- modal update user -->
  <div class="example-modal">
  <div id="tampilangsuran<?php echo $id2; ?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-80p">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" style="padding-left: 20px;padding-bottom: 8px;height: 30px;font-size: 19;">JADWAL ANGSURAN</h4>
    <!-- <button type="button" class="close" data-dismiss="modal">&times;</button> -->
      </div>
    <div class="modal-body">
                            

<form  action="" method="POST">

<?php

  
$no=1; 
$id2= $dataja['id2'];
$queryp = "SELECT a.tgl as tgl,a.rekening as rekening,a.bakidebet, a.plafond AS plafond, a.margin AS margin, a.jangkawaktu/15 AS jangkawaktu,b.nama AS nama,b.alamat AS alamat from pinjaman a LEFT JOIN registrasi b ON a.register=b.id where a.no='".$id2."'  ";
$query  =mysqli_query($koneksi,$queryp);
if(mysqli_num_rows($query)){
while ($data = mysqli_fetch_assoc($query)) {
extract($data);
$tgl=$tgl;
}
   
$nama_bln=array(1=>"01","02","03","04","05","06","07","08","09","10","11","12");
list($awal,$tengah,$akhir)=explode('-',$tgl,3);


 $tanggal=$akhir;
/* "-";*/
if ($tengah < 10)  {
$bulan=str_replace("0","",$tengah);
}else{
$bulan=$tengah;
}

//$bulan=$tengah;
$nama_bln[$bulan];
/*"-";*/
$tahun=$awal;
   
   /* $bunga_perbulan=$bunga_pertahun/12;*/
    $bunga_rp = $plafond/$margin;
    $angsuran_margin=$plafond*$margin/100;
    $angsuran_pokok = $plafond/$jangkawaktu;
    $jumlah = $angsuran_pokok+$angsuran_margin;
    $row=$plafond;  

    ?> 
    <table border="0" align="left" width="100%"> 
<tr>
    <td  style="text-align: left; padding-left: 15px;width:44px;width: 140px;">Tgl Pencairan</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 25px;"><?php  echo date('d-m-Y',strtotime($tgl)) ?></td> 
    
    <td style="= width :55px;"></td>
    <td style="text-align: left; padding-left: 15px;width: 80px; width: 145px;"  style="width: 4px;">Jangka Waktu</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 75px;"><?php echo ($jangkawaktu*15)." H"?></td>
    </tr> 

    <tr>
    <td style="text-align: left; padding-left: 15px;width: 80px; width: 25px;"  style="width: 4px;"> Plafond</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 25px;"><?php  echo  number_format($plafond)?></td> 
    
    <td style="= width :55px;"></td>
    <td style="text-align: left; padding-left: 15px;width: 80px; width: 145px;"  style="width: 4px;">Margin</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 75px; wi"><?php echo $margin."%"?></td> 
    </tr>

    <tr>
    <td style="text-align: left; padding-left: 15px;width: 80px; width: 25px;"  style="width: 4px;"> Rekening</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 25px;"><?php  echo  $rekening?></td> 
    
    <td style="= width :55px;"></td>
    <td style="text-align: left; padding-left: 15px;width: 80px; width: 145px;"  style="width: 4px;">Jenis Pinjaman</td><td style="width: 4px;">:</td><td style="text-align: left; padding-left: 5px; width: 75px;"><?php  echo  "Flat"?></td> 
    </tr>
</table>

<table border="0" width="100%" style="padding-top: 19px;padding-bottom: 16px">
<tr>
<td style="text-align: center; background: #ddd;"><b>No.</b></td>
<td style="text-align: center; background: #ddd;"><b>Tgl</b></td>
<td style="text-align: center; background: #ddd;"><b>Pokok</b></td>
<td style="text-align: center; background: #ddd;"><b>Ujroh</b></td>
<td style="text-align: center; background: #ddd;"><b>Jumlah</b></td>
<td style="text-align: center; background: #ddd;"><b>Bakidebet</b></td>
</tr> 
<tr>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="right"><b><?php echo number_format($plafond,0)?></b></td>
</tr>
<?php
while ( $row > 1){ 
$bulan=$bulan+1;
if ($bulan > 12){
$bulan=1;
$tahun=$tahun+1;
}else{
$bulan=$bulan;
}
$row=$row-$angsuran_pokok; 

?>

<tr>                      
  <td style="width: 4px;text-align: center;"> <?php echo $no++ ?> </td>
  <td style="width: 18px;" style="text-align: center;"> <?php echo $tanggal."-".$nama_bln[$bulan]."-". $tahun ?> </td>
  <td style="text-align: right; width: 19%;"><?php echo rp($angsuran_pokok,0)?> </td>
  <td style="text-align: right; width: 19%;"><?php echo rp($angsuran_margin,0); ?> </td>       
  <td style="text-align: right; width: 19%;"><?php echo rp($jumlah,0);?> </td>
  <td style="text-align: right; width: 19%;"><?php echo rp($row,0);?> </td>
     
</tr>
      <?php    
    }
    ?>
                            
 
</table>

   
  <div class="modal-footer">
  <button type="button" id="noedit" class="tombolbwh btn btn-danger" data-dismiss="modal">Keluar</button>
  </div>
  
 </form>     
 </div>
  </div>
   </div>

</td>


<!-- ANGSURAN -->
<td style=" width: 15px;">
<a href="#" class="btn btn-primary" data-toggle="modal" data-target="#tampilkartuangsuran<?php echo $id2; ?>"><i class="fas fa-apple-alt"></i></a> 
  
  <div class="example-modal">
  <div id="tampilkartuangsuran<?php echo $id2; ?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-xl" style="max-width: 65%;">
      <!-- <div class="modal-dialog modal-lg modal-80p"> -->
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" style="padding-left: 20px;padding-bottom: 8px;height: 30px;font-size: 19;">KARTU ANGSURAN</h4>
    <!-- <button type="button" class="close" data-dismiss="modal">&times;</button> -->
      </div>
    <div class="modal-body">
                            

<form  action="" method="POST">

<?php


$id2= $dataja['id2'];

$queryjka = "SELECT a.tgl as tgl,a.rekening as rekening, a.plafond AS plafond, a.margin AS margin, a.jangkawaktu AS jangkawaktu,b.nama AS nama,b.alamat AS alamat ,DATE_ADD(a.tgl, INTERVAL a.jangkawaktu MONTH)as jatuh_tempo from pinjaman a LEFT JOIN registrasi b ON a.register=b.id where a.no='".$id2."'  ";
$queryjka  =mysqli_query($koneksi,$queryjka);
if(mysqli_num_rows($queryjka)){
while ($datajk = mysqli_fetch_assoc($queryjka)) {
extract($datajk);
}
 ?>  
<table align="left" style="height:10px; width: 96%; border: 0;"> 
<tr>
    <td  style="text-align: left; padding-left: 15px;width:44px;">Nama</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 310px;" ><?php  echo ($nama) ?></td> 
    
    <td style="= width :115px;"></td>
    <td style="text-align: left; padding-left: 15px; width: 110px;" >Tgl Realisasi</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 140px;height:5px;"><?php echo $tgl?></td>
    </tr> 

    <tr>
    <td style="text-align: left; padding-left: 15px; width: 56px;" > Alamat</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 310px;"><?php  echo  $alamat;?></td> 
    
    <td style="= width :155px;"></td>
    <td style="text-align: left; padding-left: 15px; width: 110px;" >Plafond</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 12px;"><?php echo rp($plafond,0);?></td> 
    </tr>

    <tr>
    <td style="text-align: left; padding-left: 15px; width: 56px;" > Ujroh</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 310px;"><?php  echo  $margin; ?></td> 
    
    <td style="= width :155px;"></td>
    <td style="text-align: left; padding-left: 15px; width: 110px;" >Lama</td><td style="width:4px;">:</td><td style="text-align: left; padding-left: 5px; width: 12px;"><?php  echo  $jangkawaktu?></td> 
    </tr>
</table>

<table border="1" style="padding-top: 19px;padding-bottom: 16px;width: 85%;">
<tr>
<td style="text-align: center; background: #ddd;"><b>No.</b></td>
<td style="text-align: center; background: #ddd;"><b>Tgl</b></td>
<td style="text-align: center; background: #ddd;"><b>Faktur</b></td>
<td style="text-align: center; background: #ddd;"><b>Pokok</b></td>
<td style="text-align: center; background: #ddd;"><b>Ujroh</b></td>
<td style="text-align: center; background: #ddd;"><b>Jumlah</b></td>
<td style="text-align: center; background: #ddd;"><b>Bakidebet</b></td>
<td style="text-align: center; background: #ddd;"><b>Keterangan</b></td>
</tr> 
<tr>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="right"><b><?php echo rp($plafond,0)?></b></td>
    <td align="center"></td>
</tr>
<?php
$no=1;
$whereka= "where a.no='".$id2."' ";
$sqlka = "SELECT a.tgl as atgl,c.no_bukti as ano_bukti,a.rekening as rekening, a.plafond AS plafond, a.margin AS pmargin, a.jangkawaktu AS jangkawaktu,b.nama AS nama,b.alamat AS alamat,c.pokok as apokok,c.margin as amargin,c.tgl as atgl,c.keterangan as keterangan from pinjaman a LEFT JOIN angsuran c ON a.rekening=c.rekening LEFT JOIN registrasi b ON a.register=b.id  $whereka ORDER BY c.tgl ASC    ";//order by c.tgl ASC
$sqlka  =mysqli_query($koneksi,$sqlka);
$rowka = mysqli_num_rows($sqlka); // Ambil jumlah data dari hasil eksekusi $sql
if($rowka > 0){ // Jika jumlah data lebih dari 0 (Berarti jika data ada)
$jpokok =0;
$jmargin=0;
$bakidebet=0; 
while($dataka2 = mysqli_fetch_array($sqlka)){ // Ambil semua data dari hasil eksekusi $sql

    extract($dataka2);
    $jmlangsuran    = $apokok+$amargin;
    $jpokok         +=$apokok;
    $jmargin        +=$amargin;
    $tpm            =$jpokok+$jmargin;
    $bakidebet      +=-$apokok;

?>
<tr>                      
<td  style="text-align: center;width:2%;"> <?php echo $no++ ?> </td>
<td style="text-align: center;width:9%;"> <?php echo $atgl ?> </td>
<td  style="text-align: center;width:16%;"> <?php echo $ano_bukti ?> </td>
 <td style="text-align: right; width:10%;"> <?php echo rp($apokok,0)?> </td>
<td  style="text-align: right; width:10%;"> <?php echo rp($amargin,0); ?> </td>       
<td  style="text-align: right; width:10%;"> <?php echo rp($jmlangsuran,0);?> </td>
<td  style="text-align: right; width: 10%;"> <?php echo rp($bakidebet+$plafond,0);?> </td>
<td  style="text-align: left; width: 26%;"> <?php echo $keterangan;?> </td>
</tr>
      <?php    
  }?>
                            
</table> 

   
 <!--  <div class="modal-footer">
  <button type="button" id="noedit" class="tombolbwh btn btn-danger" data-dismiss="modal">Keluar2</button>
  </div> -->
  
 </form>     
 </div>
  </div>
   </div>

                
</td>             
</tr>
                <?php }}
              }// dari if(mysqli_num_rows($query)){ pertama yg paling atas
              }// dari if(mysqli_num_rows($query)){ kedua yg paling atas
              }
              ?>
              </tbody>
              </table>

              
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>

      <!-- /.row -->
    </section>
    <!-- /.content -->


<script src="desain/jquery-ui.min.js"></script>
<script>

    $('.modal-dialog').draggable();

</script>