<?php if ($ac == '') { ?>
<?php
function getRomawi($bln){
                switch ($bln){
                    case 1: 
                        return "I";
                        break;
                    case 2:
                        return "II";
                        break;
                    case 3:
                        return "III";
                        break;
                    case 4:
                        return "IV";
                        break;
                    case 5:
                        return "V";
                        break;
                    case 6:
                        return "VI";
                        break;
                    case 7:
                        return "VII";
                        break;
                    case 8:
                        return "VIII";
                        break;
                    case 9:
                        return "IX";
                        break;
                    case 10:
                        return "X";
                        break;
                    case 11:
                        return "XI";
                        break;
                    case 12:
                        return "XII";
                        break;
                }
}
$bulan = date('n');
$romawi = getRomawi($bulan);
$tahun = date ('YmdHis');
$nomor = "-".$romawi."-".$tahun;

$query = "SELECT max(id) as maxKode FROM tabungan";
$hasil = mysqli_query($koneksi,$query);
$data  = mysqli_fetch_array($hasil);
$no= $data['maxKode'];
$noUrut= $no + 1;

$kode =  sprintf("%06s", $noUrut);
$nomorbaru = "0483-2-K-".$kode.$nomor;

?>
<div class='row'>
       <div class="card">
             <div class="card-header">									
                  <h5 class="card-title"><i class="fas fa-credit-card fa-fw   "></i> Penarikan Tabungan</h5>             
         </div>
         
				 <div class='card-body'>
				<form class="form-horizontal"  method="POST" action="" >
					<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">Tanggal</label>
							<div class="col-sm-9">
								<input type="text"  class="form-control" value="<?= date('Y-m-d') ?>" readonly>
							</div>
						</div>
						<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">Id Siswa</label>
							<div class="col-sm-9">
								<div class="input-group">
									<input type="text" class="form-control" id="idsiswa" name="idsiswa" placeholder="Nomor Id Siswa" readonly>
									<span class="input-group-btn">
									  <button type="button" class="btn btn-warning btn-flat" data-bs-toggle="modal" data-bs-target="#modal">Browse</button>
									</span>
								</div>
							</div>
						</div>
						<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">ID Tabungan</label>
							<div class="col-sm-9">
								<input type="text" id="idtab" name="idtab" class="form-control" placeholder="ID Tabungan" readonly>
							</div>
						</div>
						<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">Nama Siswa</label>
							<div class="col-sm-9">
								<input type="text" id="nama" name="nama" class="form-control" placeholder="Nama Siswa" readonly >
							</div>
						</div>
						<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">No Bukti</label>
							<div class="col-sm-9">
								<input type="text" id="bukti" name="bukti" class="form-control" value="<?= $nomorbaru ?>" readonly>
							</div>
						</div>
						<div class="row mb-2">
						<label  class="col-md-3 col-form-label bold">Jumlah Penarikan</label>
							<div class="col-sm-9">
								<input name="penarikan" type="text" class="form-control" placeholder="Masukkan Jumlah" onkeyup="convertToRupiah(this);" required>
							</div>
						</div>
					
					<div class="right">
						<button type="submit" name="simpan" class="btn btn-success"><i class="fa fa-check-circle"></i>Simpan</button>
						
					</div>
				</form>
				<?php
				include"fungsi.php";
				$tanggal= date('Y-m-d');
				 if (isset($_POST['simpan'])) {
                if($_POST['idsiswa']==""){
					echo "
	  <script type='text/javascript'>
		setTimeout(function () { 	
			swal({
				title: 'Gagal !!!',
				text:  'Penarikan Gagal disimpan',
				type: 'error',
				timer: 2000,
				showConfirmButton: true
			});		
		},10);	
		window.setTimeout(function(){ 
			window.location.replace('?pg=transaksi');
		} ,2000);	
	  </script>";
		
	}else{
				$tahun=date('Y');
				$bulan=date('m');
				$idsiswa = $_POST['idsiswa'];
				$tanggal= date('Y-m-d');
				
               $penarikan = reset_rupiah($_POST['penarikan']);

$cekdata = "SELECT * FROM tabungan WHERE tanggal='$tanggal' AND idsiswa='$_POST[idsiswa]' AND ket='Tarik' ";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	
	echo "
	  <script type='text/javascript'>
		setTimeout(function () { 	
			swal({
				title: 'Gagal !!!',
				text:  'Penarikan Gagal disimpan',
				type: 'error',
				timer: 2000,
				showConfirmButton: true
			});		
		},10);	
		window.setTimeout(function(){ 
			window.location.replace('?pg=transaksi');
		} ,2000);	
	  </script>";
		
	}else{
$data = mysqli_query ($koneksi, " select  penarikan,
										  setoran,
										  sum(penarikan) as jumlah_penarikan,
										  sum(setoran) as jumlah_setoran
										  from 
										  tabungan
										  where
										  idsiswa = $idsiswa");
$row = mysqli_fetch_array ($data);
$saldo = $row['jumlah_setoran'] - $row['jumlah_penarikan'] - $penarikan;


mysqli_query($koneksi,"INSERT INTO tabungan(bukti,idsiswa,tanggal,bulan,tahun,penarikan,saldo,ket) values('$nomorbaru','$idsiswa','$tanggal','$bulan','$tahun','$penarikan','$saldo','Tarik')");
if($koneksi){
echo"

	  <script type='text/javascript'>
		setTimeout(function () { 	
			swal({
				title: 'Sukses !!!',
				text:  'Penarikan Berhasil disimpan',
				type: 'success',
				timer: 2000,
				showConfirmButton: true
			});		
		},10);	
		window.setTimeout(function(){ 
			window.location.replace('?pg=transaksi');
		} ,2000);	
	  </script>";
                }
            }
				 }
				 }
            ?>



	<div id="modal" class="modal fade" role="dialog">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<form role="form" id="form-tambah" method="post" action="input.php">
				<div class="modal-header">
					<center>
					<h3 class="modal-title">Pilih Siswa</h3>
					</center>
				</div>
					<div class="modal-body">
						
							<table width="100%" class="table table-hover"  id="tabungan">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>ID Tabungan</th>
                                        <th>Nama Siswa</th>
                                        <th>Kelas</th>
                                        
                                    </tr>
                                </thead>
                                <tbody>
										<?php
											$no = 1;
											$data = mysqli_query ($koneksi, " select  *
																					  from 
																					  penabung
																					  order by idsiswa DESC");
											while ($row = mysqli_fetch_array ($data))
											{
										?>
									<tr id="siswa" data-idsiswa="<?php echo $row['idsiswa']; ?>" data-nama="<?php echo $row['nama']; ?>" data-idtab="<?php echo $row['id_tab']; ?>">
										<td>
											<?php echo $no++; ?>
										</td>
										<td>
											<?php echo $row['id_tab']; ?>
										</td>
										<td>
											<?php echo $row['nama']; ?>
										</td>
										<td>
											<?php echo $row['kelas']; ?>
										</td>
										
									</tr>
									<?php
										}
									?>
                            </table>
						
						
					</div>	
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
					</div>
			</div>
		</div>
	</div>
	
<div id="modal" class="modal fade" role="dialog">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<form role="form" id="form-tambah" method="post" action="input.php">
				<div class="modal-header">
					<center>
					<h3 class="modal-title">Pilih Siswa</h3>
					</center>
				</div>
					<div class="modal-body">
						
						<div class='table-responsive'>
                        <table width="100%" class="table table-hover"  id="tabungan">
                                <thead>
                                    <tr>
                                        <th width="5px">No</th>
                                        <th>ID Tabungan</th>
                                        <th>Nama Siswa</th>
                                        <th>Kelas</th>
                                       
                                    </tr>
                                </thead>
                                <tbody>
										<?php
											$no = 1;
											$data = mysqli_query ($koneksi, " select  * from penabung order by id ASC");
											while ($row = mysqli_fetch_array ($data))
											{
										?>
									<tr id="siswa" data-idsiswa="<?php echo $row['idsiswa']; ?>" data-nama="<?php echo $row['nama']; ?>" data-idtab="<?php echo $row['idtab']; ?>">
										<td>
											<?php echo $no++; ?>
										</td>
										<td>
											<?php echo $row['idtab']; ?>
										</td>
										<td>
											<?php echo $row['nama']; ?>
										</td>
										<td>
											<?php echo $row['kelas']; ?>
										</td>
										
									</tr>
									<?php
										}
									?>
                            </table>
						
						
					</div>	
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
					</div>
			</div>
		</div>
	</div>
	
	<?php } ?>
	
	 <script>
			$('#example').DataTable();
            $(document).ready(function(){setTimeout(function(){$("#pesan").fadeIn('slow');}, 500);});
            setTimeout(function(){$("#pesan").fadeOut('slow');}, 3000);
        </script>
		<script type="text/javascript">
		$(document).ready(function(){
			
			$(document).on('click', '#siswa', function (e) {
				document.getElementById("idsiswa").value = $(this).attr('data-idsiswa');
				document.getElementById("nama").value = $(this).attr('data-nama');
				document.getElementById("idtab").value = $(this).attr('data-idtab');
				$('#modal').modal('hide');
			});	
			
		});
		</script>	
	
	