<?php

	//Appears only in admin section
  if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
		
?>
	
<?php

	//appears only in customer section both salesman and customer
  }else{
		
		if(mysqli_num_rows($result) === 0){
		
		
?>
			<div class="row" id="productNotFoundDiv">
				<div class="col-md-6 col-md-offset-3">
						<h4>Can't find what you are looking for? Let us know!</h4>
			<table class="table">
				<thead>
					<tr>
						<th>Number</th>
						<th>Description</th>
						<th>Qty</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>
							<input type="text" class="form-control" id="lostSaleSKU" placeholder="SKU" value="<?= isset($_REQUEST['sku'])?$_REQUEST['sku']:'' ?>">		
						</td>
						<td>
							<input type="text" class="form-control" style="width: 300px;" id="lostSaleDescription" placeholder="Description">
						</td>
						<td>
							<input type="text" class="form-control" style="width: 50px;" id="lostSaleQty" placeholder="Qty">
						</td>
						
						<td>
							<button class="btn btn-success" id="lostSaleSubmit" onclick="submitLostSale()">Enviar</button>
						</td>
					</tr>
				</tbody>
			</table>
				</div>
			</div>
			<script>
				$('#lostSaleSKU').on('keyup', function(e){
					if(e.keyCode == 13){
						e.preventDefault();
						$('#lostSaleDescription').focus();
					}
				});
				$('#lostSaleDescription').on('keyup', function(e){
					if(e.keyCode == 13){
						e.preventDefault();
						$('#lostSaleQty').focus();
					}
				});
				$('#lostSaleQty').on('keyup', function(e){
					if(e.keyCode == 13){
						if($('#lostSaleQty').val() == ""){
							$('#lostSaleQty').val("1");
							//$('#lostSaleSubmit').click();
						}
						e.preventDefault();
						$('#lostSaleSubmit').focus();
					}
				});
				$(document).ready(function(){
					setTimeout(function(){
							if($('#lostSaleDescription').length > 0){
								$('#lostSaleDescription').focus();	
							}
							
						}, 100);
						
					
					});
				function submitLostSale(){
					var sku = $('#lostSaleSKU').val();
					var description = $('#lostSaleDescription').val();
					var qty = $('#lostSaleQty').val();
					$.post('mod.php?p=lostsale&a=functions&f=saveLostSale',
								 { 	"sku" : sku ,
										"description" : description,
										"qty": qty,
										"reason": "Product Not Found"
									})
					.done(function(data){
							$('#productNotFoundDiv').html('<center><h3>Thanks!</h3></center>');	
						});
					
				}
			</script>
<?php
		}else{
?>			
			
			<div class="modal fade" tabindex="-1" role="dialog" id="lostSaleModal">
				<div class="modal-dialog modal-lg" role="document">
					<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>
							<h4 class="modal-title">Lost Sale</h4>
						</div>
						<div class="modal-body" id="lostSaleResultModalBody">
							<table class="table">
								<thead>
									<tr>
										<th>Number</th>
										<th>Description</th>
										<th>Quantity</th>
										<th>Reason</th>
										
									</tr>
								</thead>
								<tbody>
									<tr>
										<td>
											<input type="text" class="form-control" id="lostSaleSKU" placeholder="SKU" value="">		
										</td>
										<td>
											<input type="text" class="form-control" style="width: 300px;" id="lostSaleDescription" placeholder="Description">
										</td>
										<td>
											<input type="text" class="form-control" style="width: 50px;" id="lostSaleQty" placeholder="Qty">
										</td>
										<td>
											<select class="form-control" style="width: 150px;" id="lostSaleReason">
												<option>Price</option>
												<option>Insufficient Qty</option>
											</select>
											
										</td>
										
									</tr>
								</tbody>
							</table>
							<input type="hidden" class="form-control" id="lostSalePrice">
							<br>
							<div class="form-inline" id="lostSaleCompetitorPriceDiv" style="display: none;">
								<div class="form-group">
									<label for="lostSaleCompetitors">Competitor</label>								
									<select class="form-control" id="lostSaleCompetitors">
												<option>Kupey Auto Distributors</option>
												<option>Ricardo Cruz Distributors</option>
												<option></option>
												<option></option>
												<option></option>
												
												<option></option>
									</select>
								</div>
								<div class="form-group">
									<label for="lostSaleCompetitorPrice">Competitor Price</label>
									<input style="width: 100px;" type="text" id="lostSaleCompetitorPrice" class="form-control"/>
								</div>
								
							</div>
							
						</div>
						<div class="modal-footer">
							<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
							<button type="button" onclick="submitLostSaleFromResult();$(this).hide();" class="btn btn-primary">Save</button>
						</div>
					</div><!-- /.modal-content -->
				</div><!-- /.modal-dialog -->
			</div><!-- /.modal -->
			
			
			<script>
				$(document).ready(function(){
					$('#searchResultTable tbody tr')
						.append(
							$('<td/>').append(
								$('<button/>').addClass('btn btn-warning').text('Lost Sale').attr('onclick', 'lostSaleFromResult(this)')
							)																			
																										
						);	
					$('#lostSaleModal').on('shown.bs.modal', function(){
							$('#lostSaleQty').focus();
						});
					$('#lostSaleReason').on('keyup', function(e){
							if(e.keyCode == 13){
								$('#lostSaleCompetitors').focus();
							}
						});
					$('#lostSaleQty').on('keyup', function(e){
							if(e.keyCode == 13){
								$('#lostSaleReason').focus();
							}
						});
					$('#lostSaleCompetitorPrice').on('keyup', function(e){
							if(e.keyCode == 13){
								submitLostSaleFromResult();
							}
						});
				});
			
				function clearLostSaleModal(){
					$('#lostSaleSKU').val('');
					$('#lostSaleDescription').val('');
					$('#lostSaleReason').val("Precio");
					$('#lostSaleReason').trigger('change');
					$('#lostSaleQty').val('');
					$('lostSalePrice').val('');
					$('#lostSaleCompetitorPrice').val('');
				}
				function lostSaleFromResult(button){
					clearLostSaleModal();
					var lostSaleSKU = $(button).parent().parent().find('.SKU').text();
					var lostSaleLine = $(button).parent().parent().find('.line').text();
					var lostSaleDescription = $(button).parent().parent().find('.description').text();
					var lostSalePrice = $(button).parent().parent().find('.price').text();
					var lostSaleQty = $(button).parent().parent().find('.qty').text();
					$('#lostSaleSKU').val(lostSaleSKU.trim() + " (" + lostSaleLine + ")");
					$('#lostSaleDescription').val(lostSaleDescription.trim());
					$('#lostSalePrice').val(lostSalePrice.replace('$',''));
					if(lostSaleQty=="0"){
						$('#lostSaleQty').val(1);
						$('#lostSaleReason').val("No hay cantidad");
						submitLostSaleFromResult();
					}
					$('#lostSaleModal').modal('show');
				}
				$('#lostSaleReason').on('change', function(){
					if($('#lostSaleReason').val() == "Precio"){
						$('#lostSaleCompetitorPriceDiv').show();
						$('#lostSaleCompetitors').focus();
					}else{
						$('#lostSaleCompetitorPriceDiv').hide();
					}
				});
				$('#lostSaleCompetitors').on('change', function(){
					$('#lostSaleCompetitorPrice').focus();
				});
				
				function lostSaleAppCatalog(button){
					clearLostSaleModal();
					var row = $(button).parent().parent();
					var lostSaleSKU = $(row).find('.SKU').text();
					var lostSaleLine = $(row).find('.Line').text();
					var lostSaleDescription = $(row).find('.description').text();
					var lostSalePrice = $(row).find('.price').text();
					var lostSaleQty = $(row).find('.qty').text();
					$('#lostSaleSKU').val(lostSaleSKU.trim() + " (" + lostSaleLine + ")");
					$('#lostSaleDescription').val(lostSaleDescription.trim());
					$('#lostSalePrice').val(lostSalePrice.replace('$',''));
					if(lostSaleQty=="0"){
						$('#lostSaleQty').val(1);
						$('#lostSaleReason').val("No hay cantidad");
						submitLostSaleFromResult();
					}
					$('#lostSaleModal').modal('show');
				}
				$('#lostSaleReason').on('change', function(){
					if($('#lostSaleReason').val() == "Precio"){
						$('#lostSaleCompetitorPriceDiv').show();
						$('#lostSaleCompetitors').focus();
					}else{
						$('#lostSaleCompetitorPriceDiv').hide();
					}
				});
				$('#lostSaleCompetitors').on('change', function(){
					$('#lostSaleCompetitorPrice').focus();
				});
				
				
				function submitLostSaleFromResult(){
					var compSource = $('#lostSaleCompetitors').val();
					var compPrice = $('#lostSaleCompetitorPrice').val().replace('$', '');
					if($('#lostSaleReason').val() != "Precio"){
						
					}
					$.post('mod.php?p=lostsale&a=functions&f=saveLostSale',
								 { 	"sku" : $('#lostSaleSKU').val(),
										"description" : $('#lostSaleDescription').val(),
										"qty": $('#lostSaleQty').val(),
										"reason": $('#lostSaleReason').val(),
										"price": $('#lostSalePrice').val(),
										"competitionPrice": compPrice,
										"competitionSource": compSource,
										"comments": window.location.href
									}
									)
					.done(function(data){
							$('#lostSaleResultModalBody').html('<center><h3>¡Gracias por su cooperacion!</h3>Reloading page now...</center>');
							setTimeout(function(){
									location.reload();
								}, 1000);
						});
					
				
				}
				
			</script>
			
			
<?php			
		}
  }

	
?>