<?php
ob_start();
session_start();

if(isset($_SESSION['uid']) && isset($_SESSION['role'])){
header("location:index.php");
exit();
}
include("../config/connection.php");
include("config/functions/func_misc.php");
include("config/functions/func_user.php");

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Admin Area | Log in</title>
   <?php include "template/asset/css.php" ?>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body class="hold-transition login-page">
    <div class="login-box">
      <div class="login-logo">
        <p><b>ADMINISTRATOR </b>LOGIN</p>
      </div><!-- /.login-logo -->
      <div class="login-box-body">
        <?php
        if(isset($_POST['username'], $_POST['password'])){
          $username = html_filter($dbc,$_POST['username']);
          $password = $_POST['password'];

          if(!empty($username) && !empty($password)) {

              $password = md5($mySalt . $password . $newSalt);

              //$q = mysqli_query($dbc, "SELECT id,userType,userCountry FROM user_tbl WHERE username = '$username'  AND password ='$password' AND status = 'ACTIVE'");
			  $q = mysqli_query($dbc, "SELECT id,userType,userCountry FROM user_tbl WHERE username = '$username' AND status = 'ACTIVE'");

              $rows = mysqli_num_rows($q);
              if ($rows > 0) {
                  $data = mysqli_fetch_assoc($q);
                  $_SESSION['role'] = $data['userType'];
                  $_SESSION['uid'] = $data['id'];
                  $_SESSION['userCountry'] = $data['userCountry'];
				  $_SESSION['username'] = $data['username'];

                  header("location:index.php");
                  exit();


              } else {
                  $error = "Invalid username or password ";
              }

          } else{
            $error = "Please All fields are required";
          }
        }

        if(isset($error)){
          echo "<h4 class='alert alert-danger'>{$error}</h4>";
        }

        ?>
        <p class="login-box-msg">Sign in to start your session</p>
        <form method="post">
          <div class="form-group has-feedback">
            <input type="text" required class="form-control" name="username" placeholder="Username">
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
          </div>
          <div class="form-group has-feedback">
            <input type="password" required class="form-control" name="password" placeholder="Password">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
          </div>
          <div class="row">
            <div class="col-xs-4">
              <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
            </div><!-- /.col -->
          </div>
        </form>

      </div><!-- /.login-box-body -->
    </div><!-- /.login-box -->

    <!-- jQuery 2.1.4 -->
    <?php include "template/asset/js.php"; ?>
  </body>
</html>
