<?PHP
$_OPTIMIZATION["title"] = "Вход в аккаунт";
$_OPTIMIZATION["description"] = "Авторизация пользователя в системе";
$_OPTIMIZATION["keywords"] = "Авторизация пользователя в системе";

?>
<!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">
	<title>Дальнобойщики - Вход в аккаунт</title>

	<!-- Global stylesheets -->
	<link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
	<link href="/assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
	<link href="/assets/css/bootstrap.css" rel="stylesheet" type="text/css">
	<link href="/assets/css/core.css" rel="stylesheet" type="text/css">
	<link href="/assets/css/components.css" rel="stylesheet" type="text/css">
	<link href="/assets/css/colors.css" rel="stylesheet" type="text/css">
	<!-- /global stylesheets -->

	<!-- Core JS files -->
	<script type="text/javascript" src="/assets/js/plugins/loaders/pace.min.js"></script>
	<script type="text/javascript" src="/assets/js/core/libraries/jquery.min.js"></script>
	<script type="text/javascript" src="/assets/js/core/libraries/bootstrap.min.js"></script>
	<script type="text/javascript" src="/assets/js/plugins/loaders/blockui.min.js"></script>
	<!-- /core JS files -->


	<!-- Theme JS files -->
	<script type="text/javascript" src="assets/js/core/app.js"></script>
	<!-- /theme JS files -->

</head>

<body class="login-container">

	


	<!-- Page container -->
	<div class="page-container">

		<!-- Page content -->
		<div class="page-content">

			<!-- Main content -->
			<div class="content-wrapper">

				<!-- Content area -->
				<div class="content">
<?php if ($_SESSION["user_id"]) : ?>
	<?php endif;?>
	<?php if (!$_SESSION["user_id"]) : ?>
					<!-- Simple login form -->
					<form id="loginform" action="" method="post">
						<div class="panel panel-body login-form">
							<div class="text-center">
								<div class="icon-object border-slate-300 text-slate-300"><i class="icon-reading"></i></div>
								<h5 class="content-group">Авторизация <small class="display-block">Введите свои учетные данные ниже</small></h5>
							</div>
<?PHP

# Определение платежей по акции 'Накопительный банк'
$c_date = date("Ymd",time());
$c_date_begin = strtotime($c_date." 00:00:00");
$c_date_end = strtotime($c_date." 23:59:59");
$y_date = date("Ymd",time()-24*60*60);
$y_date_begin = strtotime($y_date." 00:00:00");
$y_date_end = strtotime($y_date." 23:59:59");


$db->Query("SELECT * FROM db_back WHERE date_add >='".$c_date_begin."' AND  date_add <='".$c_date_end."' ORDER BY id DESC LIMIT 1");
if($db->NumRows() == 0) {


$db->Query("SELECT sum(serebro) FROM db_insert_money WHERE date_add >='".$y_date_begin."' AND  date_add <='".$y_date_end."' AND money >= '1'" );
if($db->NumRows() == 0) $sum_c = 0;
else $sum_c = $db->FetchRow();


$db->Query("SELECT date_add as date_add, user as user, user_id as user_id, SUM(serebro) as sserebro  FROM db_insert_money WHERE date_add >='".$y_date_begin."' AND  date_add <='".$y_date_end."' GROUP BY user ORDER by sserebro DESC LIMIT 1");
$firstuser=$db->FetchArray();
$firstpercent=(1.5/100)*$sum_c;
$db->Query("UPDATE db_users_b SET money_b = money_b + '".$firstpercent."' WHERE id = '".$firstuser['user_id']."'");


$db->Query("SELECT date_add as date_add, user as user, user_id as user_id, SUM(serebro) as sserebro  FROM db_insert_money WHERE date_add >='".$y_date_begin."' AND  date_add <='".$y_date_end."' GROUP BY user ORDER by sserebro DESC LIMIT 1,2");
$seconduser=$db->FetchArray();
$secondpercent=(1.0/100)*$sum_c;
$db->Query("UPDATE db_users_b SET money_b = money_b + '".$secondpercent."' WHERE id = '".$seconduser['user_id']."'");


$db->Query("SELECT date_add as date_add, user as user, user_id as user_id, SUM(serebro) as sserebro  FROM db_insert_money WHERE date_add >='".$y_date_begin."' AND  date_add <='".$y_date_end."' GROUP BY user ORDER by sserebro DESC LIMIT 2,3");
$thirduser=$db->FetchArray();
$thirdpercent=(0.5/100)*$sum_c;
$db->Query("UPDATE db_users_b SET money_b = money_b + '".$thirdpercent."' WHERE id = '".$thirduser['user_id']."'");

$db->Query("INSERT INTO db_back (`id`,`user_id`,`bank`,`sum`,`date_add`) VALUES(NULL,'".$firstuser['user_id']."','".$sum_c."','".$firstpercent."','".time()."'),(NULL,'".$seconduser['user_id']."','".$sum_c."','".$secondpercent."','".time()."'), (NULL,'".$thirduser['user_id']."','".$sum_c."','".$thirdpercent."','".time()."')");


//$db->Query("INSERT INTO db_back (`id`,`user_id`,`bank`,`sum`,`date_add`) VALUES(NULL,'".$firstuser['user_id']."','".$sum_c."','".$firstpercent."','".time()."')");
//$db->Query("INSERT INTO db_back (`id`,`user_id`,`bank`,`sum`,`date_add`) VALUES(NULL,'".$seconduser['user_id']."','".$sum_c."','".$secondpercent."','".time()."')");
//$db->Query("INSERT INTO db_back (`id`,`user_id`,`bank`,`sum`,`date_add`) VALUES(NULL,'".$thirduser['user_id']."','".$sum_c."','".$thirdpercent."','".time()."')");



	/*$db->Query("SELECT sum(serebro) as serebro, user_id FROM db_insert_money WHERE date_add >='".$y_date_begin."' AND  date_add <='".$y_date_end."'  AND money >= '1' GROUP BY SEREBRO");
	$num_nb = $db->NumRows();
	$sum_nb = 0;
	$user_nb = '';

	if($num_nb != 0) {
		if($num_nb == 1) {
			$data_nb = $db->FetchArray();
			$sum_nb = $sum_nb + $data_nb['serebro'];
			$user_nb = $user_nb.$data_nb['user_id']." ";
		} else {
			while($data_nb = $db->FetchArray()) {
				$sum_nb = $sum_nb + $data_nb['serebro'];
				$user_nb = $user_nb.$data_nb['user_id']." ";
			}
		}

		$ar_user_nb = explode(" ", $user_nb);
		$pos_nb = rand(0, $num_nb-1);

		$db->Query("SELECT percent_back FROM db_config WHERE id = '1'");
		$percent_back = $db->FetchRow();
		$db->FreeMemory();

		$sum_nb1 = round($sum_nb * $percent_back / 100,0);

		# Зачилсяем юзверю
		$db->Query("UPDATE db_users_b SET money_b = money_b + '".$sum_nb1."' WHERE id = '".$ar_user_nb[$pos_nb]."'");

		# Пишем в табл
		$db->Query("INSERT INTO db_back (`id`,`user_id`,`bank`,`sum`,`date_add`) VALUES(NULL,'".$ar_user_nb[$pos_nb]."','".$sum_nb."','".$sum_nb1."','".time()."')");

		# echo $sum_nb." ".$num_nb." ".$pos_nb."-".$ar_user_nb[$pos_nb];
	}*/
}
	if(isset($_POST["log_email"])){

	$lmail = $func->IsMail($_POST["log_email"]);

		if($lmail !== false){

			$db->Query("SELECT id, user, pass, referer_id, banned FROM db_users_a WHERE email = '$lmail'");
			if($db->NumRows() == 1){

			$log_data = $db->FetchArray();

				if(strtolower($log_data["pass"]) == strtolower($_POST["pass"])){

					if($log_data["banned"] == 0){

						# Считаем рефералов
						$db->Query("SELECT COUNT(*) FROM db_users_a WHERE referer_id = '".$log_data["id"]."'");
						$refs = $db->FetchRow();

						$db->Query("UPDATE db_users_a SET referals = '$refs', date_login = '".time()."', ip = INET_ATON('".$func->UserIP."') WHERE id = '".$log_data["id"]."'");

						$_SESSION["user_id"] = $log_data["id"];
						$_SESSION["user"] = $log_data["user"];
						$_SESSION["referer_id"] = $log_data["referer_id"];
						?>


<meta http-equiv="refresh" content="0;url=/profile" />


<?

					}else echo "<div class='alert alert-danger'>Аккаунт заблокирован</div>";

				}else echo "<div class='alert alert-danger'>Неверные данные</div>";

			}else echo "<div class='alert alert-danger'>E-mail не зарегистрирован</div>";

		}else echo "<div class='alert alert-danger'>Недопустимый формат E-mail</div>";

	}

?>
							<div class="form-group has-feedback has-feedback-left">
								<input type="text" name="log_email" class="form-control" placeholder="E-mail">
								<div class="form-control-feedback">
									<i class="icon-user text-muted"></i>
								</div>
							</div>

							<div class="form-group has-feedback has-feedback-left">
								<input type="password" name="pass" class="form-control" placeholder="Пароль">
								<div class="form-control-feedback">
									<i class="icon-lock2 text-muted"></i>
								</div>
							</div>

							<div class="form-group">
								<button type="submit" class="btn btn-primary btn-block">Авторизоваться <i class="icon-circle-right2 position-right"></i></button>
							</div>

							<div class="text-center">
								
							</div>
						</div>
					</form>
					<!-- /simple login form -->
<?php endif;?>

					<!-- Footer -->
					<div class="footer text-muted text-center">
						© 2018 Дальнобойщик - All Rights Reserved.
					</div>
					<!-- /footer -->

				</div>
				<!-- /content area -->

			</div>
			<!-- /main content -->

		</div>
		<!-- /page content -->

	</div>
	<!-- /page container -->

</body>
</html>
