<?php 
$altaID = $_REQUEST['id'];

 ?>
 <link href="../css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
 <script src="../js/fileinput.js" type="text/javascript"></script>
<div class="panel panel-default">
  <div class="panel-heading form-inline">
  	<a class="btn btn-success" role="button" style="float: right;" href="#" download="Altas<?= "(" . date("m.d.Y") . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'altas', 'Altas');" 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="filter()">
  		      	<span class="glyphicon glyphicon-search" aria-hidden="true"></span></div>
  		</div>
  			<button class="btn btn-primary" onclick="addAltasItem()" >Add New Item</button>
  	</div>
  	
    <h3 class="panel-title">Altas</h3>
    <small>Click on a row to edit the Item</small>
  </div>
  <div class="panel-body">
  <table class="table table-hover">
    <thead>
    	<tr>
    		<th>Group</th>
    		<th>Line</th>
    		<th>SKU</th>
    		<th>Description</th>
    		<th>Qty</th>
    		<th>Delete</th>
    	</tr>
    </thead>
   <?php 
   	$query = "SELECT * FROM `ALTAS_DETAIL`, `INVENTORY` WHERE `AltaID` = $altaID AND `ALTAS_DETAIL`.`SKU` = `INVENTORY`.`SKU` AND `ALTAS_DETAIL`.`Line` = `INVENTORY`.`Line` AND `ALTAS_DETAIL`.`Group` = `INVENTORY`.`Group` ORDER BY `INVENTORY`.`Line`, `INVENTORY`.`SKU` ASC";
   	$result = mysqli_query($con, $query);
   	while ($row = mysqli_fetch_array($result)) {
   ?>
   		<tr>
   			<td><?= $row['Group'] ?></td>
   			<td><?= $row['Line'] ?></td>
   			<td><?= $row['SKU'] ?></td>
   			<td><?= $row['Description'] ?></td>
   			<td><?= $row['Qty'] ?></td>
   			<td><a class="btn btn-danger" href="javascript:deleteAltasItem('<?= $row['ID'] ?>')">Delete</a></td>
   			<!--  -->
   			
   		</tr>
   <?php		
   	}
    ?>
    </table>
  </div>
</div>


<div class="modal fade" id="AddEditAltasItem">
  <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="AddEditTitle">Edit Altas Item</h4>
      </div>
      <div class="modal-body">
      	<form class="altasItem">
      		<input type="hidden" id="isNew" name="isNew" value="" />
      		<input type="hidden" id="AltasItemID" name="AltasItemID" value="" />
      		<input type="hidden" id="AltaID" name="AltaID" value="" />
      		<div class="form-group">
      		    <label for="AltasItemLine">Line</label>
      		    <select class="form-control combobox" id="AltasItemLine" name="AltasItemLine">
      		    <option></option>
      		    <?php
      		    	$query = "SELECT * FROM `LINE` ORDER BY Name";
      		    	$result = mysqli_query($con, $query);
      		    	while ($row = mysqli_fetch_array($result)) {
      		    ?>
      		    	<option value="<?= $row['Code'] ?>"><?= $row['Code'] . " - " . $row['Name'] ?></option>
      		    <?php
      		    	}
      		    ?>
      		    </select>
      		</div>
      		<div class="form-group">
      		    <label for="AltasItemGroup">Group</label>
      		    <select class="form-control combobox" id="AltasItemGroup" name="AltasItemGroup">
      		    <option></option>
      		    <?php
      		    	$query = "SELECT * FROM `GROUP` ORDER BY Name";
      		    	$result = mysqli_query($con, $query);
      		    	while ($row = mysqli_fetch_array($result)) {
      		    ?>
      		    	<option value="<?= $row['Code'] ?>"><?= $row['Code'] . " - " . $row['Name'] ?></option>
      		    <?php
      		    	}
      		    ?>
      		    </select>
      		</div>
      		<div class="form-group">
      		    <label for="AltasItemSKU">SKU</label>
      		    <input type="text" class="form-control small" name="AltasItemSKU" autocomplete="off" id="AltasItemSKU" placeholder="SKU">
      		</div>
      		
      		
      	</form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" id="saveButton" onclick="saveAltasItemInfo()">Save</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
	$('[name=filter]').on('keypress', function(e) {
		if (e.which == 13) {
			filter();
		}
	
	});
	$($('input[type=text]')[$('input[type=text]').length-1]).keydown(function(e){
			if(e.which == 13)
				$('#saveButton').click();
		
		});
	function clearFilter() {
		$('[name=filter]').val("");
		$('#clearButton').css('display', 'none');
		$('tr').css('display', '');
		
	}
	function filter() {
		var	name = $('[name=filter]').val();
		filterByName(name);
	}
	function filterByName(name) {
		$('#clearButton').css('display', '');
		for (var i = 0; i < $('tr').length; i++) {
			if (i != 0) {
				var result = $('tr')[i].innerText.toUpperCase().indexOf(name.toUpperCase());
				if (result < 0) {
					$($('tr')[i]).css('display', 'none');
				}else {
					$($('tr')[i]).css('display', '');
				}
			}
		}
	}
	function clearModal() {
		$('#isNew').val("0");
		$('#AltasItemID').val("-1");
		$('#AltaID').val("-1");
		$('#AltasItemGroup').val("");
		$('#AltasItemLine').val("");
		$('#AltasItemSKU').val("");
		$('#AltasItemPrice').val("");
		$('#AltasItemUPC').val("");
		$('#saveButton').css('display', '');
		$('#AltasItemLine').data('combobox').clearTarget()
		$('#AltasItemLine').data('combobox').clearElement()
		$('#AltasItemLine').data('combobox').refresh();
		$("#uploadSuccess").css('display', 'none');
		
	}
	function addAltasItem() {
		clearModal();
		$('#isNew').val("1");
		$('#AddEditTitle').text("Add Altas Item");
		$('#AltaID').val("<?= $altaID ?>");
		$('#deleteButton').css('display', 'none');
		$('#imagePreviewDiv').css('display', 'none');
		$('#AddEditAltasItem').modal('toggle');
	}
	
	function saveAltasItemInfo() {
		var altasItem = '{ "AltaID" : "' + $('#AltaID').val() + '", "Line" : "' + $('#AltasItemLine').val() + '", "Group" : "' + $('#AltasItemGroup').val() + '", "SKU" : "' + $('#AltasItemSKU').val() + '", "ID" : "' + $('#AltasItemID').val() + '", "IsNew" : "' + $('#isNew').val() + '"}';
		altasItem = JSON.parse(altasItem);
		$.post( "functions.php?action=saveAltasItemInfo", altasItem)
		  .done(function( data ) {
		  	data = JSON.parse(data);
		    if (data.result === true) {
		      	clearModal();
		      	$('#AddEditAltasItem').modal('toggle')
		      	location.reload();
		      }else {
		      	alert("There has been an error saving the price list data.\r\n\r\nMySQL Error: " + data.error);
		      }
		    });
	}
	function deleteAltasItem(itemID) {
		if (confirm("Are you sure you want to delete this item from this Alta? This cannot be undone.")) {
			
			$.post( "functions.php?action=deleteAltasItem&ID=" + itemID, null)
			  .done(function( data ) {
			  data = $.parseJSON(data);
			    if (data.result == true) {
			    	clearModal();
			    	//$('#AddEditAltasItem').modal('toggle')
			    	location.reload(true);
			    }else {
			    	alert("There has been an error deleting the item.");
			    }
			  });
		  }
		
	}
	$('#AltasItemGroup').bscombobox();
	$('#AltasItemLine').bscombobox();
	
	</script>
