<?php 
session_start();
require_once("admin/production/class/connectdb.php");
$db = new DbConnect;
$con = $db->connect();

$output = '';

if(isset($_POST['submit'])){
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email = $_POST['email'];
	$password = $_POST['password'];
	$cpassword = $_POST['cpassword'];
	$phone = $_POST['phone'];
	$contact_address = $_POST['contact_address'];
	$shipping_address = $_POST['shipping_address'];
	require_once('admin/production/class/cregister.php');
	
	$user = new Register('account');
	
	$isSignedUp = json_decode($user->isSignedUp($email, $password));
	if($password == $cpassword){
		if($isSignedUp->userCount > 0){
		$output = '<div class="alert alert-danger alert-intro" role="alert">
					User already exist with this email. <a href=""> </a>
				</div>';
		}else{
			$signUp = $user->signUp($firstname, $lastname, $email, $phone, $contact_address, $shipping_address, $password);
			if($signUp == true){
				$output = '<div class="alert alert-success alert-intro" role="alert">
							Your account as successfully been created. <a href="login"> Click here to login </a>
						</div>';
						
				$isSignedUp = json_decode($user->isSignedUp($email, $password));
				
				if($isSignedUp->userCount > 0){
					$_SESSION['user_id'] = $isSignedUp->accountId;
					header('location:checkout-shipping-2');
				}
			}else{
				$output = '<div class="alert alert-danger alert-intro" role="alert">
							Failed to create account.
						</div>';
			}
		}
	}else{
		$output =   '<div class="alert alert-danger alert-intro" role="alert">
							Failed! Please check that the password entered are the same.
					</div>';
	}			
}
include('includes/head.php'); 
?>
<body>
    <div class="page-wrapper">
        <?php include('includes/header.php') ?>

        <main class="main">
            <nav aria-label="breadcrumb" class="breadcrumb-nav">
                <div class="container">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">Sign up</li>
                    </ol>
                </div><!-- End .container -->
            </nav>

            <div class="container">
                <div class="heading mb-4">
                    <h2 class="title">Sign up</h2>
                    <p>Please fill the form below to Sign up.</p>
                </div><!-- End .heading -->

                <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])  ?>" method="POST">
				    
					<div class="form-group required-field col-lg-5">
						<?php echo $output;  ?>
					</div><!-- End .form-group -->
					
					<div class="form-group required-field">
						<label for="contact-firstname">First Name</label>
						<input type="text" class="form-control" name="firstname" required>
					</div><!-- End .form-group -->

					<div class="form-group required-field">
						<label for="contact-lastname">Last Name</label>
						<input type="text" class="form-control" name="lastname" required>
					</div><!-- End .form-group -->

					<div class="form-group">
						<label for="contact-phone">Phone Number</label>
						<input type="text" class="form-control" name="phone">
					</div><!-- End .form-group -->
					
					<div class="form-group">
						<label for="contact-email">Email</label>
						<input type="email" class="form-control" name="email">
					</div><!-- End .form-group -->
					
					<div class="form-group">
						<label for="contact-phone">Password</label>
						<input type="password" class="form-control" name="password">
					</div><!-- End .form-group -->
					
					<div class="form-group">
						<label for="contact-email">Confirm password</label>
						<input type="password" class="form-control" name="cpassword">
					</div><!-- End .form-group -->

					<div class="form-group required-field">
						<label for="contact-address">Contact Address</label>
						<textarea cols="30" rows="1"  class="form-control" name="contact_address" required></textarea>
					</div><!-- End .form-group -->
					
					<div class="form-group required-field">
						<label for="contact-message">Shipping Address</label>
						<textarea cols="30" rows="1" class="form-control" name="shipping_address" required></textarea>
					</div><!-- End .form-group -->

					<div class="form-footer">
						<button type="submit" name="submit" class="btn btn-primary">Sign up</button>
					</div><!-- End .form-footer -->
					Already Registered? <a href="login">Login<a/>
				</form>
            </div><!-- End .container -->

            <div class="mb-10"></div><!-- margin -->
        </main><!-- End .main -->

        <?php include('includes/footer.php') ?>
    </div><!-- End .page-wrapper -->

    <div class="mobile-menu-overlay"></div><!-- End .mobil-menu-overlay -->

    <?php include('includes/mobile-menu.php') ?>

    <a id="scroll-top" href="#top" title="Top" role="button"><i class="icon-angle-up"></i></a>



    <!-- Plugins JS File -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/plugins.min.js"></script>

    <!-- Main JS File -->
    <script src="assets/js/main.min.js"></script>
</body>

</html>