<?php
session_start();
include ("includes/database.php");
include ("includes/clean.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>LOGIN USER</title>
</head>
<body>
<!-- youtube.com /DarkCode -->
	<meta charset="utf-8">
	<title>LOGIN FORM</title>
	<link rel="stylesheet" type="text/css" href="style.css">

<body class="hold-transition login-page">
<div class="login-box">
<div class="login-box-body">

<?php
if (isset($_POST["user"]))
  {
  
$user=cucidata($_POST["user"]);
$pwd=cucidata($_POST["pwd"]);


//step 1 - check record exist

$dataWujud=mysqli_query($con,"select * from tbl_user where username='$user'");  //sql query
$showData=mysqli_fetch_array($dataWujud); //put data into array
$bil_rekod=mysqli_num_rows($dataWujud); //check number of records
$jenis=1;

if ($bil_rekod==0)
{
$dataWujud=mysqli_query($con,"select * from tbl_shop where shop_username='$user'");  //sql query
$showData=mysqli_fetch_array($dataWujud); //put data into array
$bil_rekod=mysqli_num_rows($dataWujud); //check number of records}
$jenis=2;

if ($bil_rekod==0)
{$msg="<div class='callout callout-danger'><h4>Login Status</h4> <p>Wrong username or password!. Please try again </p></div>";}


elseif ($showData['shop_password']!=$pwd) //check password matching
{$msg="<div class='callout callout-danger'><h4>Login Status</h4> <p>Wrong username or password!. Please try again </p></div>";}


else
{
  
//set session


$_SESSION["username"]=$showData['shop_username'];
$_SESSION["status"]=$showData['status'];


header ("location: menu2.php");
}
}
else
{

if ($bil_rekod==0)
{$msg="<h4>Login Status</h4> <p>Wrong username or password!. Please try again </p></div>";}


elseif ($showData['user_password']!=$pwd) //check password matching
{$msg="<h4>Login Status</h4> <p>Wrong username or password!. Please try again </p></div>";}


else
{
  
//set session
$_SESSION["username"]=$showData['username'];
$_SESSION["status"]=$showData['status'];



header ("location: main.php");

}}

echo $msg;


} ?>

<form method="post" align="center">
<div class="form-group has-feedback">

	<h1><center>LOGIN USER</h1>
	<div class="textbox"></center>
	Username: <input type="text" placeholder="your username" name="user" required>
</div>	

<div class="textbox">
	Password: <input type="password" placeholder="your password" name="pwd"  required>
</div>
<input class="btn" type="submit" name="submit" value="Sign In">
</form>


<center>

<br><br>
Click <a href="register.php">here</a> to register user<br>
<br>
Click <a href="shoplogin.php">here</a> to login to shop<br>


</center>

</body>
</html>