<!-- tagihan -->
<div class="x_title" style="text-transform: capitalize;">
    <h2>Data Tagihan</h2>
    <div class="clearfix"></div>
</div>

<div class="accordion" id="accordionExample">

    <div class="card">
        <div class="card-header" id="headingTwo">
            <h5 class="mb-0">
                <button type="button" class="collapsible" data-toggle="collapse" data-target="#collapseTwo">TAGIHAN DAFTAR AWAL MASUK</button>
            </h5>
        </div>
        <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
            <div class="card-body ">
                <table class="Projects table table-responsive table-striped table-bordered dataTable " id="datatable">
                    <thead>
                        <tr>
                            <th width="10px">No</th>
                            <th width="110px">Jenis</th>


                            <th>dibayar</th>
                            <th>total</th>
                            <th>Pembayaran</th>
                            <th>Kekurangan</th>
                            <th>Status</th>

                        </tr>
                    </thead>
                    <!-- dam -->
                    <tbody style="background-color:lightgoldenrodyellow;">
                        <tr>
                            <td>1</td>
                            <td>DAM</td>
                            <td><?php
                                $tot = 0;
                                $dam1 = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_santri=$r[id_santri] AND id_jenis='14' order by tanggal DESC ");
                                while ($dam = mysqli_fetch_array($dam1)) {
                                    $tgl = date('d/m/y', strtotime($dam['tanggal']));
                                    echo "[" . $tgl . "~" . rupiah($dam['debit']) . "~" . jenkas($dam['jenis_kas']) . "]<br>";
                                    $tot = $tot + $dam['debit'];
                                }
                                $sisa = $r['biaya_dam'] - $tot;
                                ?></td>

                            <td><?php echo rupiah($tot); ?></td>
                            <td><?php echo rupiah($r['biaya_dam']); ?></td>
                            <td><?php echo rupiah($sisa); ?></td>
                            <td>
                                <?php $cekstatus = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri=$r[id_santri] AND id_jenis='14'");
                                $status = mysqli_fetch_array($cekstatus);

                                if ($status > 0) {
                                    echo "<font color=#0082da><b>lunas</b></font>";
                                } else {
                                    echo "<font color=#f01519><b>Belum Lunas</b></font>";
                                }


                                ?>

                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header" id="headingThree">
            <h5 class="mb-0">
                <button type="button" class="collapsible" data-toggle="collapse" data-target="#collapseThree">TAGIHAN SPP</button>
            </h5>
        </div>
        <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
            <div class="card-body">
                <table class="Projects table table-responsive table-striped table-bordered dataTable " id="datatable">
                    <thead>
                        <tr>
                            <th width="10px">No</th>
                            <th width="110px">Jenis</th>
                            <th>Bulan</th>
                            <th>Tahun</th>
                            <!--<th>No Rekening</th>-->
                            <th>dibayar</th>
                            <th>Total</th>
                            <th>Pembayaran</th>
                            <th>Kekurangan</th>
                            <th>Status</th>

                        </tr>
                    </thead>
                    <tbody style="background-color:lightgoldenrodyellow;">

                        <?php

                        //sort tagihan

                        $thsantri = date('Y', strtotime($r['tanggal_masuk']));
                        $blnsantri = date('n', strtotime($r['tanggal_masuk']));
                        $thini = date('Y');

                        $t1 = $thsantri;
                        $t2 = date('Y');
                        $n = 0;


                        for ($t2; $t2 >= $t1; $t2--) {
                            if ($thsantri == date('Y')) {
                                $b2 = date('n');
                                $b1 = $blnsantri+1;
                            } else {
                                if ($t2 > $t1 && $t2 != $thini) {
                                    $b2 = 12;
                                    $b1 = 1;
                                } elseif ($t2 == $thini) {
                                    $b2 = date('n');
                                    $b1 = 1;
                                } elseif ($t2 == $t1) {
                                    $b2 = 12;
                                    $b1 = $blnsantri+1;
                                }
                            }

                            for ($b2; $b2 >= $b1; $b2--) {
                                $n++;
                        ?>
                                <!-- spp -->
                                <tr>
                                    <td><?php echo $n; ?></td>
                                    <td>SPP</td>
                                    <td><?php echo namabulan($b2); ?></td>
                                    <td><?php echo $t2; ?></td>
                                    <td><?php
                                        $tot = 0;
                                        $keri = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_santri='$id' AND id_jenis='5' AND spp_bulan=$b2 AND spp_tahun=$t2 order by tanggal DESC ");
                                        while ($ri = mysqli_fetch_array($keri)) {
                                            $tgl = date('d/m/y', strtotime($ri['tanggal']));
                                            echo "[" . $tgl . "~" . rupiah($ri['debit']) . "~" . jenkas($ri['jenis_kas']) . "]<br>";
                                            $tot = $tot + $ri['debit'];
                                        }
                                        $sisa = $r['biaya_spp'] - $tot;
                                        ?></td>
                                    <td><?php echo rupiah($tot); ?></td>
                                    <td><?php echo rupiah($r['biaya_spp']); ?></td>
                                    <td><?php echo rupiah($sisa); ?></td>
                                    <td>
                                        <?php $cekstatus = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri='$id' AND id_jenis='5' AND spp_bulan=$b2 AND spp_tahun=$t2");
                                        $status = mysqli_fetch_array($cekstatus);

                                        if ($status > 0) {
                                            echo "<font color=#0082da><b>lunas</b></font>";
                                        } else {
                                            echo "<font color=#f01519><b>Belum Lunas</b></font>";
                                        }


                                        ?>
                                    </td>
                                </tr>

                        <?php

                            }
                            // $b2 = 12;
                        }

                        ?>
                    </tbody>

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

<div class="card">
    <div class="card-header" id="headingThree">
        <h5 class="mb-0">
            <button type="button" class="collapsible" data-toggle="collapse" data-target="#collapseone">TAGIHAN DAFTAR ULANG TAHUNAN</button>
        </h5>
    </div>
    <div id="collapseone" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
        <div class="card-body">

            <table class="Projects table table-responsive table-striped table-bordered dataTable " id="datatable">
                <thead>
                    <tr>
                        <th width="10px">No</th>
                        <th width="110px">Jenis</th>

                        <th>Tahun</th>
                        <!--<th>No Rekening</th>-->
                        <th>dibayar</th>
                        <th>Total</th>
                        <th>Pembayaran</th>
                        <th>Kekurangan</th>
                        <th>Status</th>

                    </tr>
                </thead>
                <tbody style="background-color:lightgoldenrodyellow;">

                    <?php

                    $th1 = date('Y', strtotime($r['tanggal_masuk']));
                    $th2 = date('Y');
                    $n = 0;


                    for ($th2; $th2 >= $th1; $th2--) {
                        $n++;
                    ?>
                        <!-- spp -->

                        <tr>
                            <td><?php echo $n; ?></td>
                            <td>DUT</td>
                            <td><?php echo $th2; ?></td>
                            <td><?php
                                $tot = 0;
                                $dut1 = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_santri=$r[id_santri] AND id_jenis='17' AND dut_tahun=$th2 order by tanggal DESC ");
                                while ($dut = mysqli_fetch_array($dut1)) {
                                    $tgl = date('d/m/y', strtotime($dut['tanggal']));
                                    echo "[" . $tgl . "~" . rupiah($dut['debit']) . "~" . jenkas($dut['jenis_kas']) . "]<br>";
                                    $tot = $tot + $dut['debit'];
                                }
                                $sisa = $r['biaya_dut'] - $tot;
                                ?></td>
                            <td><?php echo rupiah($tot); ?></td>
                            <td><?php echo rupiah($r['biaya_dut']); ?></td>
                            <td><?php echo rupiah($sisa); ?></td>
                            <td>
                                <?php $cekstatus = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri=$r[id_santri] AND id_jenis='17' AND  dut_tahun=$th2");
                                $status = mysqli_fetch_array($cekstatus);

                                if ($status > 0) {
                                    echo "<font color=#0082da><b>lunas</b></font>";
                                } else {
                                    echo "<font color=#f01519><b>Belum Lunas</b></font>";
                                }


                                ?>
                            </td>
                        </tr>

                    <?php
                    }

                    ?>

                </tbody>

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