<?php
/**
 * Copyright (C) DairyWindow 2019
 * admin@dairywindow.nz
 */

	/* (C) DairyWindow 2012-2017 */
	
	require "inc/connect.php";
	
	$title = "Sample status";
	require "inc/head.php";
	$menu = "barcode";
	require "inc/menu.php";
	
	if($debug) showdebug();
	
	if(isset($_POST['sample'])){
		if(strtoupper(substr($_POST['sample'],0,2))=="DW") $sample = substr($_POST['sample'], 3); //get rid of DWU
		else $sample = substr($_POST['sample'], 1); //get rid of U
	}else $sample = 0;
	
	//update retention
	if(isset($_POST['stage'])){
		
		if($_POST['stage']!=0){
			
			$sql="
				Update dw_unit set
					unitStage = ?
				where unitID = ?
			";
			if(!savesql($link, $sql, "ii", array($_POST['stage'], $sample), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			
			$sql="
				Insert into dw_barcode      (barcodeUnit, barcodeDate, barcodeUser,  barcodeStage )
				Values                      (     ?     ,    now()   ,     ?      ,        ?      )
			";
			if(!savesql($link, $sql, "iii", array(  $sample  ,      $_SESSION['userID'], $_POST['stage']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			
			if($_POST['stage']==6){ //retention
				$sql="
					Update dw_unit set
						unitBags = unitBags + 1
					where unitID = ?
				";
				if(!savesql($link, $sql, "i", array($sample), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			}
			
		}
	}
	
?>

<div id='loading' ><img src='images/loading-small.gif' alt='Please wait. Loading...' width='111' height='156' style='margin-top:150px;' class='center-block'><br><div class='text-center'><img src='images/carregando.gif' width='16' height='16'>&nbsp;Please wait while loading...</div></div>

<h1 class='text-center hidden-print'><?php echo $title; ?></h1>

<div class='col-md-3 hidden-print' role='complementary' >

	<form class="form-horizontal" method="post" >
		<div class="form-group">
			<label for="sample" class="col-sm-2 control-label">Barcode</label>
			<div class="col-sm-10 has-feedback">
				<input type="text" name="sample" id="sample" class="form-control" onkeyup="add_u('sample');" placeholder="Scan or type" >
				<span class="glyphicon glyphicon-barcode form-control-feedback" aria-hidden="true"></span>
			</div>
		</div>
		
		<div class="form-group">
			<label for="stage" class="col-sm-2 control-label">Stage</label>
			<div class="col-sm-10">
				<?php
					$sql="
						Select stageID, stageText, stageOrder
						From dw_stage
						Where stageID > ?
						Order by stageOrder, stageText
					";
					if(!$qry = mysqli_prepared_query($link, $sql, "i", array(0), __FILE__, __LINE__)) reporterror(get_error(), $sql,__FILE__,__LINE__);
					echo "<select name='stage' id='stage' class='form-control' onchange=\"document.getElementById('sample').focus();\" >
					<option value='0'>View only - don't change</option>";
						foreach($qry as $array){
							echo "<option value='".$array['stageID']."' ";
							if(isset($_POST['stage']) && $_POST['stage']==$array['stageID']) echo "selected ";
							echo ">".$array['stageText']."</option>";
						}
					?>
				</select>
			</div>
		</div>
		
		<div class="form-group">
			<label for="button" class="col-sm-2 control-label">&nbsp;</label>
			<div class="col-sm-10">
				<input type="submit" class="btn btn-primary" value='Update'>
				<a href="print_labels.php" class="btn btn-default">Print barcodes</a>
			</div>
		</div>
		
	</form>
</div>

<div id='main' class='col-md-9' >
	<?php
		if(isset($_POST['stage'])){
			
			$sql="
				Select
					dw_unit.unitBags,
					dw_cypher.cypherID,
					dw_cypher.cypherText,
					dw_cypher.cypherManuDate,
					dw_cypher.cypherOrderID,
					dw_spec.specText,
					dw_spec.specStyle,
					dw_location.locationText,
					dw_stage.stageText As currentStage,
					dw_unit.unitText,
					dw_unit.unitCompText,
					dw_unit.unitModDate,
					dw_user.userFirst,
					dw_user.userLast,
					dw_barcode.barcodeDate,
					dw_stage1.stageText,
					dw_user1.userFirst As userFirst1,
					dw_user1.userLast As userLast1
				From
					dw_unit Left Join
					dw_cypher
						On dw_unit.unitCypher = dw_cypher.cypherID Left Join
					dw_spec
						On dw_cypher.cypherSpec = dw_spec.specID Left Join
					dw_location
						On dw_unit.unitLocation = dw_location.locationID Left Join
					dw_stage
						On dw_unit.unitStage = dw_stage.stageID Left Join
					dw_barcode
						On dw_barcode.barcodeUnit = dw_unit.unitID Left Join
					dw_user
						On dw_barcode.barcodeUser = dw_user.userID Left Join
					dw_stage dw_stage1
						On dw_barcode.barcodeStage = dw_stage1.stageID Left Join
					dw_user dw_user1
						On dw_user1.userID = dw_unit.unitModUser
				Where
					dw_cypher.cypherSite = ? And
					dw_cypher.cypherActive = 1 And
					dw_spec.specSite = ? And
					dw_spec.specActive = 1 And
					dw_location.locationActive = 1 And
					dw_location.locationSite = ? And
					dw_unit.unitActive = 1 And
					dw_unit.unitID = ?
				Order By
					dw_barcode.barcodeDate
			";
			if(!$qry = mysqli_prepared_query($link, $sql, "iiii", array($_SESSION['userSite'], $_SESSION['userSite'], $_SESSION['userSite'], $sample), __FILE__, __LINE__)) reporterror(get_error(), $sql,__FILE__,__LINE__);
			if(mysqli_prepared_num_rows($qry)>0){
				$array = $qry[0]; //the first row
			?>
			<div class='row'>
				<div class='col-md-2'><strong>Barcode</strong></div>
				<div class='col-md-10'><?php echo $_POST['sample']; ?></div>
			</div>
			<div class='row'>
				<div class='col-md-2'><strong>Date</strong></div>
				<div class='col-md-10'><?php echo date('d/m/y',strtotime($array['cypherManuDate'])); ?></div>
			</div>
			<?php if($array['specStyle']==1){ ?>
			<div class='row'>
				<div class='col-md-2'><strong>Cypher/Batch</strong></div>
				<div class='col-md-10'><?php echo $array['cypherText']." ".$array['cypherOrderID']; ?></div>
			</div>
			<?php } ?>
			<div class='row'>
				<div class='col-md-2'><strong>Product</strong></div>
				<div class='col-md-10'><?php echo $array['specText']; ?></div>
			</div>
			<div class='row'>
				<div class='col-md-2'><strong>Location</strong></div>
				<div class='col-md-10'><?php echo $array['locationText']; ?></div>
			</div>
			<div class='row'>
				<div class='col-md-2'><strong>Sample</strong></div>
				<div class='col-md-10'><?php echo $array['unitText']; ?></div>
			</div>
			<?php
				if($array['unitCompText']!="" && $array['unitCompText']!=null){
			?>
			<div class='row'>
				<div class='col-md-2'><strong>Part of Comp</strong></div>
				<div class='col-md-10'><?php echo substr($array['unitCompText'],strpos($array['unitCompText'],"_")+1); ?></div>
			</div>
				<?php } ?>
			<div class='row'>
				<div class='col-md-2'><strong>Last result entered</strong></div>
				<?php
					$sql="
						Select
							dw_result.resultModDate
						From
							dw_result
						Where
							dw_result.resultUnit = ?
						Order By
							dw_result.resultModDate Desc
						Limit 1
					";
					if(!$qry2 = mysqli_prepared_query($link, $sql, "i", array($sample), __FILE__, __LINE__)) reporterror(get_error(), $sql,__FILE__,__LINE__);
					if(mysqli_prepared_num_rows($qry2)>0) $lastdate = date('d/m/y H:i:s',strtotime($qry2[0]['resultModDate']));
					else $lastdate = "Never";
				?>
				<div class='col-md-10'><a href='results.php?r=<?php echo $array['cypherID']; ?>'><?php echo $lastdate; ?></a></div>
			</div>
			<div class='row'>
				<div class='col-md-2'><strong>Current stage</strong></div>
				<div class='col-md-10'><?php echo $array['currentStage']; ?></div>
			</div>
			
			<?php if($debug) echo "rows for table:".mysqli_prepared_num_rows($qry)."<br>"; ?>
			
			<table class='table table-striped'>
				<thead>
					<tr>
						<th>Date</th>
						<th>Time</th>
						<th>User</th>
						<th>Stage</th>
					</tr>
				</thead>
				<tbody>
					<?php
						if(mysqli_prepared_num_rows($qry)>0 && isset($qry[0]['barcodeDate']) && $qry[0]['barcodeDate']!="" && $qry[0]['barcodeDate']!=null){
							echo "
								<tr>
									<td>".date('d/m/y',strtotime($array['unitModDate']))."</td>
									<td>".date('H:i:s a',strtotime($array['unitModDate']))."</td>
									<td>".$array['userFirst1']." ".$array['userLast1']."</td>
									<td>Produced</td>
								</tr>
							";
							foreach($qry as $row){
								echo "
									<tr>
										<td>".date('d/m/y',strtotime($row['barcodeDate']))."</td>
										<td>".date('H:i:s a',strtotime($row['barcodeDate']))."</td>
										<td>".$row['userFirst']." ".$row['userLast']."</td>
										<td>".$row['stageText']."</td>
									</tr>
								";
							}
						}else{
							echo "
								<tr>
									<td>".date('d/m/y',strtotime($array['unitModDate']))."</td>
									<td>".date('H:i:s a',strtotime($array['unitModDate']))."</td>
									<td>".$array['userFirst1']." ".$array['userLast1']."</td>
									<td>Produced</td>
								</tr>
							";
						}
					?>
				</tbody>
			</table>

		<?php
			}else{
				if(isset($_POST['sample']) && $_POST['sample']!="") echo "<p class='bg-danger'>Sample not found! <a href='print_labels.php'>Print labels</a></p>";
			}
		}
		?>
</div>

<script src="js/barcode.js?v=2.2"></script>

<?php require "inc/footer.php"; ?>