<?php
	/*
		(C) DairyWindow 2017
		admin@dairywindow.nz
	*/
	
	require "inc/connect.php";
	$title = "Purchase orders";
	require "inc/head.php";
	$menu = "po";
	require "inc/menu.php";
	if($debug) showdebug();

	if(isset($_POST['edit_id']) && $_POST['edit_id']!='' && $_POST['edit_id']!=null){
		$edit_id     = clean($_POST['edit_id']);
		$poShortDesc = clean($_POST['short_desc']);
		$poLongDesc  = clean($_POST['long_desc']);
		
		$sql="
			# update this po
			UPDATE dw_po SET
				poShortDesc  = ?,
				poLongDesc   = ?,
				poUserDate   = now(),
				poUserMod    = ?
			WHERE poID     = ?
		";
		if(!savesql($link, $sql, "ssii", array($poShortDesc, $poLongDesc, $_SESSION['userID'], $edit_id), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
	}else{
		if(isset($_POST['short_desc']) && $_POST['short_desc']!=""){
			//create new po
			
			$poShortDesc = clean($_POST['short_desc']);
			if($_POST['long_desc']!="") $poLongDesc = clean($_POST['long_desc']);
			else $poLongDesc = "";
			
			$sql="
				# check if adding a duplicate
				Select
					dw_po.poID
				From
					dw_po
				Where
					dw_po.poShortDesc = ? And
					dw_po.poLongDesc = ? And
					dw_po.poSite = ?
			";
			if(!$select_query = mysqli_prepared_query($link, $sql, "ssi", array($poShortDesc, $poLongDesc, $_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			if(mysqli_prepared_num_rows($select_query)>0){
				//already exists
				$message['type'] = "error";
				$message['text'] = "<span class='glyphicon glyphicon-warning-sign' aria-hidden='true'></span> PO ".$poShortDesc." (".$poLongDesc.") already exists";
			}else{
				$sql="
					INSERT INTO dw_po            ( poShortDesc,  poLongDesc,       poUserMod    ,       poSite      , poUserDate)
					VALUES                       (      ?     ,       ?    ,           ?        ,         ?         ,    now() )
				";
				if(!savesql($link, $sql, "ssii", array($poShortDesc, $poLongDesc, $_SESSION['userID'], $_SESSION['userSite']        ), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			}
		}
	}
	
	
?>

<div id="topDIV" >
	<h1 class="text-center">
		<?php echo $title; ?>&nbsp;<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#new_po" ><span class='glyphicon glyphicon-plus' aria-hidden='true'></span>&nbsp;Create new</button>
	</h1>
</div>

<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>

<div id="main" class="container-fluid" style="display:none;" role="main">
	<?php
		$sql="
			# sql for main table
			Select
				dw_po.poID,
				dw_po.poShortDesc,
				dw_po.poLongDesc
			From
				dw_po
			Where
				dw_po.poSite = ?
			Order By
				dw_po.poShortDesc
		";
		if(!$po_query = mysqli_prepared_query($link, $sql, "i", array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
	?>
	<table class="table table-hover" id="table">
		<thead>
			<tr id="table-head" style="background-color:white; color:inherit; white-space:nowrap;">
				<th class="text-center" id="th_short" >Short desc</th>
				<th class="text-center" id="th_long"  >Long description</th>
			</tr>
		</thead>
		<tbody>
			<?php
				foreach($po_query as $po_row){
			?>
					<tr class="text-nowrap" id="po_<?php echo $po_row['poID']; ?>" data-toggle="modal" data-target="#edit_po_form" onclick="update_po_edit(<?php echo $po_row['poID']; ?>, 0);">
						<td class="text-center" id="td_short_<?php echo $po_row['poID']; ?>" ><?php echo $po_row['poShortDesc']; if($debug) echo " [".$po_row['poID']."]"; ?></td>
						<td class="text-center" id="td_long_<?php  echo $po_row['poID']; ?>" ><?php echo $po_row['poLongDesc' ]; ?></td>
					</tr>
			<?php
				}
			?>
		</tbody>
	</table>
</div>

<script src="js/po.js?v=2.1"><!-- Include JS specific to po.php (this file) --></script>

<div id="new_po" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="new_poLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<form class="container-fluid panel-info form-horizontal" 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_poLabel">New PO details</h2>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="short_desc" class="col-sm-2 control-label">Short desc</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="short_desc" name="short_desc" placeholder="Text" required >
						</div>
					</div>
					<div class="form-group">
						<label for="long_desc" class="col-sm-2 control-label">Long desc</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="long_desc" name="long_desc" placeholder="Text" >
						</div>
					</div>
				</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="submit" class="btn btn-primary" id="new_po_button" onclick="please_wait('new_po_button');"><span class='glyphicon glyphicon-ok' aria-hidden='true'></span>&nbsp;Create</button>
				</div>
			</form>
		</div>
	</div>
</div>

<div id="edit_po_form" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="edit_poLabel" 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_poLabel">Edit PO details</h2>
			</div>
			<form class="container-fluid panel-info" method="post">
				<div class="modal-body">
					<div class="form-group">
						<label for="short_desc" >Short desc</label>
						<input type="text" class="form-control" id="edit_short" name="short_desc" placeholder="Short desc" required >
					</div>
					<div class="form-group">
						<label for="long_desc" >Long desc</label>
						<input type="text" class="form-control" id="edit_long"  name="long_desc"  placeholder="Long desc" >
					</div>
					
					<input type="<?php echo $debug ? "text" : "hidden"; ?>" id="edit_id" name="edit_id">
				</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="submit" class="btn btn-primary" id="edit_po_button" onclick="please_wait('edit_po_button');"><span class='glyphicon glyphicon-ok' aria-hidden='true'></span>&nbsp;Update</button>
				</div>
			</form>
		</div>
	</div>
</div>

<?php require "inc/footer.php"; ?>