<?php
include 'config/rupiah.php';
include 'config/variabel_default.php';
if ($_GET[act] == '') {
	if (isset($_GET['kelas']) && $_GET['kelas'] != "") {
		if (isset($_GET['status']) && $_GET['status'] != "") {
			$tampil = mysqli_query($conn, "SELECT * FROM view_detil_siswa WHERE idKelas='$_GET[kelas]' AND statusSiswa='$_GET[status]' ORDER BY idKelas ASC");
		} else {
			$tampil = mysqli_query($conn, "SELECT * FROM view_detil_siswa WHERE idKelas='$_GET[kelas]' ORDER BY idKelas ASC");
		}
		$kelas = $_GET['kelas'];
	} else {
		$tampil = mysqli_query($conn, "SELECT * FROM view_detil_siswa ORDER BY idKelas ASC");
	}
	$dtsiswa = mysqli_query($conn, "SELECT rb_nilai.*, 
	
	siswa.nmSiswa
	FROM
	rb_nilai
	 INNER JOIN siswa ON rb_nilai.nilai1 = siswa.nisnSiswa 
	");


?>
	<div class="col-xs-12">
		<div class="box box-warning">
			<div class="box-header with-border">
				<h3 class="box-title"> Data Raport Siswa </h3>
			</div><!-- /.box-header -->
			<div class="box-body">
				<?php
				if (isset($_GET['sukses'])) {
					echo "<div class='alert alert-success alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Sukses!</strong> - Data telah Berhasil Di Proses,..
					</div>";
				} elseif (isset($_GET['gagal'])) {
					echo "<div class='alert alert-danger alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Gagal!</strong> - Data tidak Di Proses, terjadi kesalahan dengan data..
					</div>";
				}
				?>
				<form method="GET" action="" class="form-horizontal">
					<input type="hidden" name="view" value="siswa" />
					<table class="table table-striped">
						<tbody>
							<tr>

								<td>

									<span class="pull-right">

										<a class="btn btn-danger  btn-sm" href="?view=raport&act=import">
											<i class="fa fa-file-excel-o"></i> Import Data Raport
										</a>
									</span>
								</td>
							</tr>
						</tbody>
					</table>
				</form>

				<div class="box-body">

				</div><!-- /.box-body -->

				<div class="table-responsive">
					<table id="example1" class="table table-bordered table-striped table-condensed">
						<thead>
							<tr>
								<th>No</th>
								<th>NISN</th>
								<th>Nama Siswa</th>
								<th>Jenis Kelamin</th>
								<th>Kelas</th>
								<th>Nama Ortu</th>
								<th>No.Hp</th>

								<th width="190">Aksi</th>

							</tr>
						</thead>

						<tbody>
							<?php
							$no = 1;
							while ($r = mysqli_fetch_array($tampil)) {
								echo "<tr><td>$no</td>
							
								<td>" . $r['nisnSiswa'] . "</td>
								<td>" . $r['nmSiswa'] . "</td>
								<td>" . $r['jkSiswa'] . "</td>
								<td>" . $r['nmKelas'] . "</td>
								<td>" . $r['nmOrtu'] . "</td>
								<td>" . $r['noHpOrtu'] . "</td>
								<td align='center'>
								<a class='btn btn-info btn-xs' title='Edit Data' target='_blank' href='cetak_raport_siswa.php?nisn=$r[nisnSiswa]'><span class='glyphicon glyphicon-print'> </span> Cetak Raport</a>
								<a class='btn btn-success btn-xs' title='Edit Data' target='_blank' href='excel_raport_siswa.php?nisn=$r[nisnSiswa]'><span class='fa fa-file-excel-o'> </span> Export Raport</a>
								</td>
								";
								echo "</tr>";
								$no++;
							}
							if (isset($_GET[hapus])) {
								mysqli_query($conn, "DELETE FROM siswa where idSiswa='$_GET[id]'");
								echo "<script>document.location='index.php?view=siswa';</script>";
							}

							?>
						</tbody>
					</table>
				</div>
			</div><!-- /.box -->


		</div><!-- /.box-body -->
	</div>

<?php
} elseif ($_GET[act] == 'import') {
?>
	<div class="col-md-12">
		<div class="box box-warning">
			<div class="box-header with-border">
				<h3 class="box-title"> Import Data Raport</h3>
				<span class="pull-right">
					<button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#ModalCetakSemuaSlip"><span class="fa fa-eye"></span> Lihat Data Kelompok Mapel</button>
				</span>
			</div><!-- /.box-header -->
			<div id="ModalCetakSemuaSlip" class="modal fade" role="dialog">
				<form method="GET" action="./slip_bulanan_persiswa_peritem.php" class="form-horizontal" target="_blank" title="Cetak Slip">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal">&times;</button>
								<h4 class="modal-title"> Data Kelompok Mapel</h4>
							</div>
							<div class="modal-body">

								<?php
								$sw = mysqli_query($conn, "SELECT *

							FROM rb_kelompok_mata_pelajaran 
							
							
							 ");
								?>
								<table class="table table-striped">
									<thead>
										<tr>
											<th>No.</th>
											<th>ID Kelompok Mapel</th>
											<th>Nama Kelompok Mata Pelajaran</th>
										</tr>
									</thead>
									<tbody>
										<?php
										$no = 1;
										while ($ds = mysqli_fetch_array($sw)) {
											echo "<tr>
												<td>$no</td>
												<td>$ds[id_kelompok_mata_pelajaran]</td>
												<td>$ds[nama_kelompok_mata_pelajaran]</td>
												
												
												
											</tr>";
											$no++;
										}
										?>
									</tbody>
								</table>
							</div>
							<div class="modal-footer">
								<input type="submit" value="Cetak" class="btn btn-success" onclick="checkTanggal('#tgl1','#tgl2');">
								<button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
							</div>
						</div>
					</div>
				</form>
			</div>
			<div class="box-body">
				<?php
				//jika tombol import ditekan
				if (isset($_POST['prosesimport'])) {

					$target = "temp/" . basename($_FILES['fileSiswa']['name']);
					move_uploaded_file($_FILES['fileSiswa']['tmp_name'], $target);

					$data = new Spreadsheet_Excel_Reader("temp/" . $_FILES['fileSiswa']['name'], false);

					//  menghitung jumlah baris file xls
					$baris = $data->rowcount($sheet_index = 0);

					//    import data excel mulai baris ke-2 (karena tabel xls ada header pada baris 1)
					for ($i = 2; $i <= $baris; $i++) {
						// membaca data (kolom ke-1 sd terakhir)
						$nis 		= $data->val($i, 1);
						$nisn   	= $data->val($i, 2);
						$nama  	= $data->val($i, 3);
						$jk  		= $data->val($i, 4);
						$agama  	= $data->val($i, 5);
						$idkelas 	= $data->val($i, 6);
						$ud 	= $data->val($i, 7);
						$lokasi_siswa = '1';
						// setelah data dibaca, masukkan ke tabel pegawai sql
						$hasil = mysqli_query($conn, "INSERT INTO rb_nilai(id_kelompok_mapel,paralel,kode,nisn,nama_mapel,nilai_peng,nilai_ket,user_akses,waktu) 
							VALUES('$nis','$nisn','$nama','$jk','$agama','$idkelas','$ud','$lokasi_siswa','$waktu_sekarang')");
					}

					if (!$hasil) {
						//          jika import gagal

						echo "<div class='alert alert-danger' role='alert'>
						Data Gagal Diimport....!<br>" . die(mysqli_error()) . "
						</div>";
					} else {
						//          jika impor berhasil
						echo "<div class='alert alert-success' role='alert'>
						Data Berhasil Diimport....!
						</div>";
					}

					//    hapus file xls yang udah dibaca
					unlink("temp/" . $_FILES['fileSiswa']['name']);
				}
				?>

				<form method="POST" action="" class="form-horizontal" onSubmit="return validateForm()" enctype="multipart/form-data">
					<div class="form-group">
						<label for="" class="col-sm-4 control-label">Download Format Data Raport</label>
						<div class="col-sm-8">
							<a href="./files/format_nilai_pengetahuan.xls" class="btn btn-info">formatdataraport.xls</a>
						</div>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-4 control-label">Pilih File Excel (.xls / Format Excel 2003)</label>
						<div class="col-sm-8">
							<input type="file" name="fileSiswa" class="form-control" id="fileSiswa" placeholder="" required>
							Lihat Data Kelompok Mapel sebelum Import
						</div>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-4 control-label"></label>
						<div class="col-sm-8">
							<input type="submit" name="prosesimport" value="Proses Import" class="btn btn-success">
							<a href="?view=raport" class="btn btn-default">Cancel</a>
						</div>
					</div>
				</form>
				
			</div>
		</div>
	</div>
	<script type="text/javascript">
		//    validasi form (hanya file .xls yang diijinkan)
		function validateForm() {
			function hasExtension(inputID, exts) {
				var fileName = document.getElementById(inputID).value;
				return (new RegExp(' (' + exts.join('|').replace(/\./g, '\\.') + ')$')).test(fileName);
			}

		}
	</script>
<?php
}
?>

<script type="text/javascript">
	function readURL(input) {
		if (input.files && input.files[0]) {
			var reader = new FileReader();
			reader.onload = function(e) {
				$('#target').attr('src', e.target.result);
			};

			reader.readAsDataURL(input.files[0]);
		}
	}

	$("#foto").change(function() {
		readURL(this);
	});
</script>