<?php
include '../../config/koneksi.php';
include 'config/rupiah.php';
if ($_GET['act'] == '') { ?>


  <div class="col-xs-12">
    <div class="box">
      <div class="box-body">

        <div class="col-sm-12">
          <a class="btn btn-success pull-right" href="?view=nasabah&act=tambah"><span class='glyphicon glyphicon-plus'></span> Tambah Data</a>
        </div>
      </div>
      <!-- /.box-header -->
      <div class="table-responsive">

        <table id="example1" class="table table-responsive no-padding table-striped">
          <thead>
            <tr>
              <th width="50">No</th>
              <th>ID Nasabah</th>
              <th>Nisn</th>
              <th>Nama</th>
              <th>Kelas</th>
              <th>Orang Tua</th>
              <th>Saldo</th>

            </tr>
          </thead>
          <tbody>
            <?php
            $no = 0;
            $query = mysqli_query($conn,"SELECT rb_nasabah.*,
									siswa.nmSiswa,siswa.nisnSiswa,siswa.nmOrtu,siswa.saldo, kelas_siswa.nmKelas FROM rb_nasabah 
                  JOIN siswa ON rb_nasabah.idSiswa=siswa.idSiswa 
                  JOIN kelas_siswa ON rb_nasabah.idKelas=kelas_siswa.idKelas 
            ORDER BY rb_nasabah.idSiswa DESC");
            while ($row = mysqli_fetch_array($query)) {
              $no++;
            ?>
              <tr>
                <td><?php echo $no; ?></td>
                <td><?php echo $row['idSiswa']; ?></td>
                <td><?php echo $row['nisnSiswa']; ?></td>
                <td><?php echo $row['nmSiswa']; ?></td>
                <td><?php echo $row['nmKelas']; ?></td>
                <td><?php echo $row['nmOrtu']; ?></td>
                <td>Rp. <?php echo rupiah($row['saldo']); ?></td>


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


<?php
} elseif ($_GET[act] == 'tambah') {
  if (isset($_POST['simpanekstra'])) {
    //$nData = $_POST['jmldata']; // membaca jumlah data

    $siswa = $_POST['pilih'];
    $nData = count($siswa);
    $idEkstra = $_POST['id'];
    // looping
    for ($i = 0; $i < $nData; $i++) {
      $nisnSiswa = $siswa[$i];
      $ekstra = $idEkstra[$i];


      $query = mysqli_query($conn,"INSERT INTO rb_nasabah(idSiswa,idKelas)
									VALUES('$nisnSiswa','$ekstra'
											)");
    }

    if ($query) {
      echo "<script>document.location='?view=nasabah&sukses';</script>";
    } else {
      echo "<script>document.location='?view=nasabah&gagal';</script>";
    }
  }

  $sqlEdit = mysqli_query($conn,"SELECT
							rb_jenis_ekstra.*,
							rb_pos_extra.nmPosEkstra
							
						FROM
							rb_jenis_ekstra
						INNER JOIN rb_pos_extra ON rb_jenis_ekstra.idPos = rb_pos_extra.idPos
						WHERE rb_jenis_ekstra.idEkstra='$_GET[jenis]'");
  $record = mysqli_fetch_array($sqlEdit);
?>
  <div class="col-md-12">
    <div class="box box-solid box-primary">
      <div class="box-header with-border">
        <h3 class="box-title"> Data Siswa Per Kelas</h3>
      </div><!-- /.box-header -->
      <div class="box-body">
        <div class="col-sm-5">
          <div class="box box-solid box-danger">
            <div class="box-header">
              <h3 class="box-title">Pilih Kelas</h3>
            </div>
            <div class="box-body">
              <form method="GET" action="" class="form-horizontal">
                <input type="hidden" name="view" value="nasabah">

                <input type="hidden" name="act" value="tambah">


                <div class="form-group">
                  <label for="" class="col-sm-4 control-label">Kelas</label>
                  <div class="col-sm-8">
                    <select name="idKelas" class="form-control">
                      <?php
                      $sqk = mysqli_query($conn,"SELECT * FROM kelas_siswa ORDER BY idKelas ASC");
                      while ($k = mysqli_fetch_array($sqk)) {
                        echo "<option value=" . $k['idKelas'] . ">" . $k['nmKelas'] . "</option>";
                      }
                      ?>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="" class="col-sm-4 control-label"></label>
                  <div class="col-sm-8">
                    <input type="submit" name="tampil" class="btn btn-warning btn-sm" value="Tampilkan">
                  </div>
                </div>
              </form>
            </div>
          </div>

        </div>
        <?php
        $sqlEdit = mysqli_query($conn,"SELECT
							rb_jenis_ekstra.*,
							rb_pos_extra.nmPosEkstra
							
						FROM
							rb_jenis_ekstra
						INNER JOIN rb_pos_extra ON rb_jenis_ekstra.idPos = rb_pos_extra.idPos
						WHERE rb_jenis_ekstra.idEkstra='$_GET[jenis]'");
        $record = mysqli_fetch_array($sqlEdit); ?>
        <div class="col-sm-7">
          <?php
          if (isset($_GET['tampil'])) {
            $sqlSiswaTagihan = mysqli_query($conn,"SELECT siswa.*, kelas_siswa.nmKelas
									FROM
										siswa
									INNER JOIN kelas_siswa ON siswa.idKelas = kelas_siswa.idKelas 
									WHERE siswa.idKelas='$_GET[idKelas]' ");
            //$n = mysql_num_rows($sqlSiswaTagihan); // membaca jumlah data
          ?>
            <div class="box box-solid box-success">
              <div class="box-header">
                <h3 class="box-title">Tentukan Siswa Sebagai Nasabah</h3>
              </div>
              <form method="POST" action="" class="form-horizontal">

                <div class="table-responsive">
                  <table class="table table-striped">
                    <tr>
                      <th>No.</th>
                      <th><input type="checkbox" id="parent"></th>
                      <th>NIS</th>
                      <th>Nama Siswa</th>
                    </tr>
                    <?php
                    $no = 1;
                    while ($ft = mysqli_fetch_array($sqlSiswaTagihan)) {
                    ?>
                      <tr>
                        <td><?php echo $no; ?></td>
                        <input type="hidden" name="id[]" value="<?php echo $ft['idKelas']; ?>">

                        <td><input type="checkbox" name="pilih[]" value="<?php echo $ft['idSiswa']; ?>" class="child"></td>
                        <td><?php echo $ft['nisSiswa']; ?></td>
                        <td><?php echo $ft['nmSiswa']; ?></td>


                      </tr>
                    <?php $no++;
                    } ?>
                  </table>
                </div>
                <div class="box-footer">
                  <!--<input type="hidden" name="jmldata" value="<?php echo $n; ?>">-->
                  <input type="submit" name="simpanekstra" value="Simpan " class="btn btn-success">
                  <a href="?view=nasabah" class="btn btn-default">Cancel</a>
                </div>
              </form>
            </div>
          <?php } ?>
        </div>
      </div>
    </div>
  </div>
<?php } ?>