<?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 view_detil_siswa.*, tagihan_bulanan.statusBayar,tagihan_bebas.statusBayar,
		nmSiswa,nisnSiswa,jkSiswa,nmKelas,nmOrtu,noHpOrtu,saldo FROM view_detil_siswa 
		LEFT JOIN tagihan_bulanan ON view_detil_siswa.idSiswa = tagihan_bulanan.idSiswa
		LEFT JOIN tagihan_bebas ON view_detil_siswa.idSiswa = tagihan_bebas.idSiswa
		where view_detil_siswa.statusSiswa='Lulus' and view_detil_siswa.idSiswa='$_SESSION[idsa]' group by view_detil_siswa.idSiswa  ASC");
	}


	$ta = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tahun_ajaran where aktif='Y'"));
	$idTahun = $ta['idTahunAjaran'];
	$tahun = $ta['nmTahunAjaran'];

	$idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas where npsn='10700295'"));
	$pos = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM pos_bayar where idPosBayar='$_GET[pos]'"));

	$idsiswa = $_GET['siswa'];
	$dtsiswa = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM view_detil_siswa where idSiswa='$_SESSION[idsa]'"));
	if ($dtsiswa['foto'] == '') {
		$foto_users = $lokasi_default_fotoPengguna;
	} else {
		$foto_users = $lokasi_penyimpanan_fotoSiswa . $dtsiswa['foto'];
	}
	$nissiswa = $dtsiswa['nisSiswa'];
	$namasiswa = $dtsiswa['nmSiswa'];
	$namakelas = $dtsiswa['nmKelas'];
?>
	<div class="col-xs-12">
		<div class="box box-warning">
			<div class="box-header with-border">
				<h3 class="box-title"> Data Surat Kelulusan 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>";
				}
				?>
				<?php
				$no = 1;
				$n = mysqli_num_rows($tampil); // membaca jumlah data
				while ($r = mysqli_fetch_array($tampil)) {

					echo "";

					$no = 1;
					$totDibayar = 0;
					$totTagihan = 0;
					$sqlJenisBayar = mysqli_query($conn, "SELECT jenis_bayar.*, pos_bayar.nmPosBayar 
									FROM jenis_bayar INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
									 WHERE jenis_bayar.idTahunAjaran='$idTahun'");
					while ($djb = mysqli_fetch_array($sqlJenisBayar)) {
						if ($djb['tipeBayar'] == 'bulanan') {
							//menghitung semua tagihan bulanan
							$tgbul 	=	mysqli_fetch_array(mysqli_query($conn, "SELECT
											jenis_bayar.idPosBayar,
											pos_bayar.nmPosBayar,
											tagihan_bulanan.idSiswa,
											Sum(tagihan_bulanan.jumlahBayar) AS TotalSemuaTagihanBulanan
											FROM
											tagihan_bulanan
											INNER JOIN jenis_bayar ON tagihan_bulanan.idJenisBayar = jenis_bayar.idJenisBayar
											INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
											WHERE tagihan_bulanan.idJenisBayar='$djb[idJenisBayar]' AND tagihan_bulanan.idSiswa='$r[idSiswa]'
											GROUP BY
											jenis_bayar.idPosBayar"));
							$semuaTagihan = $tgbul['TotalSemuaTagihanBulanan'];

							$dbayar = mysqli_fetch_array(mysqli_query($conn, "SELECT
											jenis_bayar.idPosBayar,
											pos_bayar.nmPosBayar,
											jenis_bayar.idTahunAjaran,
											tahun_ajaran.nmTahunAjaran,
											jenis_bayar.nmJenisBayar,
											Sum(tagihan_bulanan.jumlahBayar) AS TotalPembayaranPerJenis,
											tagihan_bulanan.statusBayar
											FROM
											tagihan_bulanan
											INNER JOIN jenis_bayar ON tagihan_bulanan.idJenisBayar = jenis_bayar.idJenisBayar
											INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
											INNER JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
											WHERE tagihan_bulanan.idJenisBayar='$djb[idJenisBayar]' AND tagihan_bulanan.statusBayar='1' AND tagihan_bulanan.idSiswa='$r[idSiswa]'
											GROUP BY
											tagihan_bulanan.idJenisBayar"));
							$jBayar 	= $dbayar['TotalPembayaranPerJenis'];
							$tagihan 	= $semuaTagihan - $jBayar;
						} else {
							//menghitung semua tagihan bebas
							$tgb 	= 	mysqli_fetch_array(mysqli_query($conn, "SELECT
												tagihan_bebas.idTagihanBebas,
												jenis_bayar.idPosBayar,
												pos_bayar.nmPosBayar,
												tagihan_bebas.idSiswa,
												SUM(tagihan_bebas.totalTagihan) As TotalSemuaTagihanBebas
												FROM
												tagihan_bebas
												INNER JOIN jenis_bayar ON tagihan_bebas.idJenisBayar = jenis_bayar.idJenisBayar
												INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
												WHERE tagihan_bebas.idJenisBayar='$djb[idJenisBayar]' AND tagihan_bebas.idSiswa='$idsiswa'
												GROUP BY
												jenis_bayar.idPosBayar"));
							$semuaTagihan = $tgb['TotalSemuaTagihanBebas'];

							$dbayar = mysqli_fetch_array(mysqli_query($conn, "SELECT
											tagihan_bebas.idJenisBayar,
											jenis_bayar.nmJenisBayar,
											jenis_bayar.idTahunAjaran,
											tahun_ajaran.nmTahunAjaran,
											tagihan_bebas_bayar.idTagihanBebas,
											Sum(tagihan_bebas_bayar.jumlahBayar) AS TotalPembayaranPerJenis,
											tagihan_bebas_bayar.ketBayar
											FROM
											tagihan_bebas_bayar
											INNER JOIN tagihan_bebas ON tagihan_bebas_bayar.idTagihanBebas = tagihan_bebas.idTagihanBebas
											INNER JOIN jenis_bayar ON tagihan_bebas.idJenisBayar = jenis_bayar.idJenisBayar
											INNER JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
											WHERE tagihan_bebas_bayar.idTagihanBebas='$tgb[idTagihanBebas]'
											GROUP BY
											tagihan_bebas_bayar.idTagihanBebas"));
							$jBayar 	= $dbayar['TotalPembayaranPerJenis'];
							$tagihan 	= $semuaTagihan - $jBayar;
						}
						echo "";
						$no++;
						$totDibayar += $jBayar;
						$totTagihan += $tagihan;
					}
					if ($totTagihan == 0) {
						$status = "<label class='label label-danger'>Belum Bayar</label>";
						$icon = "fa-plus";
						$btn = "btn-success";
						$color = "red";
						$alt = "Bayar";
						$btncetak = "";
					} else {
						$status = "<label class='label label-warning'>Belum Lengkap</label>";
						$icon = "fa-plus";
						$btn = "btn-danger";
						$color = "red";
						$alt = "Bayar";
						$btncetak = "disabled";
					}
					// 	echo "<td align='center'><a class='btn $btn btn-xs $btncetak' title='Edit Data' target='_blank' href='cetak_skl.php?id=$r[idSiswa]'><span class='glyphicon glyphicon-print'> </span> Cetak</a>
					// </td>";
					echo "</tr>";
					$no++;
				}


				?>

				<div class="col-md-8">
					<!-- About Me Box -->
					<div class="box box-success">
						<div class="box-header with-border">
							<h3 class="box-title">About Me</h3>
						</div>
						<!-- /.box-header -->
						<div class="box-body">
							<strong><i class="fa fa-book margin-r-5"></i> Nama Lengkap</strong>
							<p class="text-muted"><?= $dtsiswa['nmSiswa'] ?></p>

							<hr>

							<strong><i class="fa fa-envelope margin-r-5"></i> NIS</strong>
							<p class="text-muted"><?= $dtsiswa['nisSiswa'] ?></p>

							<hr>

							<strong><i class="fa fa-file-text-o margin-r-5"></i> NISN</strong>
							<p><?= $dtsiswa['nisnSiswa'] ?></p>


							<td>
								<center><a class='btn <?php echo $btn; ?> btn-lg <?php echo $btncetak; ?>' title='Edit Data' target='_blank' href='cetak_skl.php?id=<?php echo $dtsiswa['idSiswa']; ?>'><span class='glyphicon glyphicon-print'> </span> Cetak Surat Keterangan Lulus</a>
								</center>
							</td>
						</div>
						<!-- /.box-body -->
					</div>
					<!-- /.box -->

				</div>
				<div class="col-md-4">
					<!-- Profile Image -->
					<div class="box box-success">
						<div class="box-body box-profile">
							<img src="<?= $foto_users ?>" class="profile-user-img img-responsive img-circle">

							<h3 class="profile-username text-center"><?= $dtsiswa[nmSiswa] ?></h3>

							<p class="text-muted text-center"><?= $dtsiswa[username] ?></p>
							<center>
								<h4>Selamat, Telah menyelesaikan seluruh program pembelajaran di MA <?php echo $idt['nmSekolah']; ?> Kota <?php echo $idt['kabupaten']; ?></h4>
							</center><br>
							<p class="text-muted text-center">Silahkan cetak Surat Keterangan Lulus Anda, Tombol akan aktif jika anda telah menyelesaikan seluruh biaya administrasi.</p>
						</div>
						<!-- /.box-body -->
					</div>
					<!-- /.box -->
				</div>


			</div><!-- /.box-body -->
		</div><!-- /.box -->
	</div>
<?php
} elseif ($_GET[act] == 'edit') {
	if (isset($_POST[update])) {
		$nama_file = $_FILES['fotoSiswa']['name'];
		$ukuran_file = $_FILES['fotoSiswa']['size'];
		$tipe_file = $_FILES['fotoSiswa']['type'];
		$tmp_file = $_FILES['fotoSiswa']['tmp_name'];
		// Set path folder tempat menyimpan gambarnya
		$path = "gambar/" . $nama_file;
		if ($ukuran_file <= 1000000) { // Cek apakah ukuran file yang diupload kurang dari sama dengan 1MB
			// Jika ukuran file kurang dari sama dengan 1MB, lakukan :
			// Proses upload
			if (move_uploaded_file($tmp_file, $path)) { // Cek apakah gambar berhasil diupload atau tidak
				// Jika gambar berhasil diupload, Lakukan :  
				// Proses simpan ke Database
				$query = mysqli_query($conn, "UPDATE siswa SET nisSiswa='$_POST[nisSiswa]',nisnSiswa='$_POST[nisnSiswa]',nmSiswa='$_POST[nmSiswa]',jkSiswa='$_POST[jkSiswa]',agamaSiswa='$_POST[agamaSiswa]',tmptLahir='$_POST[tmptLahir]',tglLahirSiswa='$_POST[tglLahir]',level='$_POST[level]',statusKeluarga='$_POST[statusKeluarga]',anakKe='$_POST[anakKe]',sekolah='$_POST[sekolah]',diterimaKelas='$_POST[diterimaKelas]',tglMasuk='$_POST[tglMasuk]',
				nmOrtuIbu='$_POST[nmOrtuIbu]',kerjaAyah='$_POST[kerjaAyah]',kerjaIbu='$_POST[kerjaIbu]',nmWali='$_POST[nmWali]',kerjaWali='$_POST[kerjaWali]',alamatWali='$_POST[alamatWali]',idKelas='$_POST[idKelas]',statusSiswa='$_POST[statusSiswa]',nmOrtu='$_POST[nmOrtu]',username='$_POST[username]',password='$_POST[password]',alamatOrtu='$_POST[alamat]',noHpOrtu='$_POST[noHp]',noHpSis='$_POST[noHpSis]', foto='$nama_file'
					WHERE idSiswa='$_POST[id]'");
			} else {
				$query = mysqli_query($conn, "UPDATE siswa SET nisSiswa='$_POST[nisSiswa]',nisnSiswa='$_POST[nisnSiswa]',nmSiswa='$_POST[nmSiswa]',jkSiswa='$_POST[jkSiswa]',agamaSiswa='$_POST[agamaSiswa]',tmptLahir='$_POST[tmptLahir]',tglLahirSiswa='$_POST[tglLahir]',level='$_POST[level]',statusKeluarga='$_POST[statusKeluarga]',anakKe='$_POST[anakKe]',sekolah='$_POST[sekolah]',diterimaKelas='$_POST[diterimaKelas]',tglMasuk='$_POST[tglMasuk]',
				nmOrtuIbu='$_POST[nmOrtuIbu]',kerjaAyah='$_POST[kerjaAyah]',kerjaIbu='$_POST[kerjaIbu]',nmWali='$_POST[nmWali]',kerjaWali='$_POST[kerjaWali]',alamatWali='$_POST[alamatWali]',idKelas='$_POST[idKelas]',statusSiswa='$_POST[statusSiswa]',nmOrtu='$_POST[nmOrtu]',username='$_POST[username]',password='$_POST[password]',alamatOrtu='$_POST[alamat]',noHpOrtu='$_POST[noHp]',noHpSis='$_POST[noHpSis]'
					WHERE idSiswa='$_POST[id]'");
			}
		}

		if ($query) {
			echo "<script>document.location='index.php?view=siswa&sukses';</script>";
		} else {
			echo "<script>document.location='index.php?view=siswa&gagal';</script>";
		}
	}
	$edit = mysqli_query($conn, "SELECT * FROM view_detil_siswa where idSiswa='$_GET[id]'");
	$record = mysqli_fetch_array($edit);
?>
	<div class="col-md-12">
		<div class="box box-warning">
			<div class="box-header with-border">
				<h3 class="box-title"> Edit Data Siswa</h3>
			</div><!-- /.box-header -->
			<div class="box-body">
				<form method="post" action="" class="form-horizontal" enctype="multipart/form-data">
					<input type="hidden" name="id" value="<?php echo $record['idSiswa']; ?>">
					<div class="col-sm-6">
						<label for="" class=" control-label">NIS</label>

						<input type="text" name="nisSiswa" class="form-control" value="<?php echo $record['nisSiswa']; ?>" placeholder="" required>

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">NISN</label>

						<input type="text" name="nisnSiswa" class="form-control" value="<?php echo $record['nisnSiswa']; ?>" placeholder="">

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Nama Siswa</label>

						<input type="text" name="nmSiswa" class="form-control" value="<?php echo $record['nmSiswa']; ?>" placeholder="" required>

					</div>
					<div class="col-sm-6">
						<label for="" class="control-label">Jenis Kelamin</label>

						<select class="form-control" name="jkSiswa">
							<option value="<?php echo $record['jkSiswa']; ?>"><?php echo $record['jkSiswa']; ?></option>
							<option value="L">L</option>
							<option value="P">P</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Tempat Lahir</label>

						<input type="text" name="tmptLahir" class="form-control" value="<?php echo $record['tmptLahir']; ?>" id="" placeholder="" required>

					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Tanggal Lahir</label>

						<input type="text" name="tglLahir" value="<?php echo $record['tglLahirSiswa']; ?>" class="form-control pull-right date-picker">


					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Status Dalam Keluarga</label>

						<input type="text" name="statusKeluarga" class="form-control" value="<?php echo $record['statusKeluarga']; ?>" placeholder="">

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Anak Ke</label>

						<input type="text" name="anakKe" class="form-control" value="<?php echo $record['anakKe']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Agama</label>

						<select class="form-control" name="agamaSiswa">
							<option value="<?php echo $record['agamaSiswa']; ?>"><?php echo $record['agamaSiswa']; ?></option>
							<option value="Islam">Islam</option>
							<option value="Katolik">Katolik</option>
							<option value="Protestan">Protestan</option>
							<option value="Hindu">Hindu</option>
							<option value="Budha">Budha</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Level</label>

						<select class="form-control" name="level">
							<option value="<?php echo $record['level']; ?>"><?php echo $record['level']; ?></option>
							<option value="siswa">Siswa</option>
							<option value="ketuakelas">Ketua Kelas</option>

						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Kelas</label>

						<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)) {
								$selected = ($k['idKelas'] == $record['idKelas']) ? ' selected="selected"' : "";

								echo '<option value="' . $k['idKelas'] . '" ' . $selected . '>' . $k['nmKelas'] . '</option>';
							}
							?>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Sekolah Asal</label>

						<input type="text" name="sekolah" class="form-control" value="<?php echo $record['sekolah']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Diterima Kelas</label>
						<select name="diterimaKelas" class="form-control">
							<?php
							$sqk = mysqli_query($conn, "SELECT * FROM kelas_siswa ORDER BY idKelas ASC");
							while ($k = mysqli_fetch_array($sqk)) {
								$selected = ($k['idKelas'] == $record['idKelas']) ? ' selected="selected"' : "";

								echo '<option value="' . $k['idKelas'] . '" ' . $selected . '>' . $k['nmKelas'] . '</option>';
							}
							?>
						</select>
					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Diterima Pada</label>

						<input type="text" name="tglMasuk" value="<?php echo $record['tglMasuk']; ?>" class="form-control pull-right date-picker">


					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Alamat</label>

						<input type="text" name="alamat" class="form-control" value="<?php echo $record['alamatOrtu']; ?>" placeholder="" required>

					</div>
					<div class="col-md-3">
						<label for="" class=" control-label">No.Hp Ortu</label>

						<input type="text" name="noHp" class="form-control" value="<?php echo $record['noHpOrtu']; ?>" placeholder="" required>

					</div>
					<div class="col-md-3">
						<label for="" class=" control-label">No.Hp Siswa</label>

						<input type="text" name="noHpSis" class="form-control" value="<?php echo $record['noHpSis']; ?>" placeholder="" required>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Ayah</label>

						<input type="text" name="nmOrtu" class="form-control" value="<?php echo $record['nmOrtu']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Ibu</label>

						<input type="text" name="nmOrtuIbu" class="form-control" value="<?php echo $record['nmOrtuIbu']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Ayah</label>

						<input type="text" name="kerjaAyah" class="form-control" value="<?php echo $record['kerjaAyah']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Ibu</label>

						<input type="text" name="kerjaIbu" class="form-control" value="<?php echo $record['kerjaIbu']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Wali</label>

						<input type="text" name="nmWali" class="form-control" value="<?php echo $record['nmWali']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Wali</label>

						<input type="text" name="kerjaWali" class="form-control" value="<?php echo $record['kerjaWali']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Alamat Wali</label>

						<input type="text" name="alamatWali" class="form-control" value="<?php echo $record['alamatWali']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Username</label>

						<input type="text" name="username" class="form-control" value="<?php echo $record['username']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Password</label>

						<input type="text" name="password" class="form-control" value="<?php echo $record['password']; ?>" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Status</label>

						<select class="form-control" name="statusSiswa">
							<option value="<?php echo $record['statusSiswa']; ?>"><?php echo $record['statusSiswa']; ?></option>
							<option value="Aktif">Aktif</option>
							<option value="Non Aktif">Non Aktif</option>
							<option value="Drop Out">Drop Out</option>
							<option value="Pindah">Pindah</option>
							<option value="Lulus">Lulus</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Foto</label>


						<img src="<?= 'gambar/' . $record[foto] ?>" id="target" alt="Choose image to upload" class="img-thumbnail img-responsive">
						<input type="file" class="form-control form-control-sm" name="fotoSiswa" id="foto" accept=".png, .jpg, .jpeg">

					</div>

					<div class="form-group">
						<label for="" class=" control-label"></label>
						<div class="col-sm-10">
							<input type="submit" name="update" value="Update" class="btn btn-success">
							<a href="index.php?view=siswa" class="btn btn-default">Cancel</a>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>

<?php
} elseif ($_GET[act] == 'tambah') {
	if (isset($_POST[tambah])) {
		$nama_file = $_FILES['fotoSiswa']['name'];
		$ukuran_file = $_FILES['fotoSiswa']['size'];
		$tipe_file = $_FILES['fotoSiswa']['type'];
		$tmp_file = $_FILES['fotoSiswa']['tmp_name'];
		// Set path folder tempat menyimpan gambarnya
		$path = "gambar/" . $nama_file;
		if ($ukuran_file <= 1000000) { // Cek apakah ukuran file yang diupload kurang dari sama dengan 1MB
			// Jika ukuran file kurang dari sama dengan 1MB, lakukan :
			// Proses upload
			if (move_uploaded_file($tmp_file, $path)) { // Cek apakah gambar berhasil diupload atau tidak
				// Jika gambar berhasil diupload, Lakukan :  
				// Proses simpan ke Database
				$query = mysqli_query($conn, "INSERT INTO siswa(nisSiswa,nisnSiswa,nmSiswa,jkSiswa,level,agamaSiswa,tglLahirSiswa,tmptLahir,statusKeluarga,anakKe,sekolah,diterimaKelas,tglMasuk,nmOrtuIbu,kerjaAyah,kerjaIbu,nmWali,kerjaWali,alamatWali,idKelas,alamatOrtu,noHpOrtu,noHpSis,nmOrtu,username,password, foto) 
				VALUES('$_POST[nisSiswa]','$_POST[nisnSiswa]','$_POST[nmSiswa]','$_POST[jkSiswa]','$_POST[level]','$_POST[agamaSiswa]','$_POST[tglLahir]','$_POST[statusKeluarga]','$_POST[anakKe]','$_POST[sekolah]','$_POST[diterimaKelas]','$_POST[tglMasuk]','$_POST[nmOrtuIbu]','$_POST[kerjaAyah]','$_POST[kerjaIbu]','$_POST[nmWali]','$_POST[kerjaWali]','$_POST[alamatWali]','$_POST[idKelas]','$_POST[alamat]','$_POST[noHp]','$_POST[nmOrtu]','$_POST[username]','$_POST[password]','$nama_file')");
			} else {
				$query = mysqli_query($conn, "INSERT INTO siswa(nisSiswa,nisnSiswa,nmSiswa,jkSiswa,level,agamaSiswa,tglLahirSiswa,tmptLahir,statusKeluarga,anakKe,sekolah,diterimaKelas,tglMasuk,nmOrtuIbu,kerjaAyah,kerjaIbu,nmWali,kerjaWali,alamatWali,idKelas,alamatOrtu,noHpOrtu,nmOrtu,username,password) 
									  VALUES('$_POST[nisSiswa]','$_POST[nisnSiswa]','$_POST[nmSiswa]','$_POST[jkSiswa]','$_POST[level]','$_POST[agamaSiswa]','$_POST[tglLahir]','$_POST[statusKeluarga]','$_POST[anakKe]','$_POST[sekolah]','$_POST[diterimaKelas]','$_POST[tglMasuk]','$_POST[nmOrtuIbu]','$_POST[kerjaAyah]','$_POST[kerjaIbu]','$_POST[nmWali]','$_POST[kerjaWali]','$_POST[alamatWali]','$_POST[idKelas]','$_POST[alamat]','$_POST[noHp]','$_POST[nmOrtu]','$_POST[username]','$_POST[password]')");
			}
		}

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

?>
	<div class="col-md-12">
		<div class="box box-warning">
			<div class="box-header with-border">
				<h3 class="box-title"> Tambah Data Siswa</h3>
			</div><!-- /.box-header -->
			<div class="box-body">
				<form method="POST" action="" class="form-horizontal" enctype="multipart/form-data">
					<div class="col-sm-6">
						<label for="" class=" control-label">NIS</label>

						<input type="text" name="nisSiswa" class="form-control" placeholder="" required>

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">NISN</label>

						<input type="text" name="nisnSiswa" class="form-control" placeholder="">

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Nama Siswa</label>

						<input type="text" name="nmSiswa" class="form-control" placeholder="" required>

					</div>
					<div class="col-sm-6">
						<label for="" class="control-label">Jenis Kelamin</label>

						<select class="form-control" name="jkSiswa">
							<option value="L">L</option>
							<option value="P">P</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Tempat Lahir</label>

						<input type="text" name="tmptLahir" class="form-control" id="" placeholder="" required>

					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Tanggal Lahir</label>

						<input type="text" name="tglLahir" class="form-control pull-right date-picker">


					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Status Dalam Keluarga</label>

						<input type="text" name="statusKeluarga" class="form-control" placeholder="">

					</div>
					<div class="col-sm-6">
						<label for="" class=" control-label">Anak Ke</label>

						<input type="text" name="anakKe" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Agama</label>

						<select class="form-control" name="agamaSiswa">

							<option value="Islam">Islam</option>
							<option value="Katolik">Katolik</option>
							<option value="Protestan">Protestan</option>
							<option value="Hindu">Hindu</option>
							<option value="Budha">Budha</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Level</label>

						<select class="form-control" name="level">

							<option value="siswa">Siswa</option>
							<option value="ketuakelas">Ketua Kelas</option>

						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Kelas</label>

						<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)) {
								$selected = ($k['idKelas'] == $record['idKelas']) ? ' selected="selected"' : "";

								echo '<option value="' . $k['idKelas'] . '" ' . $selected . '>' . $k['nmKelas'] . '</option>';
							}
							?>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Sekolah Asal</label>

						<input type="text" name="sekolah" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Diterima Kelas</label>
						<select name="diterimaKelas" class="form-control">
							<?php
							$sqk = mysqli_query($conn, "SELECT * FROM kelas_siswa ORDER BY idKelas ASC");
							while ($k = mysqli_fetch_array($sqk)) {
								$selected = ($k['idKelas'] == $record['idKelas']) ? ' selected="selected"' : "";

								echo '<option value="' . $k['idKelas'] . '" ' . $selected . '>' . $k['nmKelas'] . '</option>';
							}
							?>
						</select>
					</div>
					<div class="col-md-6">
						<label for="" class="control-label">Diterima Pada</label>

						<input type="text" name="tglMasuk" class="form-control pull-right date-picker">


					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Alamat</label>

						<input type="text" name="alamat" class="form-control" placeholder="" required>

					</div>
					<div class="col-md-3">
						<label for="" class=" control-label">No.Hp Ortu</label>

						<input type="text" name="noHp" class="form-control" placeholder="" required>

					</div>
					<div class="col-md-3">
						<label for="" class=" control-label">No.Hp Siswa</label>

						<input type="text" name="noHpSis" class="form-control" placeholder="" required>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Ayah</label>

						<input type="text" name="nmOrtu" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Ibu</label>

						<input type="text" name="nmOrtuIbu" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Ayah</label>

						<input type="text" name="kerjaAyah" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Ibu</label>

						<input type="text" name="kerjaIbu" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Nama Wali</label>

						<input type="text" name="nmWali" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Pekerjaan Wali</label>

						<input type="text" name="kerjaWali" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Alamat Wali</label>

						<input type="text" name="alamatWali" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Username</label>

						<input type="text" name="username" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Password</label>

						<input type="text" name="password" class="form-control" placeholder="">

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Status</label>

						<select class="form-control" name="statusSiswa">
							<option value="Aktif"> Aktif</option>
							<option value="Non Aktif">Non Aktif</option>
							<option value="Drop Out">Drop Out</option>
							<option value="Pindah">Pindah</option>
							<option value="Lulus">Lulus</option>
						</select>

					</div>
					<div class="col-md-6">
						<label for="" class=" control-label">Foto</label>


						<img src="" id="target" alt="Choose image to upload" class="img-thumbnail img-responsive">
						<input type="file" class="form-control form-control-sm" name="fotoSiswa" id="foto" accept=".png, .jpg, .jpeg">

					</div>


					<div class="form-group">
						<label for="" class=" control-label"></label>
						<div class="col-sm-10">
							<input type="submit" name="tambah" value="Simpan" class="btn btn-success">
							<a href="index.php?view=siswa" class="btn btn-default">Cancel</a>
						</div>
					</div>
				</form>
			</div>
		</div>
	</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 Siswa</h3>
			</div><!-- /.box-header -->
			<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);
						$alamat = $data->val($i, 7);
						$noHp 	= $data->val($i, 8);
						$nmOrtu 	= $data->val($i, 9);
						$username	= $data->val($i, 10);
						$password	= $data->val($i, 11);
						$noHpSis 	= $data->val($i, 12);
						$level		=	'siswa';
						// setelah data dibaca, masukkan ke tabel pegawai sql
						$hasil = mysqli_query($conn, "INSERT INTO siswa(nisSiswa,nisnSiswa,nmSiswa,jkSiswa,level,agamaSiswa,idKelas,alamatOrtu,noHpOrtu,noHpSis,nmOrtu,username,password) 
							VALUES('$nis','$nisn','$nama','$jk','$level','$agama','$idkelas','$alamat','$noHp','$noHpSis','$nmOrtu','$username','$password')");
					}

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

						echo "<div class='alert alert-danger' role='alert'>
						Data Gagal Diimport....!<br>" . die(mysql_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 Siswa</label>
						<div class="col-sm-8">
							<a href="./files/formatdatasiswa.xls" class="btn btn-info">formatdatasiswa.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>
						</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="index.php?view=siswa" 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>