<?php

//Cek User
$format = new format;
$time = new time;
$clslistTanggungan = new listTanggungan();
$hariini = $time->tanggalIni();

if ((int)substr($time->getDate(), 5, 2) > 6) {
    $blnNow = (int)substr($time->getDate(), 5, 2) - 6;
} else {
    $blnNow = (int)substr($time->getDate(), 5, 2) + 6;
}

if (isset($_POST['bulan'])) {
    $bulan = $_POST['bulan'];
    $periode = $lembaga->valPeriodeEng($bulan, $tahun1, $tahun2);
    $v_bulan = $lembaga->ValPeriode($bulan, $tahun1, $tahun2);
} else {
    $bulan = $lembaga->nowPeriode();
    $periode = $lembaga->valPeriodeEng($blnNow, $tahun1, $tahun2);
    $v_bulan = $lembaga->ValPeriode($blnNow, $tahun1, $tahun2);
};

//TOTAL KEKURANGAN
//Jumlah
$dt_jenis = new listTanggungan();
$classTrans = new transaksi();
$jumlah = $dt_jenis->tanggunganSiswa($tb_siswa, 'id', $id_siswa);
$total_tang = number_format($jumlah, 0, ".", ".");

//Terbayar & Kurang Total
$infoTerbayartotal = $classTrans->infoterbayartotal($tb_transaksi, $nis);
$terbayarTotal = @(number_format($infoTerbayartotal, 0, ".", "."));
$kurangTotal = @(number_format(($jumlah - $infoTerbayartotal), 0, ".", "."));

$persenTotal = $format->persen($infoTerbayartotal, $jumlah);

if ($persenTotal < 50) {
    $fgcolor = "red";
} elseif ($persenTotal >= 50 && $persenTotal < 100) {
    $fgcolor = "orange";
} else {
    $fgcolor = "green";
};
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <!-- Main content -->
    <section class="content">
        <div class="container">
            <!-- Info boxes -->
            <!-- /.row -->
            <div class="row mt-5">
                <div class="col-md-12  mt-5">
                    <div class="timeline-header bg-secondary pt-0 pb-1">
                        <b class="text-light judul my-2 ml-2" style="font-family:consolas;font-size:14px"><span class="bi bi-house-fill"></span> Beranda</b>
                        <a class="btn bg-transparent btn-xs  mx-1 text-light float-right" <?php echo $link->page('logout'); ?> style="font-family: consolas;" title="KELUAR"><img src="../../asset/dokumen/foto/switch.png" width="20" height="20">

                        </a>
                    </div>

                    <div class='row mt-3'>
                        <div class='col-12 col-sm-12 col-md-12'>
                            <div class='info-box  bg-transparent'>

                                <div class='info-box-content small'>
                                    <span class='info-box-text text-danger'><b class='col-4 float-left'>Nama</b><b>: <?php echo strtoupper($nama); ?> </b></span>
                                    <span class='info-box-text text-dark'><b class='col-4 float-left'>NIS</b><b>: <?php echo $nis; ?> </b></span>
                                    <span class='info-box-text text-dark'><b class='col-4 float-left'>Kelas</b><b>: <?php echo $kelas; ?> </b></span>

                                    <span class='info-box-text float-right'>
                                        <a <?php echo $link->pageID($user_id, 'seting/userAdmin/user_profil'); ?> class="badge badge-secondary small px-2"> <i class="bi bi-person-plus"></i> SETING PROFIL & PASSWORD</a>

                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row col-md-12">

                    <div class="col-md-5">
                        <div class="card-body">
                            <span class='text-success'><b>PEMBAYARAN HARI INI</b></span>
                            <?php include "beranda/realisasi.php"; ?>

                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card-body">
                            <span class='text-danger'><b>KEKURANGAN</b></span>
                            <div class="row mt-3">
                                <div class="col-12 col-sm-12">
                                    <div class="bg-light py-2 px-3 mt-1">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <h2 class="mb-0 text-danger" style="font-family:cambria">
                                                    <b>Rp. <?php echo $format->ribuan($jumlah - $infoTerbayartotal); ?> </b>
                                                </h2>
                                                <small>Kekurangan Per <?php echo $format->dateTimeToIndo($time->getTime()); ?></small>
                                            </div>
                                        </div>
                                    </div>
                                    <hr>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-3 mt-4">
                        <div class="card-body text-center ">

                            <div class="box-body mt-0">
                                <input type="text" class="knob" value="<?php echo  $format->persen(@($crud->sum($tb_transaksi, 'jumlah', "where nis='$nis'")), $clslistTanggungan->tanggunganSiswaIf($tb_siswa,  " nis='$nis'")); ?>" data-skin="tron" data-thickness="0.2" data-width="120" data-height="120" data-fgColor="<?php echo $fgcolor; ?>">
                            </div>
                        </div>
                    </div>

                    <div class="row col-12  ml-1 mb-4">
                        <a href="view/transaksi/report.php?tahun=<?php echo $tahun; ?>&nis=<?php echo $nis; ?>" class="btn btn-xs btn-danger pl-2"><i class=" bi bi-file-earmark-pdf"></i> DOWNLOAD REPORT SISWA</a>
                    </div>
                </div>

            </div>


        </div>
        <!--/. container-fluid -->
    </section>

    <section class="content">
        <div class="container">
            <!-- Info boxes -->

            <!-- /.row -->
            <div class="row mb-3">
                <div class="col-md-12">
                    <div class="timeline-header bg-secondary pt-1 pb-1">
                        <b class="text-light judul my-2 ml-2" style="font-family:consolas;font-size:14px"><span class="bi bi-card-list"></span> Daftar Tanggungan</b>
                    </div>
                </div>

            </div>

            <?php
            // $crud
            $showData = $crud->editData($tb_siswa, 'id', $id_siswa);
            $data = $showData->fetch(PDO::FETCH_OBJ);

            $namaBayar = new namaBayar();
            $siswa = new siswa();
            $showJenis = $namaBayar->NamaTransaksi('tb_nama_bayar', 'tp', $tahun, $data->jenjang, 'kode');
            $count = $showJenis->rowCount();
            while ($datajenis = $showJenis->fetch(PDO::FETCH_OBJ)) {
                $field_tang = 'tang_' . $datajenis->kode;

                if ($data->{$field_tang} == "" || !is_numeric($data->{$field_tang})) {
                    $val_tang = 0;
                } else {
                    $val_tang = $data->{$field_tang};
                }; //Cek jika jumlah tanggungan kosong / bukan angka

                $setbulanan = $datajenis->bulanan; //Cek Apakah Bulanan

                $rupiah_tang = @(number_format($val_tang, 0, ".", "."));
                $jenis = $datajenis->kode;

                //Terbayar & Kurang
                $classTrans = new transaksi();
                $infoTerbayar = $classTrans->infoterbayar($tb_transaksi, $data->nis, $jenis);
                $terbayar = @(number_format($infoTerbayar, 0, ".", "."));
                $kurang = @(number_format(($val_tang - $infoTerbayar), 0, ".", "."));

                if ($setbulanan == "Ya" || $setbulanan == "ya") {
                    $bulanan = @(number_format(($val_tang / (12 - ($startAt - 1))), 0, ".", "."));
                } else {
                    $bulanan = "--";
                };

                echo  $siswa->modeListTanggungan($tahun, $jenjang, $jenis, $val_tang, $infoTerbayar, '#');
            };
            //Jumlah
            $dt_jenis = new listTanggungan();
            $jumlah = $dt_jenis->tanggunganSiswa($tb_siswa, 'id', $id_siswa);
            $total_tang = number_format($jumlah, 0, ".", ".");

            //Terbayar & Kurang Total
            $infoTerbayartotal = $classTrans->infoterbayartotal($tb_transaksi, $data->nis);
            $terbayarTotal = @(number_format($infoTerbayartotal, 0, ".", "."));
            $kurangTotal = @(number_format(($jumlah - $infoTerbayartotal), 0, ".", "."));

            $persenTotal = $format->persen($infoTerbayartotal, $jumlah);

            ?>



            <div class='row'>
                <div class='col-12 col-sm-12 col-md-12'>

                    <div class='info-box  bg-secondary'>

                        <span class='info-box-icon bg-transparent elevation-1'></span>
                        <div class='info-box-content small'><b>REKAPITULASI</b>
                            <span class='info-box-text text-light'><i class='col-4 float-left'>Tanggungan</i>: Rp. <?php echo $format->ribuan($jumlah); ?> </span>
                            <span class='info-box-text text-light'><i class='col-4 float-left'>Terbayar</i>: Rp. <?php echo $format->ribuan($infoTerbayartotal); ?> </span>
                            <span class='info-box-text text-warning'><i class='col-4 float-left'><b>Kurang</i>: Rp. <?php echo $format->ribuan($jumlah - $infoTerbayartotal); ?> </b></span>
                        </div>

                    </div>

                </div>

            </div>
            <div class="row mb-5 pb-4">
                <div class="col-md-12 col-sm-12">
                    <div class="col-md-12 col-sm-12">
                        <div class="card mb-5">

                            <a <?php echo $link->pageID($id_siswa, 'billing/index'); ?> class="btn btn-block btn-warning" style="font-family: consolas;"><i class="bi bi-journal-bookmark"></i> BUAT BILLING PEMBAYARAN </a>
                        </div>

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

    </section>
    <div class="clearfix hidden-md-up"></div>




</div>