<?php 
include '../backend/db/db.php';
session_start();

date_default_timezone_set('Asia/Colombo');
$date = date('Y-m-d H:i:s');

//require("../PHPMailer/class.phpmailer.php");

/***************************** New User Reg ****************************/

if (isset($_POST['reg_btn'])) {
	$fname = htmlspecialchars(strip_tags(trim($_POST['fname'])), ENT_QUOTES, 'UTF-8');
	$lname = htmlspecialchars(strip_tags(trim($_POST['lname'])), ENT_QUOTES, 'UTF-8');
	$address_1 = htmlspecialchars(strip_tags(trim($_POST['address_1'])), ENT_QUOTES, 'UTF-8');
	$address_2 = htmlspecialchars(strip_tags(trim($_POST['address_2'])), ENT_QUOTES, 'UTF-8');
	$city = htmlspecialchars(strip_tags(trim($_POST['city'])), ENT_QUOTES, 'UTF-8');
	$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');
	$tel_no = htmlspecialchars(strip_tags(trim($_POST['tel_no'])), ENT_QUOTES, 'UTF-8');
	$pw = htmlspecialchars(strip_tags(trim($_POST['password'])), ENT_QUOTES, 'UTF-8');
	$name_full = $fname.' '.$lname;
	
	$password = hash('sha256', $pw);
	$sql = "INSERT INTO customer SET userID='$email', passWord='$password', order_id=1, f_name='$fname', l_name='$lname', tel_no='$tel_no', address_1='$address_1', address_2='$address_2', city='$city', email='$email', val=1, reg_date='$date', name_full='$name_full'";
	
	$sql_u = mysqli_query($conn, "SELECT userID FROM customer WHERE userID='$email'");
	if(mysqli_num_rows($sql_u) == 0) {
	  
	  if (mysqli_query($conn, $sql)) {
	  	$sql_uid = mysqli_fetch_assoc(mysqli_query($conn, "SELECT cusID FROM customer WHERE userID='$email' AND val=1"));
		$uid = $sql_uid['cusID'];
		
		if (isset($_SESSION['temp_user_cey'])) {
			$tmp_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_cey'])), ENT_QUOTES, 'UTF-8');
			$sqlt = mysqli_query($conn, "SELECT live_cart.inv_id FROM live_cart Inner Join order_details_temp ON order_details_temp.inv_id = live_cart.inv_id WHERE live_cart.tmp_id = $tmp_id");
			if(mysqli_num_rows($sqlt) == 0) {
				
			} else {
				$row_inv = mysqli_fetch_assoc($sqlt);
				$inv = $row_inv['inv_id'];
			}			
			
			$sql_up = "UPDATE live_cart SET User_ID=$uid, tmp_id=0 WHERE tmp_id=$tmp_id";
			
			if (mysqli_query($conn, $sql_up)) {
				if(mysqli_num_rows($sqlt) == 0) {
					
				} else {
					mysqli_query($conn, "UPDATE order_details_temp SET user_id=$uid, guest=0 WHERE user_id=$tmp_id AND inv_id='$inv'");
				}
				
				unset($_SESSION['temp_user_cey']);
				unset($_SESSION['guest_user_cey']);
				
				$_SESSION['normal_user_cey'] = $uid;
				$response['status'] = '1'; // log in
				$response['tel_no'] = $tel_no;
				$response['email'] = $email;
				$response['message'] = 'Sucessfully Registered.';				
			}
		
		} else {
			$_SESSION['normal_user_cey'] = $uid;
			$response['status'] = '2'; // log in
			$response['tel_no'] = $tel_no;
			$response['email'] = $email;
			$response['message'] = 'Sucessfully Registered.';			
		}
  
	  } else {
		  $response['status'] = 'error';
		  // wrong details
		  $response['message'] = 'Something went wrong, please try again.';
	  }		
		
	} else {
	  $response['status'] = 'error';
	  $response['message'] = 'Provided Email is already in use. Please Try Again';		
	}
	echo json_encode($response);
}

/***************************** User Login *****************************/

if (isset($_POST['login_btn'])) {
    $email = htmlspecialchars(strip_tags(trim($_POST['username'])), ENT_QUOTES, 'UTF-8');
    $pass = htmlspecialchars(strip_tags(trim($_POST['password'])), ENT_QUOTES, 'UTF-8');
    $password = hash('sha256', $pass);
	
    $sql = mysqli_query($conn, "SELECT cusID, password, order_id FROM customer WHERE userID='$email' AND val=1");
    $row = mysqli_fetch_assoc($sql);

    if (mysqli_num_rows($sql) == 1 && $row['password'] == $password) {
		$uid = $row['cusID'];
		$oid = $row['order_id'];
		
		if (isset($_SESSION['temp_user_cey'])) {
			$tmp_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_cey'])), ENT_QUOTES, 'UTF-8');
			
			$sql_invo = mysqli_query($conn, "SELECT inv_id FROM live_cart WHERE User_ID=$uid AND tmp_id=0");
			if(mysqli_num_rows($sql_invo) == 0) {
				$sql_up = "UPDATE live_cart SET User_ID=$uid, order_id=$oid, tmp_id=0 WHERE tmp_id=$tmp_id";			
			
			} else {
				$row_invo = mysqli_fetch_assoc($sql_invo);
				$inv_cart = $row_invo['inv_id'];
				$sql_up = "UPDATE live_cart SET User_ID=$uid, inv_id='$inv_cart', order_id=$oid, tmp_id=0 WHERE tmp_id=$tmp_id";				
			}
			
			$sqlt = mysqli_query($conn, "SELECT live_cart.inv_id FROM live_cart Inner Join order_details_temp ON order_details_temp.inv_id = live_cart.inv_id WHERE live_cart.tmp_id = $tmp_id");
			if(mysqli_num_rows($sqlt) == 0) {
				
			} else {
				$row_inv = mysqli_fetch_assoc($sqlt);
				$inv = $row_inv['inv_id'];
			}
			
			if (mysqli_query($conn, $sql_up)) {
				if(mysqli_num_rows($sqlt) == 0) {
					
				} else {
					mysqli_query($conn, "UPDATE order_details_temp SET user_id=$uid, guest=0 WHERE user_id=$tmp_id AND inv_id='$inv'");
				}
				
				unset($_SESSION['temp_user_cey']);
				unset($_SESSION['guest_user_cey']);
				
				$_SESSION['normal_user_cey'] = $uid;
				$response['status'] = '1'; // log in to checkout
			}
		
		} else {
			$sql_invo = mysqli_query($conn, "SELECT inv_id FROM live_cart WHERE User_ID = $uid AND tmp_id=0");
			if(mysqli_num_rows($sql_invo) == 0) {
			
			} else {
				$row_invo = mysqli_fetch_assoc($sql_invo);
				$inv = $row_invo['inv_id'];
				mysqli_query($conn, "UPDATE live_cart SET inv_id='$inv' WHERE User_ID = $uid");
			}
			
			$_SESSION['normal_user_cey'] = $uid;
			$response['status'] = '2'; // log in to website
		}
		    
	} else {
        $response['status'] = 'error';
        // wrong details
        $response['message'] = 'Incorrect Username or Password.';
    }

	echo json_encode($response);
}
?>