<?php include 'header.php'; ?>

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Kelas
      <!-- <small>Siswa Kelas</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active">Dashboard</li>
    </ol> -->
  </section>

  <section class="content">
    <div class="row">
      <section class="col-lg-6">       
        <div class="box box-primary">

          <div class="box-header">
            <h3 class="box-title">Tentang kelas</h3>
            <a href="kelas.php" class="btn btn-primary btn-sm pull-right"><i class="fa fa-reply"></i> &nbsp Kembali</a> 
          </div>
          <div class="box-body">
            <?php 
            $km = $_GET['km'];              
            $kelas = mysqli_query($koneksi, "select * from kelas, kelas_mapel, jurusan, ta, mapel, guru where km_guru=guru_id and km_mapel=mapel_id and km_kelas=kelas_id and km_id='$km' and kelas_ta=ta_id and kelas_jurusan=jurusan_id");
            $k = mysqli_fetch_assoc($kelas);
            $id_kelas = $k['kelas_id'];
            $id_ta = $k['kelas_ta'];
            $id_km = $k['km_id'];
            ?>

            <div class="table-responsive">
              <table class="table table-bordered">
                <tr>
                  <th>Mata Pelajaran</th>
                  <td><?php echo $k['mapel_nama'] ?></td>
                </tr>
                <tr>
                  <th>Guru Pengajar</th>
                  <td><?php echo $k['guru_nama'] ?></td>
                </tr>
                <tr>
                  <th width="30%">Nama Sekolah</th>
                  <td><?php echo $k['kelas_nama'] ?></td>
                </tr>
                <tr>
                  <th>Kelas</th>
                  <td><?php echo $k['jurusan_nama'] ?></td>
                </tr>
                <tr>
                  <th>Tahun Ajaran</th>
                  <td><?php echo $k['ta_nama'] ?></td>
                </tr>
              </table>
            </div>

          </div>

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


    <div class="box box-primary">

      <div class="box-header">
        <h3 class="box-title">Siswa Kelas</h3>             
        <div class="btn-group pull-right">
          <a href="kelas_absen_cetak.php?km=<?php echo $id_km ?>" target="_blank" class="btn btn-primary btn-sm"><i class="fa fa-file-excel-o"></i> &nbsp; Cetak</a>
          <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#modal_jurusan">
            <i class="fa fa-edit"></i> &nbsp Input Absen
          </button>
        </div>

        <!-- The Modal -->
        <div class="modal" id="modal_jurusan">
          <div class="modal-dialog modal-lg">
            <div class="modal-content">

              <!-- Modal Header -->
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Input Absen & Poin</h4>
              </div>

              <!-- Modal body -->
              <div class="modal-body">

                <form method="post" action="kelas_absen_act.php" onsubmit="return cek(this)">

                  <div class="form-group">
                    <label>Tanggal</label>
                    <input type="hidden" class="form-control" name="km" value="<?php echo $k['km_id'] ?>">
                    <input type="date" class="form-control" name="tanggal" required="required">
                  </div>

                  <div class="table-responsive">
                    <table class="table table-bordered table-striped" id="table-datatable">
                      <thead>
                        <tr>
                          <th width="1%">NO</th>
                          <th>NAMA</th>
                          <th>NIS</th>
                          <th class="text-center" colspan="2">POIN</th>           
                        </tr>
                      </thead>
                      <tbody>
                        <?php 
                        $no=1;
                        $id = $id_kelas; 
                        $data = mysqli_query($koneksi,"SELECT * FROM siswa, jurusan where siswa_jurusan=jurusan_id and siswa_id in (select ks_siswa from kelas_siswa where ks_kelas='$id') order by siswa_id desc");
                        while($d = mysqli_fetch_array($data)){
                          ?>
                          <tr>
                            <td><?php echo $no++; ?></td>
                            <td><?php echo $d['siswa_nama']; ?></td>
                            <td><?php echo $d['siswa_nis']; ?></td>
                            <td>
                              <input type="hidden" class="form-control" name="siswa[]" value="<?php echo $d['siswa_id'] ?>">
                              <select class="form-control inputan" name="inputan[]" required="required">
                                <option value="">- Pilih inputan</option>                            
                                <option value="plus">Plus</option>                            
                                <option value="minus">Minus</option>                            
                              </select>

                              <br>

                              <select style="display:none" class="form-control  form_plus" name="prestasi[]">
                                <option value="">- Pilih Ketertiban</option>
                                <?php 
                                $prestasi = mysqli_query($koneksi,"select * from prestasi");
                                while($p = mysqli_fetch_array($prestasi)){
                                  ?>
                                  <option value="<?php echo $p['prestasi_id'] ?>"><?php echo $p['prestasi_nama'] ?> (+<?php echo $p['prestasi_point'] ?>)</option>
                                  <?php 
                                }
                                ?>
                              </select>

                              <select style="display:none" class="form-control  form_minus" name="pelanggaran[]">
                                <option value="">- Pilih Pelanggaran</option>
                                <?php 
                                $pelanggaran = mysqli_query($koneksi,"select * from pelanggaran");
                                while($p = mysqli_fetch_array($pelanggaran)){
                                  ?>
                                  <option value="<?php echo $p['pelanggaran_id'] ?>"><?php echo $p['pelanggaran_nama'] ?> (-<?php echo $p['pelanggaran_point'] ?>)</option>                              
                                  <?php 
                                }
                                ?>
                              </select>
                            </td>                            
                          </tr>
                          <?php 
                        }
                        ?>
                      </tbody>
                    </table>
                  </div>


                  <input type="submit" class="btn btn-primary" value="Simpan">

                </form>

              </div>

            </div>
          </div>
        </div>
      </div>
      <div class="box-body">
        <div class="table-responsive">
          <table class="table table-bordered table-striped" id="table-datatable">
            <thead>
              <tr>
                <th rowspan="2" width="1">NO</th>
                <th rowspan="2">NAMA</th>
                <?php 
                $jumlah_input = mysqli_query($koneksi,"select * from absen where absen_km='$id_km' group by absen_nomor");
                $ji = mysqli_num_rows($jumlah_input); 
                if($ji > 0){
                  ?>
                  <th class="text-center" colspan="<?php echo $ji ?>">Tanggal Pelajaran</th>
                <?php } ?>
                <th class="text-center" colspan="2">POIN</th>           
              </tr>

              <tr>
                <?php 
                $id_km = $k['km_id'];
                $tgl = mysqli_query($koneksi,"select * from absen where absen_km='$id_km' group by absen_nomor order by date(absen_tanggal) asc");                
                while($t = mysqli_fetch_array($tgl)){
                  ?>
                  <th class="text-center">

                    <button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modal_jurusan_edit_<?php echo $t['absen_id'] ?>">
                      <i class="fa fa-cog"></i>
                    </button>     






                    <!-- The Modal -->
                    <div class="modal" id="modal_jurusan_edit_<?php echo $t['absen_id'] ?>">
                      <div class="modal-dialog modal-lg">
                        <div class="modal-content" style="font-weight: normal; text-align: left;">

                          <!-- Modal Header -->
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Input Absen & Poin</h4>
                          </div>

                          <!-- Modal body -->
                          <div class="modal-body">

                            <form method="post" action="kelas_absen_update.php" onsubmit="return cek(this)">

                              <div class="form-group">
                                <label>Tanggal</label>                              
                                <input type="hidden" class="form-control" name="nomor" value="<?php echo $t['absen_nomor'] ?>">
                                <input type="hidden" class="form-control" name="km" value="<?php echo $t['absen_km'] ?>">
                                <input type="date" class="form-control" name="tanggal" required="required" value="<?php echo $t['absen_tanggal'] ?>">
                              </div>

                              <div class="table-responsive">
                                <table class="table table-bordered table-striped" id="table-datatable">
                                  <thead>
                                    <tr>
                                      <th width="1%">NO</th>
                                      <th>NAMA</th>
                                      <th>NIS</th>
                                      <th class="text-center" colspan="2">POIN</th>           
                                    </tr>
                                  </thead>
                                  <tbody>
                                    <?php 
                                    $no=1;
                                    $id = $id_kelas; 
                                    $data = mysqli_query($koneksi,"SELECT * FROM siswa, jurusan where siswa_jurusan=jurusan_id and siswa_id in (select ks_siswa from kelas_siswa where ks_kelas='$id') order by siswa_id desc");
                                    while($d = mysqli_fetch_array($data)){
                                      $id_km = $k['km_id'];
                                      $id_siswa = $d['siswa_id'];
                                      $nom = $t['absen_nomor'];
                                      $tabsen = mysqli_query($koneksi,"select * from absen where absen_km='$id_km' and absen_siswa='$id_siswa' and absen_nomor='$nom'");
                                      $tab = mysqli_fetch_assoc($tabsen);

                                      ?>
                                      <tr>
                                        <td><?php echo $no++; ?></td>
                                        <td><?php echo $d['siswa_nama']; ?></td>
                                        <td><?php echo $d['siswa_nis']; ?></td>
                                        <td>
                                          <input type="hidden" class="form-control" name="siswa[]" value="<?php echo $d['siswa_id'] ?>">
                                          <select class="form-control inputan" name="inputan[]" required="required">
                                            <option value="">-Pilih inputan</option>                            
                                            <option <?php if($tab['absen_inputan'] == "plus"){ echo "selected='selected'"; } ?> value="plus">Plus</option>                            
                                            <option <?php if($tab['absen_inputan'] == "minus"){ echo "selected='selected'"; } ?> value="minus">Minus</option>
                                                                        
                                          </select>

                                          <br>
                                          <?php 
                                        // echo $tab['absen_inputan'] ;
                                          if($tab['absen_inputan'] == "plus"){ 
                                            $tampil_plus = "";
                                          }else{
                                            $tampil_plus = 'style="display:none"';
                                          }

                                          if($tab['absen_inputan'] == "minus"){ 
                                            $tampil_minus = "";
                                          }else{
                                            $tampil_minus = 'style="display:none"';
                                          }
                                          ?>
                                          <select <?php echo $tampil_plus; ?> class="form-control  form_plus" name="prestasi[]">
                                            <option value="">- Pilih Prestasi</option>
                                            <?php 
                                            $prestasi = mysqli_query($koneksi,"select * from prestasi");
                                            while($p = mysqli_fetch_array($prestasi)){
                                              ?>
                                              <option <?php if($tab['absen_prestasi'] == $p['prestasi_id']){ echo "selected='selected'"; } ?> value="<?php echo $p['prestasi_id'] ?>"><?php echo $p['prestasi_nama'] ?> (+<?php echo $p['prestasi_point'] ?>)</option>
                                              <?php 
                                            }
                                            ?>
                                          </select>

                                          <select <?php echo $tampil_minus; ?> class="form-control  form_minus" name="pelanggaran[]">
                                            <option value="">- Pilih Pelanggaran</option>
                                            <?php 
                                            $pelanggaran = mysqli_query($koneksi,"select * from pelanggaran");
                                            while($p = mysqli_fetch_array($pelanggaran)){
                                              ?>
                                              <option <?php if($tab['absen_pelanggaran'] == $p['pelanggaran_id']){ echo "selected='selected'"; } ?> value="<?php echo $p['pelanggaran_id'] ?>"><?php echo $p['pelanggaran_nama'] ?> (-<?php echo $p['pelanggaran_point'] ?>)</option>                              
                                              <?php 
                                            }
                                            ?>
                                          </select>
                                        </td>                                      
                                      </tr>
                                      <?php 
                                    }
                                    ?>
                                  </tbody>
                                </table>
                              </div>


                              <input type="submit" class="btn btn-primary" value="Simpan">

                            </form>

                          </div>

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







                    <a class="btn btn-danger btn-xs" onclick="return confirm('yakin ingin hapus absen pada tanggal ini?')" href="kelas_absen_hapus.php?km=<?php echo $t['absen_km'] ?>&nomor=<?php echo $t['absen_nomor'] ?>"><i class="fa fa-close"></i></a>
                    <br>
                    <?php 
                    
                    echo date('l,d/m/Y', strtotime($t['absen_tanggal'])) ?>
                  </th>
               


                  <?php 
                }
                ?>
                <th class="text-center bg-success">Plus</th>
                <th class="text-center bg-danger">Minus</th>
              </tr>

            </thead>
            <tbody>
              <?php 
              $no=1;

              $id = $id_kelas; 
              $data = mysqli_query($koneksi,"SELECT * FROM siswa, jurusan where siswa_jurusan=jurusan_id and siswa_id in (select ks_siswa from kelas_siswa where ks_kelas='$id') order by siswa_id desc");
              while($d = mysqli_fetch_array($data)){

                $total_plus = 0;
                $total_minus = 0;

                ?>
                <tr>
                  <td><?php echo $no++; ?></td>
                  <td><?php echo $d['siswa_nama']; ?></td>
                  <?php 
                  $id_km = $k['km_id'];
                  $id_siswa = $d['siswa_id'];
                  $tgl = mysqli_query($koneksi,"select * from absen where absen_siswa='$id_siswa' and absen_km='$id_km' order by date(absen_tanggal) asc");
                  while($t = mysqli_fetch_array($tgl)){
                    ?>
                    <?php
                    $stat = $t['absen_inputan'];
                    if($stat == "plus"){
                      $id_prestasi = $t['absen_prestasi'];
                      $pres = mysqli_query($koneksi,"select * from prestasi where prestasi_id='$id_prestasi'");
                      $p = mysqli_fetch_assoc($pres);
                      $x_text = $p['prestasi_nama'];   
                      $total_plus += $p['prestasi_point'];
                    }else if($stat == "minus"){
                      $id_pelanggaran = $t['absen_pelanggaran'];
                      $pres = mysqli_query($koneksi,"select * from pelanggaran where pelanggaran_id='$id_pelanggaran'");
                      $p = mysqli_fetch_assoc($pres);
                      $x_text = $p['pelanggaran_nama'];                      
                      $total_minus += $p['pelanggaran_point'];
                    }
                    ?>

                    <?php 
                    if($x_text == "A"){
                      $warna = "text-bold bg-red text-white"; 
                    }else if($x_text == "I"){
                      $warna = "text-bold bg-yellow";
                    }else if($x_text == "X"){
                      $warna = "text-bold bg-black text-white"; 
                    }else{
                      $warna = "";
                    }
                    ?>

                    <td class="text-center <?php echo $warna; ?>">
                      <?php echo $x_text; ?>
                    </td>

                    <?php 
                  }
                  ?>
                  <td class="text-center text-bold bg-success">+<?php echo $total_plus ?></td>
                  <td class="text-center text-bold bg-danger">-<?php echo $total_minus ?></td>
                </tr>


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

    </div>

  </section>

</div>
<?php include 'footer.php'; ?>