<?php
include("../includes/session.php");
include("../includes/functions.php");
include("../includes/db.php");
session(1);

if(!empty($_POST['imp_cod'])){
	
	$imp_cod = intval($_POST['imp_cod']);
	$result = mysql_query("SELECT * FROM imprese WHERE imprese.imp_cod = ".$imp_cod);
	$imp_indirizzo 	= mysql_result($result,0,'imp_indirizzo');
	$imp_citta 		= mysql_result($result,0,'imp_citta');
	$imp_cap		  = mysql_result($result,0,'imp_cap');
	$imp_prov 		 = mysql_result($result,0,'imp_prov');

	if(!empty($_POST['password']) || !empty($_POST['email'])){
		if(!is_clean($_POST['password']) || strlen($_POST['password']) < 6)
			die("Password non valida:\ndeve essere fra i 6 e i 20 caratteri e contenere solo lettere e numeri");
		
		if(empty($_POST['email']) || strlen($_POST['email']) < 6)
			die("Nome utente non valido:\ndeve essere fra i 6 e i 20 caratteri");
	}
	mysql_query("UPDATE imprese SET email='".$_POST['email']."',password='".$_POST['password']."',active=". ($_POST['active'] == 1 ? "1" : "NULL" ) ." WHERE imp_cod = ".$imp_cod);
	if(mysql_error() <> '')
		echo "Errore durante il salvataggio";

	if(
	$imp_indirizzo	<> strtoupper($_POST['indirizzo']) ||
	$imp_citta   		<> strtoupper($_POST['citta']) ||	
	$imp_cap   		  <> strtoupper($_POST['cap']) ||	
	$imp_prov   		 <> strtoupper($_POST['prov']))
	{
		mysql_query("UPDATE imprese SET 
		imp_indirizzo='".addslashes(strtoupper($_POST['indirizzo']))."',
		imp_citta='".addslashes(strtoupper($_POST['citta']))."',
		imp_cap='".intval($_POST['cap'])."',
		imp_prov='".addslashes(strtoupper($_POST['prov']))."',
		blocked=1
		WHERE 
		imp_cod = ".$imp_cod);

		if(mysql_error() <> '')
			echo "Errore durante il salvataggio";
	}
	
	die();
}

if(!empty($_GET['cod'])){
	$imp_cod = intval($_GET['cod']);
	$result = mysql_query("SELECT * FROM imprese 
	LEFT JOIN lavoratori ON imprese.imp_cod = lavoratori.imp_cod 
	LEFT JOIN lavoratori_tgl ON lavoratori.lav_cod = lavoratori_tgl.lav_cod 
	LEFT JOIN lavoratori_lav ON lavoratori_tgl.lavoro_id = lavoratori_lav.lavoro_id 
	LEFT JOIN imprese_tipo ON imprese.tipoimp_id = imprese_tipo.tipoimp_id
	WHERE imprese.imp_cod = ".$imp_cod." ORDER BY lav_anagrafica");
	$imp_indirizzo 	= mysql_result($result,0,'imp_indirizzo');
	$imp_citta 		= mysql_result($result,0,'imp_citta');
	$imp_cap		  = mysql_result($result,0,'imp_cap');
	$imp_prov 		 = mysql_result($result,0,'imp_prov');
	$imp_codfisc	  = mysql_result($result,0,'imp_codfisc');
	$imp_piva 		 = mysql_result($result,0,'imp_piva');
	$tipoimp_descr 	= mysql_result($result,0,'tipoimp_descr');
	$email		    = mysql_result($result,0,'email');
	$password		 = mysql_result($result,0,'password');
	$active		   = mysql_result($result,0,'active');

	mysql_data_seek($result,0);
	while($row = mysql_fetch_array($result)){
		if($row['lav_anagrafica'] <> NULL)
			$lavoratori .= '
			<div class="tbl_row" onclick="diag(event,700,\'lavoratore_show.php?cod='.$row['lav_cod'].'\',\''.addslashes($row['lav_anagrafica']).'\')">
				<div class="tbl_col" style="width:300px">'.$row['lav_anagrafica'].'</div>
				<div class="tbl_col" style="width:200px">'.$row['lav_codfisc'].'</div>
				<div class="tbl_col" style="width:200px">'.neat_trim($row['lavoro_descr'],40).'</div>
				<div class="clear"><!-- --></div>
			</div>
			';
	}
	if($lavoratori){
		$lavoratori = '
		<div class="diag_list_outer">
			<span class="title">Lavoratori</span>
			<div class="diag_list_cont">'.$lavoratori.'</div>
		</div>
		';
	}
}
?>
<div class="imp_dett">
    <form id="formImp" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="return false">
    <div class="fLeft">
        <div class="f_row">
            <label style="">Indirizzo</label><input type="text" name="indirizzo" maxlength="50" style="width:180px" value="<?php echo $imp_indirizzo;?>" />
        </div>
        <div class="f_row">
            <label>Cap</label><input type="text" name="cap" maxlength="5" style="width:50px" value="<?php echo $imp_cap;?>" />
        </div>
        <div class="f_row">
            <label>Codice fiscale</label><?php echo $imp_codfisc;?>
        </div>
        <div class="f_row">
            <label>Tipo impresa</label><?php echo $tipoimp_descr;?>
        </div>
	</div>
        
    <div class="fRight">
        <div class="f_row">
            <label>Citt&agrave;</label><input type="text" name="citta" maxlength="50" style="width:180px" value="<?php echo $imp_citta;?>" />
        </div>
        <div class="f_row">
            <label>Prov</label><input type="text" name="prov" maxlength="2" style="width:25px" value="<?php echo $imp_prov;?>" />
        </div>
        <div class="f_row">
            <label>Partita iva</label><?php echo $imp_piva;?>
        </div>
        <div class="f_row">
            <label>Codice CE</label><?php echo $imp_cod;?>
        </div>
    </div>
    
    <div class="clear"><!-- --></div>
    <hr />
        <div class="fLeft">
            <div class="f_row">
                <label>Nome utente</label><input type="text" name="email" maxlength="150" style="width:180px" value="<?php echo $email;?>" />
            </div>
            <div class="f_row">
                <label>Password</label><input type="text" name="password" maxlength="20" style="width:180px" value="<?php echo $password;?>" />
            </div>
    
        </div>
        <div class="fRight">
            <div class="f_row">
                <label>Attivo</label><input type="checkbox" name="active" value="1"<?php echo $active <> NULL ? ' checked="checked"' : '';?> />
            </div>
            <div class="f_row" style="text-align:right">
                <button type="button" class="btn"  onclick="post('formImp',false,function(data){if(data == '') closeDiag(); else alert(data);})">SALVA DATI ACCESSO</button>
            </div>
        </div>
        <input type="hidden" name="imp_cod" value="<?php echo $imp_cod;?>" />
    </form>
    <div class="clear"><!-- --></div>

    
	<?php echo $lavoratori;  ?>
</div>