<?php

$banksoalx = mysqli_num_rows(mysqli_query($koneksi, "select * from banksoal where status='1'"));
$soalx = mysqli_num_rows(mysqli_query($koneksi, 'select * from soal'));
$siswax = mysqli_num_rows(mysqli_query($koneksi, "select * from siswa "));
$ujianx = mysqli_num_rows(mysqli_query($koneksi, "select * from ujian "));
?>
<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php if ($ac == '') : ?>
<div class='row'>
        <div class='col-md-7'>
           <div class="card">
             <div class="card-header">               
                    
				 <h5 class='card-title'><i class="fas fa-sync-alt fa-fw"></i> Tarik Data</h5>
                </div>
				 <div class="example-container">
                       <div class="example-content">	
                      <div class='card-body'>
                   				  
                        <center>
						<h4 style="color:blue"><i class='fa fa-sync-alt'></i> Sinkron Tarik Data</h4>
						<br><br>
						<h5 class="sandik" style="color:red"> Pastikan Token API dan Kode Server Sudah Sama</h5>
						<br><br>
						<button class='btn btn-outline-success' id='btnstatus' data-bs-toggle='modal' data-bs-target='#cekstatus'><i class='fa fa-sync-alt'></i> &nbsp;Sinkron</button>                           
							<a href="?pg=sinkron&ac=lihat" class='btn btn-outline-primary' ><i class='fa fa-search'></i> &nbsp;Data Sinkron</a>
                        </center>
                    </div>
                </div>              
                    <div class='box-body' id='resethasil'>
                        <div id='hasilsinkron'>                      
                        </div>
                       
                    </div>
                </div>
             </div>
                </div>
			<div class="col-md-5">
						<div class="card">
                      <div class="card-header">
                      <h5 class='card-title'><i class="fas fa-wifi fa-fw"></i> Koneksi Server</h5>
                </div>	
                    <div class="example-container">
                       <div class="example-content">				
                        <div class='card-body'>	
                       						
                       <center><div id='server'></center>
                    </div>
                </div>              
                  </div>
                </div>  
            </div>
        </div>
    </div>
</div>
<div class='modal fade' id='cekstatus' style='display: none;'>
    <div class='modal-dialog'>
        <div class='modal-content'>
            <div class='modal-header'>
                <h4 class='modal-title'>Tarik Data Pusat</h4>
            </div>
           <div class='modal-body' id='bodystatu'>
                <form id="formsinkron">                  
                        <div class="form-group">
                            <label for="tokenapi"></label>
                            <input type="hidden" class="form-control" name="tokenapi" id="tokenapi" value="<?= $setting['token_api'] ?>" readonly>

                        </div>
						
					<div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="pengumuman" checked>
                            Tarik Data Pengumuman
                        </label>
                    </div>
					<div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="siswa" checked>
                            Sinkron data siswa
                        </label>
                    </div>
					 <div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="pel" checked>
                            Mata Pelajaran
                        </label>
                    </div>
					<div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="menu" checked>
                            Menu Ujian
                        </label>
                    </div>
					<div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="jenis" checked>
                            Jenis Ujian
                        </label>
                    </div>
                  <div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="soal" checked>
                            Tarik Data Soal Ujian
                        </label>
                    </div>
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="datasinkron[]" value="jadwal" checked>
                            Tarik Data Jadwal Ujian
                        </label>
                    </div>
					 
					 <center>
                        <button type="submit" class="btn btn-primary"><i class='fa fa-sync-alt'></i>  Sinkron</button>
                    </center>
                   
                </form>
            </div>

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


<script>
    $('#btnstatus').click(function() {
        $.ajax({
            type: 'POST',
            url: 'server/sinkronstatus.php',
            beforeSend: function() {
                $('#loading-image').show();
            },
            success: function(response) {
                $('#bodystatus').html(response);

                $('#mulaitarik').removeAttr('disabled');

            },
            complete: function() {
                $('#loading-image').hide();
            },
        });

    });
    $('#formsinkron').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'server/sinkron.php',
            data: $(this).serialize(),
            beforeSend: function() {
                $(this).attr('disabled', 'disabled');
                $('.loader').css('display', 'block');

            },

            success: function(response) {
                $('.loader').css('display', 'none');
                $('#cekstatus').modal('hide');
                $('#hasilsinkron').html(response);
                $('#tablestatus').load(location.href + ' #tablestatus');
            }
        });
        return false;
    });


</script>
<?php elseif ($ac == 'lihat') : ?>
<div class='row'>
        <div class='col-md-12'>
           <div class="card">
                      <div class="card-header">                 
                    <div class='right'>
					  <button class='btn btn-outline-warning' id='btnreset'><i class='fa fa-trash'></i> Reset</button>
                       <a href="?pg=sinkron" class='btn btn-outline-danger' ><i class='fa fa-reply'></i> Kembali</a>
                     
                    </div>
				 <h4 class='box-title'><i class="fas fa-sync-alt fa-fw"></i> Tarik Data Pusat</h4>
                </div>
				
                <div class='card-body'>                        
                        <table class='table  table-hover' id='tablestatus'>
                            <thead>
                                <th>No</th>
                                <th>Nama Data</th>
                                <th>Jumlah</th>
                                <th>Update Terakhir</th>
                                <th>Status Sinkron</th>
                               
                            </thead>
                            <tbody>
                                <?php $no = 0;
                                $dataq = mysqli_query($koneksi, "select * from sinkron");
                                while ($data = mysqli_fetch_array($dataq)) {
                                    if ($data['status_sinkron'] <> 0) {
                                        $status = "<b style='color:blue'>Sudah sinkron</b>";
                                       
                                    } else {
                                        $status = "<b style='color:red'>Belum sinkron</b>";
                                     
                                    }
                                    if ($data['tanggal'] <> null) {
                                        $update = "$data[tanggal]";
                                    } else {
                                        $update = "Belum Ada";
                                    }
                                    $no++; ?>

                                    <tr>
                                        <td><?= $no ?></td>
                                        <td><?= $data['nama_data'] ?> - <?= strtoupper($data['data']); ?></td>
                                        <td><?= $data['jumlah'] ?></td>
                                        <td><?= $update ?></td>
                                        <td><?= $status ?></td>
                                       
                                    </tr>
                                <?php    } ?>

                            </tbody>

                        </table>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class='modal fade' id='resetsinkron' style='display: none;'>
    <div class='modal-dialog'>
        <div class='modal-content'>
            <div class='modal-header'>
                <h4 class='modal-title'>Reset Sinkronisasi</h4>
            </div>
            <div class='modal-body' id='bodystatu'>
                <center>Aksi ini akan menyebabkan semua data yang telah masuk terhapus, dan harus sinkronisasi ulang, apa anda yakin akan melanjutkan ??</center>
            </div>
            <div class='modal-footer'>

                <center>
                    <button class='btn btn-danger' name='reset' id='btnresetsinkron'><i class='fa fa-close'></i> Reset Sinkron</button>
                </center>

            </div>
        </div>
    </div>
</div>
<script>
$('#btnreset').click(function() {
	swal({
            title: 'Konfirmasi ',
            text: 'Hapus Data Sinkron ??',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Ya, Buat!'
        }).then((result) => {
            if (result.value) {
        $.ajax({
            type: 'POST',
            url: 'server/sinkronreset.php',
            
            success: function(response) {
                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>
<?php endif; ?>