
<?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 ('Y');
$nomor = "-".$romawi."-".$tahun;

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

$kode =  sprintf("%04s", $noUrut);
$nomorbaru = $kode.$nomor;

?>
  <div class='row'>
        <div class='col-md-12'>
           <div class="card">
             <div class="card-header">
                <div class='right'>
				 <button class='btn btn-outline-primary' data-bs-toggle="modal" data-bs-target="#tambahsiswa"><i class="fa fa-plus-circle" data-bs-toggle="tooltip" data-bs-placement="top" title="Tambah Konsumen"></i>Konsumen</button>
		  </div>			 
               <h5 class="card-title"><i class="fas fa-graduation-cap"></i> Konsumen</h4>
                </div>
				
           <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" id='example1' class='table table-hover'>
                            <thead>
                            <tr>
                                <tr>
                                    <th width='5%'></th>
                                    <th width='15%'>ID Tabungan</th>
                                    <th width='15%'>NIS</th>
                                    <th>Nama Siswa</th>
                                   <th width='15%'>Kelas</th>
								  <th width='5%'>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $query = mysqli_query($koneksi, "select * from penabung ");
                            $no = 0;
                            while ($siswa = mysqli_fetch_array($query)) {
							$siswaQ=fetch($koneksi,'siswa',['id_siswa'=>$siswa['idsiswa']]);
                                $no++;
                              
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['id_tab'] ?></td>
									 <td><?= $siswaQ['nis'] ?></td>
                                    <td><?= $siswa['nama'] ?></td>
                                     <td><?= $siswa['kelas'] ?></td>
                                    
									 <td>
									 <button data-id="<?= $siswaQ['id_siswa'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
									 </td>
									  </tr>
                            <?php }
                            ?>


                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
 
  <div class="modal fade" id="tambahsiswa" tabindex="-1" aria-labelledby="tambahjadwal" aria-hidden="true">
   <div class="modal-dialog">
       <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="tambahjadwal">Tambah Konsumen</h5>
               <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                      <div class="modal-body">
                                    <form id='form-tambah' action=''>
                                        
                                        <div class="form-group">
							<div class="col-sm-12">
								<div class="input-group">
								
									<input type="text" class="form-control" id="id_siswa" name="id_siswa" placeholder="Nomor Id Siswa" required readonly>
									<span class="input-group-btn">
									  <button type="button" class="btn btn-success btn-flat" data-bs-toggle="modal" data-bs-target="#modal"> Cari</button>
									</span>
								</div>
							</div>
						</div>
						<div class='form-group-sm'>
                                               <label><hr></label>
											   </div>
						
						                      <div class='form-group-sm'>
                                               <label>ID Tabungan</label>
                                            <input type='text' name='idtab' value="<?= $nomorbaru ?>" class='form-control' readonly />
                                        </div>
										 <div class='form-group-sm'>
                                                    <label>Nama Siswa</label>
                                                    <input type="text" id="nama" name="nama" class="form-control" placeholder="Nama Siswa" >
                                                </div>
                                        <div class='form-group-sm'>
                                            
                                                    <label>Kelas</label>
                                                      <input type="text" id="id_kelas" name="id_kelas" class="form-control" placeholder="Kelas" >
                                                </div>
                                                <div class='form-group'>
                                            
                                                   
                                                      <input type="hidden" id="hp" name="hp" class="form-control" >
                                                </div>
                                        </div>
                                        <div class='modal-footer'>
                                            <div class='box-tools pull-right btn-group'>
                                                <button type='submit' name='simpan' class='btn btn-sm btn-primary'><i class='fa fa-check'></i> Simpan</button>
                                                <button type='button' class='btn btn-default btn-sm pull-left' data-bs-dismiss='modal'>Close</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </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="datatable1">
                                <thead>
                                    <tr>
                                        <th width="5px">No</th>
                                        <th>NIS</th>
                                        <th>Nama Siswa</th>
                                        <th>Kelas</th>
                                       <th>No WA</th>
                                    </tr>
                                </thead>
                                <tbody>
										<?php
											$no = 1;
											$data = mysqli_query ($koneksi, " select  *
																					  from 
																					  siswa
																					  order by id_siswa ASC");
											while ($row = mysqli_fetch_array ($data))
											{
										?>
									<tr id="siswa" data-id_siswa="<?php echo $row['id_siswa']; ?>" data-nama="<?php echo $row['nama']; ?>" data-id_kelas="<?php echo $row['id_kelas']; ?>" data-hp="<?php echo $row['hp']; ?>">
										<td>
											<?php echo $no++; ?>
										</td>
										<td>
											<?php echo $row['nis']; ?>
										</td>
										<td>
											<?php echo $row['nama']; ?>
										</td>
										<td>
											<?php echo $row['id_kelas']; ?>
										</td>
										<td>
											<?php echo $row['hp']; ?>
										</td>
									</tr>
									<?php
										}
									?>
                            </table>
						
						
					</div>	
					<p>
					<div class="modal-footer">
						<button type="button" class="btn btn-outline-danger" data-bs-dismiss="modal">Batal</button>
					</div>
			</div>
		</div>
	</div>
	
	 <script>
			$('#datatable1').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("id_siswa").value = $(this).attr('data-id_siswa');
				document.getElementById("nama").value = $(this).attr('data-nama');
				document.getElementById("id_kelas").value = $(this).attr('data-id_kelas');
				document.getElementById("hp").value = $(this).attr('data-hp');
				$('#modal').modal('hide');
			});	
			
		});
		</script>	
	<script>
	$('#form-tambah').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_tabungan/crud_siswa.php?pg=tambah',
            data: $(this).serialize(),
            beforeSend: function() {
                $('form button').on("click", function(e) {
                    e.preventDefault();
                });
            },
            success: function(data) {
                console.log(data);
                if (data == 'OK') {
                    iziToast.success({
                        title: 'Sukses!',
                        message: 'Data berhasil disimpan',
                       titleColor: '#FFFF00',
						messageColor: '#fff',
						backgroundColor: 'rgba(0, 0, 0, 0.5)',
						 progressBarColor: '#FFFF00',
						 position: 'topRight'
                    });
                    setTimeout(function() {
                        window.location.reload();
                    }, 2000);
                } else {
                    iziToast.error({
                        title: 'Data Gagal disimpan!',
                        message: data,
                        position: 'topRight'
                    });
                }

            }
        });
        return false;
    });
	</script>
	<script>
	$('#example1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Are you sure?',
            text: 'Akan menghapus Semua data Transaksi Siswa ini!',
			type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_tabungan/crud_siswa.php?pg=hapus',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                       iziToast.info(
									{
						title: 'Sukses!',
						message: 'Semua Transaksi Siswa ini berhasil dihapus',
						titleColor: '#FFFF00',
						messageColor: '#fff',
						backgroundColor: 'rgba(0, 0, 0, 0.5)',
						 progressBarColor: '#FFFF00',
						 position: 'topRight'					  
						});
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })
 });
	</script>