
<div class="container">
  <div class="row justify-content-center">
    <div class="col-lg-12">
       <div class="card">
    <div class="card-header d-flex justify-content-between">
      <span class="card-title fw-semibold align-self-start mb">DATA PINJAMAN</span>
      <buton>
        <a href="index.php" class="btn btn-primary btn-sm"><i class="bi bi-house-fill"></i> &nbsp Home</a> 
        <?php if ($_SESSION['level']<>'ANGGOTA') { ?>
            <a href="javascript:history.go(-1)" class="btn btn-info btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a>
        <?php } ?>
      </buton>
    </div>
      <div class="table-responsive">
        <table class="table table-bordered table-striped" id="table-datatable1">
          <thead>
            <tr>
                      <th width="1%" class="text-center">NO</th>
                      <th width="3%" class="text-center">NoRek</th>
                      <th width="3%" class="text-center">Jenis</th>
                      <th width="12%" class="text-center">Tanggal</th>
                      <th width="10%" class="text-center">Plafond</th>
                      <th width="2%" class="text-center">X</th>
                      <th width="2%" class="text-center">%</th>
                      <th width="10%" class="text-center">Penarikan</th>
                      <th width="10%" class="text-center">Byr Pokok</th>
                      <th width="10%" class="text-center">Byr Bunga</th>
                      <th width="10%" class="text-center">Saldo</th>
                      <th width="1%" class="text-center">BUKA</th>
            </tr>
          </thead>
          <tbody>
            <?php 
            $no=1;
            // and pinj_hari=$hari_Indonesia
                $data = mysqli_query($koneksi,"SELECT * FROM pinjaman WHERE angg_no='$noang' ");
            while($d = mysqli_fetch_array($data)){
                $noang      =$d['angg_no'];
                $norek      =$d['norek'];
                $tgl_cair   =$d['tanggal'] ;
                $pokok      =$d['pokok']+$d['plafond'];
                $jasa       =$d['jasa'];
                $jkw        =$d['jk'];
                $kds        =$d['kds'];
                
                $angs       =($pokok+$jasa)/$jkw ;

                $sql2 = mysqli_query($koneksi, "SELECT count(norek) AS tangs,sum(byrpokok) AS tbyrp,sum(byrbunga) AS tbyrb,sum(byrlain) AS tbyrl,sum(trkpokok+trkbunga+trklain) AS trk_all FROM pinj_angs WHERE norek='$norek'");
                list($tangs,$tbyrp,$tbyrb,$tbyrl,$trk_all) = mysqli_fetch_array($sql2);
                
                $sql3 = mysqli_query($koneksi, "SELECT nama AS jnama,keterangan AS jket FROM kdsektor WHERE kds='$kds' ");
                list($jnama,$jket) = mysqli_fetch_array($sql3);
                
                if ($kds=='01') {
                    $jumlah_tarik=$trk_all ;
                    $sisa=($jumlah_tarik)-($tbyrp+$tbyrb+$tbyrl) ;
                } else {
                    $jumlah_tarik=$d['pokok'] ;
                    $sisa=($pokok)-$tbyrp ;
                }

                $usia_byr=$tbyrjml/$angs ;


/////////////       MENGHITUNG KOLETIFIBILITAS PINJAMAN         ///////////////
                
              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td> <a title='Lihat detail data <?php echo $d['pinj_no'] ?>' data-bs-toggle="modal" data-bs-target="#edit_<?php echo $d['pinj_no'] ?>"> <font color='#0000FF'><?php echo $d['norek'] ?></font></td>
                <td><?php echo $jnama; ?></td>
                <td style="text-align:center; font-size: 12pt; font-weight:normal;"><?php echo date('d-m-Y', strtotime($tgl_cair)); ?></td>
                <td align='right'><?php echo "" . number_format("$pokok", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$d[jk]", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$d[jasa]", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$jumlah_tarik", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$tbyrp", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$tbyrb", 0, ",", "."); ?></td>
                <td align='right'><?php echo "" . number_format("$sisa", 0, ",", "."); ?></td>
                <td>    
                  <a data-toggle='tooltip' data-placement='top' title='Kartu Angsuran <?php echo $norek ?> a/n <?php echo $angg_nama; ?>' style='margin-right:3px' class="btn btn-success btn-sm" href="a1_pinj_kart_per_norek.php?id=<?php echo $norek ?>&noang=<?php echo $noang ?>"><i class="bi bi-text-indent-left"></i></a>
                </td>    
              </tr>
              <?php 
            }
            ?>
          </tbody>
                        <?php 
                            $sql7 = mysqli_query($koneksi, "SELECT sum(pokok+plafond) AS tpiut FROM pinjaman WHERE angg_no='$noang'");
                            list($tpiut) = mysqli_fetch_array($sql7);
                            
                            $sql8 = mysqli_query($koneksi, "SELECT sum(byrpokok) AS tbyrpkk,sum(byrbunga) AS tbyrbng,sum(trkpokok+trkbunga+trklain) AS ttarik FROM pinj_angs WHERE angg_no='$noang' ");
                            list($tbyrpkk,$tbyrbng,$ttarik) = mysqli_fetch_array($sql8);
                            
                            if ($kds=='01') {
                                $tt_tarik=$ttarik;
                                $saldo_skr=($ttarik-($tbyrpkk+$tbyrbng)) ;
                            } else {
                                $tt_tarik=$tpiut;
                                $saldo_skr=($tpiut-$tbyrpkk) ;
                            }                            
                        ?>
							<tr>
						    	<td colspan="4" style="font-weight: bold;text-align: center;">Total Pinjaman</td>																																									
								</td>	
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tpiut", 0, ",", "."); ?>
								</td>	
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" ; ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" ; ?>
								</td>    								
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tt_tarik", 0, ",", "."); ?>
								</td>
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tbyrpkk", 0, ",", "."); ?>
								</td>    								
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tbyrbng", 0, ",", "."); ?>
								</td>
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$saldo_skr", 0, ",", "."); ?>
								</td>
							</tr>          

        </table>
      </div>
    </div>
  </div>
</div>
        