<?php

$a = isset($_REQUEST['a'])?$_REQUEST['a']:"";
$pl = isset($_REQUEST['pl'])?$_REQUEST['pl']:"";

if ($a == "") {
	header("Location: index.php");
}

 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <script src="../js/functions.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
       <script src="../js/functions.js"></script>
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="../js/bootstrap.min.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
       	 <script src="../js/excellentexport.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
             
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>
<?php 
	include("navbar.php");
?>
        
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
     <div class="container " style="padding-top: 10px;">
		<div class="panel panel-default">
			<div class="panel-heading form-inline">
			  <a class="btn btn-success" role="button" style="float: right;" href="#" download="PricePlanListItems<?= "(" . date("m.d.Y") . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'dataTable', 'PricePlanListItems');" data-toggle="tooltip" data-placement="left" title="Does not work with Internet Explorer">Export To Excel</a>
			  <div style="margin-right: 10px; float: right;">
				  <div class="input-group">
					  <div id="clearButton" style="display: none;" class="input-group-addon" onclick="clearFilter()">
						  <span class="glyphicon glyphicon-remove" aria-hidden="true"></span></div>
					  <input type="text"   class="form-control" name="filter" placeholder="Filter" value="" />
					  <div class="input-group-addon" onclick="filterReport()">
						  <span class="glyphicon glyphicon-search" aria-hidden="true"></span></div>
				  </div>
					  <button class="btn btn-primary" onclick="addPricePlanListItem()" >Add New Price Plan Item</button>
			  </div>
			  <?php
				$query = "SELECT * FROM PRICE_PLAN_LIST_DEF WHERE id = $pl";
				$result = mysqli_query($con, $query);				
				$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
				

			  ?>
			  <h3 class="panel-title">Price Plans - Manage Price Lists - <?= $row['Name'] ?></h3>
			  <br>
			</div>
			<div class="panel-body">
			<table class="table table-hover table-condensed" id="dataTable">
			  <thead>
				  <tr>
					  <th>Line</th>
					  <th>Group</th>
					  <th>SKU</th>
					  <th>Description</th>
					  <th>Qty Available</th>
					  <th>Avg Cost</th>
					  <th>PriceA</th>
					  <th>PP Price</th>
					  <th>Margin</th>
					  <th>Edit</th>
					  <th>Delete</th>
				  </tr>
			  </thead>
			 <?php 
			  $query = "SELECT `PRICE_PLAN_LIST_DETAIL`.*, `INVENTORY`.Qty, `INVENTORY`.PriceA,`INVENTORY`.AvgCost, `INVENTORY`.Group, `INVENTORY`.Description ,`INVENTORY`.Notes FROM `PRICE_PLAN_LIST_DETAIL` LEFT JOIN `INVENTORY` ON (INVENTORY.Line = PRICE_PLAN_LIST_DETAIL.Line AND INVENTORY.SKU = PRICE_PLAN_LIST_DETAIL.SKU) WHERE PriceListID = $pl ORDER BY PRICE_PLAN_LIST_DETAIL.`Line`, PRICE_PLAN_LIST_DETAIL.SKU ASC";
			  $result = mysqli_query($con, $query);
			  while ($row = mysqli_fetch_array($result)) {
					  
			 ?>
				  <tr onclick="">
					  <td><?= $row['Line'] ?></td>
					  <td><?= $row['Group'] ?></td>
					  <td><?= $row['SKU'] ?></td>
					  <td><?= $row['Description'] . " " . $row['Notes'] ?></td>
					  
					  <td><?= $row['Qty'] ?></td>
					  <td><?= $row['AvgCost'] ?></td>
					  <td><?= $row['PriceA'] ?></td>
					  <td><?= $row['Price'] ?></td>
					  <td><?= round(($row['Price']-$row['AvgCost']/.85)/$row['Price']*100,2) ?>%</td>
					  <td><a onclick="editPricePlanListItem('<?= $row['Line'] ?>', '<?= $row['SKU'] ?>', <?= $pl ?>, <?= $row['Price'] ?>)" class="btn btn-warning">Edit</a></td>
					  <td><a onclick="deletePricePlanItem('<?= $row['Line'] ?>', '<?= $row['SKU'] ?>', <?= $pl ?>)" class="btn btn-danger">Delete</a></td>
					  
				  </tr>
			 <?php		
			  }
			  ?>
			  </table>
			</div>
		  </div>
		  
		  
		  <div class="modal fade" id="AddEditPricePlanItem">
			<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>
				  <h4 class="modal-title" id="AddEditPricePlanItemTitle">Edit Price Plan Item</h4>
				</div>
				<div class="modal-body">
				  <form>
					  <input type="hidden" id="isNew" name="isNew" value="" />
						<div class="form-group">
							<label for="AddEditPricePlanItemLine">Line</label>
							<select class="form-control combobox" id="AddEditPricePlanItemLine" name="AddEditPricePlanItemLine">
							<option></option>
							<?php
								$query = "SELECT DISTINCT(Line) FROM INVENTORY ORDER BY Line";
								$result = mysqli_query($con, $query);
								while ($row = mysqli_fetch_array($result)) {
							?>
								<option value="<?= $row['Line'] ?>"><?= $row['Line'] ?></option>
							<?php
								}
							?>
							</select>
						</div>
						<div class="form-group">
							<label for="AddEditPricePlanItemSKU">SKU</label>
							
							<input class="form-control" id="AddEditPricePlanItemSKU" name="AddEditPricePlanItemSKU" type="text" val=""/>
						</div>
					  <div class="form-group">
						  <label for="AddEditPricePlanItemPrice">Price</label>
						  <input type="text" class="form-control small" name="AddEditPricePlanItemPrice" autocomplete="off" id="AddEditPricePlanItemPrice" placeholder="Price">
						</div>
				  </form>
				</div>
				<div class="modal-footer">
				  <button type="button" class="btn btn-danger" id="deletePricePlanItemButton" onclick="deletePricePlanItem($('#AddEditPricePlanItemLine').val(), $('#AddEditPricePlanItemSKU').val(), <?= $pl ?>);" data-dismiss="modal">Delete</button>
				  <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
				  <button type="button" class="btn btn-primary" id="savePricePlanItemButton" onclick="savePricePlanItemInfo($('#AddEditPricePlanItemLine').val(), $('#AddEditPricePlanItemSKU').val(), <?= $pl ?>, $('#AddEditPricePlanItemPrice').val());">Save</button>
				</div>
			  </div><!-- /.modal-content -->
			</div><!-- /.modal-dialog -->
		  </div><!-- /.modal -->
		  <script type="text/javascript">
				function deletePricePlanItem(Line, SKU, PricePlanID){
					if(confirm("Are you sure you want to delete this item?")){
						if(PricePlanID != ""){
							$.get("mod.php?p=pricingplanlist&a=functions&f=deletePricePlanListItem&ppline=" + Line + "&ppsku=" + SKU + "&ppid=" + PricePlanID , function(data){
									if(data == "true"){
										$('#AddEditPricePlanItem').modal('hide');
										location.reload();
									}
							});
						}	
					}
					
				}
				function savePricePlanItemInfo(Line, SKU, PricePlanID, Price){
					if(PricePlanID != ""){
						$.get("mod.php?p=pricingplanlist&a=functions&f=savePricePlanListItem&ppline=" + Line + "&ppsku=" + SKU + "&ppPrice=" + Price + "&ppid=" + PricePlanID , function(data){
								if(data == "true"){
									$('#AddEditPricePlanItem').modal('hide');
									location.reload();
								}
						});
					}
				}
		  </script>
	

     </div>    
	<script>
		function clearAddEditPricePlanItem(){
			$('#AddEditPricePlanItemSKU').val('');
			$('#AddEditPricePlanItemLine').val('');
		}
		function addPricePlanListItem(){
			$('#AddEditPricePlanItem').modal('show');
		}
		function editPricePlanListItem(Line, SKU, PricePlanID, Price){
			clearAddEditPricePlanItem();
			$('#AddEditPricePlanItemSKU').val(SKU);
			$('#AddEditPricePlanItemLine').val(Line);
			$('#AddEditPricePlanItemPrice').val(Price);
			$('#AddEditPricePlanItem').modal('show');
		}
		function filterReport(){
			var query = $('[name=filter]').val();
			$('#dataTable tbody tr').show();
			$('#dataTable tbody tr').not(':contains("' + query.toUpperCase() + '")').hide();
		}
		$(document).ready(function() {
				$('[name=filter]').on('keypress', function(e){
					if(e.keyCode == 13){
						filterReport();
					}
				});
		});
	</script>
   </body>
 </html>