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

/**
	 * Created by PhpStorm.
	 * User: kurdtpage
	 * Date: 6/6/18
	 * Time: 12:28 PM
	 */

require "inc/connect.php";
$title = "Specification";
require "inc/head.php";
$menu = "spec";
require "inc/menu.php";

?>
<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"><?php echo $title; ?></h1>

<div class="col-md-3" role="complementary">
	<form class="form-horizontal" style="position:fixed;" method="get" >
		<div class="form-group"><!-- Product -->
			<label for="form" class="col-sm-2 control-label">Product</label>
			<div class="col-sm-10">
				<?php
					$sql="
						Select
							specID,
							specText,
							specShortDesc
						From
							dw_spec
						Where
							specSite = ? and 
							specStyle != 4 and 
							specActive = 1
						Order By
							specStyle,
							specOrder,
							specText
					";
					if(!$query = mysqli_prepared_query($link, $sql, "i", array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

					echo '<select id="spec" style="min-width:200px;" name="spec" class="form-control selectpicker" data-live-search="true" data-width="100%" data-size="20" onchange="submit();">
						<option value="0">&nbsp;</option>';
					foreach($query as $array){
						echo "<option value='".$array['specID']."' ";
						if($array['specText'] != $array['specShortDesc']) echo 'data-content="'.$array['specText'].' <small>('.$array['specShortDesc'].')</small>"';
						if(isset($_GET['spec']) && $_GET['spec']==$array['specID']) echo " selected";
						echo ">".$array['specText'];
						if($array['specText'] != $array['specShortDesc']) echo '&nbsp;<small>('.$array['specShortDesc'].')</small>';
						if($debug) echo " [".$array['specID']."]";
						echo "</option>";
					}
					echo "</select>";
				?>
			</div>
		</div>

		<div class="form-group"><!-- Button -->
			<div class="col-sm-offset-2 col-sm-10">
				<?php
					if($debug){
						echo "<input type='hidden' name='debug' value='true'>";
					}
				?>
				<button type="submit" class="btn btn-primary"><span class='glyphicon glyphicon-search' aria-hidden='true'></span>&nbsp;Refresh</button>
			</div>
		</div>
	</form>
</div>
<div id="main" class="table-responsive col-md-9" style="display:none;" role="main">
	<?php
		if(!empty($_GET['spec'])){
			$sql='# get all data
				SELECT
				  dw_sub.subID,
				  dw_test.testID,
				  dw_test.testText,
				  dw_test.testFormat,
				  dw_test.testRound,
				  dw_spec.specID,
				  dw_spec.specText,
				  dw_group.groupID,
				  dw_group.groupText,
				  dw_frequency.freqID,
				  dw_frequency.freqText,
				  dw_form.formID,
				  dw_form.formText
				FROM
				  dw_spec LEFT JOIN
				  dw_sub ON dw_spec.specID = dw_sub.subSpec RIGHT JOIN
				  dw_test ON dw_test.testID = dw_sub.subTest LEFT JOIN
				  dw_group ON dw_group.groupID = dw_test.testGroup LEFT JOIN
				  dw_frequency ON dw_frequency.freqID = dw_sub.subFreq LEFT JOIN
				  dw_form ON dw_form.formID = dw_sub.subForm
				WHERE
				  dw_group.groupActive = 1 AND
				  dw_test.testActive = 1 AND
				  dw_spec.specID = ? AND
				  dw_group.groupSite = ?
				ORDER BY
				  dw_group.groupOrder,
				  dw_group.groupText,
				  dw_test.testOrder,
				  dw_test.testText,
				  dw_form.formOrder,
				  dw_form.formText
			';
			$types = 'ii';
			$params = array(clean($_GET['spec']), $_SESSION['userSite']);
			if(!$qry = mysqli_prepared_query($link, $sql, $types, $params, __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			$num_rows = mysqli_prepared_num_rows($qry);
			if($num_rows === 1) $qry = $qry[0]; //single row
			?>
			<table class="table table-hover">
				<thead>
					<tr>
						<th>Group</th>
						<th style="display:none;">Test</th>
						<th>Test</th>
						<th style="display:none;">Form</th>
						<th>Form</th>
						<th style="display:none;">Frequency</th>
						<th>Frequency</th>
						<th>Limits</th>
					</tr>
				</thead>
				<tbody id="tbody">
					<?php
						foreach($qry as $row){
							echo '<tr style="cursor:pointer;" id="row'.$row['subID'].'">';
								if($debug){
									echo '
										<td data-toggle="modal" data-target="#edit_sub_form" id="group'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',1);"><small>['.$row['subID'].']</small> '.$row['groupText'].' <small>['.$row['groupID'].']</small></td>
										<td style="display:none;" id="test2'.$row['subID'].'" >'.$row['testID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="test'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',1);">'.$row['testText'].' <small>['.$row['testID'].']</small></td>
										<td style="display:none;" id="form2'.$row['subID'].'" >'.$row['formID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="form'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',1);">'.$row['formText'].' <small>['.$row['formID'].']</small></td>
										<td style="display:none;" id="freq2'.$row['subID'].'" >'.$row['freqID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="freq'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',1);">'.$row['freqText'].' <small>['.$row['freqID'].']</small></td>
										<td class="t'.$row['testID'].'s'.$row['specID'].'" id="t'.$row['testID'].'s'.$row['specID'].'l"></td>
								  ';
								}else{
									echo '
										<td data-toggle="modal" data-target="#edit_sub_form" id="group'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',0);">'.$row['groupText'].'</td>
										<td style="display:none;" id="test2'.$row['subID'].'" >'.$row['testID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="test'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',0);">'.$row['testText'].'</td>
										<td style="display:none;" id="form2'.$row['subID'].'" >'.$row['formID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="form'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',0);">'.$row['formText'].'</td>
										<td style="display:none;" id="freq2'.$row['subID'].'" >'.$row['freqID'].'</td>
										<td data-toggle="modal" data-target="#edit_sub_form" id="freq'.$row['subID'].'" onclick="update_dialog('.$row['subID'].',0);">'.$row['freqText'].'</td>
										<td class="t'.$row['testID'].'s'.$row['specID'].'" id="t'.$row['testID'].'s'.$row['specID'].'l"></td>
								  ';
								}
							echo '</tr>';
							?>
							<script>
								$.get( "ajax/get_limit.php", {
							    test: <?php echo $row['testID']; ?>,
									format: <?php echo $row['testFormat']; ?>,
									round: <?php echo $row['testRound']; ?>,
									spec: <?php echo $row['specID']; ?>,
									debug: <?php echo $debug ? 'true' : 'false'; ?>
								}).done(function(data) {
							    //do something with data
									if(data === '0') $('.t<?php echo $row['testID']; ?>s<?php echo $row['specID']; ?>').html('<a data-toggle="modal" data-target="#new_limit_form" onclick="update_new(<?php echo $row['testID']; ?>,<?php echo $row['specID']; ?>);" title="No limit set"><span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span>&nbsp;None</a>');
									else $('.t<?php echo $row['testID']; ?>s<?php echo $row['specID']; ?>').html(data);
								}).fail(function(){
							    alert('Error getting data (are you logged in?)');
								});
							</script>
							<?php
						}

					?>
				</tbody>
				<?php
					if($_SESSION['userLevel'] <= $_SESSION['production_submod_add']){
				?>
				<tfoot>
					<tr>
						<td colspan="2"><select id="test_new" class="form-control"></select></td>
						<td><select id="form_new" class="form-control"></select></td>
						<td><select id="freq_new" class="form-control"></select></td>
						<td>
							<button class="btn btn-default" type="button" id="insert"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;Insert</button>
						</td>
					</tr>
				</tfoot>
				<?php
					}
				?>
			</table>
		<?php
		}
	?>
</div>
<?php if($_SESSION['userLevel'] <= $_SESSION['production_submod_edit']){ ?>
	<div id="edit_sub_form" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="edit_testLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h2 class="modal-title" id="edit_title">Edit details</h2>
				</div>
				<form class="container-fluid panel-info" method="post">
					<div class="modal-body">

						<div class="form-group"><!-- spec -->
							<label for="edit_product">Product</label>
							<p class="form-control-static" id="edit_product"></p>
						</div>
						<div class="form-group"><!-- group -->
							<label for="edit_group">Group</label>
							<p class="form-control-static" id="edit_group"></p>
						</div>
						<div class="form-group"><!-- test -->
							<label for="edit_test">Test</label>
							<p class="form-control-static" id="edit_test"></p>
						</div>
						<div class="form-group"><!-- form -->
							<label for="edit_form">Form</label>
							<p class="form-control-static" id="edit_form"></p>
						</div>
						<div class="form-group"><!-- freq -->
							<label for="edit_freq">Frequency</label>
							<select id="edit_freq" class="form-control"></select>
						</div>

						<input type="hidden" id="edit_id" name="edit_id">
						<input type="hidden" id="test_id" >
						<input type="hidden" id="form_id" >

					</div>
					<div class="modal-footer">
						<?php if($_SESSION['userLevel'] <= $_SESSION['production_submod_delete']){ ?><button type="button" class="btn btn-danger" id="delete_submod"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>&nbsp;Delete</button><?php } ?>
						<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>&nbsp;Cancel</button>
						<button type="button" class="btn btn-primary" id="edit_test_button" onclick="please_wait('edit_test_button');"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>&nbsp;Update</button>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php }
	if($_SESSION['userLevel'] <= $_SESSION['admin_limit_add']){
?>
	<div id="new_limit_form" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="new_limitLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<form class="container-fluid panel-info" method="post">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<h2 class="modal-title" id="new_limitLabel">New limit details</h2>
					</div>
					<div class="modal-body">

						<div class="form-group">
							<label for="new_spec" >Product</label>
							<?php
								$sql='
									Select
										dw_spec.specID,
										dw_spec.specText,
										dw_spec.specShortDesc
									From
										dw_spec
									Where
										dw_spec.specSite = ? And
										dw_spec.specActive = 1
									Order By
									    dw_spec.specStyle,
										dw_spec.specOrder,
										dw_spec.specText
								';
								if(!$spec_query = mysqli_prepared_query($link, $sql, 'i', array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
								echo '<select name="new_spec" id="new_spec" class="form-control" >';
								foreach($spec_query as $specrow){
									echo '<option value="'.$specrow['specID'].'" ';
									if(isset($_POST['new_spec']) && $_POST['new_spec']==$specrow['specID']) echo 'selected ';
									echo '>'.$specrow['specText'].' ('.$specrow['specShortDesc'].')';
									if($debug) echo ' ['.$specrow['specID'].']';
									echo '</option>';
								}
								echo '</select>';
							?>
						</div><!-- /product -->

						<div class="form-group">
							<label for="new_test" >Test</label>
							<?php
								$test_sql='
									Select
										dw_group.groupText,
										dw_test.testID,
										dw_test.testText,
										dw_test.testUnits
									From
										dw_group Right Join
										dw_test On dw_group.groupID = dw_test.testGroup
									Where
										dw_test.testSite = ? And
										dw_test.testActive = 1
									Order By
										dw_group.groupOrder,
										dw_test.testOrder
								';
								if(!$test_query = mysqli_prepared_query($link, $test_sql, 'i', array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $test_sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
								echo '<select name="new_test" id="new_test" class="form-control" >';
								$group = array();
								foreach($test_query as $row){
									$group[$row['groupText']][] = $row;
								}
								foreach ($group as $key => $values) {
									echo '<optgroup label="'.$key.'">';
									foreach ($values as $value) {
										echo '<option value="'.$value['testID'].'" ';
										if(isset($_POST['new_test']) && $_POST['new_test']==$value['testID']) echo 'selected ';
										echo '>'.$value['testText'];
										if(!empty($value['testUnits'])) echo ' ('.$value['testUnits'].')';
										if($debug) echo ' ['.$value['testID'].']';
										echo '</option>';
									}
									echo '</optgroup>';
								}
								echo '</select>';
							?>
							<input type="hidden" id="limitFormat" value="">
						</div><!-- /test -->

						<div class='form-group'>
							<label for='new_location' >Location</label>
							<?php
								$loc_sql='
									Select
										dw_locgroup.locGroupText,
										dw_location.locationID,
										dw_location.locationText
									From
										dw_locgroup Right Join
										dw_location
											On dw_locgroup.locGroupID = dw_location.locationGroup
									Where
										dw_location.locationSite = ? And
										dw_location.locationActive = 1
									Order By
										dw_locgroup.locGroupOrder,
										dw_location.locationOrder,
										dw_location.locationText
								';
								if(!$loc_query = mysqli_prepared_query($link, $loc_sql, 'i', array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $loc_sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
								echo '<select name="new_location" id="new_location" class="form-control" >';
								$group = array();
								foreach($loc_query as $row){
									$group[$row['locGroupText']][] = $row;
								}
								foreach ($group as $key => $values) {
									echo '<optgroup label="'.$key.'">';
									foreach ($values as $value) {
										echo '<option value="'.$value['locationID'].'"';
										if($_SESSION['default_location']==$value['locationID']) echo ' selected';
										echo '>'.$value['locationText'];
										if($debug) echo ' ['.$value['locationID'].']';
										echo '</option>';
									}
									echo '</optgroup>';
								}
								echo '</select>';
							?>
						</div><!-- /group -->

						<div class="form-group">
							<label for="new_limitDate" >Effective date range</label>
							<div id="limitsdate" class="row">
								<div class="col-md-6">
									<input type="date" class="form-control" id="new_dateFrom" name="new_dateFrom" required value="<?php echo date('Y-m-d'); ?>" >
								</div>
								<div class="col-md-6">
									<input type="date" class="form-control" id="new_dateTo"   name="new_dateTo"   required value="<?php echo date('Y-m-d', strtotime('10 years')); ?>" >
								</div>
							</div>
						</div><!-- /date -->

						<div class="form-group">
							<label for="new_limitLow" ><?php echo $_SESSION['comp_name']; ?> limit</label>
							<div id="limits" class="row">
								<div class="col-md-6">
									<input type="number" class="form-control" id="new_limitLow"  name="new_limitLow"  step="0.00001" value="0" placeholder="0">
								</div>
								<div class="col-md-6">
									<input type="number" class="form-control" id="new_limitHigh" name="new_limitHigh" step="0.00001" required >
								</div>
							</div>
						</div><!-- /limit -->

					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal"><span class='glyphicon glyphicon-remove' aria-hidden='true'></span>&nbsp;Cancel</button>
						<button type="button" class="btn btn-primary" id="new_limit_button" onclick="please_wait('new_limit_button');"><span class='glyphicon glyphicon-ok' aria-hidden='true'></span>&nbsp;Add</button>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php } ?>
<script src="js/spec.js"></script>
<?php require "inc/footer.php"; ?>
