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

dbConnect();

checkLogin('adm');
checkEnteService($_GET['serv']);

$rif_id = intval($_GET['rif_id']);
$grp_id = intval($_GET['grp_id']);

if($rif_id == 0){
	$p_title = 'Gruppi principali';
	$placeholder = 'inserisci nome gruppo';
}else{
	$p_title = 'Sottogruppo';
	$is_sub = true;
	$placeholder = 'inserisci valore sottogruppo';
}

function print_grp_list(){
	$rif_id = intval($_GET['rif_id']);
	foreach(get_gruppi($_GET['serv'],$rif_id) as $id=>$nome){
		
		$nome = entities($nome);
		
		if($rif_id == 0){
			$sub_btn = '
			<a href="#" class="m-left-15 text-primary" data-open="modal" data-url="/servizi/diag/gruppi/?serv='.$_GET['serv'].'&rif_id='.$id.'" data-small="1">
				<span class="glyphicon glyphicon-chevron-right"></span>
			</a>';
		}
		
		echo '
		<li id="entry_grp_'.$id.'" class="list-group-item">
			<div class="row">
				<div class="col-xs-6 col-sm-9 txt-overflow" id="nome_grp_'.$id.'">'.$nome.'</div>
				<div class="col-xs-6 col-sm-3 text-right">
					<a href="javascript:;" onClick="editgrp(this,'.$id.')"><span class="text-warning glyphicon glyphicon-pencil"></span></a> 
					<a href="javascript:;" class="m-left-10" onClick="delgrp('.$id.')"><span class="glyphicon glyphicon-remove text-danger"></span></a>
					'.$sub_btn.'
				</div>
			</div>
		</li>';
	}
}

//elimino
if(intval($_GET['del'])>0){
	$id = intval($_GET['del']);
	$result = $mysqli->query("DELETE FROM gruppi WHERE ente_id = ".$_SESSION['user']['ente_id']." AND (grp_id = ".$id." OR rif_id = ".$id.")");
	if($mysqli->affected_rows > 0)
		echo $id;
	die();
}

//salvo impostazioni
if(isset($_POST['nome'])){
	if(!is_empty($_POST['nome'])){
		$q = "INSERT INTO gruppi VALUES (NULL, ".$_SESSION['user']['ente_id'].",".dbClean($_GET['serv']).",".dbClean($_POST['nome']).",".$_SESSION['user']['ente_anno'].",".$rif_id.")";
		$result = $mysqli->query($q);
		if(!$result)
			$error = "Errore durante l'inserimento";
	}else{
		$error = "Inserire un valore";
	}
	
	if($error)
		printMsg($error,"danger",false);
	else
		Js("$('#grp-name".$rif_id."').val('');");
		
	print_grp_list();	
	die();

}

//estraggo impostazioni
?>
<div>
    <div class="modal-d-title"><?php echo $p_title;?></div>
    <div class="modal-body modal-body-diag">    
        
        <form role="form" class="form-horizontal"action="<?php echo $_SERVER['REQUEST_URI'];?>" onSubmit="return post(this,'result_grp_edit<?php echo $rif_id;?>')">
            <div class="form-group">
                <div class="col-xs-9 col-sm-10">
                	<input type="text" maxlength="80" class="form-control" name="nome" id="grp-name<?php echo $rif_id;?>" placeholder="<?php echo $placeholder;?>">
                </div>
                <div class="col-xs-3 col-sm-2 text-right">
                	<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span></button>
                </div>
            </div>
        </form>
        
        <ul class="list-group" id="result_grp_edit<?php echo $rif_id;?>">
            <?php print_grp_list(); ?>
        </ul>
                
	</div>
    
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
    </div>

</div>
<script>
function delgrp(id){
	confirm("Sei sicuro di volere eliminare questo <?php echo $is_sub ? 'Sottogruppo' : 'Gruppo';?>?<br />Eliminerai anche le eventuali associazioni alle anagrafiche",function(){
		$.get('/servizi/diag/gruppi/?del='+id+'&rif_id=<?php echo $rif_id;?>&serv=<?php echo $_GET['serv'];?>', function(data){
			$('#entry_grp_'+data).remove();
			closeDialog();
		});
	});
}
function editgrp(el,id){
	dialog('/servizi/diag/gruppi_edit/?id='+id+'&n=' + encodeURIComponent($(el).parent().prev().html()),1);	
}
</script>