<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
    $jenis = $_GET['type'];

?>
    <!-- page content -->
    <div class="col" role="main">
        <div class="">
            <div class="clearfix"></div>

            <div class="row">
                <div class="col-md-12 col-md-12 col-sm-12 col-xs-12 ">
                    <div class="x_panel">
                        <div class="x_title" style="text-transform: capitalize;">

                            <?php if ($_GET['periode'] == 'y') {
                                $bln = $_POST['bulan'];
                                $thn = $_POST['tahun'];
                            } elseif ($_GET['periode'] == 'x') {
                                $bln = $_GET['bln'];
                                $thn = $_GET['thn'];
                            } else {
                                $bln = date('n');
                                $thn = date('Y');
                            }

                            ?>
                            <h2>Laporan Tagihan Donatur Tetap Bulan <b><?php echo namabulan($bln); ?></b> tahun <b><?php echo $thn; ?></b> <small></small></h2>

                            <div class="clearfix"></div>



                            <form action="?module=pembayaran-donatur-tetap&periode=y" id="form_periode" enctype="multipart/form-data" method="POST">


                                <div class="well" style="overflow: auto">

                                    <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                                        <label for="nama">Pilih Periode :</label>
                                    </div>

                                    <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">

                                        <div class="input-group">
                                            <div class="input-group-addon">Bulan</div>
                                            <select id="heard" class="form-control" name="bulan" required onChange="document.getElementById('form_periode').submit();">

                                                <?php
                                                $query  = mysqli_query($conn, "SELECT * FROM bulan order by id_bulan asc");
                                                while ($r = mysqli_fetch_array($query)) { ?>
                                                    <option value="<?php echo $r['id_bulan']; ?>" <?php if ($r['id_bulan'] == $bln) {
                                                                                                        echo "selected";
                                                                                                    } ?>><?php echo $r['nama_bulan']; ?></option>
                                                <?php
                                                }
                                                ?>
                                            </select>

                                            <div class="input-group-addon">Tahun</div>
                                            <select id="heard" class="form-control" name="tahun" required onChange="document.getElementById('form_periode').submit();">

                                                <?php

                                                for ($tahun = 2019; $tahun <= 2030; $tahun++) {
                                                ?>

                                                    <option value="<?php echo $tahun; ?>" <?php if ($tahun == $thn) {
                                                                                                echo "selected";
                                                                                            } ?>><?php echo $tahun; ?></option>
                                                <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">


                            </form>


                        </div>
                        <?php
                        if (isset($_GET['periode']) and $_GET['periode'] == "y") {
                            $link = "?module=pembayaran-donatur-tetap&periode=x&bln=$bln&thn=$thn";
                        } elseif (isset($_GET['periode']) and $_GET['periode'] == "x") {
                            $link = "?module=pembayaran-donatur-tetap&periode=x&bln=$bln&thn=$thn";
                        } else {
                            $link = "?module=pembayaran-donatur-tetap";
                        }
                        ?>

                        <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                            <form action="<?php echo $link; ?>" id="form_id" enctype="multipart/form-data" method="POST">

                                <div class="col-md-3">
                                    <div class="input-group">
                                        <div class="input-group-addon">Status</div>
                                        <select id="heard" class="form-control" required name="tampildata" onChange="document.getElementById('form_id').submit();">
                                            <option value="0">Semua data</option>
                                            <option value="1" <?php if ($_POST['tampildata'] == 1) {
                                                                    echo "selected";
                                                                } ?>>Lunas</option>
                                            <option value="2" <?php if ($_POST['tampildata'] == 2) {
                                                                    echo "selected";
                                                                } ?>>Bayar belum lunas</option>
                                            <option value="3" <?php if ($_POST['tampildata'] == 3) {
                                                                    echo "selected";
                                                                } ?>>Belum bayar</option>
                                        </select>
                                    </div>

                                    <!-- eksport -->
                                    <br>
                                    <?php
                                    //sort lunas
                                    if (isset($_POST['tampildata'])) {
                                        if ($_POST['tampildata'] == 0) {
                                            $sort_lunas = "&tampildata=0";
                                        } elseif ($_POST['tampildata'] == 1) {
                                            $sort_lunas = "&tampildata=1";
                                        } elseif ($_POST['tampildata'] == 2) {
                                            $sort_lunas = "&tampildata=2";
                                        } elseif ($_POST['tampildata'] == 3) {
                                            $sort_lunas = "&tampildata=3";
                                        }
                                    } else {
                                        $sort_lunas = "&tampildata=0";
                                    }




                                    ?>

                                    <a href="component/com_pembayaran_donatur/export_donatur_tetap.php<?php echo $link . $sort_lunas; ?>" class='open_modal btn  btn-primary btn-xs'>Eksport</a>




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








                        <div class="x_content">

                            <table id="datatable" class="table table-bordered">
                                <thead>
                                    <tr>
                                        <!--<th>Tanggal</th>-->
                                        <!--<th>id transaksi</th> -->
                                        <th width="15px">NO.</th>
                                        <th>Id Donatur</th>
                                        <th>Nama Donatur</th>
                                        <th>Dibayar</th>
                                        <th>Total Bayar</th>
                                        <th>Pembayaran</th>
                                        <th>Sisa</th>
                                        <th width="100">Status</th>
                                        <th width="100">Aksi</th>
                                        <th width="100">Bayar</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $count = 1;
                                    $total_rekap = 0;

                                    $hari_ini = "$thn-$bln-01";
                                    $tgl_pertama = date('Y-m-01', strtotime($hari_ini));
                                    $tgl = date('Y-m-t', strtotime($hari_ini));




                                    //sorting data per jenis status lunas
                                    if ($_POST['tampildata'] == 1) {
                                        $query = mysqli_query($conn, "SELECT * FROM donatur WHERE id_donatur IN(SELECT id_donatur FROM status_lunas_donatur WHERE bulan='$bln' AND tahun='$thn') order by nama_donatur ASC ");
                                    } elseif ($_POST['tampildata'] == 2) {
                                        $query = mysqli_query($conn, "SELECT * FROM donatur WHERE id_donatur IN(SELECT id_donatur FROM transaksi WHERE spp_bulan='$bln' AND spp_tahun='$thn')
                            AND id_donatur NOT IN(SELECT id_donatur FROM status_lunas_donatur WHERE bulan='$bln' AND tahun='$thn') order by nama_donatur ASC ");
                                    } elseif ($_POST['tampildata'] == 3) {
                                        $query = mysqli_query($conn, "SELECT * FROM donatur WHERE id_donatur NOT IN(SELECT id_donatur FROM transaksi WHERE spp_bulan='$bln' AND spp_tahun='$thn')
                            AND id_donatur NOT IN(SELECT id_donatur FROM status_lunas_donatur WHERE bulan='$bln' AND tahun='$thn') order by nama_donatur ASC ");
                                    } else {
                                        $query = mysqli_query($conn, "SELECT * FROM donatur order by nama_donatur 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_donatur']; ?></td>
                                            <td><?php echo $row['nama_donatur']; ?></td>
                                            <td><?php
                                                $keri = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_donatur='$row[id_donatur]' AND id_jenis='10' AND spp_bulan='$bln' AND spp_tahun='$thn' order by tanggal ASC ");
                                                while ($ri = mysqli_fetch_array($keri)) {
                                                    $tgl = date('d/m/y', strtotime($ri['tanggal']));
                                                    echo "[" . $tgl . "~" . rupiah($ri['debit']) . "]<br>";
                                                }
                                                ?></td>

                                            <td><?php
                                                $total = mysqli_query($conn, "SELECT sum(debit) AS total_bayar FROM transaksi WHERE id_donatur='$row[id_donatur]' AND id_jenis='10' AND spp_bulan='$bln' AND spp_tahun='$thn' order by tanggal ASC ");
                                                $total1 = mysqli_fetch_array($total);
                                                echo rupiah($total1['total_bayar']);
                                                ?></td>
                                            <td><?php echo rupiah($row['biaya_donasi']);  ?></td>
                                            <td><?php $sisa = $row['biaya_donasi'] - $total1['total_bayar'];
                                                echo rupiah($sisa);  ?></td>
                                            <td><?php
                                                $tet1 = mysqli_query($conn, "SELECT * FROM status_lunas_donatur WHERE id_donatur='$row[id_donatur]' AND bulan='$bln' AND tahun='$thn' ");
                                                $tet = mysqli_fetch_array($tet1);
                                                if (isset($tet['id_donatur'])) {
                                                    echo "<font color=#0082da><b>lunas</b></font>";
                                                } else {
                                                    echo "<font color=#f01519><b>Belum Lunas</b></font>";
                                                }

                                                ?></td>
                                            <td>
                                                <?php
                                                if (isset($tet['id_donatur'])) {
                                                    echo "<a class=\"btn  btn-success btn-xs\"  onclick=\"confirm_modal('?module=pembayaran-donatur-tetap&aksi=ubahstatus&bln=$bln&thn=$thn&id=$row[id_donatur]');\">Batal Lunas</a>";
                                                } else {
                                                    echo "<a class=\"btn  btn-danger btn-xs\"  onclick=\"confirm_modal('?module=pembayaran-donatur-tetap&aksi=ubahstatus&bln=$bln&thn=$thn&id=$row[id_donatur]');\">Lunas</a>";
                                                } ?>
                                            </td>
                                            <td>
                                                <a class='open_modal btn  btn-primary btn-xs' href="?module=pilih-donatur&id=<?php echo $row['id_donatur']; ?>"><i class="glyphicon glyphicon-edit"></i> Bayar</a>
                                            </td>




                                            <?php
                                            $count++

                                            ?>

                                        </tr>

                                    <?php } ?>





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

            </div>
        </div>
    </div>
    <!-- /page content --><br>





<?php } elseif ($_GET['aksi'] == "ubahstatus") {
    $tet1 = mysqli_query($conn, "SELECT * FROM status_lunas_donatur WHERE id_donatur='$_GET[id]' AND bulan='$_GET[bln]' AND tahun='$_GET[thn]' ");
    $tet = mysqli_fetch_array($tet1);
    if (isset($tet['id_donatur'])) {
        mysqli_query($conn, "DELETE from status_lunas_donatur WHERE id_donatur='$_GET[id]' AND bulan='$_GET[bln]' AND tahun='$_GET[thn]' ");
    } else {
        mysqli_query($conn, "INSERT INTO status_lunas_donatur(id_donatur,
    bulan,
    tahun
    ) VALUES('$_GET[id]',
                   '$_GET[bln]',
                   '$_GET[thn]')");
        //echo "hore";
    }

    echo "<script language='javascript'>document.location='?module=pembayaran-donatur-tetap&periode=x&bln=$_GET[bln]&thn=$_GET[thn]';</script>";
}
?>
<div class="modal fade" id="modal_delete">
    <div class="modal-dialog">
        <div class="modal-content" style="margin-top:100px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" style="text-align:center;">Ubah status lunas ?</h4>
            </div>
            <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
                <a href="#" class="btn btn-danger btn-sm" id="delete_link">Ubah</a>
                <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
            </div>
        </div>
    </div>
</div>