<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Cetak Rapor Mata Pelajaran Khusus</title>
    <style>
        body {
            font-family: 'Arial Narrow', Helvetica, sans-serif;
            font-size: 10pt;
        }

        @page {
            size: A4;
            margin: 0;
        }

        header {
            width: 100%;
            height: 100px;
            position: fixed;
            top: 0;
            margin: 5mm;
            padding: 5mm;
        }

        .content {
            width: 100%;
            margin-top: 18mm;
            margin-left: 5mm;
            margin-right: 5mm;
            padding: 5mm;
        }

        footer {
            width: 100%;
            height: 50px;
            text-align: center;
            position: fixed;
            bottom: 0;
            font-size: 8pt;
        }
    </style>
</head>


<!-- halaman 2 -->

<body>

    <div style="page-break-after:none">
        <?php
        if (isset($_GET['nis'])) {
            $kode_tapel = $_GET['ktp'];
            $nis = $_GET['nis'];
            $database_file = '../../db/rapor.db';
            $pdo = new PDO("sqlite:$database_file");

            $siswa = "SELECT
        sekolah.nama_sekolah, 
        sekolah.alamat, 
        sekolah.desa_kelurahan, 
        sekolah.kecamatan, 
        tahun_pelajaran.semester, 
        tahun_pelajaran.tahun_pelajaran, 
        tahun_pelajaran.tanggal_rapor, 
        peserta_didik.nama, 
        peserta_didik.nis, 
        peserta_didik.nisn, 
        peserta_didik.kelas, 
        wali_kelas.nama AS walikelas, 
        wali_kelas.nip, 
        sekolah.namaks, 
        sekolah.nipks, 
        penilaian.fase
        FROM
        sekolah,
        peserta_didik
        INNER JOIN
        tahun_pelajaran
        ON 
        peserta_didik.kode_tapel = tahun_pelajaran.kode_tapel
        INNER JOIN
        wali_kelas
        ON 
        peserta_didik.kelas = wali_kelas.kelas
        INNER JOIN
        penilaian
        ON 
        wali_kelas.kelas = penilaian.kelas WHERE nis = :nis AND peserta_didik.kode_tapel='$kode_tapel'";
            $siswalist = $pdo->prepare($siswa);
            $siswalist->bindParam(':nis', $nis, PDO::PARAM_STR);
            $siswalist->execute();

            if ($data_siswa = $siswalist->fetch(PDO::FETCH_ASSOC)) {
                $filename = $data_siswa['nis'] . "-" . $data_siswa['nama'];
        ?>
                <header>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                        <tr>
                            <td>
                                <table>
                                    <tr>
                                        <td>Nama</td>
                                        <td>: <?= $nama = $data_siswa['nama'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>NISN/NIS</td>
                                        <td>: <?= $nisn = $data_siswa['nisn'] ?> / <?= $data_siswa['nis'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Nama Sekolah</td>
                                        <td>: <?= $data_siswa['nama_sekolah'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Alamat</td>
                                        <td>: <?= $data_siswa['alamat'] ?>, <?= $data_siswa['desa_kelurahan'] ?>, <?= $kecamatan = $data_siswa['kecamatan'] ?></td>
                                    </tr>
                                </table>
                            </td>
                            <td>
                                <table>
                                    <tr>
                                        <td>Kelas</td>
                                        <td>: <?= $kelas = $data_siswa['kelas'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Fase</td>
                                        <td>: <?= $data_siswa['fase'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Semester</td>
                                        <td>: <?= $semester = $data_siswa['semester'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Tahun Pelajaran</td>
                                        <td>: <?= $tahun_pelajaran = $data_siswa['tahun_pelajaran'] ?></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                    <hr>
                </header>
                <div class="content">
                    <center>
                        <h3>LAPORAN HASIL BELAJAR</h3>
                    </center>
                    <table border="1" cellspacing="0" cellpadding="3" width="100%">
                        <tr style="background-color:whitesmoke;">
                            <td nowrap align="center">
                                <strong>No</strong>
                            </td>
                            <td nowrap align="center">
                                <strong>Mata Pelajaran</strong>
                            </td>
                            <td nowrap align="center">
                                <strong>Nilai Akhir</strong>
                            </td>
                            <td nowrap align="center">
                                <strong>Capaian Kompetensi</strong>
                            </td>
                        </tr>
                        <?php
                        $database_file = '../../db/rapor.db';
                        $pdo = new PDO("sqlite:$database_file");

                        $no = 1;
                        $mapel = "SELECT
                    nilai_sumatif.nis, 
                    penilaian.materi, 
                    nilai_sumatif.penilaian_id, 
                    nilai_sumatif.kode_mapel, 
                    nilai_sumatif.nama, 
                    nilai_sumatif.s1, 
                    nilai_sumatif.s2, 
                    nilai_sumatif.s3, 
                    nilai_sumatif.s4, 
                    nilai_sumatif.s5, 
                    nilai_sumatif.s6, 
                    nilai_sumatif.s7, 
                    nilai_sumatif.s8, 
                    nilai_sumatif.nas, 
                    nilai_sumatif.sts, 
                    nilai_sumatif.sas, 
                    nilai_sumatif.nr2, 
                    mata_pelajaran.mata_pelajaran, 
                    mata_pelajaran.ket, 
                    mata_pelajaran.mapel_id
                    FROM
                    nilai_sumatif
                    INNER JOIN
                    penilaian
                    ON 
                    nilai_sumatif.penilaian_id = penilaian.penilaian_id
                    INNER JOIN
                    mata_pelajaran
                    ON 
                    nilai_sumatif.kode_mapel = mata_pelajaran.kode_mapel
                    WHERE mata_pelajaran.ket='khusus' AND nilai_sumatif.nis=:nis AND nilai_sumatif.kode_tapel='$kode_tapel'
                    GROUP BY
                    nilai_sumatif.kode_mapel
                    ORDER BY mata_pelajaran.mapel_id limit 6";
                        $mapellist = $pdo->prepare($mapel);
                        $mapellist->bindParam(':nis', $nis, PDO::PARAM_STR);
                        $mapellist->execute();
                        while ($mapeldata = $mapellist->fetch(PDO::FETCH_ASSOC)) {
                            $kode_mapel = $mapeldata['kode_mapel'];
                        ?>
                            <tr>
                                <td height="25mm" nowrap rowspan="2" align="center">
                                    <?= $no ?>
                                </td>
                                <td rowspan="2">
                                    <?= $mapeldata['mata_pelajaran'] ?>
                                </td>

                                <td nowrap rowspan="2" align="center">
                                    <?= number_format($mapeldata['nr2']) ?>
                                </td>
                                <td rowspan="2" align="justify">
                                    <?php
                                    $s1 = number_format(floatval($mapeldata['s1'] ?? 0), 0);
                                    $s2 = number_format(floatval($mapeldata['s2'] ?? 0), 0);
                                    $s3 = number_format(floatval($mapeldata['s3'] ?? 0), 0);
                                    $s4 = number_format(floatval($mapeldata['s4'] ?? 0), 0);
                                    $s5 = number_format(floatval($mapeldata['s5'] ?? 0), 0);
                                    $s6 = number_format(floatval($mapeldata['s6'] ?? 0), 0);
                                    $s7 = number_format(floatval($mapeldata['s7'] ?? 0), 0);
                                    $s8 = number_format(floatval($mapeldata['s8'] ?? 0), 0);

                                    $sumatif = $s1 . "," . $s2 . "," . $s3 . "," . $s4 . "," . $s5 . "," . $s5 . "," . $s7 . "," . $s8;

                                    $data1Array = explode(',', $sumatif);
                                    $data2Array = explode(",", $mapeldata['materi']);
                                    $data1Array = array_filter($data1Array);
                                    $resultArray = array();

                                    for ($i = 0; $i < count($data1Array); $i++) {
                                        $kd = "M-" . ($i + 1);
                                        $data1Value = $data1Array[$i];
                                        $data2Value = $data2Array[$i];
                                        $resultArray[] = array('Data1' => $data1Value, 'No' => $kd, 'Data2' => $data2Value);
                                    }
                                    ?>


                                    <?php
                                    $tahun_pelajaran;
                                    $database_file = '../../db/rapor.db';
                                    $pdo = new PDO("sqlite:$database_file");

                                    $kktp = "SELECT * FROM kktp_sumatif WHERE tahun_pelajaran='$tahun_pelajaran'";
                                    $kktplist = $pdo->prepare($kktp);
                                    $kktplist->execute();
                                    while ($kktpdata = $kktplist->fetch(PDO::FETCH_ASSOC)) {
                                        if ($kktpdata['abjad'] == 'A') {
                                            $a_min = $kktpdata['min'];
                                            $a_max = $kktpdata['max'];
                                            $a_ket = $kktpdata['ket'];
                                        }
                                        if ($kktpdata['abjad'] == 'B') {
                                            $b_min = $kktpdata['min'];
                                            $b_max = $kktpdata['max'];
                                            $b_ket = $kktpdata['ket'];
                                        }
                                        if ($kktpdata['abjad'] == 'C') {
                                            $c_min = $kktpdata['min'];
                                            $c_max = $kktpdata['max'];
                                            $c_ket = $kktpdata['ket'];
                                        }
                                        if ($kktpdata['abjad'] == 'D') {
                                            $d_min = $kktpdata['min'];
                                            $d_max = $kktpdata['max'];
                                            $d_ket = $kktpdata['ket'];
                                        }
                                    }
                                    ?>


                                    <?php
                                    $max = max(array_column($resultArray, 'Data1'));
                                    $min = min(array_column($resultArray, 'Data1'));

                                    arsort($resultArray);

                                    foreach ($resultArray as $row) {
                                        $nilai = $row['Data1'];
                                        $materi_id = $row['Data2'];

                                        $materi = "SELECT
                            materi.tujuan_pembelajaran, 
                            materi.materi_id
                            FROM
                            materi where materi_id='$materi_id'";
                                        $materilist = $pdo->prepare($materi);
                                        $materilist->execute();

                                        while ($materidata = $materilist->fetch(PDO::FETCH_ASSOC)) {
                                            $tujuan_pembelajaran = $materidata['tujuan_pembelajaran'];

                                            if ($nilai == $max && $nilai >= $a_min && $nilai <= $a_max) {
                                                echo "Sangat baik dalam hal " . $tujuan_pembelajaran . " ";
                                            } elseif ($nilai == $max && $nilai >= $b_min && $nilai <= $b_max) {
                                                echo "Baik dalam hal " . $tujuan_pembelajaran . " ";
                                            } elseif ($nilai != $max && $max >= $b_min && $max <= $b_max) {
                                                echo "Baik dalam hal " . $tujuan_pembelajaran . " ";
                                            }

                                            if ($nilai == $min && $nilai >= $c_min && $nilai <= $c_max) {
                                                echo "Perlu penguatan dalam hal " . $tujuan_pembelajaran . " ";
                                            } elseif ($nilai == $min && $nilai <= $d_max) {
                                                echo "Perlu pendampingan dalam hal " . $tujuan_pembelajaran . " ";
                                            }
                                        }
                                    }
                                    ?>

                                </td>
                            </tr>
                            <tr></tr>
                        <?php $no++;
                        } ?>
                    </table>
                    <br>
                    <table width="100%">
                        <tr>
                            <td width="60%">&nbsp;</td>
                            <td width="40%">
                                <table>
                                    <tr>
                                        <td><?= $data_siswa['kecamatan'] ?>, <?= $data_siswa['tanggal_rapor'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>Wali Kelas</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td><?= $data_siswa['walikelas'] ?></td>
                                    </tr>
                                    <tr>
                                        <td>NIP. <?= $data_siswa['nip'] ?></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                    <br>
                </div>
    </div>
    <footer>
        <?= $nisn ?>/<?= $nis ?> | <?= $nama ?> | Kelas : <?= $kelas ?> | TP : <?= $tahun_pelajaran ?>
    </footer>
<?php } ?>
<?php } ?>
</body>


<!-- halaman 2 -->

</html>