<?php
$show_stuff = "";
$update_status = "";

if(isset($_GET['change_pwd']) AND isset($_POST['submit'])) {

	if(!check_password($_SESSION['id'], '', $_POST['old_pwd'])) {
		return false;
	}

	if($_POST['new_pwd'] == $_POST['new_rpt']) {

		$hash_pwd = password_hash($_POST['new_pwd'], PASSWORD_BCRYPT);

		$this_var['id'] = $_SESSION['id'];
		$this_var['password'] = $hash_pwd;

		if(edit_user($this_var)) {
			update_last_login($_SESSION['id']);
			$update_status = "<div class='pwd_update pwd_update_success'>Password updated successfully</div>";
		} else {
			$update_status = "<div class='pwd_update pwd_update_fail'>Password updated failed</div>";
		}
	}
} elseif(isset($_GET['change_name']) AND isset($_POST['submit'])) {
	
	if($_POST['user_name'] == "") {
		return false;
	}
	
	$this_var = array('id' => $_SESSION['id'], 'user_name' => check_vars($_POST['user_name']), 'email' => check_vars($_POST['email']), 'lang' => check_vars($_POST['lang']));
	if(edit_user($this_var)) {
		$update_status = "<div class='pwd_update pwd_update_success'>Accpunt settings updated successfull</div>";
		$_SESSION['user_name'] = $_POST['user_name'];
	} else {
		$update_status = "<div class='pwd_update pwd_update_fail'>Name/e-mail update failed!</div>";
	}
	
}

$content .= profile_page();

/*
// We don't have the password or email info stored in sessions so instead we can get the results from the database.
$stmt = $con->prepare('SELECT password, email, lang, last_login FROM accounts WHERE id = ?');
// In this case we can use the account ID to get the account info.
$stmt->bind_param('s', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($password, $email, $lang, $last_login);
$stmt->fetch();
$stmt->close();
*/

$change_your_password = "";

if($last_login == '0000-00-00 00:00:00') {
	$change_your_password = "<div class='pwd_update pwd_update_fail'>".$l_Change_your_password."</div>";
}

function profile_page() {
	include(LANGUAGEFILE);
	global $update_status, $change_your_password;
	$returndata = '';
	
	$variables = array('name' => 'lang', 'id' => 'lang', 'selected_value' => $_SESSION['lang']);
	$lang_select = lang_select_menu($variables);
	
	$returndata .= "
			<div class='content'>
				<h2>Profile Page</h2>
				".$update_status.$change_your_password."
				<div>
					<p>".$l_Your_account_details_are_below.":</p>
					<table>
						<tr>
							<td>".$l_user_name.":</td>
							<td>".$_SESSION['login_name']."</td>
						</tr>
						<tr>
							<td>".$l_name.":</td>
							<form action='index.php?profile=edit&change_name' method='post'>
							<td><input type='text' name='user_name' value='".$_SESSION['user_name']."'> </td>
							
						</tr>
						<tr>
							<td>".$l_e_mail.":</td>
							<td><input type='text' name='email' value='".$_SESSION['login_name']."' DISABLED></td>
						</tr>
						<tr>
							<td>".$l_Language.":</td>
							<td>".$lang_select."</td>
						</tr>
						<tr>
							<td></td>
							<td><input type='submit' name='submit' value='Save'></td>
							</form>
						</tr>
						<tr>
							<td>".$l_Change_password.":</td>
							<td>
							<form action='index.php?profile=edit&change_pwd' method='post'>
								<div class='ml-4'><div style='width:70px;' class='row'>".$l_Old_Password.":</div><div class='row'><input type='password' name='old_pwd'></div></div>
								<div class='ml-4'><div style='width:70px;' class='row'>".$l_New_Password.":</div><div class='row'><input type='password' name='new_pwd'></div></div>
								<div class='ml-4'><div style='width:70px;' class='row'>".$l_Rpt_Password.":</div><div class='row'><input type='password' name='new_rpt'></div></div>
								<div class='ml-4 mt-2'><div style='width:70px;' class='row'></div><div class='row text-right'><input type='submit' name='submit' value='Save'></div></div>
							</form>
							</td>
						</tr>
					</table>
				</div>
			</div>

	";
	
	return $returndata;
}

?>