<!DOCTYPE html>
<html lang="en">
<head>
	<title>Register Antrian</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
	<link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="css/util.css">
	<link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->
</head>
<body>
	<?php
	require ('connection/connect.php');
	//inisialisasi session
	session_start();
	$error = '';
	$validate = '';
	//mengecek apakah form registrasi di submit atau tidak
	if( isset($_POST['btn_register']) ){
	$name = mysqli_real_escape_string($mysqli, $name);
	$name     = stripslashes($_POST['name']);
	$name     = mysqli_real_escape_string($mysqli, $name);
	$email    = stripslashes($_POST['email']);
	$email    = mysqli_real_escape_string($mysqli, $email);
	$password = stripslashes($_POST['password']);
	$password = mysqli_real_escape_string($mysqli, $password);
	$repass   = stripslashes($_POST['repassword']);
	$repass   = mysqli_real_escape_string($mysqli, $repass);
	//cek apakah nilai yang diinputkan pada form ada yang kosong atau tidak
	if(!empty(trim($name)) && !empty(trim($name)) && !empty(trim($email)) && !empty(trim($password)) && !empty(trim($repass))){
	//mengecek apakah password yang diinputkan sama dengan re-password yang diinputkan kembali
	if($password == $repass){
	//memanggil method cek_nama untuk mengecek apakah user sudah terdaftar atau belum
	if( cek_nama($name,$mysqli) == 0 ){
	//hashing password sebelum disimpan didatabase
	$pass  = password_hash($password, PASSWORD_DEFAULT);
	//insert data ke database
	$query = "INSERT INTO users (name ,email, password ) VALUES ('$nama','$email','$pass')";
	$result   = mysqli_query($mysqli, $query);
	//jika insert data berhasil maka akan diredirect ke halaman index.php serta menyimpan data name ke session
	if ($result) {
	$_SESSION['email'] = $email;

	header('Location: index.php');

	//jika gagal maka akan menampilkan pesan error
	} else {
	$error =  'Register User Gagal !!';
	}
	}else{
	$error =  'Username sudah terdaftar !!';
	}
	}else{
	$validate = 'Password tidak sama !!';
	}

	}else {
	$error =  'Data tidak boleh kosong !!';
	}
	} 
	//fungsi untuk mengecek email apakah sudah terdaftar atau belum
	function cek_nama($email,$mysqli){
	$nama = mysqli_real_escape_string($mysqli, $email);
	$query = "SELECT * FROM users WHERE email = '$nama'";
	if( $result = mysqli_query($mysqli, $query) ) return mysqli_num_rows($result);
	}
?>
<?php if($error != ''){ ?>
	<div class="alert alert-danger" role="alert"><?= $error; ?></div>
<?php } ?>
	<div class="limiter">
		<div class="container-login100" style="background-image: url('images/bg-01.jpg');">
			<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54">
				<form class="login100-form validate-form" action="" method="POST">
					<span class="login100-form-title p-b-49">
						Register
					</span>

					<div class="wrap-input100 validate-input m-b-23" data-validate = "Name is required">
						<span class="label-input100">Name</span>
						<input class="input100" type="text" name="name" placeholder="Type your name">
						<span class="focus-input100" data-symbol="&#xf206;"></span>
					</div>

					<div class="wrap-input100 validate-input m-b-23" data-validate = "Email is required">
						<span class="label-input100">Email</span>
						<input class="input100" type="email" name="email" placeholder="Type your email">
						<span class="focus-input100" data-symbol="&#xf206;"></span>
					</div>

					<div class="wrap-input100 validate-input" data-validate="Password is required">
						<span class="label-input100">Password</span>
						<input class="input100" type="password" name="password" placeholder="Type your password">
						<span class="focus-input100" data-symbol="&#xf190;"></span>
					</div>

					<div class="wrap-input100 validate-input" data-validate="Repassword is required">
						<span class="label-input100">Re-Password</span>
						<input class="input100" type="password" name="repassword" placeholder="Type your repassword">
						<span class="focus-input100" data-symbol="&#xf190;"></span>
					</div>
					
					<div class="text-right p-t-8 p-b-31">
					</div>
					
					<div class="container-login100-form-btn">
						<div class="wrap-login100-form-btn">
							<div class="login100-form-bgbtn"></div>
							<button class="login100-form-btn" name="btn_register" type="submit">
								Register
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	

	<div id="dropDownSelect1"></div>
	
<!--===============================================================================================-->
	<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/bootstrap/js/popper.js"></script>
	<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/daterangepicker/moment.min.js"></script>
	<script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="js/main.js"></script>

</body>
</html>