<?php


  if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
    //if the user is in admin section...
?>
        <div class="modal fade in" id="selectTruckModal" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="false">
          <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">×</span></button>
                <h4 class="modal-title" id="selectTruckModalTitle">Select Truck</h4>
              </div>
              <div class="modal-body">
                <form>
                <div class="form-group">
                	<label for="truckSelect">Select Truck:</label>
                		<select class="form-control combobox" id="truckSelect">
                		<option></option>
                		<?php
                			$query = "SELECT * FROM MULTIPLE_WAREHOUSE WHERE WarehouseType LIKE 'T' ORDER BY WarehouseName";
                			$result = mysqli_query($con, $query);
                			while ($row = mysqli_fetch_array($result)) {
                		?>
                			<option value="<?= $row['WarehouseID'] ?>"><?= $row['WarehouseName'] ?></option>
                		<?php
                			}
                		?>
                		</select>
                </div>

                </form>
              </div>
              <div class="modal-footer">

              	<button type="button" onclick="addNewTruck();" id="addNewTruckButton" class="btn btn-success">Add New</button>
              	<button type="button" onclick="editTruck();" style="display: none;" id="editTruckButton" class="btn btn-warning">Edit</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                <button type="button" onclick="goSelectTruck();" id="modalGoSelectTruck" class="btn btn-primary">Go</button>
              </div>
            </div>
          </div>
        </div>
        <script type="text/javascript">
        $(document).ready(function(){
    			$('#adminManageMenu ul').append($('<\li>').append($('<\a>').html("Load Inventory Truck").attr('onclick', '$("#selectTruckModal").modal();')));
        });
        function goSelectTruck(){
            window.location = "mod.php?p=truckinventory&a=addToTruck&wid=" + $('#truckSelect').val();
        }
        function addNewTruck() {
          $('#priceListName').val('');
          $('#addEditPriceListTitle').text('Add Price List Definition');
          $('#priceListIsNew').val('1');
          $('#modalDeletePriceList').css('display', 'none');
          $('#addEditPriceList').modal('toggle');
        }

        function editTruck() {
          $('#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 saveNewTruck() {

          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 deleteTruck() {
          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");
                }
              });
          }
        }
        </script>
<?php
  }elseif(!$isCustomer){
    //if the user is in sales section and its a salesman.
?>


<?php

}else{
  //if the user is a customer...


}

?>
