<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<div class='row'>
     <div class='col-md-12'>
             <div class="card">
              <div class="card-header">	
			    <div class='right '>
                    <a href="?pg=buatberita" class="btn btn-sm btn-outline-primary" >
                        <i class="fas fa-plus"></i> Berita Acara</button></a>
                </div>
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Berita Acara</h5></div>    
									
            <div class='card-body'>
               
                <div id='tableberita' class='table-responsive'>
                    <table class='table table-hover'>
                        <thead>
                            <tr>
                                <th width='5px'>#</th>
                                <th>Mata Pelajaran</th>
                                <th>Level-PK-Kelas</th>
                                <th>Sesi</th>
                                <th>Ruang</th>
                                <th>Hadir</th>
                                <th>Tidak Hadir</th>
                                <th>Mulai</th>
                                <th>Selesai</th>
                               
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $beritaQ = mysqli_query($koneksi, "SELECT * FROM berita");
                            ?>
                            <?php while ($berita = mysqli_fetch_array($beritaQ)) : ?>
                                <?php
                                $mapel = mysqli_fetch_array(mysqli_query($koneksi, "select * from banksoal a left join mata_pelajaran b ON a.nama=b.kode_mapel where a.id_bank='$berita[id_bank]'"));
                                $no++
                                ?>
                                <tr>
                                    <td><?= $no ?></td>
                                    <td>
                                        <b><?= $mapel['nama_mapel'] ?></b> <small class='label bg-red'><?= $berita['jenis'] ?></small>
                                    </td>
                                    <td>
                                        <small class='label label-warning'><?= $mapel['level'] ?></small>
                                        <?php
                                        $dataArray = unserialize($mapel['idpk']);
                                        foreach ($dataArray as $key => $value) {
                                            echo "<small>$value </small>&nbsp;";
                                        }
                                        ?>
                                        <?php
                                        $dataArray = unserialize($mapel['kelas']);
                                        foreach ($dataArray as $key => $value) {
                                            echo "$value &nbsp;";
                                        }
                                        ?>
                                    </td>
                                    <td style="text-align:center">
                                        <b><?= $berita['sesi'] ?></b>
                                    </td>
                                    <td style="text-align:center">
                                        <?= $berita['ruang'] ?>
                                    </td>
                                    <td style="text-align:center">
                                        <?= $berita['ikut'] ?>
                                    </td>
                                    <td style="text-align:center">
                                        <?= $berita['susulan'] ?>
                                    </td>
                                    <td style="text-align:center">
                                        <?= $berita['mulai'] ?>
                                    </td>
                                    <td style="text-align:center">
                                        <?= $berita['selesai'] ?>
                                    </td>
                                    
                                    <td style="text-align:center">
                                        <div class=''>
                                            <a href='?pg=beritaacara&id=<?= $berita['id_berita'] ?>' class='btn btn-sm btn-outline-primary'><i class='fas fa-print'></i></a>
                                            <button data-id='<?= $berita['id_berita'] ?>' class="hapus btn btn-outline-danger btn-sm"><i class="fas fa-trash-alt"></i></button>
                                        </div>
                                    </td>
                                </tr>


                            <?php endwhile ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
  
    $('#tableberita').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        Swal.fire({
				  title: 'Are you sure?',
				  text: "You won't be able to revert this!",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#3085d6',
				  cancelButtonColor: '#d33',
				  confirmButtonText: 'Yes, delete it!'	
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_ujian/crud_berita.php?pg=hapus',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                    iziToast.info({
					title: 'Sukses!',
					message: 'Data berhasil dihapus',
					titleColor: '#FFFF00',
					messageColor: '#fff',
					backgroundColor: 'rgba(0, 0, 0, 0.5)',
					progressBarColor: '#FFFF00',
					position: 'topRight'				  
                });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });
</script>