<?php
session_start();


if(@$_GET['action'] == "logout"){
	session_unset();
	$url = "../e-shop.php";  
	echo "<script language='javascript' 
	type='text/javascript'>";  
	echo "window.location.href='$url'";  
	echo "</script>";  
	exit();
}
if(!isset($_POST['submit'])){
	exit('Fail!');
}
$username = htmlspecialchars($_POST['username']);
$password = base64_encode($_POST['password']);
$_SESSION['page'] = "0";
include('../../conf/conn.php');

$query = "select * from clients where username='$username' and password='$password' limit 1";
$result=mysqli_query($conn,$query);
$query = "select * from clients where email='$username' and password='$password' limit 1";
$result2=mysqli_query($conn,$query);

if($row = mysqli_fetch_assoc($result)){
	$_SESSION['username'] = $username;
	$_SESSION['id'] = $row['id'];
	$_SESSION['email'] = $row['email'];
	$_SESSION["phone"] = $row['phone'];
	$_SESSION["first_name"] = $row['first name'];
	$_SESSION["last_name"] = $row['last name'];
	$_SESSION["address"] = $row['address'];
    $login = date("Y-m-d H:i:s"); 
	$query = "UPDATE clients SET logintime = '$login' WHERE username = '$username'";
	$result=mysqli_query($conn,$query);
	$url = "../e-shop.php";  
	echo "<script language='javascript' 
	type='text/javascript'>";  
	echo "window.location.href='$url'";  
	echo "</script>";  
} elseif($row = mysqli_fetch_assoc($result2)) {
	$_SESSION['username'] = $row['username'];
	$_SESSION['id'] = $row['id'];
	$_SESSION['email'] = $row['email'];
	$_SESSION["phone"] = $row['phone'];
	$_SESSION["first_name"] = $row['first name'];
	$_SESSION["last_name"] = $row['last name'];
	$_SESSION["address"] = $row['address'];
	$username = $row['username'];
    $login = date("Y-m-d H:i:s"); 
	$query = "UPDATE clients SET logintime = '$login' WHERE username = '$username'";
	$result=mysqli_query($conn,$query);
	$url = "../e-shop.php";  
	echo "<script language='javascript' 
	type='text/javascript'>";  
	echo "window.location.href='$url'";  
	echo "</script>";  
}else{	
	$_SESSION["page"] = 5;
	$_SESSION["fail"] = 1;
	$url = "../e-shop.php";  
	echo "<script language='javascript' 
	type='text/javascript'>";  
	echo "window.location.href='$url'";  
	echo "</script>";  
	exit();
}
if(isset($_SESSION["Tusername"])){
	$Tusername=$_SESSION["Tusername"];
	$username=$_SESSION['username'];
	$query = "select * from cart where username='$Tusername'";
	$result=mysqli_query($conn,$query);
	$row = mysqli_fetch_assoc($result);
	$product_name=$row["product_name"];
	$amount=$row["amount"];
	$query = "select * from cart where username='$username' and product_name='$product_name'";
	$result=mysqli_query($conn,$query);
	if($row = mysqli_fetch_assoc($result)){
	$query = "select * from cart where username='$username' and product_name='$product_name'";
	$result=mysqli_query($conn,$query);
	$row = mysqli_fetch_assoc($result);
	$amount=$row["amount"]+$amount;
	$query = "UPDATE cart SET amount = '$amount' WHERE cart.username = '$username' and cart.product_name = '$product_name'";
	$result=mysqli_query($conn,$query);
	$_SESSION["page"]=2;;
	unset($_SESSION["Tusername"]);
	}else{
	$query = "UPDATE cart SET username = '$username' WHERE username = '$Tusername'";
	$result=mysqli_query($conn,$query);
	$_SESSION["page"]=2;
	unset($_SESSION["Tusername"]);}
}

?>