<div class='row'>
 <div class='col-md-8'>
            	<div class="card">
             <div class="card-header">		
			   <div class='right '>
                    <button id='btnabsen' class='btn btn-sm btn-outline-primary' onclick="frames['frameresult'].print()"><i class='fa fa-print'></i> Print</button>
                </div>
                  <h5 class='box-title'><i class="fas fa-graduation-cap"></i> Daftar Hadir Peserta</h5></div>    
           	
            <div class='card-body'>
                <form  class="form-horizontal" >
                    <div class='form-group'>
                        <label>Pilih Mapel</label>
                        <div class="col-sm-9">
						<select id='absenmapel' class='form-select' required='true' onchange="printabsen();" >
                            <?php $sql_mapel = mysqli_query($koneksi, "SELECT * FROM ujian"); ?>
                            <option value=''>Pilih Jadwal Ujian</option>
                            <?php while ($mapel = mysqli_fetch_array($sql_mapel)) : ?>
                                <option value="<?= $mapel['id_bank'] ?>">
								<?php echo "$mapel[nama] $mapel[level]";
                                   $dataArray = unserialize($mapel['id_pk']);
                                    foreach ($dataArray as $key => $value) {
                                       echo " $value ";
                                       }
                                ?></option>
                            <?php endwhile ?>
                        </select>
                    </div>
                   </div>
				 <p>
                    <div class='form-group'>
                        <label>Pilih Ruang</label>
						<div class="col-sm-9">
                        <select id='absenruang' class='form-select' onchange="printabsen();" >

                        </select>
                    </div>
					</div>
					  <p>
                    <div class='form-group'>
                        <label>Pilih Sesi</label>
						<div class="col-sm-12">
                        <select id='absensesi' class='form-select' onchange="printabsen();" >
                        </select>
                    </div>
					</div>
					<p>
                    <div class='form-group'>
                        <label>Pilih Kelas</label>
						<div class="col-sm-12">
                        <select id='absenkelas' class='form-select' onchange="printabsen();">
                        </select>
                    </div>
					</div>
					</form>
                </div>
            </div>
        </div>
    </div>
</div>
<iframe id='loadabsen' name='frameresult' src='sandik_ujian/print_absen.php' style='border:none;width:0px;height:0px;'></iframe>
<script>
    function printabsen() {
        var idsesi = $('#absensesi option:selected').val();
        var idmapel = $('#absenmapel option:selected').val();
        var idruang = $('#absenruang option:selected').val();
        var idkelas = $('#absenkelas option:selected').val();
        if (!idkelas) {
            idkelas = '';
        }
        if (!idsesi) {
            idsesi = '';
        }
        $('#loadabsen').attr('src', 'sandik_ujian/print_absen.php?id_sesi=' + idsesi + '&id_ruang=' + idruang + '&id_bank=' + idmapel + '&id_kelas=' + idkelas);
    }
    $("#absenmapel").change(function() {
        var mapel_id = $(this).val();
        console.log(mapel_id);
        $.ajax({
            type: "POST", 
            url: "sandik_ujian/crud_absen.php?pg=ambil_ruang", 
            data: "mapel_id=" + mapel_id, 
            success: function(response) { 
                $("#absenruang").html(response);
                console.log(response);
            },
            error: function(xhr, status, error) {
                console.log(error);
            }
        });
    });

    $("#absensesi").change(function() {
        var sesi = $(this).val();
        var mapel_id = $("#absenmapel").val();
        var ruang = $("#absenruang").val();
        console.log(sesi + ruang + mapel_id);
        $.ajax({
            type: "POST",
            url: "sandik_ujian/crud_absen.php?pg=ambil_kelas", 
            data: "mapel_id=" + mapel_id + '&sesi=' + sesi + '&ruang=' + ruang, 
            success: function(response) { 
                $("#absenkelas").html(response);
                console.log(response);
            },
            error: function(xhr, status, error) {
                console.log(error);
            }
        });
    });

    $("#absenruang").change(function() {

        var ruang = $(this).val();
        console.log(ruang);
        $.ajax({
            type: "POST", 
            url: "sandik_ujian/crud_absen.php?pg=ambil_sesi", 
            data: "ruang=" + ruang, 
            success: function(response) { 
                $("#absensesi").html(response);
                console.log(response);
            },
            error: function(xhr, status, error) {
                console.log(error);
            }
        });
    });
</script>