<?php
include("../../../functions.inc.php");
checkLogin('adm');
checkEnteService('pps');
include("../functions.inc.php");
include("../../anagrafiche/functions.inc.php");
include("../../servizi/functions.inc.php");

dbConnect();

//rimuovi delegato
if(isset($_GET['rdel'])){
	del_delegato($_GET['rdel'],'bus');
}

$anagr_id = intval($_REQUEST['id']);	
check_my_anagr($anagr_id);

//salvo impostazioni
if(isset($_POST['save'])){
	
	$anagr_id = intval($_GET['id']);
	
	if(!anagr_has_serv($anagr_id,'pps'))
		printMsg('Utente non associato a questo servizio',"danger");
	
	if(intval($_POST['scuola']) == 0)
		printMsg('Selezionare la Scuola',"danger");
	if(intval($_POST['classe']) == 0)
		printMsg('Selezionare la Classe',"danger");
	
	$result = $mysqli->query("
	UPDATE serv_pps SET
	scuola_id = ".intval($_POST['scuola']).",
	classe_id = ".intval($_POST['classe']).",
	servpps_sospeso = ".intval($_POST['sospeso']).",
	servpps_pre = ".intval($_POST['pres']).",
	servpps_post = ".intval($_POST['posts']).",
	servpps_congedo = ".intval($_POST['congedo'])."
	WHERE anagr_id = ".$anagr_id);

	if(!$result)
		printMsg("Errore durante il salvataggio","danger");

	Js('closeDialog();');
	die();
}

//estraggo impostazioni
$s = get_servpps($anagr_id);
?>
<div>
    <div class="modal-d-title">Servizio Pre / Post scuola</div>
	<form role="form" class="form-horizontal" name="anagr_serv" action="<?php echo $_SERVER['REQUEST_URI'];?>" onSubmit="return post(this,'result_anagr_serv')">
        <div class="modal-body modal-body-diag">    

        <div class="form-group form-subtitle"><h4>Scuola frequentata</h4></div>
        
        <ul class="list-group">
            <li class="list-group-item">
                <div class="row">
                    <div class="col-sm-8">
                        <div class="row">
                        	<label class="control-label col-sm-3 xs-ptop-0">Scuola</label>
                            <div class="col-sm-8">
                        		<select class="form-control" name="scuola" onChange="selXmlPop('#classe','sottogruppi',this.value)">
                                <option value=""></option>
                                <?php
								foreach(get_gruppi('pps') as $grpid=>$name){
									$sel = $grpid == $s['scuola_id'] ? ' selected' : '';
									echo '<option value="'.$grpid.'"'.$sel.'>'.$name.'</option>';
								}
								?>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-sm-4">
                        <div class="row">
                        	<label class="control-label col-sm-4">Classe</label>
                            <div class="col-sm-8">
                        		<select class="form-control" name="classe" id="classe"<?php if(!$s['scuola_id']) echo ' disabled'; ?>>
                                <option value=""></option>
                                <?php
								if($s['scuola_id']){
									foreach(get_gruppi('pps',$s['scuola_id']) as $grpid=>$name){
										$sel = $grpid == $s['classe_id'] ? ' selected' : '';
										echo '<option value="'.$grpid.'"'.$sel.'>'.$name.'</option>';
									}
								}
								?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
        </ul>

    	<div class="row">
			<div class="col-sm-6">

        		<div class="form-group form-subtitle"><h4>Impostazioni servizio</h4></div>
	            
                <ul class="list-group">
                    <li class="list-group-item">
                        <div class="row">
                            <label class="control-label col-xs-6 col-md-8" for="sospeso">Sospendi</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input onChange="servsosp(this)" type="checkbox" id="sospeso" name="sospeso" value="1" class="switch-btn"<?php if($s['servpps_sospeso'] == 1) echo ' checked';?> />
                            </div>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="row">
                            <label class="control-label col-xs-6 col-md-8" for="pres">Pre scuola</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="pres" id="pres" value="1" class="switch-btn"<?php if($s['servpps_pre'] == 1) echo ' checked';?> />
                            </div>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="row">
                            <label class="control-label col-xs-6 col-md-8" for="posts">Post scuola</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="posts" id="posts" value="1" class="switch-btn"<?php if($s['servpps_post'] == 1) echo ' checked';?> />
                            </div>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="row">
                            <label class="control-label col-xs-6 col-md-8" for="congedo">Auto congedo</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="congedo" id="congedo" value="1" class="switch-btn"<?php if($s['servpps_congedo'] == 1) echo ' checked';?> />
                            </div>
                        </div>
                    </li>
                </ul>
                
            </div>
            
			<div class="col-sm-6">
                <div class="form-group form-subtitle"><h4>Delegati al ritiro del minore</h4></div>
                <ul class="list-group">
                    <li class="list-group-item">
                        <div id="delegati-add">
                            <button type="button" class="btn btn-primary" onClick="confDelegato('pps','<?php echo $s['anagr_id'];?>')">
                            	<span class="glyphicon glyphicon-plus"></span> Aggiungi
                            </button>
                            <input type="hidden" id="n_delegati" value="10" />
                        </div>
                        <div class="form-group">
                            <div id="delegati" class="col-xs-12"></div>
                        </div>
                    </li>
                </ul>   
			</div> 
                       
        </div>  

		<div id="result_anagr_serv"></div>
        
	</div>
    
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Salva</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
    </div>
    
    <input type="hidden" name="save" value="1" />
</form>
</div>
<script language="javascript">
<?php
if($s['delegati'])
	foreach($s['delegati'] as $k=>$v)
		echo 'addDelegato('.$k.',"'.entities($v).'","pps",'.$s['anagr_id'].');';
?>
$(function(){
	btnDelegati();
	servsosp($('[name="sospeso"]'));	
});
</script>
