<?php
include("../../../functions.inc.php");
checkLogin();
dbConnect();

//controllo non abbia appena cambiato il numero di cellulare
$result = $mysqli->query("SELECT user_id FROM users WHERE user_id = ".$_SESSION['user']['id']." AND user_newcell=1");
$new_cell = $result->num_rows == 1 ? true : false;

if(isset($_POST['pass_old'])){
	
	if(!is_password($_POST['pass_old']))
		printMsg("Vecchia password non valida","danger");
	if(!is_password($_POST['pass_new']))
		printMsg("Nuova password non valida","danger");
	if($_POST['pass_new'] <> $_POST['pass_confirm'])
		printMsg("Nuova password e Conferma password non coincidono","danger");
	
	
	$result = $mysqli->query("SELECT user_id FROM users WHERE user_id = ".$_SESSION['user']['id']." AND user_pass=".dbClean($_POST['pass_old']));
	if($result->num_rows == 1){
		$result = $mysqli->query("UPDATE users SET user_pass = ".dbClean($_POST['pass_new'])." WHERE user_id = ".$_SESSION['user']['id']." AND user_pass=".dbClean($_POST['pass_old']));
		if($result){
			Js('resetForm("formPass");');
			printMsg('Password modificata con successo','success');
		}else{
			printMsg("Errore durante l'aggiornamento. Prova una password differente o contatta l'ente",'danger');
		}
	}else{
		printMsg("Vecchia password non corretta",'danger');
	}
}
?>
<div>
    <div class="modal-d-title">Cambia password</div>

	<?php if(!$new_cell) : ?>
    <form id="formPass" role="form" class="form-horizontal" name="opt_pass" action="<?php echo $_SERVER['REQUEST_URI'];?>" onSubmit="return post(this,'result_pass')">
    
    <div class="modal-body modal-body-diag">
        <div class="form-group">
            <label for="pass_old" class="control-label col-sm-4">Vecchia password</label>
            <div class="col-sm-5">
                <input type="password" maxlength="20" class="form-control" id="pass_old" name="pass_old">
            </div>
        </div>
    
        <div class="form-group">
            <label for="pass_new" class="control-label col-sm-4">Nuova password</label>
            <div class="col-sm-5">
                <input type="password" maxlength="20" class="form-control" id="pass_new" name="pass_new">
                <p class="help-block">Fra 6 e 20 caratteri alfanumerici</p>
            </div>
        </div>
        
        <div class="form-group">
            <label for="pass_confirm" class="control-label col-sm-4">Conferma password</label>
            <div class="col-sm-5">
                <input type="password" maxlength="20" class="form-control" id="pass_confirm" name="pass_confirm">
            </div>
        </div>
    
        <div id="result_pass"></div>
        
    </div>
    
    <div class="modal-footer">
        <button id="okButton" type="submit" class="btn btn-primary">Salva</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
    </div>
    
	<?php else : ?>
    <div class="modal-body modal-body-diag">
	<div class="alert alert-warning">Hai modificato il numero di Cellulare e ti &egrave; stata inviata una nuova password. Per poterla modificare devi prima eseguire almeno un accesso con la nuova password.</div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
    </div>
	<?php endif ?>
    
    </form>
</div>