<?php 
session_start();
	if($_SESSION['status']!="USER"){
		header("location:../logout.php");
	}

include("header.php"); // memanggil file header.php
include("koneksi.php"); // memanggil file koneksi.php untuk koneksi ke database
date_default_timezone_set('Asia/Jakarta');
?>
<script src="jquery-3.5.1.min.js"></script>
<script src="sweetalert2.all.min.js"></script>

	<div class="container">
		<div class="content">
			<h2>E-Voting PILKETOS &raquo; VOTE</h2>
			<hr />
			
			<?php
			$id = $_GET['id']; // assigment nisn dengan nilai nisn yang akan diedit
			$sql = mysqli_query($koneksi, "SELECT * FROM vote WHERE id='$id'"); // query untuk memilih entri data dengan nilai nisn terpilih
			if(mysqli_num_rows($sql) == 0){
				header("Location: index.php");
			}else{
				$row = mysqli_fetch_assoc($sql);
			}
			if(isset($_POST['save'])){ // jika tombol 'Simpan' dengan properti name="save" pada baris 162 ditekan
				$id		= $_POST['id'];
				$id_siswa	= $_POST['id_siswa'];
				$nama		= addslashes($_POST['nama']);
				$kelas		= $_POST['kelas'];
				$pilihan	= $_POST['pilihan'];
				$tanggal	= date('d-m-Y H:i:s');
				
				$ceknik=mysqli_num_rows(mysqli_query($koneksi,"SELECT * FROM vote WHERE id='$_POST[id]' and status='BERHASIL' OR status_vote='TUTUP'"));
    				if ($ceknik<>0) {
        			echo "<script>Swal.fire('GAGAL','Anda sudah memilih sebelumnya','error',{showConfirmButton: false});setTimeout(function(){window.location.href = 'index.php';}, 3500);</script>";    				}
				else {
				$update = mysqli_query($koneksi, "UPDATE vote SET id='$id', nama='$nama', kelas='$kelas', pilihan='$pilihan', tanggal='$tanggal', status='BERHASIL' WHERE id='$id'") or die(mysqli_error()); // query untuk mengupdate nilai entri dalam database
				if($update){ // jika query update berhasil dieksekusi
					echo "<script>Swal.fire('BERHASIL','Pilihan anda sudah berhasil disimpan','success',{showConfirmButton: false});setTimeout(function(){window.location.href = 'index.php';}, 3500);</script>"; // tambahkan pesan=sukses pada url
				}else{ // jika query update gagal dieksekusi
					echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Data gagal disimpan, silahkan coba lagi.</div>'; // maka tampilkan 'Data gagal disimpan, silahkan coba lagi.'
				}
				}
			}
			
			if(isset($_GET['pesan']) == 'sukses'){
						echo "<script>alert('Data Telah Berhasil Disimpan');window.location='index.php'</script>";
						}

			?>
			<!-- bagian ini merupakan bagian form untuk mengupdate data yang akan dimasukkan ke database -->
			<form class="form-horizontal" action="" method="post">
				

				<div class="form-group">
					<label class="col-sm-3 control-label">ID</label>
					<div class="col-sm-4">
						<input type="text" name="id" value="<?php echo $_SESSION['id']; ?>" class="form-control" placeholder="NISN" readonly>
					</div>
				</div>

				<div class="form-group">
					<label class="col-sm-3 control-label">Nama</label>
					<div class="col-sm-4">
						<input type="text" name="nama" value="<?php echo $_SESSION['nama']; ?>" class="form-control" placeholder="nama" readonly>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">Kelas</label>
					<div class="col-sm-4">
						<input type="text" name="kelas" value="<?php echo $_SESSION['kelas']; ?>" class="form-control" placeholder="nama" readonly>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">PILIHAN..!!</label>
					<div class="col-sm-4" >
						<input type="radio" name="pilihan" value="1" class="col-sm-6" required> NO URUT 01</p>
						<input type="radio" name="pilihan" value="2" class="col-sm-6" required> NO URUT 02</p>
						<input type="radio" name="pilihan" value="3" class="col-sm-6" required> NO URUT 03</p>
						<input type="radio" name="pilihan" value="4" class="col-sm-6" required> NO URUT 04</p>
						<input type="radio" name="pilihan" value="5" class="col-sm-6" required> NO URUT 05</p>
						<input type="radio" name="pilihan" value="6" class="col-sm-6" required> NO URUT 06</p>
	
					</div>
				</div>
				   <div class="form-group">
					<label class="col-sm-3 control-label">Foto Calon Ketua OSIS</label>
       					 <div class="col-sm-3">
            				     <div class="card">
                				<div class="card-body" id="output">
                    				<!-- Output -->
                				</div>    
               				 	<div class="card-footer" id="text">
                    				<!-- Text -->
                				</div>
            				    </div>
            
        				</div>
    		    		    </div><hr/>
				<div class="form-group">
					<label class="col-sm-3 control-label">PERINGATAN..!!</label>
					<div class="col-sm-4">
						<marquee width="425" scrollamount="8"><font color="red"><b>Setelah anda tekan tombol VOTE maka pilihan anda akan terekam dan sudah TIDAK BISA DIUBAH </b> </font></marquee>
					</div>
				</div>

				
				<div class="form-group">
					<label class="col-sm-3 control-label">&nbsp;</label>
					<div class="col-sm-6">
						<!--<button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal-default"> SIMPAN </button>-->
						<input type="submit" name="save" class="btn btn-lg btn-danger" value="VOTE" data-toggle="tooltip" title="Simpan Data siswa">
						<!--<a href="modal-default" data-toggle="modal" onclick="" class="btn btn-success btn-sm">Simpan</a>	-->	
						
					</div>
				</div>
			</form>


		</div> <!-- /.content -->
	</div> <!-- /.container -->
	<div class="modal fade" id="modal-default">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Status BERHASIL</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <form>
			<button class="btn btn-default" data-dismiss="modal">Tidak</button>
			<input type="submit" name="save" class="btn btn-sm btn-primary" value="Simpan" data-toggle="tooltip" title="Simpan Data siswa">
		</form>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
<script>
    var _select = document.querySelectorAll('input[type="radio"]');
    var _output = document.getElementById('output');
    var _text = document.getElementById('text');

    for(var i = 0; i < _select.length; i++){
        _select[i].onclick = function() {
            console.log(this.value)
            if ( this.value === "1" ) {
                _output.innerHTML = `<img src="img/1.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>1</strong>";
            }
            if ( this.value === "2" ) {
                _output.innerHTML = `<img src="img/2.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>2</strong>";
            }
            if ( this.value === "3" ) {
                _output.innerHTML = `<img src="img/3.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>3</strong>";
            }
            if ( this.value === "4" ) {
                _output.innerHTML = `<img src="img/4.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>4</strong>";
            }
            if ( this.value === "5" ) {
                _output.innerHTML = `<img src="img/5.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>5</strong>";
            }
            if ( this.value === "6" ) {
                _output.innerHTML = `<img src="img/6.jpg" width="200px">`;
                _text.innerHTML = "Saya Nomor Urut <strong>6</strong>";
            }

	 
        }
    }    
</script>
<?php 
include("footer.php"); // memanggil file footer.php
?>