<?
require('../include/startup.php');
if(isset($_REQUEST['action']) && $_REQUEST['action']=='check_login')
{
	session_start();
	
	if(isset($_REQUEST['tel2']) && $_REQUEST['tel2'] !=""){
		die("not okay");
		
	}
	
	$message = "";
	$user_name		= $_REQUEST['user_name'];
	$password		= md5($_REQUEST['password']);	
	
	if($password =="" || $user_name ==""){
		die("Username or Password can't be blank <a href='index' style='color:blue;'>Login here</a>");
	}
	if($_SERVER['HTTP_HOST'] == "localhost"){
		$sql=mysqli_query($conn,"SELECT * FROM admin WHERE `uname` ='".$user_name."' AND `password`='".$password."' and is_active=1");	
		$num=mysqli_num_rows($sql);	
		
		if($num >0){
		
			$rows = mysqli_fetch_array($sql);		
			$_SESSION['user_id']=$rows['id'];
			$_SESSION['uname']=$rows['uname'];
			$_SESSION['admin_user_id']=$rows['id'];
			$_SESSION['admin_group_id']=$rows['group_id'];
			$_SESSION['login']=$user_name;
			$_SESSION['email_admin']=$rows['email_admin'];
			$_SESSION['email_info']=$rows['email_info'];
			$_SESSION['email_sales']=$rows['email_sales'];
			
			$_SESSION['email_enquiry']=$rows['email_enquiry'];
			$_SESSION['admin_color']= $rows['admin_color'];
			$_SESSION['access_control']= $rows['access_control'];
			$_SESSION['is_super_admin']= ($rows['is_super_admin'] ==1) ? true : false; 
			if($rows['group_id'] == 2){
				$_SESSION['is_consultant'] = true;
			}
					
			$session_history = session_id();		
			$_SESSION['session_history']=$session_history;
			mysqli_query($conn,"UPDATE admin SET session_history = '$session_history' WHERE id = '".$_SESSION['user_id']."'");
			
			if(isset($_SESSION['redirect_to'])){
				$url = "index";
				////////////////// dont redirect when admin first time login ////////////////
				if($url == $siteURL."/"){
					unset($_SESSION['redirect_to']);
					header('Location: index');
					die;
				}
				unset($_SESSION['redirect_to']);
				header("Location: $url");
			}else{
				header('Location: index');
			}
			exit;
		}else{ 
		
			$user_name		= $_REQUEST['user_name'];
			$password		= str_replace(" ","",$_REQUEST['password']);	
			$sql1=mysqli_query($conn,"SELECT * FROM job WHERE `email` ='".$user_name."' AND `password` ='".$password."' ");	
			$num1=mysqli_num_rows($sql1);	
			if($num1 >0){
				$rows = mysqli_fetch_array($sql1);
				$_SESSION['job_user_id'] = $rows['id'];
				$_SESSION['job_username'] = $rows['email'];
				$_SESSION['login']=$rows['first_name1']." ".$rows['last_name1'];
				header('Location: index');
				exit;
			}else{
				$message = "Invalid login. Please try again";
			}
		}
	}else{
		// login with PDO
		/* $dbh->query("SELECT * FROM admin WHERE (uname = :user_name OR email = :user_name) AND password = :password AND is_active = 1");
$dbh->bind(':user_name', $user_name);
$dbh->bind(':password', $password);
$getResult = $dbh->resultset(); */

		$dbh->query("SELECT * FROM admin WHERE uname =:user_name AND password=:password and is_active=1");
		$dbh->bind(':user_name',$user_name);	
		$dbh->bind(':password',$password);
		$getRessult = $dbh->resultset();	
		//$num=mysql_num_rows($sql);	
		if($dbh->rowcount()>0){
		   foreach($getRessult as $rows){
				$_SESSION['user_id']=$rows['id'];
				$_SESSION['uname']=$rows['uname'];
				$_SESSION['admin_user_id']=$rows['id'];
				$_SESSION['admin_group_id']=$rows['group_id'];
				$_SESSION['login']=$user_name;
				$_SESSION['email_admin']=$rows['email_admin'];
				$_SESSION['email_info']=$rows['email_info'];
				$_SESSION['email_sales']=$rows['email_sales'];
				
				$_SESSION['email_enquiry']=$rows['email_enquiry'];
				$_SESSION['admin_color']= $rows['admin_color'];
				$_SESSION['access_control']= $rows['access_control'];
				$_SESSION['is_super_admin']= ($rows['is_super_admin'] ==1) ? true : false; 
				if($rows['group_id'] == 2){
					$_SESSION['is_consultant'] = true;
				}
				$session_history = session_id();		
				$_SESSION['session_history']=$session_history;
				mysqli_query($conn,"UPDATE admin SET session_history = '$session_history' WHERE id = '".$_SESSION['user_id']."'");
				
				if(isset($_SESSION['redirect_to'])){
					$url = "index";
					////////////////// dont redirect when admin first time login ////////////////
					if($url == $siteURL."/"){
						unset($_SESSION['redirect_to']);
						header('Location: index');
						die;
					}
					unset($_SESSION['redirect_to']);
					header("Location: $url");
				}else{
					header('Location: index');
				}
				exit;
		   }
		}else{
			
			$dbh->query("SELECT * FROM job WHERE email =:user_name AND password=:password ");
			$dbh->bind(':user_name',$user_name);	
			$dbh->bind(':password',$password);
			$getRessult = $dbh->resultset();	
			//$num=mysql_num_rows($sql);	
			if($dbh->rowcount()>0){
			//$sql1=mysql_query("SELECT * FROM job WHERE `email` ='".$user_name."' AND `password` ='".$password."' ");	
			//$num1=mysql_num_rows($sql1);	
			//if($num1 >0){
				//$rows = mysql_fetch_array($sql1);
				foreach($getRessult as $rows){
					$_SESSION['job_user_id'] = $rows['id'];
					$_SESSION['job_username'] = $rows['email'];
					$_SESSION['login']=$rows['first_name1']." ".$rows['last_name1'];
					header('Location: index');					
					exit;
				}
			}else{
				$message = "Invalid login. Please try again";
			}
		}
	}
}
?>

<!DOCTYPE html>
<html>
  <head>
    <meta name="robots" content="noindex, nofollow">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Thermotec</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	
	<link rel="shortcut icon" href="<?php echo $siteURL ?>/favicon.png"/>
	
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="<?php echo $siteURL ?>/bootstrap/css/bootstrap.css">
    <!-- Font Awesome -->
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">-->
    <!-- Ionicons -->
    <!--<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">-->
    <!-- Theme style -->
    <link rel="stylesheet" href="<?php echo $siteURL ?>/dist/css/adminlte.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="<?php echo $siteURL ?>/plugins/iCheck/square/blue.css">
	
	<link rel="stylesheet" href="<?php echo $siteURL ?>/plugins/fontawesome-free/css/all.min.css">

</head>
<body class="hold-transition login-page">
	<div class="login-box">
      <div class="login-logo text-center" >
		<?php
		$logo = utility::getTableFieldById('sites', 'logo_name', 'site_name', $config->siteName);
		$src = "../images/logo/" . $logo;
		?>
		<img id="" src="<?php echo $src; ?>" style="width: 151px;"><br>
        <!--<b>BEB System</b>-->
      </div><!-- /.login-logo -->
      <div class="login-box-body">
		<? if(isset($message) && $message!=''){?>
		<div class="callout callout-danger">
		  <?=$message;?>
		</div>
		<? }else{ ?>
			<div class="callout callout-info">Sign in to start your session</div>
		<? } ?>
        <form name="form1" method="post" action="index.php?action=check_login">
          <div class="form-group has-feedback">
            <input type="text" class="form-control" name="user_name" placeholder="Username" required>
			<input class="forminputset" type="text" name="tel2" id="tel2" value="" />
            <span class="glyphicon glyphicon-user form-control-feedback"></span>
          </div>
          <div class="form-group has-feedback">
            <input type="password" class="form-control" name="password" placeholder="Password" required>
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
          </div>
          <div class="row">
				<div class="col-sm">
					 <a href="<?=$config->siteURL;?>/forgot_password">I forgot my password</a>
				</div><!-- /.col -->
				<div class="col-sm">
				  <!--<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>-->
				  <input name="submit" class="btn btn-primary btn-block btn-flat pull-right" type="submit" value="Sign In">
				</div><!-- /.col -->
          </div>
        </form>
      </div><!-- /.login-box-body -->
	  <div class="company-social-area">
		<p>&nbsp;</p>
		<p class="copyright-login" style="margin-top:8px"><a target="_blank" href="https://www.web-alliance.co.uk" class="webdeclaration">Powered By Web Alliance<br>
		<img src="<?=$config->siteURL;?>/images/web_alliance.png" style="height:65px;margin:5px 0px 5px 0px"></a>
		</p>
		<p class="copyright-login"> 
			<a href="tel:+44 (0) 7594 27 96 60" >+44 (0) 800 677 1786</a> 
			<a href="mailto:ashish@web-alliance.co.uk" style="padding-left:5px">ashish@web-alliance.co.uk</a>
		</p>	
		
		<div class="red-strip-login">
			<a href="https://www.facebook.com/pages/Web-Alliance-Limited/434950883246019" title="facebook" target="_blank"><i class="fab fa-facebook-f"></i></a>	
			<a href="https://www.linkedin.com/in/ashish-kumar-50bb6516" title="LinkedIn" target="_blank"><i class="fab fa-linkedin-in"></i></a>
		</div>
	  </div>
    </div><!-- /.login-box -->
  <!-- jQuery 2.1.4 -->
    <script src="<?php echo $siteURL ?>/plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="<?php echo $siteURL ?>/bootstrap/js/bootstrap.min.js"></script>
    <!-- iCheck -->
    <script src="<?php echo $siteURL ?>/plugins/iCheck/icheck.min.js"></script>
</body>
</html>
<style>
.login-box, .register-box{
	  margin: 4% auto;
}
.forminputset{
	display:none !important;
}
.company-social-area{
	width:100%;
	height:auto;
	/* position: fixed; */
    bottom: 15px;
    left: 0;
}

.copyright-login {
	font-size: 14px;
	margin: 0;
	padding: 0 15px;
	width: 100%;
	color: #fff;
	text-align: center;
}

.copyright-login a {
	font-size: 14px;
	color: #3f3f3f;
}

.copyright-login a:hover {
	font-size: 14px;
	color: #3f3f3f;
}

.copyright-login:hover {
	font-size: 14px;	
	/* text-decoration:underline; */
}

.red-strip-login {
    border-top: 0 solid #333;
    padding: 0;
    margin-top: 8px;
    text-align: center;	
}
.red-strip-login span {
    color: #3f3f3f;
    font-size: 20px;
    font-family: 'SegoeUI-Semilight';
    text-transform: uppercase;
    display: inline-block;
    vertical-align: middle;
    padding-left: 12px;
    margin-right: 22px;
}
.red-strip-login a {
    display: inline-block;
    vertical-align: middle;
    text-align: center;
    transition: all ease .3s;
    -webkit-transition: all ease .3s;
    -moz-transition: all ease .3s;
    -ms-transition: all ease .3s;
    -o-transition: all ease .3s;
    margin: 1px 1px 0 0;
	font-size: 1.5rem;
    width: 36px;
    height: 36px;
	color:#3f3f3f;
}
.red-strip-login a:hover {
    /*background: #ffffff none repeat scroll 0 0;*/
}
.red-strip-login a:hover .fa {
    color: #333;
	/*text-shadow:0 0 3px #3f3f3f;*/
}
.red-strip-login a .fa {
    height: 34px;
    width: 34px;
    border: 0px solid #000;
    font-size: 20px;
    color: #3f3f3f;
    display: table-cell;
    vertical-align: middle;
    transition: all ease .3s;
    -webkit-transition: all ease .3s;
    -moz-transition: all ease .3s;
    -ms-transition: all ease .3s;
    -o-transition: all ease .3s;
}	
</style>