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

//cambio stato ordine
if(intval($_GET['status']) > 0){
	
	//0=da completare 1=attivo 2=chiuso
	
	$id = intval($_GET['status']);
	$result = mysql_query("SELECT ord_status FROM ordini WHERE ord_id = ".$id);
	$ord_status = mysql_result($result,0,'ord_status');
	
	$new_status = $ord_status < 2 ? $ord_status+1 : 0;
	
	//se il nuovo status è "attivo", controllo non ci siano altri ordini attivi.
	//Posso avere 1 solo ordine attivo per volta, quindi skippo lo status e passa da completare a chiuso
	if($new_status == 1){
		$result = mysql_query("SELECT ord_status FROM ordini WHERE ord_id <> ".$id." AND ord_status = 1");
		if(mysql_num_rows($result) > 0){
			$new_status = 2;
		}
	}

	mysql_query("UPDATE ordini SET ord_status = ".$new_status." WHERE ord_id = ".$id);
	$status = getOrderStatus($new_status);
	
	die($status->icon);
}

if(isset($_GET['getIcons'])){
	$result = mysql_query("
	SELECT * FROM ordini_abb_lav 
	LEFT JOIN ordini_abb ON ordini_abb_lav.ordabb_id = ordini_abb.ordabb_id
	");
	while($row = mysql_fetch_array($result)){
		echo $row['ord_id'].'-'.$row['abbcat_id'].'-'.$row['lavoro_id'].';';
	}
	die();
}

if(intval($_GET['del']) > 0){
	$id = intval($_GET['del']);
	mysql_query("DELETE FROM ordini WHERE ord_id=".$id);
	mysql_query("DELETE FROM ordini_generated WHERE ord_id=".$id);
	mysql_query("DELETE FROM ordini_abb_lav WHERE ordabb_id IN (SELECT ordabb_id FROM ordini_abb WHERE ord_id=".$id.")");
	$result = mysql_query("SELECT ordabb_id FROM ordini_abb WHERE ord_id=".$id);
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_array($result)){
			@unlink('../../artimages/'.$row['ordabb_id'].'.png');
			@unlink('../../artimages/thumbs/'.$row['ordabb_id'].'.png');
		}
	}
	mysql_query("DELETE FROM ordini_abb WHERE ord_id=".$id);
	mysql_query("DELETE FROM ordini_customchoice WHERE ord_id=".$id);
	
	@unlink('../../orders/ord'.str_pad($id,6,'0',STR_PAD_LEFT).'_sch.pdf');
	@unlink('../../orders/ord'.str_pad($id,6,'0',STR_PAD_LEFT).'_gen.pdf');
	@unlink('../../orders/ord'.str_pad($id,6,'0',STR_PAD_LEFT).'_trac.txt');
	
}
?>
<h2>Ordini</h2>
<div class="sub_menu">
	<a href="javascript:void(0)" onclick="diag(event,400,'order_new.php','Nuovo ordine')" class="btn">NUOVO</a>
</div>
<?php
//elenco ordini
$result = mysql_query("SELECT * FROM ordini ORDER BY ord_datafine DESC");
if(mysql_num_rows($result) > 0){
	
	//elenco dei lavori
	$lavori = getLavori();
	$abbigliamento = getOrdineAbb();
	$abbigliamentoCat = getAbbigliamentoCat();
	
	while($row = mysql_fetch_array($result)){
		
		$status = getOrderStatus($row['ord_status']);
		
		//testa dell'ordine
		echo '
		<div class="order">
			<div class="ord_header" onclick="$(this).next(\'.ord_dett\').slideToggle()">
				<div class="ord_stato"><img class="tt" title="Cambia stato ordine" src="../../../images/'.$status->icon.'" onclick="orderStatus(this, '.$row['ord_id'].');  p(event);" />'.$row['ord_anno'].'</div>
				<div class="ord_stagione"><img src="../../../images/icons/'. ($row['ord_stagione'] == 0 ? 'winter' : 'summer') .'.png" />'. getStagione($row['ord_stagione']) .'</div>
				<div class="ord_datafine"><img src="../../../images/icons/date.png" />'. getDateIta($row['ord_datafine']) .'</div>
				<div class="ord_actions">
					<img class="tt" title="Modifica" onclick="diag(event,400,\'order_new.php?id='.$row['ord_id'].'\',\'Modifica ordine\')" src="../../../images/icons/edit.png" />
					<img class="tt" title="Genera" onclick="diag(event,400,\'order_generate.php?id='.$row['ord_id'].'\',\'Generazione ordine\')" src="../../../images/icons/generate.png" />
					<img class="tt" title="Elimina" onclick="if(confirm(\'Sei sicuro?\')) get(\'home.php?del='.$row['ord_id'].'\',\'maincontent\'); p(event)" src="../../../images/icons/del.png" />
				</div>
				<div class="clear"><!-- --></div>
			</div>
			<div class="ord_dett">';
			
			//griglia lavori -> abbigliamento
			foreach($lavori as $lav){
				echo '<div class="ord_lavdescr"><label>'.$lav->lavoro_descr.'</label>';

				foreach($abbigliamentoCat as $cat){
		
					$icoId = 'icoabb'.$row['ord_id'].'-'.$cat->abbcat_id.'-'.$lav->lavoro_id;					
					$on_off = !$abbigliamento[$row['ord_id']][$lav->lavoro_id][$cat->abbcat_id] ? '-off' : '-on';
					$img = '../../../images/icons/'.strtolower($cat->abbcat_descr).$on_off.'.png';
					$qs = 'ord_id='.$row['ord_id'].'&lav_id='.$lav->lavoro_id.'&cat_id='.$cat->abbcat_id.'&ico='.$icoId;
					echo '<img class="lavIcon tt" id="'.$icoId.'" onclick="diag(event,700,\'abbigliamento_add.php?'.$qs.'\',\''.$cat->abbcat_descr.' '.$lav->lavoro_descr.'\')" title="'.$cat->abbcat_descr.'" src="'.$img.'" />';
					
				}
				echo '</div>';
			}
		
		echo '
				
			</div>
		</div>
		';
	}
	
	echo '</div>';
}else{
	echo 'Nessun ordine inserito';	
}
?>