<?php

namespace PHPMaker2021\rdigital21;

use PHPMaker2021\rdigital21\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

// Relative path
$RELATIVE_PATH = "../";

// Require files
require_once "../vendor/autoload.php";
require_once "../src/constants.php";
require_once "../src/config.php";
require_once "../src/phpfn.php";
require_once "../src/userfn.php";
?>

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Leger Nilai Kelas <?= $_GET['k'] ?></title>
    <style type="text/css">
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 10pt;
        }
    </style>
    <style>
        .str {
            mso-number-format: \@;
        }
    </style>
</head>

<body>

    <?php
    $semester = substr($_GET['ktp'], -1);
    $tahun_awal = substr($_GET['ktp'], 0, 4);
    $tahun_akhir = substr($_GET['ktp'], 4, 4);
    $format_tahun = $tahun_awal . '/' . $tahun_akhir;
    $tahun_pelajaran = $format_tahun;
    ?>

    <?php
    $kode_tapel = $_GET['ktp'];
    $kelas = $_GET['k'];

    header('Content-Type: text/xls');
    header('Content-Disposition: attachment; filename="LEGER-' . $_GET['k'] . '-' . $kode_tapel . '.xls"');
    header("Content-Transfer-Encoding: BINARY");

    if (isset($_GET['act'])) { ?>
        <div style="page-break-before: auto">
            <h3>Data Kumpulan Nilai Kelas <?= $_GET['k'] ?> Tahun Pelajaran <?= $format_tahun ?> Semester <?= $semester ?></h3>
            <table border="1" cellspacing="0" cellpadding="3" width="100%" style="zoom:50%">
                <thead>
                    <tr>
                        <th>NO</th>
                        <th>TAPEL</th>
                        <th>NIS</th>
                        <th>NAMA</th>
                        <th>KELAS</th>
                        <th>PABP</th>
                        <th>PP</th>
                        <th>BIND</th>
                        <th>MAT</th>
                        <th>IPA</th>
                        <th>IPS</th>
                        <th>BING</th>
                        <th>PJOK</th>
                        <th>INFTK</th>
                        <th>SP</th>
                        <th>BJ</th>
                        <th style="background-color:yellow;">JML</th>
                        <th style="background-color:yellow;">RATA-RATA</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $no = 1;
                    $sql = ExecuteQuery("SELECT * FROM peserta_didik Where kelas='$kelas' and kode_tapel='$kode_tapel'");
                    while ($leger = $sql->fetch()) {
                        $nis = $leger['nis'];
                        echo "<tr>";
                        echo "<td align='right'>" . $no . "</td>";
                        echo "<td align='center'>" . $leger['kode_tapel'] . "</td>";
                        echo "<td align='center'>" . $nis . "</td>";
                        echo "<td style='white-space: nowrap;'>" . $leger['nama'] . "</td>";
                        echo "<td>" . $leger['kelas'] . "</td>";
                        echo "<td align='right'>" . number_format($pabp = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='PABP' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($pp = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='PP' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($bind = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='BIND' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($mat = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='MAT' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($ipa = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='IPA' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($ips = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='IPS' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($bing = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='BING' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($pjok = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='PJOK' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($inftk = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='INFTK' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($sp = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='SP' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right'>" . number_format($bj = ExecuteScalar("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='BJ' AND nis='$nis' AND kode_tapel='$kode_tapel'")) . "</td>";
                        echo "<td align='right' style='background-color:yellow;'>" . number_format($jml = $pabp + $pp + $bind + $mat + $ipa + $ips + $bing + $pjok + $inftk + $sp + $bj) . "</td>";
                        echo "<td class='str' align='right' style='background-color:yellow;'>" . number_format($rata = $jml / 11, 2) . "</td>";
                        echo "</tr>";
                        $no++;
                    }
                    ?>
                </tbody>
            </table>



        <?php  } ?>
</body>

</html>