<?php
ob_start();
session_start();


if ($_GET['ref'] > 0){
    
    $id = strip_tags($_GET['ref']);

    $servername = "localhost";
    $database = "adminell_ella";
    $username = "adminell_ella";
    $password = 'Ln4-MV*3s9^$';

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $database);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }


    $sql = "SELECT * FROM ella WHERE id = '$id' LIMIT 1";
    $result = mysqli_query($conn, $sql);
	while($data = mysqli_fetch_assoc($result)){
    $user = $data;
	}
    
    $sql = "UPDATE ella SET svc=svc+1 WHERE id='$id' ";
    if ($conn->query($sql) === TRUE) {
      //echo "Record updated successfully";
    } else {
     // echo "Error updating record: " . $conn->error;
    }
}


require 'lang.php';


// 86400 = 1 day
if ($_GET['tracking'] != "") {
	setcookie('tracking', $_GET['tracking'], time() + (86400 * 30), "/");
}
if ($_GET['tag'] != "") {
	setcookie('tag', $_GET['tag'], time() + (86400 * 30), "/");
}
if ($_GET['affid'] != "") {
	setcookie('affid', $_GET['affid'], time() + (86400 * 30), "/");
}

$affid = $_GET['affid'];
$tag = $_GET['tag'];
$sub1 = $_GET['sub1'];
$sub2 = $_GET['sub2'];
$lng = 'en';

if ($_GET['tracking'] != "") {
	$tag = $_GET['tracking'];
}
if ($_GET['cid'] != "") {
	$tag = $_GET['cid'];
}
if ($_GET['sid'] != "") {
	$affid = $_GET['sid'];
}
if ($_GET['campid'] != "") {
	$campid = $_GET['campid'];
}
if ($_GET['zid'] != "") {
	$zid = $_GET['zid'];
}
if ($_GET['lng'] != "") {
	$lng = $_GET['lng'];
}

if ($_GET['sub1'] != "") {
	$sub1 = $_GET['sub1'];
}
if ($_GET['sub1'] != "") {
	$sub2 = $_GET['sub2'];
}
if ($_GET['ref'] != "") {
	$ref = $_GET['ref'];
}

$ip = $_SERVER['REMOTE_ADDR'];
$geo = "";
if (function_exists("geoip_country_code_by_name")) {
	$geo = geoip_country_code_by_name($ip);
}
?>

<!DOCTYPE html>
<html lang="<? if ($_GET['lng'] != "") echo $_GET['lng'] ?>">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>
		<? echo $title ?>
	</title>
	<link rel="stylesheet" href="css/styles.min.css">
	<script>
		var emailLink;
		var redirectLink;

		function formSubmit() {

			// Show loader
			$("#submit_button").hide();
			$("#loader").show();

			let name = $('#nameInput').val();
			let phone = $('#phoneInput').val();
			let email = $('#emailInput').val();

			if (name !== "" && phone !== "" && email !== "") {
				emailLink = email.split("@")[1];

				$.ajax({
					url: "api.php",
					dataType: "json",
					data: {
						"name": name,
						"phone": phone,
						"email": email,
						"lng": "<? echo $lng ?>",
						"geo": "<? echo $geo ?>",
						"link": window.location.href,
						"affid": "<? echo $affid ?>",
						"tag": "<? echo $tag ?>",
						"zid": "<? echo $zid ?>",
						"ref": "<? echo $ref ?>",
						"sub1": "<? echo $sub1 ?>",
						"sub2": "<? echo $sub2 ?>",
						"campid": "<? echo $campid ?>",
					},
					method: 'GET',
					success: function(data) {
						redirectLink = data.redirect;

						$(".content__s_2").slideUp();
						$(".content__s_3").slideDown();
					},
					error: function(data) {
						console.error(data.responseJSON);
						$("#errorResponse").html(data.responseJSON.error);
						$(".content__s_2").slideUp();
						$(".content__s_4").slideDown();
					}
				});
			}

			return false;
		}

		function goToMail() {
			window.location.href = "https://" + emailLink;
		}

		function redirect() {
			window.location.href = redirectLink;
		}

		function goBack() {
			$("#submit_button").show();
			$("#loader").hide();

			$('#emailInput').val("");

			$(".content__s_4").slideUp();
			$(".content__s_2").slideDown();
		}
	</script>
	<style>
		.loader {
			display: none;
			border: 16px solid #ffffff;
			border-top: 16px solid #00e871;
			border-radius: 50%;
			width: 80px;
			height: 80px;
			animation: spin 2s linear infinite;
		}

		@keyframes spin {
			0% {
				transform: rotate(0deg);
			}

			100% {
				transform: rotate(360deg);
			}
		}
	</style>

</head>

<body>
	<div class="wrap">
		<div class="bg"></div>

		<div class="content">

			<div class="content__l1">
				<div class="content__s content__s_active">
					<? echo $headline ?>
				</div>
			</div>

			<div class="content__l2">

				<div class="content__s content__s_active center">
					<span class="content__timer"></span>
				</div>

			</div>

			<div class="content__l3">

				<form action="" method="post" class="content__form" onsubmit="return formSubmit();">

					<div class="content__s content__s_active content__s_1">
						<? echo $q1 ?>
						<div class="content__buttons">
							<button type="button" class="content__button content__button_yes content__button_go" data-step="1">
								<? echo $yes ?>
							</button>
							<button type="button" class="content__button content__button_yes content__button_go" data-step="1">
								<? echo $no ?>
							</button>
						</div>
					</div>


					<div class="content__s content__s_2">
						<? echo $q2 ?>
						<div class="content__input-group">
							<div>
								<img src="images/user-regular.svg" alt="" class="content__icn-envelope">
								<input id="nameInput" required type="text" name="name" value="" placeholder="Name" class="content__input">
							</div>
						</div>
						<div class="content__input-group">
							<div>
								<img src="images/phone-solid.svg" alt="" class="content__icn-envelope">
								<input id="phoneInput" required type="tel" name="phone" value="" placeholder="Phone" class="content__input">
							</div>
						</div>
						<div class="content__input-group">
							<div>
								<img src="images/envelope-regular.svg" alt="" class="content__icn-envelope">
								<input id="emailInput" required type="email" name="email" value="" placeholder="Email" class="content__input">
							</div>
						</div>
						<br>

						<input type="hidden" name="tag" value="<? echo $_GET['tag'] ?>">
						<input type="hidden" name="affid" value="<? echo $_GET['affid'] ?>">
						<input type="hidden" name="lp" value="<? echo $_GET['lp'] ?>">
						<input type="hidden" name="sub1" value="<? echo $_GET['sub1'] ?>">
						<input type="hidden" name="sub2" value="<? echo $_GET['sub2'] ?>">
						<input type="hidden" name="ref" value="<? echo $_GET['ref'] ?>">

						<div class="center_content">

							<button type="submit" name="submit" class="content__button content__button_submit" id="submit_button">
								<? echo $content['testafterlead'] ?>
							</button>

							<div id="loader" class="loader"></div>
						</div>

						<br>
						<? echo $text3 ?>

					</div>


					<div class="content__s content__s_3"><b>
							<center></center>
							</be><? echo $content['wait'] ?>
							<div class="content__buttons">
								<!--<button class="content__button" onclick="goToMail();">
								<? echo $content['checkmail'] ?>
							</button>-->
								&nbsp;
								<button class="content__button" onclick="redirect();">
									<? echo $content['redirect'] ?>
								</button>
							</div>
					</div>

					<div class="content__s content__s_4">
						<div class="center_content warning">
							<? echo $q3 ?>&nbsp;(<span id="errorResponse"></span>)
						</div>
						<br>
						<div class="center_content">
							<button class="content__button" onclick="goBack();">
								<? echo $content['retry'] ?>
							</button>
						</div>
					</div>

				</form>

			</div>

		</div>

	</div>

	</div>

	</div>

	<script src="js/scripts.min.js"></script>
	
	<!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-2L4ZEZMTWM"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'G-2L4ZEZMTWM');
    </script>
	
	
</body>

</html>