<?php

$cekid = new cekID();
$time = new time;
$cekint = new cekint;
$link = new linkpage;
$random = new random;
$crud = new crudData();
$clssekolah = new sekolah;

$waktu = $time->getTime();
$ran = $random->create(20);

$webUrl = $_SERVER['HTTP_HOST'];

//Status Aktif Payment
$aktif = $crud->getValue('payment_set', "where no='1'", 'aktif');
//ID Page
$tahun_tabel = substr($tahun, 2, 2) . substr($tahun, 7, 2);
$cekid->Cek($tb_siswa, 'id', $id_page);

$sql = $crud->editData($tb_siswa, 'id', $id_page);
$data = $sql->fetch(PDO::FETCH_OBJ);

//Cek If Get Record Draf Payment
$cek = $crud->countData($payment_list, "where nis='$data->nis' && (status='DRAF' || status='CONFIRM')");
if ($cek > 0) {
    $id_billing = $crud->getValue($payment_list, "where nis='$data->nis' && (status='DRAF' || status='CONFIRM')", 'payment_id');
    $total = (int)$crud->getValue($payment_list, "where nis='$data->nis' && (status='DRAF' || status='CONFIRM')", 'jumlah');
    $status = $crud->getValue($payment_list, "where nis='$data->nis' && (status='DRAF' || status='CONFIRM')", 'status');
} else {
    $id_billing = strtoupper("SIMAKOM" . $tahun_tabel . "_" . $nis . "_" . substr(md5($id_page . $waktu . $ran), 5, 10) . substr(md5($ran), 12, 5));
    $total = 0;
    $status = "--";
}
$id_siswa_tabungan = $crud->getValue($tabungan_siswa, "where nis='$data->nis'", 'id');
$cekint = $cekint->is_konek();
?>
<style>
    .fontInput {
        font-size: 12px;
        font-family: arial;
    }

    .judul {
        font-size: 15px;
        font-family: arial;
    }
</style>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <?php require('./layout/headerpage.php');
    ?>

    <section class="content">
        <div class="container mb-1 pb-1">
            <div class="row">
                <div class="col-md-12">
                    <div class="col-12">
                        <div class="card  mx-2 mt-4">
                            <div class="timeline-header bg-secondary  py-1 pl-1">
                                <b class="text-light judul pl-2" style="font-family:consolas;font-size:14px"><span class="bi bi-journal-bookmark"></span> Mutasi Tabungan</b>
                            </div>
                            <div class="row col-12">
                                <div class="col-12">
                                    <div class="card-body  px-0 mx-0 pl-3">

                                        <div class="row">
                                            <label for="exampleEmail" class="col-lg-3 col-md-5 col-sm-12 col-form-label">NAMA SISWA / NASABAH </label>
                                            <div class="col-lg-9 col-md-7 col-sm-12">
                                                <input type="text" name="id" value="<?php echo $user_name; ?>" class="form-control col-sm-9 col-lg-12 form-control-sm fontInput" placeholder="ID Billing" disabled>
                                                <input type="text" name="id_siswa" value="<?php echo $id_page; ?>" class="form-control form-control-sm fontInput" hidden>
                                                <input type="text" name="tahun" value="<?php echo $tahun; ?>" class="form-control form-control-sm fontInput" hidden>
                                                <input type="text" name="id_billing" value="<?php echo $id_billing; ?>" class="form-control form-control-sm fontInput" hidden>
                                                <input type="text" name="webUrl" value="<?php echo $webUrl; ?>" class="form-control form-control-sm fontInput" hidden>
                                            </div>
                                        </div>

                                        <hr>
                                        <div class="row">
                                            <div class="col-lg-9 col-md-7 col-sm-12">
                                                <a href='<?php echo $clssekolah->dataSekolah('webaddress') . "/app/validate/?t4h0n=" . $tahun . "&i4=" . $id_siswa_tabungan; ?>&p463=796df5cc3715db6f0e941fba3ca5d717' target="_blank" class='btn btn-secondary btn-xs mb-2 btn-rounded small px-2'> <i class='bi bi-printer'></i> CETAK BUKU TABUNGAN</a>
                                            </div>
                                        </div>

                                        <div class=" col-md-12 mt-2">

                                            <?php
                                            $crud = new crudData();
                                            $format = new format();
                                            $tabungan = new tabungan();

                                            $saldo = $crud->getValue($tabungan_siswa, " where nis='$nis'", 'saldo');
                                            $saldoAwal = $format->ribuan($saldo);

                                            $db = database::getInstance();
                                            //$show = $db->runQuery("SELECT * FROM  $tabungan_transaksi where  nis = '$nis' order by tanggal asc, jenis asc, no asc, created_at asc");

                                            echo "<div class='row bg-light small'>
                                            <div class='info-box  bg-secondary'>
                                                <div class='info-box-content'><b class='text-light'> -- Saldo Awal TP. $tahun --</b> 
                                                    <span class='info-box-text text-light'><b class='col-6 float-left'> </b><b class='float-right'></b></span>
                                                    <span class='info-box-text text-light'><b class='col-6 float-left'>Saldo </b><b class='float-right'>Rp. " . $format->ribuan($saldoAwal) . "</b></span>
                                                </div>
                                            </div>
                                            </div>
                                        ";


                                            $show = $crud->showTable($tabungan_transaksi . " where  nis=$nis order by tanggal asc, jenis asc, no asc, created_at asc");

                                            $row = $crud->countData($tabungan_transaksi, " where  nis = '$data->nis'");
                                            $no = 0;
                                            $saldo = $saldo;

                                            if ($row != 0) {
                                                while ($data = $show->fetch(PDO::FETCH_OBJ)) {
                                                    // $clsTabungan = new tabungan;
                                                    $tanggal = $format->dateTimeToIndo($data->tanggal);
                                                    $debet = $tabungan->debetKredit($data->jenis, 'D', $data->jumlah);
                                                    $kredit = $tabungan->debetKredit($data->jenis, 'K', $data->jumlah);
                                                    $saldo = ($saldo + $debet - $kredit);
                                                    $jenis = $data->jenis;
                                                    $uraian = $tabungan->uraianJenis($data->jenis);

                                                    $petugas = $crud->getValue('user', "where id='$data->created_by'", 'nama');

                                                    if ($data->jurnal == 1) {
                                                        $keterangan = "Jurnal " . $data->catatan;
                                                    } else {
                                                        $keterangan = "";
                                                    };
                                                    $no++;
                                                    if ($jenis == "D") {
                                                        $bgColor = "success";
                                                        $textColor = "danger";
                                                    } else {
                                                        $bgColor = "danger";
                                                        $textColor = "success";
                                                    };

                                                    echo "<div class='row bg-light small'>
                                                                <div class='info-box  bg-$bgColor'>
                                                                    <div class='info-box-content'><b class='text-light'> $tanggal - $uraian</b> 
                                                                        <span class='info-box-text text-$textColor'><b class='col-6 float-left'>Jumlah </b><b class='float-right'>Rp. " . $format->ribuan($debet + $kredit) . "</b></span>
                                                                        <span class='info-box-text text-warning'><b class='col-6 float-left'>Saldo </b><b class='float-right'>Rp. " . $format->ribuan($saldo) . "</b></span>
                                                                    </div>
                                                                </div>
                                                                </div>
                                                            ";

                                                    // echo $modal->linkModaltiny('#', 'detailTr',  $data->id . $tahun, $conten);
                                                }
                                            };


                                            ?>
                                        </div>






                                        <?php $clsTabungan = new tabungan;
                                        $saldoAkhir = $clsTabungan->saldoAkhir($tabungan_siswa, $tabungan_transaksi, $nis);
                                        ?>
                                        <div class="row float-left mb-1">
                                            <label for="exampleEmail" class="col-lg-5 col-md-6 col-sm-12 col-form-label mb-0">SALDO AKHIR </label>
                                            <div class="col-lg-7 col-md-6 col-sm-12">
                                                <input type="text" name="id_billling" value="<?php echo "Rp. " . $format->ribuan($saldoAkhir); ?>" style=" font-style:bold;color:red" class="form-control form-control-sm col-lg-12 text-bold" placeholder="ID Billing" disabled>
                                            </div>
                                        </div>


                                    </div>


                                </div>



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

</div>