
<?php 
    session_start();  
    include 'header.php'; 
    $oleh   =$_SESSION['id_kar'];
    $kdc    =$_SESSION['kdc'];
?>              


<div class="container">
 <div class="my-2 border-bottom">
   <h5 class="fw-semibold fs-3">Daftar Hutang Masjid</h5>
 </div>
  <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">Silahkan klik (+) warna hijau untuk menambah transaksi angsuran</span>
      <button type="button" class="btn btn-dark btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
        <a href="@_hutang_tambah.php" class="btn btn-warning btn-sm"><i class="bi bi-plus-lg"></i> &nbsp Tambah Hutang</a> 
       <a href="index.php" class="btn btn-primary btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a> 
      </button>
    </div>
    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered table-striped" id="table-datatable">
          <thead>
             <tr>
                <th Style="text-align:center" colspan="7" bgcolor="#D3D3D3">Hutang</th>
                <th Style="text-align:center" colspan="5" bgcolor="#DCDCDC">Pembayaran Angsuran</th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Saldo Akhir</th>
                <th width="6%" Style="text-align:center" colspan="1" rowspan="2" bgcolor="#DCDCDC">Opsi</th>
             </tr>                
            <tr>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nomor</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nama</th>
              <th width="4%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Tanggal</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Pokok</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Jkw</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Angs/Bln</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">X</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Pokok</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">BaHas</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Jumlah</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Trans</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Pokok</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">BaHas</th>              
            </tr>
          </thead>
          <tbody>
            <?php 
            $no=1;
            $data = mysqli_query($koneksi,"SELECT * FROM hutang ");
            while($d = mysqli_fetch_array($data)){
                $norek=$d['norek'] ;
                $total=($d['angsuran']*$d['jkw']) ;
                $bunga=($total-$d['pokok']) ;
                
                $sql1 = mysqli_query($koneksi, "SELECT sum(byrpokok) AS tbyr_p,sum(byrbunga) AS tbyr_b FROM hutang_angs WHERE norek='$norek' ");
                list($tbyr_p,$tbyr_b) = mysqli_fetch_array($sql1);
                $saldo_p=($d['pokok']-$tbyr_p) ;
                $saldo_b=($bunga-$tbyr_b) ;
                
                $jumlah_angs=($tbyr_p+$tbyr_b);
                
                $sql2 = mysqli_query($koneksi, "SELECT count(norek) AS tnorek FROM hutang_angs WHERE norek='$norek' ");
                    list($tnorek) = mysqli_fetch_array($sql2);
                
              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d['norek'] ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d['bank']; ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d['tanggal']; ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[pokok]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[jkw]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[angsuran]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$kali", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$tbyr_p", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$tbyr_b", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$jumlah_angs", 0, ",", "."); ?></td>
                <td>    
                  <a data-toggle='tooltip' data-placement='top' title='Tambah Trans ANGSURAN pada <?php echo $d['perk'] ?>' style='margin-right:3px' class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#trans_angs<?php echo $d['norek'] ?>"><i class="bi bi-plus-square"></i></a>
                </td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$saldo_p", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$saldo_b", 0, ",", "."); ?></td>
                <td>    
                    <a data-toggle='tooltip' data-placement='top' title='Buka Data Trans <?php echo $d['nama'] ?>' class="btn btn-primary btn-sm" href="@_hutang_view_trans_per_no.php?id=<?php echo $d['norek'] ?>"><i class="bi bi-eye"></i></a> 
                <?php
                    if ($tnorek<=0) { ?>
                          <a onclick="return confirm('Yakin ingin hapus transaksi PENERIMAAN hutang <?php echo $d['norek'] ?>.....?')" class="btn btn-danger btn-sm" href="@_hutang_pokok_hapus.php?id=<?php echo $d['notrans'] ?>&oleh=<?php echo $oleh ?>">
                            <i class="bi bi-trash"></i>
                          </a>
                    <?php } ?>

                    
                  <form action="@_hutang_angsuran_act.php" method="post">
                    <div class="modal fade" id="trans_angs<?php echo $d['norek'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-header">
                            <h1 class="modal-title fs-5" id="exampleModalLabel">Bayar ANGSURAN ke <?php echo $d['bank'] ?></h1>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                          </div>
                          <div class="modal-body">
<div style="background-color: <?php echo$_SESSION['warna_bag']; ?>; padding: 15px; border-radius: 15px; box-shadow: 0 0 15px <?php echo$_SESSION['warna_bag']; ?>;">                              
                            <?php
                                $angs_pokok=($d['pokok']/$d['jkw']) ;
                                $angs_bunga=($d['angsuran']-$angs_pokok) ;
                            ?>
                            <div class="form-group" style="width:100%">
                              <label>Nomor Kode Hutang</label>
                              <input type="text" name="tnomor" readonly="readonly" class="form-control" value="<?php echo $d['norek']; ?>">
                              <input type="hidden" name="toleh" readonly="readonly" class="form-control" value="<?php echo $oleh; ?>" style="width:100%">
                              <input type="text" name="tnama" readonly="readonly" class="form-control" value="<?php echo $d['bank'] ?>" style="width:100%">
                              <input type="text" name="talamat" readonly="readonly" class="form-control" value="<?php echo $d['alamat'] ?>" style="width:100%">
                              <input type="hidden" name="tkdc" readonly="readonly" class="form-control" value="<?php echo $kdc; ?>" style="width:100%">
                            </div>
                            <div class="form-group">
                              <label>Angsuran Pokok <?php echo "Rp. " . number_format("$angs_pokok", 0, ",", "."); ?></label>
                              <input type="text" class="form-control nilai rupiah" id="nilai" name="tpokok" value="<?php echo "" . number_format("$angs_pokok", 0, ",", "."); ?>" required="required" placeholder="Masukkan nominal ..">
                            </div>         
                            <div class="form-group">
                              <label>Angsuran BaHas <?php echo "Rp. " . number_format("$angs_bunga", 0, ",", "."); ?> </label>
                              <input type="text" class="form-control nilai rupiah" id="nilai" name="tbunga" value="<?php echo "" . number_format("$angs_bunga", 0, ",", "."); ?>" required="required" placeholder="Masukkan nominal ..">
                            </div>                                     
                          <div class="form-group mb-2">
                            <label class="form-label">Tanggal Bayar</label> 
                            <input type="date" class="form-control" name="ttgl" required="required" value="<?php echo date('d/m/Y', strtotime($d['tglmasuk'])); ?>">
                          </div>   
                        <div class="form-group mb-2">
                                <label class="form-label">Angsuran dikreditkan dari <font color='#ff0000'><i>(pilih akunt)</i></font color></label>
                                <select class="form-control" name="takunt" required="required">
                                  <option value="">- Pilih Akunt Lawan -</option>
                                  <?php 
                                  $nawal = mysqli_query($koneksi,"select * from nawal Where nosub<'113' order by perk ASC");
                                  while($rx = mysqli_fetch_array($nawal)){
                                    ?>
                                    <option value="<?php echo $rx['noperk'] ?>"><?php echo $rx['noperk'] ?> - <?php echo $rx['perk'] ?></option>
                                    <?php 
                                  }
                                  ?>
                                </select>
                        </div>                                           
                          </div>
                          <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                          </div>
                        </div>
                      </div>
                    </div>
</div>                    
                  </form>
                    </td>
                    </tr>                  
            <?php };?>  
          </tbody>
        </table>

      </div>
    </div>

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

<?php include 'foot_ku.php'; ?>
