<?php
include("../../../functions.inc.php");
checkLogin('adm');
checkEnteService('bus');
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'])){
	
	
	if(!anagr_has_serv($anagr_id,'bus'))
		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");
	if(!is_empty($_POST['sosp_and_dal']) && !$sosp_and_dal = is_date($_POST['sosp_and_dal']))
		printMsg('Data sospensione andata "Dal" errata. Formato corretto: GG/MM/AAAA',"danger");
	if(!is_empty($_POST['sosp_and_al']) && !$sosp_and_al = is_date($_POST['sosp_and_al']))
		printMsg('Data sospensione andata "Al" errata. Formato corretto: GG/MM/AAAA',"danger");
	if(!is_empty($_POST['sosp_rit_dal']) && !$sosp_rit_dal = is_date($_POST['sosp_rit_dal']))
		printMsg('Data sospensione ritorno "Dal" errata. Formato corretto: GG/MM/AAAA',"danger");
	if(!is_empty($_POST['sosp_rit_al']) && !$sosp_rit_al = is_date($_POST['sosp_rit_al']))
		printMsg('Data sospensione ritorno "Al" errata. Formato corretto: GG/MM/AAAA',"danger");
	
	//indirizzi fermata obbligatori se servizio attivo
	if(intval($_POST['andata']) == 1 && is_empty($_POST['indirizzo_and']))
		printMsg("Inserire l'indirizzo della fermata all'<strong>Andata</strong>","danger");
	if(intval($_POST['ritorno']) == 1 && is_empty($_POST['indirizzo_rit']))
		printMsg("Inserire l'indirizzo della fermata al <strong>Ritorno</strong>","danger");

	//attestazioni pagamento
	if(intval($_POST['esente']) == 0){
		$arr_rata = array_filter($_POST['rata']);
		if(!empty($arr_rata)){
			if(is_empty($_POST['rata']['cro']))
				printMsg('Inserire il numero CRO / Bollettino della 1&ordf; rata',"danger");
			 if(!$rata_data = is_date($_POST['rata']['data']))
				printMsg('Data pagamento <strong>1&ordf; rata</strong> errata. Formato corretto: GG/MM/AAAA',"danger");
			 if(!is_importo($_POST['rata']['imp']))
				printMsg('Importo <strong>1&ordf; rata</strong> non corretto. Formato corretto: 1234,00',"danger");
		}
		
		$arr_saldo = array_filter($_POST['saldo']);
		if(!empty($arr_saldo)){
			if(is_empty($_POST['saldo']['cro']))
				printMsg('Inserire il numero CRO / Bollettino del <strong>saldo</strong>',"danger");
			 if(!$saldo_data = is_date($_POST['saldo']['data']))
				printMsg('Data pagamento <strong>saldo</strong> errata. Formato corretto: GG/MM/AAAA',"danger");
			 if(!is_importo($_POST['saldo']['imp']))
				printMsg('Importo <strong>saldo</strong> non corretto. Formato corretto: 1234,00',"danger");
		}
	}
	
	$result = $mysqli->query("
	UPDATE serv_bus SET
	scuola_id = ".intval($_POST['scuola']).",
	classe_id = ".intval($_POST['classe']).",
	servbus_sospeso = ".intval($_POST['sospeso']).",
	servbus_sosp_and_dal = ".dbClean($sosp_and_dal).",
	servbus_sosp_and_al = ".dbClean($sosp_and_al).",
	servbus_sosp_rit_dal = ".dbClean($sosp_rit_dal).",
	servbus_sosp_rit_al = ".dbClean($sosp_rit_al).",
	servbus_esente = ".intval($_POST['esente']).",
	servbus_andata = ".intval($_POST['andata']).",
	servbus_ritorno = ".intval($_POST['ritorno']).",
	servbus_congedo = ".intval($_POST['congedo']).",
	servbus_ferm_and_ind = ".dbClean(welltxt($_POST['indirizzo_and'])).",
	servbus_ferm_and_n = ".dbClean(welltxt($_POST['civico_and'])).",
	servbus_ferm_rit_ind = ".dbClean(welltxt($_POST['indirizzo_rit'])).",
	servbus_ferm_rit_n = ".dbClean(welltxt($_POST['civico_rit'])).",
	servbus_rata_cro = ".dbClean($_POST['rata']['cro']).",
	servbus_rata_data = ".dbClean($rata_data).",
	servbus_rata_imp = ".dbFloat($_POST['rata']['imp']).",
	servbus_saldo_cro = ".dbClean($_POST['saldo']['cro']).",
	servbus_saldo_data = ".dbClean($saldo_data).",
	servbus_saldo_imp = ".dbFloat($_POST['saldo']['imp']).",
	servbus_dirotta = ".intval($_POST['dirotta'])."
	WHERE serv_bus.anagr_id = ".$anagr_id);

	if(!$result)
		printMsg("Errore durante il salvataggio","danger");
	
	Js('closeDialog();');
	die();
}

//estraggo impostazioni
$s = get_servbus($anagr_id);
?>
<div>
    <div class="modal-d-title">Servizio Scuolabus</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('bus') 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('bus',$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['servbus_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="andata">Andata</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="andata" id="andata" value="1" class="switch-btn"<?php if($s['servbus_andata'] == 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="ritorno">Ritorno</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="ritorno" id="ritorno" value="1" class="switch-btn"<?php if($s['servbus_ritorno'] == 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">Congedo fermata</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['servbus_congedo'] == 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="esente">Esente pagamento</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="esente" id="esente" value="1" class="switch-btn"<?php if($s['servbus_esente'] == 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="esente">Dirotta su altro Bus</label>
                            <div class="col-xs-3 col-sm-5 col-md-4 col-lg-3">
                                <input type="checkbox" name="dirotta" id="dirotta" value="1" class="switch-btn"<?php if($s['servbus_dirotta'] == 1) echo ' checked';?> />
                            </div>
                        </div>
                    </li>
                </ul>
                
            </div>
            
			<div class="col-sm-6">

                <div class="form-group form-subtitle"><h4>Programma sospensione</h4></div>
                <ul class="list-group">
                    <li class="list-group-item">
                        <h5 class="list-group-subtitle p-bottom-0">Sospendi andata</h5>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group m-bottom-10">
                                    <label class="control-label col-xs-2">dal</label>
                                    <div class="col-xs-10">
                                        <input maxlength="10" class="form-control" type="text" name="sosp_and_dal" value="<?php echo date_ita($s['servbus_sosp_and_dal']);?>" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group m-bottom-10">
                                    <label class="control-label col-xs-2">al</label>
                                    <div class="col-xs-10">
                                        <input maxlength="10" class="form-control" type="text" name="sosp_and_al" value="<?php echo date_ita($s['servbus_sosp_and_al']);?>" />
                                    </div>
                                </div>
                            </div>
                        </div>
                	</li>

                    <li class="list-group-item">
                        <h5 class="list-group-subtitle p-bottom-0">Sospendi ritorno</h5>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group m-bottom-10">
                                    <label class="control-label col-xs-2">dal</label>
                                    <div class="col-xs-10">
                                        <input maxlength="10" class="form-control" type="text" name="sosp_rit_dal" value="<?php echo date_ita($s['servbus_sosp_rit_dal']);?>" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group m-bottom-10">
                                    <label class="control-label col-xs-2">al</label>
                                    <div class="col-xs-10">
                                        <input maxlength="10" class="form-control" type="text" name="sosp_rit_al" value="<?php echo date_ita($s['servbus_sosp_rit_al']);?>" />
                                    </div>
                                </div>
                            </div>
                        </div>
                	</li>

                </ul>        

			</div>            
        </div>  
        
        
        <div class="form-group form-subtitle"><h4>Indirizzo fermata</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">Andata</label>
                            <div class="col-sm-9 col-md-8">
                        		<input class="form-control" type="text" name="indirizzo_and" maxlength="40" value="<?php echo entities($s['servbus_ferm_and_ind']);?>" />
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-sm-4">
                        <div class="row">
                        	<label class="control-label col-sm-4">Civico</label>
                            <div class="col-sm-8">
                        		<input class="form-control" type="text" name="civico_and" style="max-width:80px" maxlength="10" value="<?php echo entities($s['servbus_ferm_and_n']);?>" />
                            </div>
                        </div>
                    </div>
                </div>
            </li>
            <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">Ritorno</label>
                            <div class="col-sm-9 col-md-8">
                        		<input class="form-control" type="text" name="indirizzo_rit" maxlength="40" value="<?php echo entities($s['servbus_ferm_rit_ind']);?>" />
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-sm-4">
                        <div class="row">
                        	<label class="control-label col-sm-4">Civico</label>
                            <div class="col-sm-8">
                        		<input class="form-control" type="text" name="civico_rit" style="max-width:80px" maxlength="10" value="<?php echo entities($s['servbus_ferm_rit_n']);?>" />
                            </div>
                        </div>
                    </div>
                </div>
            </li>
        </ul>

        <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('bus','<?php echo $s['anagr_id'];?>')">
                        <span class="glyphicon glyphicon-plus"></span> Aggiungi
                    </button>
                    <input type="hidden" id="n_delegati" value="3" />
                </div>
                <div class="form-group">
                    <div id="delegati" class="col-xs-12 col-sm-8"></div>
                </div>
            </li>
        </ul>   

        
        <div class="form-group form-subtitle"><h4>Attestazione pagamento</h4></div>

        <ul class="list-group">
            <li class="list-group-item">
                <h5 class="list-group-subtitle m-bottom-0">1&ordf; rata</h5>
                <div class="row">
                    <div class="col-sm-4">
                        <label class="control-label">N. CRO / Bollettino</label>
                        <input type="text" name="rata[cro]" class="form-control" value="<?php echo entities($s['servbus_rata_cro']);?>">
                    </div>

                    <div class="col-sm-4 col-md-3 col-md-offset-1">
                        <label class="control-label">Data pagamento</label>
                        <input type="text" name="rata[data]" class="form-control" value="<?php echo date_ita($s['servbus_rata_data']);?>">
                    </div>

                    <div class="col-sm-4 col-md-3 col-md-offset-1">
                        <label class="control-label">Importo</label>
                        <input type="text" name="rata[imp]" class="form-control" placeholder="&euro;" value="<?php echo importo($s['servbus_rata_imp']);?>">
                    </div>

				</div>
            </li>

            <li class="list-group-item">
                <h5 class="list-group-subtitle m-bottom-0">Saldo</h5>
                <div class="form-group">
                    <div class="col-sm-4">
                        <label class="control-label">N. CRO / Bollettino</label>
                        <input type="text" name="saldo[cro]" class="form-control" value="<?php echo entities($s['servbus_saldo_cro']);?>">
                    </div>

                    <div class="col-sm-4 col-md-3 col-md-offset-1">
                        <label class="control-label">Data pagamento</label>
                        <input type="text" name="saldo[data]" class="form-control" value="<?php echo date_ita($s['servbus_saldo_data']);?>">
                    </div>

                    <div class="col-sm-4 col-md-3 col-md-offset-1">
                        <label class="control-label">Importo</label>
                        <input type="text" name="saldo[imp]" class="form-control" value="<?php echo importo($s['servbus_saldo_imp']);?>">
                    </div>

				</div>
            </li>

            
        </ul>
    
		<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>
<?php
if($s['delegati'])
	foreach($s['delegati'] as $k=>$v)
	  echo 'addDelegato('.$k.',"'.entities($v).'","bus",'.$s['anagr_id'].');';
?>
$(function(){
	btnDelegati();
	servsosp($('[name="sospeso"]'));	
});
</script>
