<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
$bulan= date('m');
$tahun= date('Y');
$jum = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM absen_daring WHERE idsiswa='$_SESSION[id_siswa]' and tanggal='$tanggal' and materi='$_GET[id]'"));
if($jum==0):
mysqli_query($koneksi,"INSERT INTO absen_daring(tanggal,idsiswa,waktu,bulan,tahun,materi) VALUES('$tanggal','$_SESSION[id_siswa]','$datetime','$bulan','$tahun','$_GET[id]')");
Endif;

$materi = mysqli_fetch_array(mysqli_query($koneksi, "select * from materi where id_materi='$_GET[id]'"));
function youtube($url)
{
    $link = str_replace('http://www.youtube.com/watch?v=', '', $url);
    $link = str_replace('https://www.youtube.com/watch?v=', '', $link);
    $data = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/' . $link . '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
    return $data;
}
?>
<div class='row'>
    <div class='col-md-8'>
      <div class='box box-solid'>
            <div class='box-header with-border'>
                <h3 class='box-title'>Materi Belajar</h3>
            </div>
            <div class='box-body'>
                <table class='table table-striped'>
                    <tr>
                        <th width='150'>Mata Pelajaran</th>
                        <td width='10'>:</td>
                        <td><?= $materi['mapel'] ?></td>

                    </tr>

                    <tr>
                        <th>Tgl Pembelajaran</th>
                        <td width='10'>:</td>
                        <td><?= $materi['tgl_mulai'] ?></td>
                    </tr>
                    <?php if($materi['quiz']=='Ya' AND $nilai==0){ ?>
					 <tr>
                        <th>Quiz</th>
                        <td width='10'>:</td>
                        <td> <a href="<?= $homeurl . '/soalquiz/' . $materi['id_materi'] ?>"  class="btn btn-sm btn-outline-primary">
						<i class="fas fa-edit"></i> Quiz</a>
						</td>
                    </tr>
					<?php } ?>
					<?php if($materi['file']<>''){ ?>
					 <tr>
                        <th>Download</th>
                        <td width='10'>:</td>
                        <td> <a href="<?= $homeurl . '/datafile/' . $materi['file'] ?>" target="_blank" class="btn btn-sm btn-danger"><i class="fas fa-download"></i> Download</a></td>
                    </tr>
					<?php } ?>
                </table>
				
					<?php 
                    if(!empty($materi['file'])){
						$pecah=explode('.',$materi['file']);
						$ekstensi=$pecah[1];
					?>
					<?php if($ekstensi=='mp4'){ ?>
					<video src="<?= $homeurl ?>/datafile/<?= $materi['file'] ?>" controls autoplay width="100%" height="315"></video>
					 <?php } ?>
					 <?php if($ekstensi=='jpg' OR $ekstensi=='png'){ ?>
					<img src="<?= $homeurl ?>/datafile/<?= $materi['file'] ?>" controls autoplay width="100%" height="315">
					 <?php } ?>
					 <?php if($ekstensi=='pdf'){ ?>
					<iframe  src="<?= $homeurl ?>/datafile/<?= $materi['file'] ?>" controls autoplay width="100%" height="315"></iframe>
					<?php } ?>
					 <?php if($ekstensi=='docx'){ ?>
					<iframe src="http://docs.google.com/viewer?url=datafile/<?= $materi['file'] ?> 
					&embedded=true"  width="100%" height="315" controls autoplay style="border: none;"></iframe>

					<?php } ?>
					  <?php } ?>
                    <center>
                        <div class="callout">
                            <strong>
                                <h5><?= $materi['judul'] ?></h5>
                            </strong>
                        </div>
                    </center>
                    <?php if ($materi['youtube'] <> null) {  ?>
                        <div class="col-md-3"></div>
                        <div class="callout col-md-6">
                            <?= youtube($materi['youtube']) ?>
                        </div>
                        <div class="col-md-3"></div>
                    <?php } ?>
                    <div class="col-md-12">
                        <?= $materi['materi'] ?>
                    </div>
            </div>
</div>
        </div>
   
  <div class='col-md-4'>
       <div class='box box-solid'>
            <div class='box-body'>
                <table class='table  table-striped'>
                    <tr>
                        <th width='150'>Mata Pelajaran</th>
                        <td width='10'>:</td>
                        <td><?= $materi['mapel'] ?></td>

                    </tr>

                    <tr>
                        <th>Tgl Pembelajaran</th>
                        <td width='10'>:</td>
                        <td><?= $materi['tgl_mulai'] ?></td>
                    </tr>

                </table>
				
                  <div class="box direct-chat direct-chat-info">
                    <div class="box-header with-border">
                      <h4 class="box-title"><small>Kirim Pesan jika ada yang kurang dimengerti</small></h4>
                      
                    </div>
                    <div class="box-body">                   
                      <div class="direct-chat-messages">
                       <?php
                        $query = mysqli_query($koneksi, "SELECT * FROM chating WHERE dari='$_SESSION[id_siswa]' ORDER BY id DESC LIMIT 10");
                        while ($dari = mysqli_fetch_array($query)) : 
						$siswa =fetch($koneksi,'siswa',['id_siswa'=>$dari['dari']]);
						?>
                          
                        <div class="direct-chat-msg">
                          <div class="direct-chat-info clearfix">
                            <span class="direct-chat-name pull-left"><?= $siswa['nama'] ?></span>
                            <span class="direct-chat-timestamp pull-right"><?= $dari['waktu'] ?></span>
                          </div>
                          <img class="direct-chat-img" src="images/user.png" alt="message user image">
                          <div class="direct-chat-text">
                            <?= $dari['isi'] ?>
                          </div>
						  <?php if($dari['status']==1){ ?>
						 <span class='text-green pull-right'><i class='fa fa-check'></i></span>
						  <?php }else{ ?>
						   <span class='text-red pull-right'><i class='fa fa-check'></i></span>
						  <?php } ?>
                        </div>
                      <?php endwhile; ?> 
                      <?php
                        $que = mysqli_query($koneksi, "SELECT * FROM chating WHERE untuk='$_SESSION[id_siswa]' ORDER BY id DESC LIMIT 10");
                        while ($untuk = mysqli_fetch_array($que)) : 
						$peg =fetch($koneksi,'users',['id_user'=>$untuk['dari']]);
						?>
                        <div class="direct-chat-msg right">
                          <div class="direct-chat-info clearfix">
                            <span class="direct-chat-name pull-right"><?= $peg['nama'] ?></span>
                            <span class="direct-chat-timestamp pull-left"><?= $untuk['waktu'] ?></span>
                          </div>
                          <img class="direct-chat-img" src="images/guru.png" alt="message user image">
                          <div class="direct-chat-text">
                            <?= $untuk['isi'] ?>
                          </div>
                        </div>
                      <?php endwhile; ?> 
                       
                       
                      </div>
                    </div>
                    <div class="box-footer" style="background-color:#fff">
                      <form id="formchat" action="" method="post">
					  <input type="hidden" name="dari" value="<?= $_SESSION['id_siswa'] ?>" >
					   <input type="hidden" name="untuk" value="<?= $materi['id_guru'] ?>" >
					    <input type="hidden" name="materi" value="<?= $_GET['id'] ?>" >
                        <div class="input-group">
                          <input type="text" name="pesan" placeholder="Tulis pesan ..." class="form-control" required="true">
                          <span class="input-group-btn">
                            <button type="submit" name="submit" class="btn btn-success btn-flat">Send</button>
                          </span>
                        </div>
                      </form>
                    </div>
                  </div>
                 </div>
			</div>
		</div>
	</div>
	<script>
    $('#formchat').submit(function(e){
		e.preventDefault();
		var data = new FormData(this);
		$.ajax(
		{
			type: 'POST',
             url: 'tchat.php?pg=tambah',
            data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function(data){   		
			setTimeout(function()
				{
				window.location.reload();
						}, 500);
									  
						}
					});
				return false;
			});
		</script>	
   