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

dbConnect();

//elimino iscritto e anagrafiche associate
if(isset($_GET['del'])){
	del_personale(intval($_GET['del']));
	die();
}

$serv_ente = get_ente_servizi();
if(intval($_GET['id'])>0)
	$user = get_personale($_GET['id']);

//salvo dati
if(isset($_POST['nome'])){
	
	if(is_empty($_POST['nome']))
		printMsg("Compilare il campo <strong>Anagrafica</strong>","danger");
	
	if(is_empty($_POST['cellulare']))
		printMsg("Compilare il campo <strong>Nome utente</strong>","danger");
		
	if(!is_password($_POST['password']))
		printMsg("La password deve essere <strong>fra 6 e 20 caratteri alfanumerici</strong>","danger");
		

	//controllo che il cell non sia gia presente
	$result = $mysqli->query("SELECT user_cell FROM users WHERE user_cell = ".dbClean($_POST['cellulare'])." AND user_id <> ".intval($_GET['id'])." AND ente_id = ".$_SESSION['user']['ente_id']);
	if($result->num_rows > 0)
		printMsg("Il <strong>Nome utente risulta gi&agrave; in uso</strong>","warning");
	
	//servizi collegati
	if(count($_POST['servizio']) > 0){
		foreach($_POST['servizio'] as $serv_cod){
			foreach($serv_ente as $k=>$v)
				if($v['serv_cod']==$serv_cod){
					$serv_usr[] = $serv_cod;
				}
		}
		if($serv_usr)
			$serv_usr = implode(',',$serv_usr);
	}
	if($user){
		$result = $mysqli->query("UPDATE users SET
			user_cell =  ".dbClean($_POST['cellulare']).",
			user_nome = ".dbClean(welltxt($_POST['nome'])).",
			user_pass = ".dbclean($_POST['password']).",
			user_services = ".dbClean($serv_usr)."
			WHERE user_id = ".intval($_GET['id'])." AND ente_id = ".$_SESSION['user']['ente_id']
		);
	}else{
		$result = $mysqli->query("INSERT INTO users (ente_id,user_cell,user_nome,user_pass,user_grp,user_services) VALUES (
			".$_SESSION['user']['ente_id'].",
			".dbClean($_POST['cellulare']).",
			".dbClean(welltxt($_POST['nome'])).",
			".dbclean($_POST['password']).",
			'per',
			".dbClean($serv_usr).")"
		);
	}
	
	if(!$result)
		printMsg("Errore durante il salvataggio. Ricontrollare i dati","danger");
	elseif($user)
		printMsg("Modifiche salvate con successo!","success");
	else{
		Js('window.location.reload();');
		die();
	}
}


?>
<div>
    <div class="modal-d-title">Modifica dati</div>
    <form id="form_usr" class="form-horizontal" action="<?php echo $_SERVER['REQUEST_URI'];?>" role="form" onSubmit="return post(this,'f-user-result')">
    <div class="modal-body modal-body-diag">    

            <div class="form-group-inline form-horizontal">
                <div class="form-group">
                    <div class="col-sm-6">
                        <label class="control-label">Anagrafica</label>
                        <input class="form-control" type="text" maxlength="40" name="nome" value="<?php echo entities($user['user_nome']);?>" />
                    </div>
                </div>
                
                <div class="form-group">
                    <div class="col-sm-6">
                        <label class="control-label">Nome utente</label>
                        <input class="form-control" type="text" maxlength="10" name="cellulare" value="<?php echo entities($user['user_cell']);?>" />
                    </div>
                    <div class="col-sm-6">
                        <label class="control-label">Password</label>
                        <input class="form-control" type="password" maxlength="20" name="password" value="<?php echo entities($user['user_pass']);?>" />
                    </div>
                </div>


                <div class="form-group form-subtitle"><h4>Servizi da gestire</h4></div>
                <ul class="list-group">
                
                <?php
                if(count($serv_ente) > 0){
    
                    foreach($serv_ente as $k=>$v){
	                    $checked = $user['servizi'][$v['serv_cod']] ? ' checked' : '';
                        echo '
                            <li class="list-group-item">
                                <div class="row">
                                    <label class="control-label xs-ptop-0 col-sm-4 col-md-3" for=""s'.$v['serv_id'].'"">'.$v['serv_name'].'</label>
                                    <div class="col-sm-8 col-md-9">
                                        <input type="checkbox" id="s'.$v['serv_id'].'" name="servizio[]" value="'.$v['serv_cod'].'" class="switch-btn"'.$checked.'>
                                    </div>
                                </div>
                            </li>
                        ';
                    }
                }
                ?>
                </ul>

			</div>   	

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

    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Salva</button>
        <?php if($user) : ?>
        <button type="button" class="btn btn-danger" onClick="delPersonale(<?php echo intval($_GET['id']);?>)">Elimina</button>
        <?php endif ?>
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
    </div>
    </form>
</div>