<?php 
$a = isset($_REQUEST['a'])?$_REQUEST['a']:"";
$pl = isset($_REQUEST['pl'])?$_REQUEST['pl']:"";
if ($a == "") {
	header("Location: index.php");
}
//var_dump($_SESSION);
 ?>
 
 <!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="PricePlanListCustomers<?= "(" . date("m.d.Y") . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'priceplanlistcustomer', 'PricePlanListCustomers');" 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="addPricePlanListCustomer();" >Add New Customer</button>
			  </div>
			  
			  <h3 class="panel-title">Price Plans - List</h3>
			  <small>Click on a row to edit the Price Plan definition</small>
			</div>
			<div class="panel-body">
			<table class="table table-hover table-condensed" id="dataTable">
			  <thead>
				  <tr>
					  <th>Account Number</th>
					  <th>Name</th>
					  <th>City</th>
					  <th>Delete</th>
				  </tr>
			  </thead>
			 <?php 
			  $query = "SELECT * FROM `PRICE_PLAN_LIST_CUSTOMER` WHERE PriceListID = $pl ORDER BY `AccountNumber` ASC";
			  $result = mysqli_query($con, $query);
			  while ($row = mysqli_fetch_array($result)) {
				  $customer = new Customer($row['AccountNumber']);
			 ?>
				  <tr>
					  <td><?= $customer->AccountNumber ?></td>
					  <td><?= $customer->Name ?></td>
					  <td><?= $customer->City ?></td>
					  <td><a href="#" class="btn btn-danger" onclick="deletePricePlanListCustomer('<?= $customer->AccountNumber ?>', <?= $pl ?>);">Delete</a></td>
				  </tr>
			 <?php		
			  }
			  ?>
			  </table>
			</div>
		  </div>
		  <div class="modal fade" id="AddEditPricePlanCustomer">
			<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="AddEditPricePlanCustomerTitle">Add Price Plan Customer</h4>
				</div>
				<div class="modal-body">
				  <form>
					  <input type="hidden" id="isNew" name="isNew" value="" />
						<div class="form-group">
							<label for="AddEditPricePlanCustomerAccountNumber">Account Number</label>
							<select class="form-control combobox" id="AddEditPricePlanCustomerAccountNumber" name="AddEditPricePlanCustomerAccountNumber">
							<option></option>
							<?php
								$query = "SELECT AccountNumber, Name, City FROM CUSTOMER ORDER BY AccountNumber";
								$result = mysqli_query($con, $query);
								while ($row = mysqli_fetch_array($result)) {
							?>
								<option value="<?= $row['AccountNumber'] ?>"><?= $row['AccountNumber'] . ' - ' . $row['Name'] . ' - ' . $row['City'] ?></option>
							<?php
								}
							?>
							</select>
						</div>
				  </form>
				</div>
				<div class="modal-footer">
				  <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
				  <button type="button" class="btn btn-primary" id="savePricePlanCustomerButton" onclick="savePricePlanCustomerInfo($('#AddEditPricePlanCustomerAccountNumber').val(), <?= $pl ?>, $('#AddEditPricePlanCustomerPrice').val());">Save</button>
				</div>
			  </div><!-- /.modal-content -->
			</div><!-- /.modal-dialog -->
		  </div><!-- /.modal -->
			<script type="text/javascript">
				function clearAddEditPricePlanListCustomerModal(){
					$('#AddEditPricePlanCustomerAccountNumber').val('');
					
					//$('#AddEditPricePlanCustomerAccountNumber').data('combobox').refresh();
					//$('#AddEditPricePlanCustomerAccountNumber').data('combobox').$button.click();
				}
				function deletePricePlanListCustomer(accountNumber, PricePlanListID){
					if(confirm("Are you sure you want to delete this customer from this list?")){
						if(PricePlanListID != ""){
							$.get("mod.php?p=pricingplanlist&a=functions&f=deletePricePlanListCustomer&ppan=" + accountNumber + "&ppid=" + PricePlanListID , function(data){
									if(data == "true"){
										$('#AddEditPricePlanCustomer').modal('hide');
										location.reload();
									}
							});
						}	
					}
				}
				function savePricePlanCustomerInfo(AccountNumber, PricePlanListID){
					if(PricePlanListID != ""){
						$.get("mod.php?p=pricingplanlist&a=functions&f=savePricePlanListCustomer&ppan=" + AccountNumber + "&ppid=" + PricePlanListID , function(data){
								if(data == "true"){
									$('#AddEditPricePlanCustomer').modal('hide');
									location.reload();
								}
						});
					}
				}
			  function addPricePlanListCustomer(){
				clearAddEditPricePlanListCustomerModal();
				$('#AddEditPricePlanCustomer').modal();
			  }
			$(document).ready(function(){
				$('#AddEditPricePlanCustomerAccountNumber').combobox();
					
			});
			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>
	

     </div>    
     
   </body>
 </html>