<?php
session_start();
$success="";
if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE) {
    header("Location: " . $config_basedir."access-login.php");
}
require("config.php");
require("db.php");

$seller= $_SESSION['SESS_USERID']; 
$select=mysqli_query($db,"SELECT * FROM logins WHERE id='$seller'");
$serow=mysqli_fetch_assoc($select);
$forename = $serow['firstname'];
$surname = $serow['lastname'];
$country = $serow['country'];
$state = $serow['state'];
$city = $serow['city'];
$postcode = $serow['currency'];
$phone = $serow['phone'];
$email = $serow['email'];
$username = $serow['username'];
$password = $serow['password'];
$userImage = $serow['user_image'];

if(isset($_POST['update']))
{
     
    $forename = mysqli_real_escape_string($db,$_POST['forenameBox']);
    $surname = mysqli_real_escape_string($db, $_POST['surnameBox']);
    //$country = mysqli_real_escape_string($db, $_POST['country']);
    //$state = mysqli_real_escape_string($db, $_POST['state']);
    //$city = mysqli_real_escape_string($db, $_POST['city']);
    $postcode = mysqli_real_escape_string($db, $_POST['postcodeBox']);
    $phone = mysqli_real_escape_string($db, $_POST['phoneBox']);
    $email = mysqli_real_escape_string($db, $_POST['emailBox']);
	
	
	$country=mysqli_real_escape_string($db, $_POST['txtcountry']);
	$state=mysqli_real_escape_string($db, $_POST['txtstate']);
	$city=mysqli_real_escape_string($db, $_POST['txtcity']);
	
	$userImage=$_FILES['userImage'];
	
	if(!empty($forename) && !empty($surname) && !empty($email) && !empty($phone) && !empty($postcode)){
	   
	   //var_dump($userImage);
	   
	   $imageName=$userImage['name'];
	   $fileType=$userImage['type'];
	   $fileSize=$userImage['size'];
	   
	   $fileTmpName=$userImage['tmp_name'];
	   $fileError=$userImage['error'];
	   
	   $fileImageData = explode('/', $fileType);
	   $fileExtention = $fileImageData[count($fileImageData)-1];
	   
	   if($fileExtention == 'jpg' || $fileExtention == 'png' || $fileExtention == 'jpeg'){
		   //Process file upload
		   
		   if($fileSize < 1000000){
			$fileNewName = "userImages/".$imageName;
		    $uploaded=move_uploaded_file($fileTmpName,$fileNewName);
		   
		     if($uploaded){
			   
			   $sql = "UPDATE logins SET user_image='$imageName', firstname='$forename', lastname='$surname', email='$email', country='$country', state='$state', city='$city', phone='$phone', currency='$postcode' WHERE id=$seller"; 
               $result=mysqli_query($db,$sql);
			   			   			   
			   header('location: account-details.php?success=userUpdated');
		        exit;			 
			 }
		   
	   
		   }else{
			     header('location: account-details.php?error=invalidFileSize');
		        exit;
		   }	
		   
	   }else{
		   header('location: account-details.php?error=invalidFileType');
		exit;
	   }





	   
	}
	
	
	else{
		header('location: account-details.php?error=dataEmpty');
		exit;
	}
     
}
?>