<?PHP session_start(); ?>
<!DOCTYPE html>
<style type="text/css">
    body, html {
    background-image: url("../includes/pos2.jpg");
    /* Full height */
    height: 100%; 

  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}


  </style>
<html>
  <head>
  <?php require_once("../includes/header-links.php");

  include '../includes/connection.php';
  
  ?>
  <?php

$checkgenerauser=mysqli_query($conn,"SELECT * from users,permissions  where user_name='gadmin' && permissions.role='General Admin'");
if (mysqli_num_rows($checkgenerauser)<1) {
    $pasg=md5("gadmin");
    $guname="gadmin";
    $guser="General Admin";
    $grole="General Admin"; 
//     $sql = "INSERT INTO users (name, user_name, contact, password, role,user_code,branch_id,business_id)
// VALUES ('$fullname', '$username2', '$contact','$password2', '$role','$code','$brr','$bnss')";
   $in=mysqli_query($conn,"INSERT INTO users (name, user_name, contact, password, role,user_code,branch_id,business_id)
VALUES ('$guser', '$guname', '00000000','$pasg', '$grole','0000','0','0')");
}
?>
    <title>Admin Login</title>
   <!-- <?php
	
  $today = date("Y-m-d");
$sql = mysqli_query($con,"SELECT * FROM backups WHERE bupdate='$today' ");
    $num2 = mysqli_num_rows($sql);
    if($num2<1){
      
      ?><meta http-equiv="refresh" content="0;url=backup.php">
    <?php } ?>-->
  
  <?php
    $buziness_name='';
  ?>
  
  </head>
  <body id="login">
    <div class="container">
<h1 align="center" style="color: white; text-shadow: 4px 4px 8px #000000;"><?php echo $buziness_name; ?></h1>
<h3 class="text-center"  style="color: white; text-shadow: 4px 4px 8px #000000;">Welcome To Poscream Point of sales system</h3>
<hr>
<br>
      <form class="form-signin" method="POST" >
        <h2 class="form-signin-heading">Please sign in</h2>
        <input type="text" class="input-block-level" placeholder="User Name" name="username" title="Please enter your User Name!">
        <input type="password" class="input-block-level" placeholder="Password" name="password" title="Please enter your password">
        
        <button class="btn btn-large btn-primary" type="submit" name="login">Sign in</button>
      </form>

<?php
include '../includes/connection.php';

if(isset($_POST['login'])){
  $username2=$_POST['username'];
  $password2=md5($_POST['password']);

  // Create connection
$sql = mysqli_query($con,"SELECT * FROM users where user_name='$username2' and password='$password2' && status=1 && suspend=1");
if (mysqli_num_rows($sql) > 0) {
    // output data of each row
  $row = mysqli_fetch_assoc($sql);

  $_SESSION['fullname']=$row['name'];
  $_SESSION['username']=$row['user_name'];
  
  $_SESSION['contact']=$row['contact'];
  $_SESSION['id']=$row['id'];
  $_SESSION['branch_id']=$row['branch_id'];
  $_SESSION['business_id']=$row['business_id'];
  $bnssid=$row['business_id'];

$chechourbss=mysqli_query($con,"SELECT * from business where business_id='$bnssid' && status=1");
$ros=mysqli_fetch_array($chechourbss);
if (mysqli_num_rows($chechourbss)<=0 && $ros['business_id']!=0 && $ros['branch_id']!=0 ) {
  echo "<script>alert('Your Business may not be Active, Contact your admin')</script>";
}else{
  if ($_SESSION['business_id']==0) {
    ?>
    <script>window.location='../business/business.php'</script>
    <?php
  }else{


  $id_user=$row['id'];
  $activity="logged into the system";
  $bran=$row['branch_id'];
  $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id) VALUES 
('$id_user','$activity','$bran')");
?>

	<script>window.location='dashboard.php'</script>
	
  <?php
    
  }
}
} else {
  $id_user="2";
  $activity="Tried to login but failed";
  $log_sql22=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id) VALUES 
('$id_user','$activity','0')");
  ?>
    <div class="alert alert-error alert-block">
                    <a class="close" data-dismiss="alert" href="#">&times;</a>
                    <h4 class="alert-heading">Error!</h4>
                    Invalid User Nameor Password. Pease try again or contact your administrator!
                  </div>

    <?php
}

//mysqli_close($con);
}

?>


    </div> <!-- /container -->
    <div class="row">
      <div class="col-lg-12">
        <div class="col-lg-2"></div>
   <div  class="col-lg-8"  style="background-color: #08C; color: white;font-size: 20px;">
    <?php

  $businessname="Product of Nugsoft Technologies Limited";

?>
     <p align="center" > <br><?php echo $businessname ?> &copy;&nbsp;<?php echo date('Y');?></p>
     <p>&nbsp</p>
   </div> 
 
</div>



    <script src="../vendors/jquery-1.9.1.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
  </body>
</html>
