<?php




?>

<!-- Select Price List Modal -->
        <div class="modal fade" id="selectPriceList" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-sm">
            <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="myModalLabel">Select Price List</h4>
              </div>
              <div class="modal-body">
                <form>
                <div class="form-group">
                	<label for="priceListSelect">Price List:</label>
                		<select class="form-control combobox" id="priceListSelect">
                		<option></option>
                		<?php
                			$query = "SELECT * FROM PRICE_PLAN_LIST_DEF ORDER BY Name";
                			$result = mysqli_query($con, $query);
                			while ($row = mysqli_fetch_array($result)) {
                		?>
                			<option value="<?= $row['id'] ?>"><?= $row['Name'] ?></option>
                		<?php
                			}
                		?>
                		</select>
                </div>
                
                </form>
              </div>
              <div class="modal-footer">
              
              	<button type="button" onclick="addPriceListDefinition()" id="modalAddPriceList" class="btn btn-success">Add New</button>
              	<button type="button" onclick="editPriceListDefinition()" style="display: none;" id="modalEditPriceList" class="btn btn-warning">Edit</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                <button type="button" id="modalGoPriceList" class="btn btn-primary">Go</button>
              </div>
            </div>
          </div>
        </div>
        <!-- Select Price List Modal End -->
		
		
		 <!-- AddEdit Price List Modal -->
        <div class="modal fade" id="addEditPriceList" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-sm">
            <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="addEditPriceListTitle">Add Price List Definition</h4>
              </div>
              <div class="modal-body">
                <form>
                <input type="hidden" id="priceListIsNew" name="isNew" value="" />
                <input type="hidden" id="priceListID" name="ID" value="" />
                
                <div class="form-group">
                	<label for="priceListName">Name:</label>
                		<input class="form-control" type="text" id="priceListName" name="priceListName" value="" />
                </div>
                </form>
              </div>
              <div class="modal-footer">
              	<button type="button" onclick="deletePriceListDefinition()" id="modalDeletePriceList" style="display: none;" class="btn btn-danger">Delete</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" onclick="savePriceListDefinition()" id="modalSavePriceList" class="btn btn-primary">Save</button>
              </div>
            </div>
          </div>
        </div>
        <!-- AddEdit Price List Modal End -->
		
		
		<script>
			function addPriceListDefinition() {
				
				$('#priceListName').val('');	
				$('#addEditPriceListTitle').text('Add Price List Definition');
				$('#priceListIsNew').val('1');	
				$('#modalDeletePriceList').css('display', 'none');
				$('#addEditPriceList').modal('toggle');
			}
			
			function editPriceListDefinition() {
				$('#priceListName').val('');
				var id = $('#priceListSelect').val();
				$.get("./mod.php?p=pricingplanlist&a=functions&f=getPriceListDefinitionInfo&id=" + id, function(data){
					var priceList = JSON.parse(data);
					$('#priceListIsNew').val("0");
					$('#addEditPriceListTitle').text('Edit Price List Definition');
					$('#modalDeletePriceList').css('display', '');
					$('#priceListID').val(priceList.id);
					$('#priceListAccountNumber').val(priceList.AccountNumber);
					$('#priceListName').val(priceList.Name);
					$('#addEditPriceList').modal('toggle');
				});
				
			}
			function savePriceListDefinition() {
				
				var priceListName = $('#priceListName').val();	
				var isNew = $('#priceListIsNew').val();
				var ID = $('#priceListID').val();	
				if (priceListName.trim() == "" || priceListName == null) {
					alert("A Name is required!");
					return false;
				}
				var priceListData = '{ "ID" : "' + ID + '", "Name" 	:	"' + priceListName + '", "IsNew" : "' + $('#priceListIsNew').val() + '"}';
				priceListData = JSON.parse(priceListData);
				$.post( "./mod.php?p=pricingplanlist&a=functions&f=savePricePlanList", priceListData)
				  .done(function( data ) {
					data = JSON.parse(data);
					if (data.result === true) {
						$('#addEditPriceList').modal('toggle');
						if (isNew > 0) {
							$('#priceListSelect').append($("<option></option>").attr("value",data.ID).text(data.ID + " - " + priceListName));
							$('#priceListSelect').data('combobox').refresh();
						}else {
							$('#priceListSelect option[value=' + ID + ']').text(data.ID + " - " + priceListName);
							$('#priceListSelect').data('combobox').refresh();
						}
						
						
					  }else {
						alert("There has been an error saving the price list data.\r\n\r\nMySQL Error: " + data.error + "\n--------\n" + data.query);
					  }
					});    		
			}
			$('#modalGoPriceList').on('click', function(){
				
					
			});
			$('#priceListSelect').on('change', function(){
				if($('#priceListSelect').val() !== "" ){
					$('#modalEditPriceList').show();	
				}else{
					$('#modalEditPriceList').hide();
				}
				
			});
			function deletePriceListDefinition() {
    		var ID = $('#priceListID').val();	
    		if (confirm("Are you sure you want to delete this Price List Definition?")) {
    			$.post( "./mod.php?p=pricingplanlist&a=functions&f=deletePriceListDefinition&id=" + ID, null)
    			  .done(function( data ) {
    			  data = $.parseJSON(data);
    			  console.log(data.result);
    			    if (data.result == true) {
    			    	$('#priceListSelect option[value=' + ID + ']').remove();
    			    	$('#priceListSelect').data('combobox').clearTarget()
    			    	$('#priceListSelect').data('combobox').clearElement()
    			    	$('#modalEditPriceList').css('display', 'none');
    			    	$('#priceListSelect').data('combobox').refresh();
    			    	$('#addEditPriceList').modal('toggle')
    			    }else {
    			    	alert("There has been an error deleting the Price List Definition");
    			    }
    			  });
    		}
    	}
			function askForPricePlanList(dest){
				
				if(dest == "parts"){
					$('#modalGoPriceList').unbind('click');
					$('#modalGoPriceList').on('click', function(){
						window.location = "./mod.php?p=pricingplanlist&a=managePPList&pl=" + $('#priceListSelect').val();
					});
				}else if(dest == "customers"){
					$('#modalGoPriceList').unbind('click');
					$('#modalGoPriceList').on('click', function(){
						window.location = "./mod.php?p=pricingplanlist&a=manageCust&pl=" + $('#priceListSelect').val();
					});
					
				}
				else if(dest == "importpplcustomers"){
					$('#modalGoPriceList').unbind('click');
					$('#modalGoPriceList').on('click', function(){
						window.location = "./import.php?a=pricingplanlistcustomer&id=" + $('#priceListSelect').val();
					});
					
				}
				else if(dest == "importpplparts"){
					$('#modalGoPriceList').unbind('click');
					$('#modalGoPriceList').on('click', function(){
						window.location = "./import.php?a=pricingplanlistparts&id=" + $('#priceListSelect').val();
					});
					
				}
				$('#selectPriceList').modal('toggle');	
			}
		</script>