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

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Kelas
      <!-- <small>Mata Pelajaran 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 
            $id = $_GET['id'];              
            $kelas = mysqli_query($koneksi, "select * from kelas, jurusan, ta where kelas_ta=ta_id and kelas_jurusan=jurusan_id and kelas_id='$id'");
            $k = mysqli_fetch_assoc($kelas);
            $id_kelas = $k['kelas_id'];
            $id_ta = $k['kelas_ta'];
            ?>

            <div class="table-responsive">
              <table class="table table-bordered">
                <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">Mata Pelajaran Kelas</h3>             
        <div class="btn-group pull-right">
          <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#modal_mapel">
            <i class="fa fa-plus"></i> &nbsp Tambahkan Mapel Ke Kelas
          </button>
        </div>

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

              <!-- Modal Header -->
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Pilih Mata Pelajaran</h4>
              </div>

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

                <form action="kelas_mapel_act.php" method="post">
                  <input type="hidden" value="<?php echo $k['kelas_id'] ?>" name="kelas">
                  <div class="form-group">
                    <label>Mata Pelajaran</label>
                    <select class="form-control" name="mapel" required="required">
                      <option value=""> - Pilih Mata Pelajaran - </option>
                      <?php 
                      $id_kelas = $_GET['id'];
                      $mapel = mysqli_query($koneksi,"select * from mapel where mapel_id not in (select km_mapel from kelas_mapel where km_kelas='$id_kelas')");
                      while($m = mysqli_fetch_array($mapel)){
                        ?>
                        <option value="<?php echo $m['mapel_id'] ?>"><?php echo $m['mapel_nama'] ?></option>
                        <?php 
                      }
                      ?>
                    </select>
                  </div>

                  <div class="form-group">
                    <label>Guru Pengajar</label>
                    <select class="form-control" name="guru" required="required">
                      <option value=""> - Pilih Guru Pengajar - </option>
                      <?php 
                      $ta = mysqli_query($koneksi,"select * from guru");
                      while($g = mysqli_fetch_array($ta)){
                        ?>
                        <option value="<?php echo $g['guru_id'] ?>"><?php echo $g['guru_nip'] ?> - <?php echo $g['guru_nama'] ?></option>
                        <?php 
                      }
                      ?>
                    </select>
                  </div>

                  <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Simpan">
                  </div>
                </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 width="1%">NO</th>
                <th>MATA PELAJARAN</th>
                <th>GURU PENGAJAR</th>
                <th width="20%">OPSI</th>
              </tr>
            </thead>
            <tbody>
              <?php 
              $no=1;
              $id = $_GET['id']; 
              $data = mysqli_query($koneksi,"SELECT * FROM mapel, kelas_mapel, guru where km_mapel=mapel_id and km_kelas='$id' and km_guru=guru_id");
              while($d = mysqli_fetch_array($data)){
                ?>
                <tr>
                  <td><?php echo $no++; ?></td>
                  <td><?php echo $d['mapel_nama']; ?></td>
                  <td><?php echo $d['guru_nama']; ?></td>
                  <td>                        
                    <a class="btn btn-success btn-sm" href="kelas_absen.php?km=<?php echo $d['km_id'] ?>"><i class="fa fa-book"></i> Absen & Poin</a>                                              
                    <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#modal_mapel_<?php echo $d['km_id'] ?>">
                      <i class="fa fa-wrench"></i>
                    </button>
                    <a class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin hapus? data yang terhubung juga akan ikut dihapus')" href="kelas_mapel_hapus.php?id=<?php echo $d['km_id'] ?>&kelas=<?php echo $d['km_kelas'] ?>"><i class="fa fa-close"></i></a>
                  </td>
                </tr>

                <!-- The Modal -->
                <div class="modal" id="modal_mapel_<?php echo $d['km_id'] ?>">
                  <div class="modal-dialog">
                    <div class="modal-content">

                      <!-- Modal Header -->
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Pilih Mata Pelajaran</h4>
                      </div>

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

                        <form action="kelas_mapel_update.php" method="post">
                          <input type="hidden" value="<?php echo $d['km_id'] ?>" name="km">
                          <input type="hidden" value="<?php echo $k['kelas_id'] ?>" name="kelas">
                          <div class="form-group">
                            <label>Mata Pelajaran</label>
                            <select class="form-control" name="mapel" required="required">
                              <option value=""> - Pilih Mata Pelajaran - </option>
                              <?php 
                              $id_kelas = $_GET['id'];
                              $id_mapel = $d['mapel_id'];
                              $mapel = mysqli_query($koneksi,"select * from mapel where mapel_id not in (select km_mapel from kelas_mapel where km_kelas='$id_kelas' and km_mapel!='$id_mapel')");
                              while($m = mysqli_fetch_array($mapel)){
                                ?>
                                <option <?php if($d['km_mapel'] == $m['mapel_id']){ echo "selected='selected'"; } ?> value="<?php echo $m['mapel_id'] ?>"><?php echo $m['mapel_nama'] ?></option>
                                <?php 
                              }
                              ?>
                            </select>
                          </div>

                          <div class="form-group">
                            <label>Guru Pengajar</label>
                            <select class="form-control" name="guru" required="required">
                              <option value=""> - Pilih Guru Pengajar - </option>
                              <?php 
                              $ta = mysqli_query($koneksi,"select * from guru");
                              while($g = mysqli_fetch_array($ta)){
                                ?>
                                <option <?php if($d['km_guru'] == $g['guru_id']){ echo "selected='selected'"; } ?> value="<?php echo $g['guru_id'] ?>"><?php echo $g['guru_nip'] ?> - <?php echo $g['guru_nama'] ?></option>
                                <?php 
                              }
                              ?>
                            </select>
                          </div>

                          <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Simpan">
                          </div>
                        </form>
                      </div>

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

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

    </div>

  </section>

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