<?php if ($_GET[act] == '') {
	$sqlEdit = mysqli_query($conn, "SELECT
								*
									
									
								FROM
								kelas_siswa
									WHERE idKelas='$_GET[kelas]'");
	$record = mysqli_fetch_array($sqlEdit);
?>
	<div class="col-xs-12">
		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title">Nama Kelas - <?php echo $record['nmKelas']; ?></h3>
				
			</div><!-- /.box-header -->
			<div class="box-body">

				<label for="" class="col-sm-2">Aksi</label>
				<div class="col-sm-10">
					<a class="btn btn-success" href="?view=inv&kelas=<?php echo $record['idKelas']; ?>&act=tambah"><span class='glyphicon glyphicon-plus'></span> Tambah Data</a>
					<a class="btn btn-warning" href="?view=inv&kelas=<?php echo $record['idKelas']; ?>"><span class='glyphicon glyphicon-refresh'></span> Refresh</a>
					<a class="btn btn-default" href="?view=kelas"><span class='glyphicon glyphicon-repeat'></span> Kembali</a>
				</div>
			</div><!-- /.box-body -->
		</div><!-- /.box -->
		<?php

		$sqlCariBulanan = mysqli_query($conn, "SELECT
												rb_inventaris.*,
												rb_inventaris.nmBarang,
												rb_inventaris.jumlahBarang,
												kelas_siswa.nmKelas
												
												
											FROM
											rb_inventaris
										
											INNER JOIN kelas_siswa ON rb_inventaris.idKelas = kelas_siswa.idKelas
											WHERE rb_inventaris.idKelas='$_GET[kelas]'
												 ");
		?>
		<div class="box box-primary">
			<div class="table-responsive">
				<?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>";
				}
				?>
				<table class="table table-striped">
					<thead>
						<tr>
							<th>No.</th>

							<th>Nama Barang</th>
							<th>Jumlah Barang</th>
							<!--<th>Dibayar</th>
									<th>Status</th>-->
							<th>Aksi</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$no = 1;
						while ($rt = mysqli_fetch_array($sqlCariBulanan)) {

							echo "<tr>
										<td>$no</td>
										
										<td>$rt[nmBarang]</td>
										<td>$rt[jumlahBarang]</td>
	
										<td style='text-align:center'>
										<a class='btn btn-success btn-xs' href='?view=inv&act=edit&kelas=$rt[idKelas]'><span class='glyphicon glyphicon-edit'></span></a>
												<a class='btn btn-danger btn-xs' href='?view=inv&act=hapus&kelas=$rt[idKelas]' onclick=\"return confirm('Apa anda yakin untuk hapus Data ini?')\"><span class='glyphicon glyphicon-remove'></span></a>
										</td>
									</tr>";
							$no++;
						}
						?>
					</tbody>
				</table>
			</div><!-- /.box-body -->
			<div class="box-footer">
				<a class="pull-right btn btn-danger" href="?view=inv&kelas=<?php echo $_GET[kelas]; ?>&act=hapussekelas" onclick="return confirm('Apa anda yakin untuk hapus Data ini?')"><span class="glyphicon glyphicon-remove"></span> Kosongkan Data Inventaris di Kelas Ini</a>
			</div>
		</div><!-- /.box -->

	</div>
	<?php
} elseif ($_GET[act] == 'edit') {
	if (isset($_POST['update'])) {
		$query = mysqli_query($conn,"UPDATE rb_inventaris SET nmBarang='$_POST[nmBarang]', jumlahBarang='$_POST[jumlahBarang]'
										WHERE idKelas='$_POST[idKelas]'");
		if ($query) {
			echo "<script>document.location='index.php?view=inv&kelas=$_POST[idKelas]&sukses';</script>";
		} else {
			echo "<script>document.location='index.php?view=inv&kelas=$_POST[idKelas]&gagal';</script>";
		}
	}

	$sqlEdit = mysqli_query($conn,"SELECT *
							
						FROM
							rb_inventaris
					
						WHERE idKelas='$_GET[kelas]'");
	$record = mysqli_fetch_array($sqlEdit);
?>
	<div class="col-md-6">
		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"> Edit Inventaris Siswa</h3>
			</div><!-- /.box-header -->
			<div class="box-body">
				<form method="post" action="" class="form-horizontal">
					<div class="col-sm-12">
						<div class="box box-solid box-danger">
							<div class="box-header">
								<h3 class="box-title">Informasi Tagihan</h3>
							</div>
							<div class="box-body">
								<input type="hidden" name="idKelas" value="<?php echo $record['idKelas']; ?>">
								
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Nama Barang</label>
									<div class="col-sm-4">
										<input type="text" name="nmBarang" class="form-control" id="" value=<?php echo $record['nmBarang']; ?>  required>
									</div>
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Jumlah</label>
									<div class="col-sm-6">
										<input type="number" name="jumlahBarang" class="form-control" id=""value=<?php echo $record['jumlahBarang']; ?>>
									</div>
								</div>
								
							
								<div class="form-group">
									<label for="" class="col-sm-4 control-label"></label>
									<div class="col-sm-8">
										<input type="submit" name="update" value="Update Tagihan" class="btn btn-primary">
									</div>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php
} elseif ($_GET[act] == 'tambah') {
	if (isset($_POST['tambahs'])) {
		//$nData = $_POST['jmldata']; // membaca jumlah data



		$query = mysqli_query($conn, "INSERT INTO rb_inventaris(idKelas,nmBarang,jumlahBarang)
									VALUES('$_GET[kelas]','$_POST[nmBarang]','$_POST[jumlahBarang]'
											)");


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

	$sqlEdit = mysqli_query($conn, "SELECT
							*

							
						FROM
						rb_inventaris
						
						WHERE rb_inventaris.idKelas='$_GET[kelas]'");
	$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 Inventaris</h3>
			</div><!-- /.box-header -->
			<div class="box-body">
				<div class="col-sm-12">
					<div class="box box-solid box-danger">
						<div class="box-header">
							<h3 class="box-title">Input Inventaris</h3>
						</div>
						<div class="box-body">
							<form method="POST" action="" class="form-horizontal">
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Nama Barang</label>
									<div class="col-sm-4">
										<input type="text" name="nmBarang" class="form-control" id="" placeholder="Nama Barang" required>
									</div>
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Jumlah</label>
									<div class="col-sm-6">
										<input type="number" name="jumlahBarang" class="form-control" id="" placeholder="Jumlah Barang">
									</div>
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label"></label>
									<div class="col-sm-10">
										<input type="submit" name="tambahs" value="Simpan" class="btn btn-success">
										<a href="index.php?view=kelas" class="btn btn-default">Cancel</a>
									</div>
								</div>
							</form>
						</div>
					</div>

				</div>
				<?php
				$sqlEdit = mysqli_query($conn, "SELECT
							*
						
							
						FROM
							rb_jenis_kegiatan_ustadz
						
						WHERE rb_jenis_kegiatan_ustadz.idKelas='$_GET[jenis]'");
				$record = mysqli_fetch_array($sqlEdit); ?>
				<div class="col-sm-7">
					<?php

					$sqlSiswaTagihan = mysqli_query($conn, "SELECT * FROM users
									WHERE level='ustadz' ");
					//$n = mysql_num_rows($sqlSiswaTagihan); // membaca jumlah data


					?>
					
				</div>
			</div>
		</div>
	</div>
<?php
} elseif ($_GET['act'] == 'hapus') {
	$idjenis = $_GET['kelas'];
	$hapus = mysqli_query($conn, "DELETE FROM rb_inventaris WHERE idKelas='$_GET[kelas]'");
	if ($hapus) {
		echo "<script>document.location='index.php?view=inv&kelas=$idjenis&sukseshapus';</script>";
	} else {
		echo "<script>document.location='index.php?view=inv&kelas=$idjenis&gagalhapus';</script>";
	}
} elseif ($_GET['act'] == 'hapussekelas') {
	$idjenis = $_GET['kelas'];
	$hapus = mysqli_query($conn, "DELETE FROM rb_inventaris WHERE idKelas='$idjenis' ");
	if ($hapus) {
		echo "<script>document.location='index.php?view=inv&kelas=$idjenis&sukseshapus';</script>";
	} else {
		echo "<script>document.location='index.php?view=inv&kelas=$idjenis&gagalhapus';</script>";
	}
}
?>