<?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>
                        <?php
                        $mapelumum = ExecuteQuery("SELECT * FROM mata_pelajaran WHERE ket='umum' ORDER BY mapel_id");
                        $mapel_kode_list = [];
                        while ($mapelumumlist = $mapelumum->fetch()) {
                            $mapel_kode_list[] = $mapelumumlist['kode_mapel'];
                            echo "<th>" . $mapelumumlist['kode_mapel'] . "</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'];
                        $total_nilai = 0;
                        $jumlah_mapel = 0;

                        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>";

                        foreach ($mapel_kode_list as $kode_mapel) {
                            $nmapelumum = ExecuteQuery("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='$kode_mapel' AND nis='$nis' AND kode_tapel='$kode_tapel'");
                            $nmapelumumlist = $nmapelumum->fetch();

                            $nilai = $nmapelumumlist ? $nmapelumumlist['nr2'] : 0;
                            echo "<td align='right'>" . number_format($nilai) . "</td>";

                            // Hitung total nilai dan jumlah mapel
                            $total_nilai += $nilai;
                            if ($nilai > 0) {
                                $jumlah_mapel++;
                            }
                        }

                        echo "</tr>";

                        $no++;
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <div style="page-break-before: always">
            <h3>Data Kumpulan Nilai Mapel Khusus 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>
                        <?php
                        $mapelkhusus = ExecuteQuery("SELECT * FROM mata_pelajaran WHERE ket='khusus' ORDER BY mapel_id");
                        $mapel_kode_list = [];
                        while ($mapelkhususlist = $mapelkhusus->fetch()) {
                            $mapel_kode_list[] = $mapelkhususlist['kode_mapel'];
                            echo "<th>" . $mapelkhususlist['kode_mapel'] . "</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'];
                        $total_nilai = 0;
                        $jumlah_mapel = 0;

                        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>";

                        foreach ($mapel_kode_list as $kode_mapel) {
                            $nmapelkhusus = ExecuteQuery("SELECT nr2 FROM nilai_sumatif WHERE kode_mapel='$kode_mapel' AND nis='$nis' AND kode_tapel='$kode_tapel'");
                            $nmapelkhususlist = $nmapelkhusus->fetch();

                            $nilai = $nmapelkhususlist ? $nmapelkhususlist['nr2'] : 0;
                            echo "<td align='right'>" . number_format($nilai) . "</td>";

                            // Hitung total nilai dan jumlah mapel
                            $total_nilai += $nilai;
                            if ($nilai > 0) {
                                $jumlah_mapel++;
                            }
                        }

                        echo "</tr>";

                        $no++;
                    }
                    ?>
                </tbody>
            </table>
        </div>



    <?php  } ?>
</body>

</html>