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

dbConnect();

checkLogin('adm','usr');
checkEnteService($_REQUEST['serv']);
check_my_anagr($_GET['anagr_id']);

//edit
if(intval($_GET['id']) > 0){
	$deleg_id = intval($_GET['id']);
	$deleg = get_delegato($deleg_id,$_GET['anagr_id']);
	$edit = true;
}


//salvo impostazioni
if(isset($_POST['nome'])){
	if(is_empty($_POST['nome']))
		printMsg("Inserire il Nome",'danger');
	if(is_empty($_POST['cognome']))
		printMsg("Inserire il Cognome",'danger');
	if(is_empty($_POST['ci']))
		printMsg("Inserire il numero della Carta d'identit&agrave;",'danger');
	if(!$ci_data = is_date($_POST['ci_data']))
		printMsg("Data rilascio errata o non valida. Formato corretto \"GG/MM/AAAA\". Es: 02/11/".date("Y"),'danger');
	if(intval($_POST['provincia']) == 0)
		printMsg("Selezionare la Provincia di rilascio",'danger');
	if(intval($_POST['comune']) == 0)
		printMsg("Selezionare il Comune di rilascio",'danger');
		
	//nuovo inserimento
	if(!$edit){
		$result = $mysqli->query("INSERT INTO servizi_delegati VALUES (
		NULL,
		".intval($_GET['anagr_id']).",
		".dbClean($_GET['serv']).",
		".dbClean(welltxt($_POST['nome'])).",
		".dbClean(welltxt($_POST['cognome'])).",
		".dbClean(strtoupper($_POST['ci'])).",
		".dbClean($ci_data).",
		".intval($_POST['provincia']).",
		".intval($_POST['comune'])."
		)");
		if(!$result)
			printMsg("Errore durante l'inserimento",'danger');
		
		Js('addDelegato('.$mysqli->insert_id.',"'.entities($_POST['cognome'].' '.$_POST['nome']).'","'.$_GET['serv'].'",'.intval($_GET['anagr_id']).');');
		
		//log
		usrlog("Servizio ".get_serv_name($_GET['serv'])." - Alunno: %s. Aggiunto delegato",intval($_GET['anagr_id']),true); //log 
			
	}else{
		$result = $mysqli->query("UPDATE servizi_delegati SET 
		deleg_nome = ".dbClean($_POST['nome']).",
		deleg_cognome = ".dbClean($_POST['cognome']).",
		deleg_ci = ".dbClean($_POST['ci']).",
		deleg_ci_data = ".dbClean($ci_data).",
		provincia_id = ".intval($_POST['provincia']).",
		comune_id = ".intval($_POST['comune'])."
		WHERE anagr_id = ".intval($_GET['anagr_id'])." AND deleg_id = ".$deleg_id);

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

		Js('$("#delegato-'.$deleg_id.'").val("'.entities($_POST['nome'].' '.$_POST['cognome']).'")');
		
		//log
		$deleg_anagr = $_POST['cognome']." ".$_POST['nome'];
		if($deleg['deleg_cognome']." ".$deleg['deleg_nome']  <> $deleg_anagr)
			usrlog("Servizio ".get_serv_name($_GET['serv'])." - Alunno: %s. Modificato delegato",intval($_GET['anagr_id']),true); //log 


	}

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

}

//estraggo impostazioni
?>
<div>
    <div class="modal-d-title">Delegato al ritiro</div>
	<form role="form" class="form-horizontal" action="<?php echo $_SERVER['REQUEST_URI'];?>" onSubmit="return post(this,'result_delegato')">
    <div class="modal-body modal-body-diag">    

        <div class="form-group-inline">
            
            <div class="form-group">
                <div class="col-sm-6">
                    <label class="control-label">Nome</label>
                    <input class="form-control" type="text" maxlength="40" name="nome" value="<?php echo entities($deleg['deleg_nome']);?>" />
                </div>
                <div class="col-sm-6">
                    <label class="control-label">Cognome</label>
                    <input class="form-control" type="text" maxlength="40" name="cognome" value="<?php echo entities($deleg['deleg_cognome']);?>" />
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-6">
                    <label class="control-label">N. Carta d'identit&agrave;</label>
                    <input class="form-control" type="text" maxlength="15" name="ci" value="<?php echo entities($deleg['deleg_ci']);?>" />
                </div>
                <div class="col-sm-6">
                    <label class="control-label">Rilasciata in data</label>
                    <input class="form-control" type="text" maxlength="10" name="ci_data" placeholder="gg/mm/aaaa" value="<?php echo date_ita($deleg['deleg_ci_data']);?>" />
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-6">
                    <label class="control-label">Provincia rilascio</label>
                    <select class="form-control" name="provincia" onChange="selXmlPop('#sel_comune_deleg','comuni',this.value)">
                    	<option value=""></option>
                    	<?php print_province_opts($deleg['provincia_id']) ;?>
                    </select>
                </div>
                <div class="col-sm-6">
                    <label class="control-label">Comune rilascio</label>
                    <select class="form-control" name="comune" id="sel_comune_deleg">
                    	<option value=""></option>
	                    <?php print_comuni_opts($deleg['provincia_id'],$deleg['comune_id']) ;?>
                    </select>
                </div>
            </div>

		
        </div>
                
		<div id="result_delegato"></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>
    </form>	
</div>