<?php
    if(!empty(GETPOST('action')) && GETPOST('action') == "login") {
        $customer_email = GETPOST('email');
        $customer_passwd = GETPOST('passwd');
        $back = GETPOST('back');
        
        $login = ws_login($customer_email, $customer_passwd);

		if (!empty($login['status']) && $login['status'] == 1) {
        	session_start(); // start session
        	if(isset($_SESSION['user'])){  //if session var already exist
                unset($_SESSION['user']);
            }
    
            $_SESSION['user']['id_user'] = $login['userid'];
            $_SESSION['user']['pseudo'] = $login['pseudo'];
            $_SESSION['user']['email'] = $login['login'];
            $_SESSION['user']['is_preferred_member'] = $login['is_preferred_member'];
            if($back == 'payement'){
                redirectToContainer('payment');
            }

            ws_refreshCart($login['socid']);
            redirectToContainer('myaccount');
        }else{
            $error++;
    		$error_messages[] = $weblangs->trans("authError");
        }
    }
?>



<?php
    $back = GETPOST('back');
?>
<div class="col-xs-12 col-sm-5">
	<form action="login-register.php" method="post" id="login_form" class="box">
			<h3 class="page-subheading"><?php print $weblangs->trans("alreadyRegistered"); ?></h3>
			<?php if (isset($error) && $error != 0): ?>
        		<div class='alert alert-danger'>
            		<?php $add_s = ($error > 1) ? "s" : ""; ?>
            		<p><?php print $weblangs->trans("errorsOccurred", $error, $add_s); ?></p>
            		<ol>
            		    <?php foreach ($error_messages as $key => $value): ?>
                    		<li><?php print $value; ?></li>
                    	<?php endforeach ?>
            		</ol>
            	</div>
        	<?php endif ?>
			<div class="form_content clearfix">
				<div class="form-group">
					<label for="email"><?php print $weblangs->trans("email"); ?></label>
					<input class="is_required validate account_input form-control" data-validate="isEmail" type="text" id="email" name="email" value="<?php if (isset($customer_email)){print $customer_email;} ?>">
				</div>
				<div class="form-group">
					<label for="passwd"><?php print $weblangs->trans("password"); ?></label>
					<span><input class="is_required validate account_input form-control" type="password" data-validate="isPasswd" id="passwd" name="passwd" value="<?php if (isset($customer_passwd)){print $customer_passwd;} ?>"></span>
				</div>
				<input type="hidden" name="action" value="login">
				<input type="hidden" name="back" value="<?php print $back; ?>">
				<p class="lost_password form-group"><a href="forgot-password.php" title="<?php print $weblangs->trans("recoverPass"); ?>" rel="nofollow"><?php print $weblangs->trans("forgotPassword"); ?></a></p>
				<p class="submit">
					<button type="submit" id="SubmitLogin" name="SubmitLogin" class="button btn btn-default button-medium">
						<span>
							<?php print $weblangs->trans("signIn"); ?>
							<i class="icon-chevron-right right"></i>
						</span>
					</button>
				</p>
			</div>
		</form>
</div>
