<?php
include('../scripts/bdd.php');
if(isset($_POST['email']) && isset($_GET['id_server'])) {
	if(!empty($_POST['email']) && !empty($_GET['id_server']) && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['country']) && !empty($_POST['zip']) && !empty($_POST['phone'])) {
		if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
			if(is_numeric($_POST['zip'])) {
				$sql = $bdd->prepare("SELECT id FROM unsubscribe WHERE email_address = ? AND id_server = ?");
				$sql->execute(array($_POST['email'],$_GET['id_server']));
				if(!$sql->rowCount()) {
					$req = $bdd->prepare("INSERT INTO unsubscribe(id_server,firstname,lastname,country,zipcode,phone,email_address,email_md5,date_add) VALUES(?,?,?,?,?,?,?,?,?)");
					$req->execute(array($_GET['id_server'],$_POST['lname'],$_POST['fname'],$_POST['country'],$_POST['zip'],$_POST['phone'],$_POST['email'],md5($_POST['email']),date("Y-m-d H:i:s", time())));
					$success = "Your unsubscription has been done";
				} else {
					$error = "This email address is already in our unsubscribe list";
				}
			} else {
				$error = "Please enter a valid zip code";
			}
		} else {
			$error = "Please enter a valid email address";
		}
	} else {
		$error = "Please complete your informations";
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Send request</title>
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/freelancer.min.css" rel="stylesheet">
    <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css">
</head>
<style type="text/css">.text-danger { color:red; }</style>
<?php if(isset($error)) { ?><script>alert('<?php echo $error; ?>');</script> <?php } ?>
<?php if(isset($success)) { ?><script>alert('<?php echo $success; ?>');</script> <?php } ?>
<body id="page-top" class="index">
    <nav id="mainNav" class="navbar navbar-default navbar-fixed-top navbar-custom">
        <div class="container">
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span> Menu <i class="fa fa-bars"></i>
                </button>
                <a class="navbar-brand" href="#page-top">Scoop Mail</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li class="hidden">
                        <a href="#page-top"></a>
                    </li>
                    <li class="page-scroll">
                        <a href="" onclick="javascript:void(0)">Unsubscribe</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <section id="contact">
        <div class="container" style="margin-top: 48px;">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2 style="font-size: 2em;">Send request to remove your informations</h2>
                    <hr class="star-primary">
                </div>
            </div>
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2">
                    <form action="" method="post" id="contactForm" >
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>First name</label>
                                <input type="text" class="form-control" placeholder="First name" name="fname" required data-validation-required-message="Please enter your first name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                         <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Last name</label>
                                <input type="text" class="form-control" placeholder="Last name" name="lname" required data-validation-required-message="Please enter your last name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                         <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Country</label>
                                <input type="text" class="form-control" placeholder="Country" name="country" required data-validation-required-message="Please enter your country.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                          <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Zip Code</label>
                                <input type="text" class="form-control" placeholder="Zip code" name="zip" required data-validation-required-message="Please enter your zip code.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Email Address</label>
                                <input type="email" class="form-control" placeholder="Email Address" name="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Phone Number</label>
                                <input type="tel" class="form-control" placeholder="Phone Number" name="phone" required data-validation-required-message="Please enter your phone number.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <br>
                        <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-success btn-lg">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
      <div class="scroll-top page-scroll hidden-sm hidden-xs hidden-lg hidden-md">
        <a class="btn btn-primary" href="#page-top">
            <i class="fa fa-chevron-up"></i>
        </a>
    </div>
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/freelancer.min.js"></script>
</body>
</html>