<?php defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!'); ?>
<?php if ($ac == '') { ?>
    <div class='row'>
        <div class='col-md-12'>
		 <div class="card">
             <div class="card-header">	                
					<h5 class='card-title'>MATERI BELAJAR</h5>
					  <div class='pull-right'>				
					 <a href="?pg=inputmateri" class='btn btn-primary'><i class="material-icons">add</i>Materi</a>
                    </div>
                </div>
				
				 <div class="card-body">	
                    <div id='tablemateri' class='table-responsive'>
                        <table id="datatable1" class='table table-bordered'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>
                                    <th>MAPEL</th>
                                    <th>JUDUL</th>
                                    <th>TANGGAL</th>
                                    <th>ROMBEL</th>
                                    <th width="5%">FILE</th>
                                   
									<th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($user['level'] == 'guru') {
                                    $materiQ = mysqli_query($koneksi, "SELECT * FROM materi where id_guru='$_SESSION[id_user]'");
                                } else {
                                    $materiQ = mysqli_query($koneksi, "SELECT * FROM materi ");
                                }
                                ?>
                                <?php while ($materi = mysqli_fetch_array($materiQ)) : ?>
                                    <?php
                                    $guru = mysqli_fetch_array(mysqli_query($koneksi, "select * FROM users where id_user='$materi[id_guru]'"));
                                    $no++
                                    ?>
                                    <tr>
                                        <td><?= $no ?></td>
                                        <td>
                                            <?= $materi['mapel'] ?>
                                        </td>
                                       
                                        <td>
                                            <?= $materi['judul'] ?>
                                        </td>

                                        <td style="text-align:center">
                                            <?= $materi['tgl_mulai'] ?>
                                        </td>
                                        <td style="text-align:center">
                                            <?php $kelas = unserialize($materi['kelas']);
                                            foreach ($kelas as $kelas) {
                                                echo $kelas . " ";
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php if ($materi['file'] <> null) { ?>
                                                <a href="../datafile/<?= $materi['file'] ?>" target="_blank">Lihat</a>
                                            <?php } ?>
                                        </td>
										
										
                                        <td>
                                           
											<a href='?pg=materi&ac=absen&id=<?= $materi['id_materi'] ?>' class='btn btn-sm btn-warning'  data-bs-toggle="tooltip" data-bs-placement="top" title="Absen"><i class='material-icons'>photo_camera</i></a>
                                                <a href='?pg=materi&ac=lihat&id=<?= $materi['id_materi'] ?>' class='btn btn-sm btn-success'  data-bs-toggle="tooltip" data-bs-placement="top" title="Pesan Masuk"><i class='material-icons'>quickreply</i></a>
                                               
                                                <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modaledit<?= $materi['id_materi'] ?>">
                                                    <i class="material-icons" data-bs-toggle="tooltip" data-bs-placement="top" title="Edit">edit</i>
                                                </button>
                                                <button data-id='<?= $materi['id_materi'] ?>' class="hapus btn btn-danger btn-sm"  data-bs-toggle="tooltip" data-bs-placement="top" title="Hapus"><i class="material-icons">delete</i></button>
                                           
                                        </td>
                                    </tr>
                                    
                                    <div class="modal fade" id="modaledit<?= $materi['id_materi'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
                                        <div class="modal-dialog modal-lg" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">

                                                   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
                                                <form id="formeditmateri<?= $materi['id_materi'] ?>">
                                                    <div class="modal-body">
                                                        <input type="hidden" value="<?= $materi['id_materi'] ?>" name='id'>
                                                        <div class="form-group">
														<label class="bold">Mata Pelajaran</label><br>
                                                            <select name='mapel' class='form-select' style='width:100%' required>
                                                                <option value='<?= $materi['mapel'] ?>' selected><?= $materi['mapel'] ?></option>
                                                                 <?php $que = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran"); ?>
                                                                <?php while ($mapel = mysqli_fetch_array($que)) : ?>

                                                                    <option value="<?= $mapel['kode'] ?>"><?= $mapel['nama_mapel'] ?></option>"

                                                                <?php endwhile ?>
                                                            </select>
                                                        </div>
														
                                                        <div class="form-group">
                                                         <label class="bold">Judul Materi</label><br>
                                                            <input type="text" class="form-control" name="judul" aria-describedby="helpId" placeholder="Judul materi" value="<?= $materi['judul'] ?>" required>

                                                        </div>
														
                                                        <div class="form-group">
														<label class="bold">Materi Belajar</label>
                                                            <textarea name='isimateri' class='editor1' rows='10' cols='80' style='width:100%;'><?= $materi['materi'] ?></textarea>
                                                        </div>
														<p>
                                                                 <div class="row">
                                                                <div class='col-md-6'>
                                                                    <label class="bold">Tanggal</label>
                                                                    <input type='text' name='tgl_mulai' class='tgl form-control' autocomplete='off' required='true' value="<?= $materi['tgl_mulai'] ?>" />
                                                                </div>
                                                                <div class='col-md-6'>
                                                                    <label class="bold">Video Youtube</label>
                                                                    <input type='text' name='youtube' class='form-control' autocomplete='off'  value="<?= $materi['youtube'] ?>" />
                                                                </div>
																		</div>
                                                           <p>
                                                        <div class="form-group">
                                                            <label class="bold">File Pendukung</label>
                                                            <input type="file" class="form-control-file" name="file" id="file" placeholder="" aria-describedby="fileHelpId">
                                                            <small id="fileHelpId" class="form-text text-muted">format file (doc/docx/xls/xlsx/pdf)</small>
                                                        </div>
                                                    </div>
										<div class='modal-footer'>
									   <div class='box-tools pull-right btn-group'>
										  <button type='submit' id="blockui-3" class='btn btn-primary'>SIMPAN</button>
											  <button type='button' class='btn btn-light' data-bs-dismiss='modal'>BATAL</button>
												</div>
												 </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                                        $('#formeditmateri<?= $materi['id_materi'] ?>').submit(function(e) {
                                            e.preventDefault();
                                           var data = new FormData(this);
      
                                            $.ajax({
                                                type: 'POST',
                                                url: 'cbtserver_learn/edit_materi.php',
                                                enctype: 'multipart/form-data',
												data: data,
												cache: false,
												contentType: false,
												processData: false,
												success: function(data) {
												
													setTimeout(function() {
														window.location.reload();
													}, 2000);

												}
											});
											return false;
										});
 
                                    </script>
                                <?php endwhile ?>
                            </tbody>
                             </table>
						 </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>	
<?php } elseif ($ac == 'lihat') { ?>
    <?php $id = $_GET['id']; ?>
    <div class='row'>
        <div class='col-md-8'>
            <div class="card">
             <div class="card-header">	
					 <h5 class='card-title'>DATA PESAN</h5>
                </div>
            
					<div class="card-body">
						<?php $m=fetch($koneksi,'materi',['id_materi'=>$_GET['id']]); ?>
						<center><h4 class='box-title'><?= $m['mapel'] ?></h4>
						<p>Tanggal <?= date('d-m-Y',strtotime($m['tgl'])); ?></p></center>	
				<div class="panel panel-default"> 
			
                      <div id='tablekomentar' class='table-responsive'>
                        <table id="datatable1" class='table table-bordered table-hover'>
                            <thead>
                                <tr>
                                    <th width='5px'>NO</th>
                                    
                                    <th>NAMA SISWA</th>
                                    <th>ISI PESAN</th>
									 <th>WAKTU</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
							       <?php     
								$no=0;
								
                              $materiQ = mysqli_query($koneksi, "SELECT * FROM chating WHERE  materi='$_GET[id]' ORDER BY id DESC");
						   while ($chat = mysqli_fetch_array($materiQ)){
								$siswa=fetch($koneksi,'siswa',['id_siswa' => $chat['dari']]);
								
                              $no++;
                                ?>
								<tr>
                                        <td><?= $no ?></td>
                                        
									   <td><?= $siswa['nama'] ?> </td>
									   <td><?= $chat['isi'] ?> </td>
									   <td><?= $chat['waktu'] ?> </td>
									   <td>
									   <button data-id="<?= $chat['id'] ?>" class="hapus btn btn-danger btn-sm"><i class="material-icons">delete</i></button>
								        <a href="?pg=materi&ac=lihat&idp=<?= $chat['id'] ?>" class="btn btn-primary btn-sm"><i class="material-icons">add</i></a>
										</td>
								</tr>
							<?php } ?>
					           </tbody>
                             </table>
						 </div>
                     </div>						
                </div>
            </div>
        </div>
		
		 <div class='col-md-4'>
           <div class="card">
             <div class="card-header">	
					</div>    
			  <div class='card-body'>
   
					<div class="d-grid gap-2">
						<button class="btn btn-dark" type="button" disabled>
							<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
							Kirim Balasan Pesan
						</button>
						</div><p>
						<form id="formbalas" >
                          <?php 
						  $chating = fetch($koneksi,'chating',['id'=>$_GET['idp']]); 
						  $siswa=fetch($koneksi,'siswa',['id_siswa' => $chating['dari']]);
						  ?>
						  <input type="hidden" name="id" class="form-control" value="<?= $_GET['idp'] ?>" >
						  <input type="hidden" name="untuk" class="form-control" value="<?= $siswa['id_siswa'] ?>" >
						   <input type="hidden" name="dari" class="form-control" value="<?= $chating['untuk'] ?>" >
							 <div class="row mb-2">
								<label class="bold">Kepada</label>
									<input type="text" class="form-control" value="<?= $siswa['nama'] ?>" readonly>
                                       </div>
									   <div class="row mb-2">
								<label class="bold">Isi Pesan</label>
									<textarea  rows="3" class="form-control" readonly><?= $chating['isi'] ?></textarea>
                                       </div> 
                                      <div class="row mb-2">
								<label class="bold">Balas Pesan</label>
									<textarea  name="pesan" rows="5" class="form-control" required="true"></textarea>
                                       </div>  
								<div class="kanan">
								<?php if($_GET['idp'] !=''): ?>
										<button type='submit' id="blockui-3" class='btn btn-primary'><i class='material-icons' >send</i>KIRIM</button>
									<?php else: ?>
									<button  class='btn btn-secondary' disabled><i class='material-icons' >send</i>KIRIM</button>
									
									<?php endif; ?>		  
                                       </div>
						                </form>
								            
											</div>
										</div>
									</div>
								</div> 
							</div>
							 <script>
                                        $('#formbalas').submit(function(e) {
                                            e.preventDefault();
                                           var data = new FormData(this);
      
                                            $.ajax({
                                                type: 'POST',
                                                url: 'cbtserver_learn/balaspesan.php',
                                                enctype: 'multipart/form-data',
												data: data,
												cache: false,
												contentType: false,
												processData: false,
												success: function(data) {
												
													setTimeout(function() {
														window.location.replace('?pg=materi');
													}, 2000);

												}
											});
											return false;
										});
 
                                    </script>
										<script>
										 $('#datatable1').on('click', '.hapus', function() {
												var id = $(this).data('id');
												console.log(id);
												swal({
													title: 'Apa anda yakin?',
													text: "akan menghapus materi ini!",
													type:'warning',
													showCancelButton: true,
													confirmButtonColor: '#3085d6',
													cancelButtonColor: '#d33',
													confirmButtonText: 'Yes!'
												}).then((result) => {
													if (result.value) {
														$.ajax({
															url: 'cbtserver_learn/hapus_komen.php',
															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>

	<?php } elseif ($ac == 'absen') { ?>
    <?php $id = $_GET['id']; ?>
    <div class='row'>
        <div class='col-md-12'>
            <div class="card">
             <div class="card-header">
					<h5 class='card-title'>DATA BUKA MATERI</h5>
                </div>
             
						<div class="card-body">
						<?php $m=fetch($koneksi,'materi',['id_materi'=>$_GET['id']]); ?>
						<center><h5 class='box-title'><?= $m['mapel'] ?></h5>
						<p>Tanggal <?= date('d-m-Y',strtotime($m['tgl'])); ?></p></center>
				<div class="panel panel-default">
								
                      <div id='tablekomentar' class='table-responsive'>
                        <table id="datatable1" class='table table-bordered table-hover'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>              
                                    <th>NAMA SISWA</th>
                                    <th>ABSEN</th>
									 <th>TANGGAL</th>
                                    <th>WAKTU</th>
                                </tr>
                            </thead>
                            <tbody>
							       <?php     
								$no=0;
                              $nilaiQ = mysqli_query($koneksi, "SELECT * FROM absen_daring WHERE materi='$_GET[id]'");
                            while ($nilai = mysqli_fetch_array($nilaiQ)){
								$siswa=fetch($koneksi,'siswa',['id_siswa' => $nilai['idsiswa']]);
								$materi=fetch($koneksi,'materi',['id_materi' => $nilai['materi']]);
                              $no++;
                                ?>
								<tr>
                                        <td><?= $no ?></td>
                                       
									   <td><?= $siswa['nama'] ?> </td>
									   <td>Hadir </td>
									   <td><?= $nilai['tanggal'] ?></td>
									    <td><?= date('H:i:s',strtotime($nilai['waktu'])); ?></td>
									  
								</tr>
							<?php } ?>
					</tbody>
                        </table>	
					</div>						
                </div>
            </div>
        </div>
    </div>
</div>
<?php } ?>

<script>
    
    $('#tablemateri').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "Akan menghapus semu data materi ini !",
            type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'cbtserver_learn/hapus_materi.php',
                    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;
        })

    });
	
    $('#tablejawaban').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "akan menghapus nilai ini!",

            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'cbtserver_learn/materi/hapus_nilai.php',
                    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>
<script>
    tinymce.init({
        selector: '.editor1',
        plugins: [
            'advlist autolink lists link image charmap print preview hr anchor pagebreak',
            'searchreplace wordcount visualblocks visualchars code fullscreen',
            'insertdatetime media nonbreaking save table contextmenu directionality',
            'emoticons template paste textcolor colorpicker textpattern imagetools uploadimage paste formula'
        ],

        toolbar: 'bold italic fontselect fontsizeselect | alignleft aligncenter alignright bullist numlist  backcolor forecolor | formula code | imagetools link image paste ',
        fontsize_formats: '8pt 10pt 12pt 14pt 18pt 24pt 36pt',
        paste_data_images: true,

        images_upload_handler: function(blobInfo, success, failure) {
            success('data:' + blobInfo.blob().type + ';base64,' + blobInfo.base64());
        },
        image_class_list: [{
            title: 'Responsive',
            value: 'img-responsive'
        }],
        setup: function(editor) {
            editor.on('change', function() {
                tinymce.triggerSave();
            });
        }
    });
</script>
 <script>
			$('.select2').select2();
			</script>