<style>
    .close:focus {
        outline: none;
    }

    .top-right {
        top: 20px;
        right: 20px;
    }

    .toast {
        border: 1px solid coral
    }
</style>
<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>

    <div class="container col-12 mb-2 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-people"></span> Tambah User Siswa</b>
                            <?php $pandu->linkPandu('view/seting/siswa/pandu_tabelsiswa'); ?>
                        </div>

                        <!-- /.card-header -->
                        <div class="card-body my-2 mx-4">

                            <form action="" method="post">
                                <table id="example15" class="table table-bordered table-striped table-hover table-sm  mb-0">
                                    <thead>
                                        <tr>
                                            <th><input type="checkbox" class="small check_all"></th>
                                            <th>NIS</th>
                                            <th>Nama Siswa</th>
                                            <th>L/P</th>
                                            <th>Telpon</th>
                                            <th>Kelas</th>
                                            <th>User</th>

                                        </tr>
                                    </thead>
                                    <tbody>

                                        <?php

                                        $cekTabel = new cekTabel;
                                        $cekTabel = $cekTabel->cek($cekDB,  $tb_siswa);

                                        if ($cekTabel > 0) {

                                            $crud = new crudData();
                                            $showSiswa = $crud->showTable($tb_siswa);
                                            while ($data = $showSiswa->fetch(PDO::FETCH_OBJ)) {
                                                $nis = $data->nis;
                                                $id = $data->id;

                                                $cekuser = $crud->countData('user_siswa', " where nis='$nis'");

                                                if ($cekuser > 0) {
                                                    echo "";
                                                } else {
                                                    echo "
                                                    <tr>
                                                        <td width='5px'>
                                                            <input type='checkbox' class='d-inline small chk_boxes1' name='nis[]' value='$data->nis'> 
                                                        </td>
                                                        <td>$data->nis</td>
                                                        <td>$data->nama</td>
                                                        <td>$data->kelamin</td>
                                                        <td>$data->hp</td>
                                                       
                                                        <td>$data->jenjang - $data->paralel</td>
                                                        <td></td>
                                                    </tr>";
                                                };
                                            }
                                        } else {
                                            echo "<tr>
                                                <td width='5px' colspan='7'>
                                                    TIDAK ADA TABEL / DATA TAHUN $tahun
                                                </td>
                                            </tr>";
                                        }
                                        ?>

                                    </tbody>
                                </table>

                                <div class="div d-inline">
                                    <button type="submit" class="btn btn-primary btn-xs float-left" name="submituser"><i class="bi-save"></i> Simpan User</button>
                                    <a <?php echo $link->page('seting/userSiswa/user_read'); ?> class="btn btn-primary btn-xs float-left mx-2"><i class="bi-arrow-left-circle"></i> Cancel</a>

                                </div>
                                <div class="div d-inline">
                                </div>
                                <div class="div d-inline float-right mt-4">
                                </div>

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

<!-- /.content-wrapper -->
<script type="text/javascript">
    $(function() {
        $('.check_all').click(function() {
            $('.chk_boxes1').prop('checked', this.checked);
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#deleteAll').on('show.bs.modal', function(e) {
            var rowid = $(e.relatedTarget).data('id');
            //menggunakan fungsi ajax untuk pengambilan data
            $.ajax({
                type: 'post',
                url: 'ubah_siswa.php?pages=<?php echo $pages; ?>&username=<?php echo $username; ?>&tahun=<?php echo $tahun; ?>&act=save',
                data: 'rowid=' + rowid,
                success: function(data) {
                    $('.fetched-data-edit').html(data); //menampilkan data ke dalam modal
                }
            });
        });
    });
</script>
<?php
if (isset($_POST['submituser'])) {

    $nis = $_POST['nis'];

    foreach ($nis as $a => $nises) {
        $s_nis = $nises;
        $pass = md5($s_nis);

        $clsCrud = new crudData;
        $nama = $clsCrud->getValue($tb_siswa, " where nis='$s_nis'", 'nama');
        $kelamin = $clsCrud->getValue($tb_siswa, " where nis='$s_nis'", 'kelamin');
        $user = $clsCrud->getValue('user', " where username='$username'", 'id');
        $hp = $clsCrud->getValue('user', " where username='$username'", 'hp');
        $email = $clsCrud->getValue('user', " where username='$username'", 'email');


        $clstime = new time;
        $waktu = $clstime->getTime();

        $id = substr(md5($nama), 0, 10) . substr(md5($s_nis), 0, 5) . substr(md5($pass), 0, 5);
        $arrayjns = [
            'nis' => $s_nis,
            'nama' => $nama,
            'kelamin' => $kelamin,
            'username' => $s_nis,
            'password' => $pass,
            'hp' => $hp,
            'email' => $email,
            'id' => $id,
            'status' => 'Aktif',
            'created_at' => $waktu,
            'updated_at' => $waktu,
            'created_by' => $user
        ];
        $clsCrud->insertData('user_siswa', $arrayjns);
    }

    $direct = new toast();
    echo  $direct->redirect('toast', 'save', 'seting/userSiswa/user_read');
} else {
    echo '';
}
