<?php
if (isset($_POST['jenj'])) {
    $jenj = $_POST['jenj'];
    $aktifinput = "";
} else {
    $jenj = "--Jenjang--";
    $aktifinput = "disabled";
};
?>
<style>
    .fontInput {
        font-size: 11px;
        font-family: arial;
    }
</style>
<script>
    function formatNumber(num) {
        return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
    }
</script>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>
    <div class="container col-12 mb-4 pb-5">
        <div class="row">
            <div class="col-12">
                <div class="col-12">
                    <div class="card  mx-3">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-person-plus"></span> Tambah Siswa</b>
                        </div>
                        <div class="row col-md-12">
                            <div class="col-md-12">
                                <div class="card-body mt-4 px-1 mx-2">

                                    <div class="row">
                                        <div class="col-12 col-sm-6 col-md-12">
                                            <div class="info-box mb-3">
                                                <div class="info-box-content">
                                                    <div class="relative mb-0 pb-4">
                                                        <input name="judul" value="INPUT DATA SISWA & TANGGUNGAN" style="background-color: gainsboro;" class="form-control-sm col-sm-3 fontInput small bg-default mt-3 text-center" disabled>
                                                    </div>

                                                    <div class="col-sm-12 invoice-col pt-0">
                                                        <form action="" method="post">
                                                            <div class="my-2 pl-3 py-2 mx-3">
                                                                <div class="row">
                                                                    <label class="col-sm-2 col-form-label text-danger">Kelas [Jenjang-Paralel]</label>
                                                                    <div class="col-md-1">
                                                                        <select name="jenj" class="form-control form-control-sm fontInput small" required onchange="this.form.submit()">
                                                                            <option value=""><?php echo $jenj; ?></option>
                                                                            <?php
                                                                            //$crud = new crudData;
                                                                            //$sekolah = $crud->editData('sekolah', 'no', 1);
                                                                            //$dataSekolah = $sekolah->fetch(PDO::FETCH_OBJ);
                                                                            $jenjang = strtolower($crud->getValue('sekolah', "where no>0", 'jenjang'));

                                                                            $opt = new form();
                                                                            $opval = $opt->optSelect('tb_tingkat', "where {$jenjang}<>'' order by no asc");
                                                                            while ($dataopt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                                                $optJenjang = $dataopt->{$jenjang};
                                                                                echo "<option>" . $optJenjang . " </option>";
                                                                            };
                                                                            ?>
                                                                        </select>
                                                                    </div>
                                                        </form>
                                                    </div>
                                                </div>

                                                <form action="" method="post">
                                                    <?php $csrf->token(); ?>
                                                    <input type="text" name="jenjang" value="<?php echo $jenj; ?>" hidden>
                                                    <div class="row mx-3 mt-2">
                                                        <div class="col-md-5">
                                                            <div class="card-body mt-4 px-1 mx-2">
                                                                <div class="row">
                                                                    <label for="exampleEmail" class="col-sm-4 col-form-label">Nomor Induk Siswa (NIS) </label>
                                                                    <div class="col-sm-8">
                                                                        <input type="text" name="nis" value="" class="form-control form-control-sm fontInput text-secondary" style='text-align:left;font-size:14px;font-weight:bold' placeholder="NIS Siswa" <?php echo $aktifinput; ?>>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label for="exampleEmail" class="col-sm-4 col-form-label">Nama Siswa </label>
                                                                    <div class="col-sm-8">
                                                                        <input type="text" name="nama" value="" class="form-control form-control-sm fontInput" placeholder="Nama Siswa" <?php echo $aktifinput; ?>>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label class="col-sm-4 col-form-label">Jenis Kelamin</label>
                                                                    <div class="col-md-8">
                                                                        <select name="kelamin" class="form-control form-control-sm fontInput small" required <?php echo $aktifinput; ?>>
                                                                            <option value="">--Pilih Jenis Kelamin--</option>
                                                                            <option value="L">Laki-laki</option>
                                                                            <option value="P">Perempuan</option>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label class="col-sm-4 col-form-label">Kelas [Jenjang-Paralel]</label>
                                                                    <div class="col-md-2">
                                                                        <select name="jenjang" class="form-control form-control-sm fontInput small" disabled>
                                                                            <option><?php echo $jenj; ?></option>
                                                                            <?php
                                                                            $data_sekolah = new crudData;
                                                                            $sekolah = $data_sekolah->editData('sekolah', 'no', 1);
                                                                            $dataSekolah = $sekolah->fetch(PDO::FETCH_OBJ);
                                                                            $jenjang = strtolower($dataSekolah->jenjang);

                                                                            $opt = new form();
                                                                            $opval = $opt->optSelect('tb_tingkat', "where {$jenjang}<>'' order by no asc");
                                                                            while ($dataopt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                                                $optJenjang = $dataopt->{$jenjang};
                                                                                echo "<option>" . $optJenjang . " </option>";
                                                                            };
                                                                            ?>
                                                                        </select>

                                                                    </div>
                                                                    <div class="col-md-6">
                                                                        <select name="paralel" class="form-control form-control-sm fontInput small" required <?php echo $aktifinput; ?>>
                                                                            <?php
                                                                            $opval = $opt->optSelect('tb_paralel', ' order by no asc');
                                                                            while ($datapar = $opval->fetch(PDO::FETCH_OBJ)) {
                                                                                echo "<option>$datapar->paralel</option>";
                                                                            };

                                                                            ?>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label for="nis" class="col-sm-4 col-form-label">No. Telpon/WA</label>
                                                                    <div class="col-sm-8">
                                                                        <input type="text" class="form-control form-control-sm fontInput" value="" placeholder="No Telpon User" name="hp" <?php echo $aktifinput; ?>>

                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label for="nis" class="col-sm-4 col-form-label">Email</label>
                                                                    <div class="col-sm-8">
                                                                        <input type="email" class="form-control form-control-sm fontInput" value="" placeholder="email@mail.com" name="email" <?php echo $aktifinput; ?>>

                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label for="nis" class="col-sm-4 col-form-label">Orang Tua/Wali</label>
                                                                    <div class="col-sm-8">
                                                                        <input type="text" class="form-control form-control-sm fontInput" value="" placeholder="Nama Orang Tua/Wali" name="ortu" <?php echo $aktifinput; ?>>

                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label for="nis" class="col-sm-4 col-form-label">Alamat</label>
                                                                    <div class="col-sm-8">
                                                                        <input type="text" class="form-control form-control-sm fontInput" value="" placeholder="Alamat" name="alamat" <?php echo $aktifinput; ?>>

                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label class="col-sm-4 col-form-label">Kategori Siswa</label>
                                                                    <div class="col-md-8">
                                                                        <select name="kategori" class="form-control form-control-sm fontInput small" required <?php echo $aktifinput; ?>>
                                                                            <?php
                                                                            $opval = $opt->optSelect('tb_siswa_kategori ', ' order by kategori asc');
                                                                            while ($datapar = $opval->fetch(PDO::FETCH_OBJ)) {
                                                                                echo "<option>$datapar->kategori</option>";
                                                                            };
                                                                            ?>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label class="col-sm-4 col-form-label">Mulai Pembayaran</label>
                                                                    <div class="col-md-8">
                                                                        <select name="startAt" class="form-control form-control-sm fontInput small" required <?php echo $aktifinput; ?>>
                                                                            <option value="">-- Pilih Bulan Mulai Pembayaran [untuk Jenis Bulanan] --</option>
                                                                            <?php
                                                                            $optVal = new Lembaga();
                                                                            $oplist = $optVal->listPeriode($tahun1, $tahun2);
                                                                            echo $oplist;
                                                                            ?>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <label class="col-sm-4 col-form-label">Status</label>
                                                                    <div class="col-md-8">
                                                                        <select name="status" class="form-control form-control-sm fontInput small" required <?php echo $aktifinput; ?>>
                                                                            <option value="Aktif">Aktif</option>
                                                                            <option value="Tidak">Non Aktif</option>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-7">
                                                            <div class="card-body mt-4 px-2 mx-1">

                                                                <?php
                                                                $dt_jenis = new namaBayar();
                                                                $showJenis = $dt_jenis->NamaTransaksi('tb_nama_bayar', 'tp', $tahun, $jenj, 'kode');
                                                                while ($datajenis = $showJenis->fetch(PDO::FETCH_OBJ)) {
                                                                    $kode = $datajenis->kode;
                                                                    $jenis = 'tang_' . $datajenis->kode;

                                                                    echo "
                                            <div class='row'>
                                                <label class='col-sm-6 col-form-label'>$datajenis->nama</label>
                                                <label class='col-sm-1 col-form-label float-right'>Rp. </label>
                                                <div class='col-md-3'>
                                                    <input type='text' class='form-control form-control-sm fontInput' style='text-align:right' name='$jenis' id='jenis_$kode' value='0' >
                                                </div>
                                                <label class='col-sm-2 col-form-label'> <small><i class='text-primary'>[ Kode : $datajenis->kode ]</i></small></label>
                                            </div>
                                            ";

                                                                    echo "
                                            <script>
                                                $('input').keyup(function() {";

                                                                    echo "var getJumlah$kode = Number($('#jenis_$kode').val());
                                              $('#jml$kode').val(getJumlah$kode);";

                                                                    echo "var totale=";
                                                                    $jns[] = $datajenis->kode;
                                                                    foreach ($jns as $j => $jenis) {
                                                                        $j_jenis = $jenis;
                                                                        $kd = "+Number($('#jml" . $j_jenis++ . "').val())";
                                                                        echo $kd;
                                                                    };
                                                                    echo ";";
                                                                    echo " $('#Total').val(totale);
                                                    var tot=formatNumber($('#Total').val());
                                                    $('#TotalAkh').val(tot);
                                                })
                                            </script>";
                                                                    echo "<input type='text' class='col-sm-4 form-control  form-control-sm fontJudul' style='text-align:left;font-size:14px;font-weight:bold' name='subtHiden' id='jml$kode'  hidden >";
                                                                    echo "<input type='text' class='col-sm-4 form-control  form-control-sm fontJudul' style='text-align:left;font-size:14px;font-weight:bold' name='subtHiden' id='Total'  hidden >";
                                                                }

                                                                echo "
                                            <div class='row'>
                                                <label class='col-sm-6 col-form-label'><b class='text-primary' style='text-align:right;font-size:14px;font-weight:bold'>JUMLAH</b></label>
                                                <label class='col-sm-1 col-form-label float-right text-primary'>Rp. </label>
                                                <div class='col-md-3'>
                                                    <input type='text' class='form-control form-control-sm fontInput text-primary' style='text-align:right;font-size:14px;font-weight:bold' id='TotalAkh' name='to' disabled>
                                                </div>
                                            </div>
                                             ";
                                                                ?>
                                                            </div>
                                                        </div>


                                                    </div>
                                                    <div class="my-2 pl-3 py-2 mx-3">
                                                        <div class="row">
                                                            <div class="col-auto">
                                                                <a <?php echo $link->page('seting/siswa/siswa_read'); ?> class="btn btn-primary btn-xs float-right mx-2"><i class="bi-arrow-left-circle"></i> Cancel</a>
                                                                <button type="submit" class="btn btn-primary btn-xs float-right" name="save"><i class="bi-save"></i> Simpan</button>

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

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

        <?php

        $class = new siswa();
        $save = $class->insertSiswa($tb_siswa, $tb_siswa_min);
