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

if(isset($_POST['new_owner'])){
	
	if(intval($_POST['new_owner']) == 0)
		printMsg("Seleziona un utente da assegnare all'anagrafica","danger");

	$new_owner = intval($_POST['new_owner']);
	check_my_user($new_owner);
	
	$result = $mysqli->query("UPDATE anagrafiche SET user_id = ".$new_owner." WHERE anagr_id = ".intval($_GET['anagr'])." AND ente_id = ".$_SESSION['user']['ente_id']);
	if($result){
		$result = $mysqli->query("SELECT CONCAT_WS(' ',user_cognome,user_nome) AS anagrafica FROM users WHERE user_id = ".$new_owner." AND ente_id = ".$_SESSION['user']['ente_id']);
		$anagr = $result->fetch_object()->anagrafica;
		Js('$("#owner-id").val('.$new_owner.'); setOwnerName("#owner-name","'. entities(welltxt($anagr)) .'"); closeDialog();');
	}else{
		printMsg("Errore durante l'aggiornamento","danger");	
	}
	
	die();	
}
?>
<div>
    <div class="modal-d-title">Seleziona utente da assegnare</div>
    <form class="form-horizontal" action="<?php echo $_SERVER['REQUEST_URI'];?>" role="form" onSubmit="return post(this,'f-seluser-result')">
    <div class="modal-body modal-body-diag">    

            <div class="form-group">
                    <label class="control-label col-sm-3">Utente</label>
                <div class="col-sm-9">
                    <select class="form-control" name="new_owner">
                    <option value=""></option>
                    <?php
					if($users = get_users(false,array('no_limit'=>1))){
						foreach($users['list'] as $k=>$v){
							echo '<option value="'.$v['user_id'].'">'.$v['user_cognome'].' '.$v['user_nome'].'</option>';	
						}
					}
					
					?>
                    </select>
                </div>
            </div>

			<div id="f-seluser-result"></div>

    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Assegna</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Annulla</button>
    </div>
    </form>
</div>