<?php
// var_dump($_POST);
// exit;

session_start();



if(isset($_POST)){
       // set the default timezone to use.
 date_default_timezone_set('UTC');
 $transdate1=date('m/d/Y H:i:s');
 $transdate=date('m/d/Y');
 include("config.php");


    $emailaddress = mysqli_real_escape_string($conn,$_POST['emailaddress']);
    $password =md5(mysqli_real_escape_string($conn,$_POST['password']));

    // echo $emailaddress;
    // echo "<br>";
    // echo $password;
    // exit;


    if (empty($emailaddress) && empty($password)){
        $error="Username or Password can not be empty";
        header("Location: login.php?message=$error");

        exit;
    }

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

        $sql_query = "SELECT * FROM users WHERE email='".$emailaddress."' AND password='".$password."'";
        $result = mysqli_query($conn,$sql_query);
        $count=mysqli_num_rows($result);
        while($row = mysqli_fetch_assoc($result)){
            $username = $row["email"];
            $fullName = $row['fname'];
            $userID = $row["userID"];
            $grplevel = $row["grplevel"];
            $lname = $row["lname"];
        }

        if($count < 1) {

            $error ="Account does not exist sign up.";
            // $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate',username='$uname',date_time_login='$transdate1'";
            // $data2=mysqli_query($con,$query2);
            
            header("Location: login.php?message=$error");

            exit;

        }

        // if(($count == 1) && $status=="suspended"){

        //     $err ="Account Suspended, contact customer service";

        //     $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate',username='$uname',date_time_login='$transdate1'";
        //     $data2=mysqli_query($con,$query2);
            
        //     header("Location: n_signin.php?message=$err");

        //     exit;

        // }

        // if(($count == 1) && $status=="blocked"){

        //     $err ="Account blocked, contact customer service";
        //     $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate',username='$uname',date_time_login='$transdate1'";
        //     $data2=mysqli_query($con,$query2);
            
        //     header("Location: n_signin.php?message=$err");
        //     exit;

        // }

          if($grplevel==200){
            $_SESSION['username'] = $username;
            $_SESSION['fname'] = $fname;
            $_SESSION['lname'] = $lname;
           
            $_SESSION['userID'] = $userID;
            $_SESSION['grplevel'] = $grplevel;
            $err ="Login Successful";

            // $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate' ,username='$uname',date_time_login='$transdate1'";
            // $data2=mysqli_query($con,$query2);
            header('Location: dash.php');
        }
        // elseif($grplevel==205){
        //     $_SESSION['username'] = $username;
        //     $_SESSION['f_name'] = $f_name;
        //     $_SESSION['l_name'] = $l_name;
        //     $_SESSION['comp_userid'] = $userid;
        //     $_SESSION['grp_level'] = $grplevel;
        //     $_SESSION['compid'] = $compid;
        //     $err ="Login Successful";

        //     $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate',username='$uname',date_time_login='$transdate1'";
        //     $data2=mysqli_query($con,$query2);
        //     header('Location: sub_users/index.php');
        // }
        else{
            $error ="Wrong login page,you dont have permission.";
            // $query2="INSERT INTO logs set userid='$userid',activity='$err',date_inserted='$transdate',username='$uname',date_time_login='$transdate1'";
            // $data2=mysqli_query($con,$query2);
            
            header("Location: login.php?message=$error");

            exit;


        }

        

    }

}
?>