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

//salvo modifiche
if(!empty($_POST['lav_cod'])){
	$lav_cod = intval($_POST['lav_cod']);
	
	//applico modifiche e lo segno come bloccato, solo se ha effettuato veramente qualche modifica
	$result = mysql_query("SELECT * FROM lavoratori LEFT JOIN lavoratori_tgl ON lavoratori.lav_cod = lavoratori_tgl.lav_cod WHERE lavoratori.lav_cod = ".$lav_cod);

	$imp_cod	  	  	 = mysql_result($result,0,'imp_cod');
	$lavoro_id	  	   = mysql_result($result,0,'lavoro_id');
	$pos_id	  	   	  = mysql_result($result,0,'pos_id');
	$lav_abb	  	   	 = mysql_result($result,0,'lav_abb');
	$lavtgl_giacca_est   = mysql_result($result,0,'lavtgl_giacca_est');
	$lavtgl_giacca_inv   = mysql_result($result,0,'lavtgl_giacca_inv');
	$lavtgl_pant_est	 = mysql_result($result,0,'lavtgl_pant_est');
	$lavtgl_pant_inv	 = mysql_result($result,0,'lavtgl_pant_inv');
	$lavtgl_giub_est	 = mysql_result($result,0,'lavtgl_giub_est');
	$lavtgl_giub_inv	 = mysql_result($result,0,'lavtgl_giub_inv');
	$lavtgl_scarp_est	= mysql_result($result,0,'lavtgl_scarp_est');
	$lavtgl_scarp_inv	= mysql_result($result,0,'lavtgl_scarp_inv');
	
	if(
		$lavoro_id 		   <> $_POST['lavoro_id'] ||
		$lavtgl_giacca_est   <> $_POST['lavtgl_giacca_est'] ||	
		$lavtgl_giacca_inv   <> $_POST['lavtgl_giacca_inv'] ||	
		$lavtgl_pant_est	 <> $_POST['lavtgl_pant_est'] ||	
		$lavtgl_pant_inv	 <> $_POST['lavtgl_pant_inv'] ||	
		$lavtgl_giub_est	 <> $_POST['lavtgl_giub_est'] ||	
		$lavtgl_giub_inv	 <> $_POST['lavtgl_giub_inv'] ||	
		$lavtgl_scarp_est	<> $_POST['lavtgl_scarp_est'] ||	
		$lavtgl_scarp_inv	<> $_POST['lavtgl_scarp_inv']
	){
		mysql_query("UPDATE lavoratori_tgl SET
		lavtgl_giacca_est = ".intval($_POST['lavtgl_giacca_est']).",
		lavtgl_giacca_inv = ".intval($_POST['lavtgl_giacca_inv']).",
		lavtgl_pant_est = ".intval($_POST['lavtgl_pant_est']).",
		lavtgl_pant_inv = ".intval($_POST['lavtgl_pant_inv']).",
		lavtgl_giub_est = ".intval($_POST['lavtgl_giub_est']).",
		lavtgl_giub_inv = ".intval($_POST['lavtgl_giub_inv']).",
		lavtgl_scarp_est = ".intval($_POST['lavtgl_scarp_est']).",
		lavtgl_scarp_inv = ".intval($_POST['lavtgl_scarp_inv']).",
		lavoro_id = ".intval($_POST['lavoro_id']).",
		blocked=1
		WHERE lav_cod = ".$lav_cod);
		
	}

	if(
		$lav_abb <> $_POST['lav_abb'] || 
		$imp_cod <> $_POST['imp_cod'] ||
		$pos_id <> $_POST['pos_id']
	)
		mysql_query("UPDATE lavoratori SET lav_abb = ".$_POST['lav_abb'].",imp_cod = ".$_POST['imp_cod'].", pos_id = ".$_POST['pos_id'].", blocked = 1 WHERE lav_cod = ".$lav_cod);

	die();	
}


if(!empty($_GET['cod'])){
	$lav_cod = intval($_GET['cod']);
	$result = mysql_query("SELECT * FROM lavoratori
	LEFT JOIN imprese ON lavoratori.imp_cod = imprese.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 
	WHERE lavoratori.lav_cod = ".$lav_cod);
	
	$imp_cod		   = mysql_result($result,0,'imp_cod');
	$imp_anagrafica 	= mysql_result($result,0,'imp_anagrafica');
	$lav_anagrafica 	= mysql_result($result,0,'lav_anagrafica');
	$lav_codfisc	   = mysql_result($result,0,'lav_codfisc');
	$lavoro_descr	  = neat_trim(mysql_result($result,0,'lavoro_descr'),40);
	$lavoro_id	  	 = mysql_result($result,0,'lavoro_id');
	$lav_abb	  	   = mysql_result($result,0,'lav_abb');
	$pos_id	  	   	= mysql_result($result,0,'pos_id');

	$lavtgl_giacca_est   = mysql_result($result,0,'lavtgl_giacca_est');
	$lavtgl_giacca_inv   = mysql_result($result,0,'lavtgl_giacca_inv');
	$lavtgl_pant_est	 = mysql_result($result,0,'lavtgl_pant_est');
	$lavtgl_pant_inv	 = mysql_result($result,0,'lavtgl_pant_inv');
	$lavtgl_giub_est	 = mysql_result($result,0,'lavtgl_giub_est');
	$lavtgl_giub_inv	 = mysql_result($result,0,'lavtgl_giub_inv');
	$lavtgl_scarp_est	= mysql_result($result,0,'lavtgl_scarp_est');
	$lavtgl_scarp_inv	= mysql_result($result,0,'lavtgl_scarp_inv');
	
	$taglie = getTaglie();
}
?>
<form id="formLavShow" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="return false">
<div class="imp_dett">
    
    <div class="fLeft" style="width:320px">
        <div class="f_row">
            <label>Codice fiscale</label><?php echo $lav_codfisc;?>
        </div>
        
        <div class="f_row">
            <label>Lavoro</label>
            <select name="lavoro_id">
	            <?php
				$lavori = getLavori();
				foreach($lavori as $lavoro)
	            	echo '<option'. ($lavoro_id == $lavoro->lavoro_id ? ' selected="selected"' : '') .' value="'.$lavoro->lavoro_id.'">'.$lavoro->lavoro_descr.'</option>';
                ?>
            </select>
        </div>

        <div class="f_row">
            <label>Posizione</label>
            <select name="pos_id">
	            <?php
				$result = mysql_query("SELECT * FROM lavoratori_pos ORDER BY pos_descr");
				while($row = mysql_fetch_array($result))
	            	echo '<option'. ($pos_id == $row['pos_id'] ? ' selected="selected"' : '') .' value="'.$row['pos_id'].'">'.utf8_encode($row['pos_descr']).'</option>';
                ?>
            </select>
        </div>
        
    </div>
    
    <div class="fRight" style="width:320px">
        <div class="f_row">
            <label>Codice CE</label><?php echo $lav_cod;?>
        </div>
        <div class="f_row">
            <label>Abbigliamento</label>
            <input name="lav_abb" type="radio" value="1"<?php if($lav_abb == 1) echo ' checked="checked"';?>  />Si 
            <input name="lav_abb" type="radio" value="0"<?php if($lav_abb == 0) echo ' checked="checked"';?> />No
        </div>
    </div>

    <div class="clear"><!-- --></div>
       
       <div class="f_row">
            <label>Impresa</label>
            <select name="imp_cod">
            	<option value="0"></option>
	            <?php
				foreach(getImprese() as $impresa)
	            	echo '<option'. ($imp_cod == $impresa->imp_cod ? ' selected="selected"' : '') .' value="'.$impresa->imp_cod.'">'.$impresa->imp_anagrafica.'</option>';
                ?>
            </select>
        </div>
    
    
    <div class="diag_list_outer">
		<span class="title">Taglie</span>
    	<div class="diag_list_cont">
            
            <div class="tbl_row nopoint" style="font-size:12pt">
                <div class="tbl_col" style="width:150px"><img class="tt" title="Giacca" src="../../../images/icons/giacca-on.png" /></div>
                <div class="tbl_col" style="width:90px">estivo</div>
                <div class="tbl_col" style="width:90px">
                	<select name="lavtgl_giacca_est">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_giacca_est ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                 </div>
                <div class="tbl_col" style="width:90px">invernale</div>
                <div class="tbl_col" style="width:50px">
                	<select name="lavtgl_giacca_inv">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_giacca_inv ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="clear"><!-- --></div>
            </div>
            <div class="tbl_row nopoint" style="font-size:12pt">
                <div class="tbl_col" style="width:150px"><img class="tt" title="Giubbotto" src="../../../images/icons/giubbotto-on.png" /></div>
                <div class="tbl_col" style="width:90px">estivo</div>
                <div class="tbl_col" style="width:90px">
                	<select name="lavtgl_giub_est">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_giub_est ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="tbl_col" style="width:90px">invernale</div>
                <div class="tbl_col" style="width:50px">
                	<select name="lavtgl_giub_inv">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_giub_inv ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="clear"><!-- --></div>
            </div>
            <div class="tbl_row nopoint" style="font-size:12pt">
                <div class="tbl_col" style="width:150px"><img class="tt" title="Pantaloni" src="../../../images/icons/pantaloni-on.png" /></div>
                <div class="tbl_col" style="width:90px">estivo</div>
                <div class="tbl_col" style="width:90px">
                	<select name="lavtgl_pant_est">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_pant_est ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="tbl_col" style="width:90px">invernale</div>
                <div class="tbl_col" style="width:50px">
                	<select name="lavtgl_pant_inv">
                    <?php
					foreach($taglie["VES"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_pant_inv ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="clear"><!-- --></div>
            </div>
            <div class="tbl_row nopoint" style="font-size:12pt">
                <div class="tbl_col" style="width:150px"><img class="tt" title="Scarpe" src="../../../images/icons/scarpe-on.png" /></div>
                <div class="tbl_col" style="width:90px">estivo</div>
                <div class="tbl_col" style="width:90px">
                	<select name="lavtgl_scarp_est">
                    <?php
					foreach($taglie["CAL"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_scarp_est ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="tbl_col" style="width:90px">invernale</div>
                <div class="tbl_col" style="width:50px">
                	<select name="lavtgl_scarp_inv">
                    <?php
					foreach($taglie["CAL"] as $tgl)
						echo '<option value="'.$tgl.'"'. ($tgl == $lavtgl_scarp_inv ? ' selected="selected"' : '') .'>'.$tgl.'</option>';

					?>
                    </select>
                </div>
                <div class="clear"><!-- --></div>
            </div>
        </div>
    </div>
    
        
    
</div>

<div class="dialog_btns" style="border:0">
    <center>
        <button class="btn" type="button" onclick="post('formLavShow',false,function(){$('#btnLavBack').trigger('click')})">SALVA</button>
        <?php
		if(!isset($_GET['noback'])){
			$onclick = "diag(event,800,'impresa_show.php?cod=".$imp_cod."','".addslashes($imp_anagrafica)."',function(){recenterDiag()})";
			$label = "INDIETRO";
		}else{
			$onclick = "closeDiag()";
			$label = "CHIUDI";
		}
		echo '<button class="btn" id="btnLavBack" type="button" onclick="'.$onclick.'">'.$label.'</button>';
	?>
    </center>
</div>
<input type="hidden" name="lav_cod" value="<?php echo $lav_cod;?>" />
</form>