<style>
    .fontInput {
        font-size: 13px;
        font-family: arial;
    }
</style>

<div class="content-wrapper">

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

    <div class="container col-12 mb-4 pb-5">
        <div class="row  mx-3 pr-3">
            <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"></span> Tambah User Siswa </b>
                        </div>

                        <div class="row col-md-12">
                            <div class="col-md-8">
                                <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="PILIH SISWA" 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="row">

                                                                <label class="col-sm-3 col-form-label">Pilih Siswa </label>
                                                                <div class="col-md-9 select2-primary">
                                                                    <select name="nis[]" id="nis" class="form-control form-control-sm fontInput small select2" multiple="multiple" data-placeholder="Pilih Siswa" data-dropdown-css-class="select2-primary fontInput">
                                                                        <option value="">--Pilih Siswa--</option>
                                                                        <?php
                                                                        $opval = $opt->optSelect($tb_siswa, ' order by jenjang asc, paralel asc, nama asc');
                                                                        while ($dataopt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                                            echo "<option value='$dataopt->nis'>$dataopt->nis : $dataopt->nama</option>";
                                                                        };

                                                                        ?>
                                                                    </select>
                                                                </div>

                                                            </div>

                                                            <div class="row">
                                                                <label class="col-sm-3 col-form-label"></label>
                                                                <div class="col-md-9">
                                                                    <div class="card-body mt-0">
                                                                        <b class="text-danger">Keterangan :</b><br> <i class="text-secondary">Username dan password Siswa Default adalah : NIS masing-masing. Password dapat diubah setelah siswa login</i>


                                                                    </div>
                                                                </div>

                                                            </div>

                                                            <div class='row '>
                                                                <div class="col-12 my-0 pl-3 py-0 my-3">
                                                                    <div class="row float-left">
                                                                        <div class="col-auto">
                                                                            <a <?php echo $link->page('seting/userSiswa/user_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="submituser"><i class="bi-save"></i> Simpan</button>
                                                                        </div>

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

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?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,
            'hp' => $hp,
            'email' => $email,
            'password' => $pass,
            'id' => $id,
            'status' => 'Aktif',
            'created_at' => $waktu,
            'updated_at' => $waktu,
            'created_by' => $user
        ];

        $cekDuplikat = $clsCrud->countData('user_siswa', "where nis='$s_nis'");
        if ($cekDuplikat > 0)
            continue;
        $clsCrud->insertData('user_siswa', $arrayjns);
    }

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

?>