<?php
include '../../config/conn.php';
$jenis = $_POST['jenis_kas'];

if ($_GET['periode'] == 'y') {
    $tanggal1 = $_POST['tanggal1'];
    $tanggal2 = $_POST['tanggal2'];
    $tgl1 = date('Y-m-d', strtotime($tanggal1));
    $tgl2 = date('Y-m-d', strtotime($tanggal2));

    if ($jenis == '') {
        $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE tanggal BETWEEN '$tgl1' AND '$tgl2'");
    } else {
        $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE tanggal BETWEEN '$tgl1' AND '$tgl2' AND jenis_kas=$jenis ");
    }
} else {
    if ($jenis == '') {
        $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_transaksi='1'");
    } else {
        $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE jenis_kas='$jenis' AND id_transaksi='1'");
    }
}
$row_saldo = mysqli_fetch_array($query_saldo);
$saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];


?>

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

        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title" style="text-transform: capitalize; margin:0;">
                        <h2>Data Transaksi <?php
                                            $query = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$jenis'");

                                            while ($row = mysqli_fetch_array($query)) {
                                                echo $row['nama'];
                                            }

                                            if ($jenis == '') {
                                                echo "Keseluruhan";
                                            }

                                            ?></h2>

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


                        <form action="?module=perkas&periode=y" 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 ml-auto col-sm-12 col-xs-12" style="padding: 4px;">

                                    <div class="input-group">
                                        <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                                        <input type="text" autocomplete="off" class="form-control " id="tanggal" required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                                        echo $tanggal1;
                                                                                                                                    } else {
                                                                                                                                        echo "";
                                                                                                                                    } ?>" name="tanggal1">
                                        <div class="input-group-addon" style="background:white;"><small>Sampai</small></i></div>
                                        <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                                        <input type="text" autocomplete="off" class="form-control " required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                        echo $tanggal2;
                                                                                                                    } else {
                                                                                                                        echo "";
                                                                                                                    } ?>" id="tanggal2" name="tanggal2">

                                    </div>
                                    <input type="hidden" value="<?php echo $jenis; ?>" name="jenis_kas">
                                    <button type="submit" class="btn btn-primary btn-sm">Pilih</button>




                                </div>






                        </form>





                        <div class="col-md-3 offset-md-3 col-sm-12 col-xs-12" style="margin-left: 0px;">

                            <h4>
                                <table>
                                    <tr style="color:green">
                                        <td><small><b>TOTAL DEBIT </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo rupiah($row_saldo['jumlah_debit']); ?></td>
                                    </tr>
                                    <tr style="color:red">
                                        <td id="hit"><small><b>TOTAL KREDIT </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo rupiah($row_saldo['jumlah_kredit']); ?></td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" height="2px" bgcolor="black"></td>
                                    </tr>
                                    <tr>
                                        <td id="hit"><small><b>TOTAL </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo rupiah($saldo_keseluruhan); ?></td>
                                    </tr>
                                </table>
                            </h4>

                        </div>

                        <div class="col-md-12 col-sm-12 col-xs-12" style="margin-left: 0px;">
                            <!-- CETAK -->
                            <a class="btn btn-primary btn-sm" href="component/tampil_data_export.php?jenis_kas=<?php echo $jenis; ?>&<?php if ($_GET['periode'] == 'y') {
                                                                                                                                            echo "periode=y&tanggal1=$tgl1&tanggal2=$tgl2";
                                                                                                                                        } else {
                                                                                                                                            echo "periode=";
                                                                                                                                        } ?>"><i class="fa fa-print"></i>cetak</a>
                            <a href="./" class="btn btn-default btn-sm">kembali</a>

                        </div>

                        <div class="col-md-12 col-sm-12 col-xs-12" style="margin-left: 0px;">
                           <?php
                           if ($_GET['periode']=='y'){

                           } else{
                            echo "<div style='color:red;font-weight:bold;font-style:underline'>PILIH PERIODE DAHULU</div>";
                           }
                           ?>
                        </div>


                        <div class="x_content">

                            <table style="color:black;" id="datatable" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th width="20">Tipe</th>
                                        <th>Kode transaksi</th>
                                        <th>Tanggal</th>
                                        <th>Jenis Sumber Tran.</th>
                                        <th>
                                            Jenis Transaksi
                                        </th>
                                        <?php if ($jenis == '') {
                                            echo "<th>Jenis Kas</th>";
                                        } ?>
                                        <th>Keterangan</th>
                                        <th>Taut Tagihan</th>
                                        <th>Debit</th>
                                        <th>Kredit</th>
                                        <th width="110">Saldo</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    if ($_GET['periode'] == 'y') {
                                        if ($jenis == '') {
                                            $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE tanggal BETWEEN '$tgl1' AND '$tgl2' order by tanggal ASC ");
                                        } else {
                                            $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE tanggal BETWEEN '$tgl1' AND '$tgl2' AND jenis_kas='$jenis' order by tanggal ASC ");
                                        }
                                    } else {
                                        if ($jenis == '') {
                                            //menghilangkan tampilan transaksi keseluruhan agar tidak berat
                                            $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_transaksi='1' order by tanggal ASC ");
                                        } else {
                                            $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE jenis_kas='$jenis' AND id_transaksi='1' order by tanggal ASC ");
                                        }
                                    }

                                    $count = 2;



                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>

                                        <tr style="background: <?php if ($row['kredit'] == 0) { ?>
                          #defff1;
                          <?php } else { ?>
                            #feeeea;
                            <?php } ?>">
                                            <td><?php if ($row['kredit'] == 0) { ?><a class="btn btn-success btn-xs"><i class="glyphicon glyphicon-save-file"></i></a> <?php } else { ?> <a class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-open-file"></i></a><?php } ?></td>
                                            <td><?php echo $row['id_transaksi']; ?></td>
                                            <td><?php echo $row['tanggal']; ?></td>

                                            <!-- Jenis kas -->
                                            <td><?php echo namajenis($row['id_jenis_sumber']); ?></td>
                                            <td><?php echo namajenis($row['id_jenis']); ?></td>
                                            <?php if ($jenis == '') {
                                                $jeniskas = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$row[jenis_kas]'");
                                                $jenkas = mysqli_fetch_array($jeniskas);

                                                echo "<td>$jenkas[nama]</td>";
                                            } ?>

                                            <td><?php
                                                echo $row['keterangan']; ?>
                                                    
                                                <!-- SPP, DAM DAN KOPERASI -->
                                              <?php if ($row['id_jenis'] == '5' or $row['id_jenis'] == '6' or $row['id_jenis'] == '17' or $row['id_jenis'] == '14') { ?>
                                                <?php $y  = mysqli_query($conn, "SELECT * FROM santri where id_santri='$row[id_santri]' order by id_santri asc");
                                                    while ($x = mysqli_fetch_array($y)) {
                                                      echo "#$x[id_santri]-$x[nama]";
                                                    ?>
                                            <?php }
                                                  } ?>
                                                </td>

                                            <td>
                                                <?php 
                                                $text = namataghut($row['id_tag_hut']);
                                                if(strlen($text) > 30) {
                                                    echo substr($text, 0, 30) . '...';
                                                } else {
                                                    echo $text;
                                                }
                                                ?>
                                            </td>

                                            <?php if ($count == 1) { ?>

                                                <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                                                <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                                                <td>
                                                    <?php
                                                    $debit = $row['debit'];
                                                    $saldo = $row['debit'];
                                                    echo "Rp." . rupiah($saldo);
                                                    ?>
                                                </td>

                                                <?php } else {
                                                if ($row['debit'] != 0) {
                                                ?>
                                                    <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                                                    <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                                                    <td>
                                                        <?php
                                                        $debit = $debit + $row['debit'];
                                                        $saldo = $saldo + $row['debit'];
                                                        echo "Rp." . rupiah($saldo);
                                                        ?>


                                                    <?php } else { ?>
                                                    <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                                                    <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                                                    <td>
                                                <?php
                                                    $kredit = $kredit + $row['kredit'];
                                                    $saldo = $saldo - $row['kredit'];
                                                    echo "Rp." . rupiah($saldo);
                                                }
                                            }
                                            $count++


                                                ?>



                                            <?php

                                        } ?>




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

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