<?php
include '../../config/conn.php';
$date = date('Y-m-d');
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=Laporan DAM RT Pagi $date.xls");
?>
<table id="datatable" border="1" class="table table-bordered">
    <thead>
        <tr>
            <td colspan="10" align="center" style="font-size: x-large;">LAPORAN DAM RT Pagi <?php echo $date; ?></td>
        </tr>
    </thead>
    <thead>
        <tr>
            <!--<th>Tanggal</th>-->
            <!--<th>id transaksi</th> -->
            <th width="15px">NO.</th>
            <th>Id Santri</th>
            <th>Nama Santri</th>
            <th>Kelas</th>
            <th>Tanggal masuk</th>
            <th>Pembayaran</th>
            <th>Total Bayar</th>
            <th>Pembayaran</th>
            <th>Sisa</th>
            <th width="100">Status</th>

        </tr>
    </thead>
    <tbody>
        <?php
        $no = 0;
        $count = 1;
        $total_rekap = 0;

        //sorting data per angkatan
        if (!isset($_GET['angkatan']) or $_GET['angkatan'] == 0) {
            $angkatan = "";
            $angkatanall = "";
        } else {
            $angkatan = "AND YEAR(tanggal_masuk)=$_GET[angkatan]";
            //$angkatanall untuk menampilkan seluruh data
            $angkatanall = "WHERE YEAR(tanggal_masuk)=$_GET[angkatan]";
        }

        //sorting data per status lunas
        if ($_GET['tampildata'] == 1) {
            //data lunas
            $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri IN(SELECT id_santri from status_lunas WHERE id_jenis='18') $angkatan
            AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') order by nama ASC ");
        } elseif ($_GET['tampildata'] == 2) {
            //bayar cicil
            $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri IN(SELECT id_santri from transaksi WHERE id_jenis='18') 
            AND id_santri NOT IN (SELECT id_santri from status_lunas WHERE id_jenis='18') $angkatan 
            AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') order by nama ASC ");
        } elseif ($_GET['tampildata'] == 3) {
            //belum bayar                 
            $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri NOT IN(SELECT id_santri from status_lunas) 
            AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') 
            AND id_santri NOT IN (select id_santri from transaksi WHERE id_jenis='18') $angkatan order by nama ASC ");
        } else {
            //semua data
            $query = mysqli_query($conn, "SELECT * FROM santri_rt $angkatanall 
            WHERE jenis_rt IN(SELECT id FROM jenis_rt WHERE kategori='1') order by nama ASC ");
        }


        $ro = 0;




        while ($row = mysqli_fetch_array($query)) {

            $no++;
        ?>

            <tr style="background: <?php if ($no % 2 == 1) { ?>
                          #ececec;
                          <?php } else { ?>
                            white;
                            <?php } ?>">

                <!--<td><?php //echo $row['id_transaksi'];
                        ?></td>-->
                <td><?php echo $no; ?></td>
                <td><?php echo $row['id_santri']; ?></td>
                <td><?php echo $row['nama']; ?></td>
                <td><?php $kueri  = mysqli_query($conn, "SELECT * FROM jenis_rt WHERE id='$row[jenis_rt]' order by id asc");
                    while ($data = mysqli_fetch_array($kueri)) {
                        echo "$data[nama_jenis]";
                    } ?></td>
                <td><?php echo $row['tanggal_masuk'];  ?></td>
                <td><?php
                    $keri = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_santri='$row[id_santri]' AND id_jenis='18' order by tanggal ASC ");
                    while ($ri = mysqli_fetch_array($keri)) {
                        $tgl = date('d/m/y', strtotime($ri['tanggal']));
                        echo "[" . $tgl . "~" . $ri['debit'] . "]<br>";
                    }
                    ?></td>

                <td><?php
                    $total = mysqli_query($conn, "SELECT sum(debit) AS total_bayar FROM transaksi WHERE id_santri='$row[id_santri]' AND id_jenis='18'order by tanggal ASC ");
                    $total1 = mysqli_fetch_array($total);
                    echo $total1['total_bayar'];
                    ?></td>
                <td><?php echo $row['biaya_dam'];  ?></td>
                <td><?php $sisa = $row['biaya_dam'] - $total1['total_bayar'];
                    echo $sisa;  ?></td>
                <td><?php
                    $tet1 = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri='$row[id_santri]' AND id_jenis='18'");
                    $tet = mysqli_fetch_array($tet1);
                    if (isset($tet['id_santri'])) {
                        echo "<font color=#0082da><b>lunas</b></font>";
                    } else {
                        echo "<font color=#f01519><b>Belum Lunas</b></font>";
                    }

                    ?></td>




                <?php
                $count++

                ?>

            </tr>

        <?php } ?>





    </tbody>
</table>
<br><br><br><br>
Data <?php echo date('d-m-Y') . "  " . date('H:i:s'); ?>