<?php
include '../functions.php';

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
	<title>Syndash - Admin Template</title>
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
	<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&amp;family=Roboto&amp;display=swap" />
	<!-- Icons CSS -->
	<link rel="stylesheet" href="assets/css/icons.css" />
	<!-- App CSS -->
	<link rel="stylesheet" href="assets/css/app.css" />
	<script type="text/javascript" src="../js/sweetalert.min.js"></script>
</head>

<body class="bg-register">
	<!-- wrapper -->
	<div class="wrapper">
		<div class="section-authentication-register d-flex align-items-center justify-content-center">
			<div class="row">
				<div class="col-12 col-lg-10 mx-auto">
					<div class="card radius-15">
						<div class="row no-gutters">
							<div class="col-lg-6">
								<div class="card-body p-md-5">
									<div class="text-center">
										<img src="assets/images/logo-icon.png" width="80" alt="">
										<h3 class="mt-4 font-weight-bold">Create an Account</h3>
									</div>
									<div class="login-separater text-center"> <span>REGISTER WITH EMAIL</span>
										<hr/>
									</div>
									<form action="" method="post">
									<div class="form-group mt-4">
										<label>Email Address</label>
										<input type="text" class="form-control" name="email" placeholder="example@user.com" />
									</div>
									<div class="form-group mt-4">
										<label>Username</label>
										<input type="text" class="form-control" name="username">
									</div>
									<div class="form-group mt-4">
										<label>Phone Number</label>
										<input type="text" class="form-control" name="phone_number">
									</div>
									<div class="form-group">
										<label>Password</label>
										<div class="input-group" id="show_hide_password">
											<input class="form-control border-right-0" type="password" name="password" value="">
											<div class="input-group-append">	<a href="javascript:;" class="input-group-text bg-transparent border-left-0"><i class='bx bx-hide'></i></a>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label>Confirm Password</label>
										<div class="input-group" id="show_hide_password">
											<input class="form-control border-right-0" type="password" name="confirm_password" value="">
											<div class="input-group-append">	<a href="javascript:;" class="input-group-text bg-transparent border-left-0"><i class='bx bx-hide'></i></a>
											</div>
										</div>
									</div>
									<div class="form-group">
										<div class="custom-control custom-checkbox">
											<input type="checkbox" class="custom-control-input" id="customCheck1">
											<label class="custom-control-label" for="customCheck1">I read and agree to Terms & Conditions</label>
										</div>
									</div>
									<div class="btn-group mt-3 w-100">
										<button type="submit" class="btn btn-primary btn-block" name="register">Register</button>
										</button>
									</div>
									<hr/>
									<div class="text-center mt-4">
										<p class="mb-0">Already have an account? <a href="authentication-login.html">Login</a>
										</p>
									</div>
									</form>
								</div>
							</div>
							<div class="col-lg-6">
								<img src="assets/images/login-images/register-frent-img.jpg" class="card-img login-img h-100" alt="...">
							</div>
						</div>
						<!--end row-->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- end wrapper -->
	<!-- JavaScript -->
	<!-- jQuery first, then Popper.js, then Bootstrap JS -->
	<script src="assets/js/jquery.min.js"></script>
	<!--Password show & hide js -->
	<script>
		$(document).ready(function () {
			$("#show_hide_password a").on('click', function (event) {
				event.preventDefault();
				if ($('#show_hide_password input').attr("type") == "text") {
					$('#show_hide_password input').attr('type', 'password');
					$('#show_hide_password i').addClass("bx-hide");
					$('#show_hide_password i').removeClass("bx-show");
				} else if ($('#show_hide_password input').attr("type") == "password") {
					$('#show_hide_password input').attr('type', 'text');
					$('#show_hide_password i').removeClass("bx-hide");
					$('#show_hide_password i').addClass("bx-show");
				}
			});
		});
	</script>
</body>
</html>

<?php
if (isset($_POST['register']) == true) {
	$username = mysqli_real_escape_string($conn, $_POST['username']);
	$phone_number = trim($_POST['phone_number']);
	$email = mysqli_real_escape_string($conn, $_POST['email']);
	$password = sha1($_POST['password']);
	$confirm_password = sha1($_POST['confirm_password']);
	$date = date("h:i:a d F Y");

    $req_alphabet = "/^[A-Z a-z]+$/";
    $req_alphanumeric = "/^[A-Za-z0-9]+$/";

    if (empty($username) == true || empty($phone_number) == true || empty($email) == true || empty($password) == true || empty($confirm_password) == true) {
    	echo "<script>swal({text:'All fiels are required', icon:'error'})</script>";
    }else{
    	if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    		echo "<script>swal({text:'Invalid Email Address', icon:'error'})</script>";
    	}else{
	    	if (!is_numeric($phone_number)) {
	    		echo "<script>swal({text:'Invalid Phone Number', icon:'error'})</script>";
	    	}else{
		    	$sel_email = $conn->query("SELECT email FROM administrator WHERE email = '$email' ");
		    	if ($sel_email->num_rows > 0) {
		    		echo "<script>swal({text:'Email Already Exist', icon:'error'})</script>";
		    	}else{
			    	$sel_username = $conn->query("SELECT username FROM administrator WHERE username = '$username' ");
			    	if ($sel_username->num_rows > 0) {
			    		echo "<script>swal({text:'Username Already Exist', icon:'error'})</script>";
			    	}else{
				    	if ($password != $confirm_password) {
				    		echo "<script>swal({text:'Password does not match', icon:'error'})</script>";
				    	}else{
				    		$conn->query("INSERT INTO administrator (username, phone_number, email, password, confirm_password, date_added) VALUES ('$username', '$phone_number', '$email', '$password', '$confirm_password', '$date')") ;
				    		echo "<script>swal({text:'Registered Successfully', icon:'success'})
				    			.then(function(){
				    				window.location = 'login'
				    				})
				    		</script>";
				    	}
				    }
				}
			}
		}
    }
}