<?php
if(!isset($_REQUEST['test'])){
	//header("Location: down.php");
	//die("El sistema de ordenes esta en proceso de mantenimiento. Disculpe los inconvenientes.");
}

//$language = $_SESSION['Language'];
if (isset($_SESSION['isCustomer'])) {
	$isCustomer = $_SESSION['isCustomer'];
}else {
	header("Location: login.php");
}

if ($isCustomer) {
	$customer = new Customer($_SESSION['AccountNumber']);
	$language = $customer->Language;
	$pricePlanMode = false;
}else {

	$salesman = new Salesman($_SESSION['SalesmanID']);
	$language = $salesman->Language;
	$pricePlanMode = $_SESSION['pricePlanMode'];

}




include_once('./modules/savecart/savecart.addon.php');
?>
<div class="ajax-loading-modal"></div>


<link rel="stylesheet" href="css/lightbox.css" type="text/css" />
<link rel="stylesheet" href="css/navbar.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" type="text/css"/>
<script src="js/lightbox.js" type="text/javascript"></script>
<script src="js/bootstrap-checkbox.min.js" type="text/javascript"></script>
<script src="js/jquery.fixedheadertable.min.js" type="text/javascript"></script>
<script src="js/jquery.redirect.js" type="text/javascript"></script>
<script src="js/navbar.js" type="text/javascript"></script>
<script src="js/functions.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<script src='https://cdn.jsdelivr.net/gh/FThompson/FormPersistence.js@1.0.1/form-persistence.min.js' type='text/javascript'></script>

<style>
	.ajax-loading-modal {
		display:    none;
		position:   fixed;
		z-index:    9000;
		top:        0;
		left:       0;
		height:     100%;
		width:      100%;
		background: rgba( 255, 255, 255, .8 )
					url('/img/loading-web.gif')
					50% 50%
					no-repeat;
	}
	/* When the body has the loading class, we turn
	   the scrollbar off with overflow:hidden */
	body.loading {
		overflow: hidden;
	}

	/* Anytime the body has the loading class, our
	   modal element will be visible */
	body.loading .ajax-loading-modal {
		display: block;
	}
</style>


<a href="javascript:$('#catalogsButton [data-toggle=dropdown]').dropdown('toggle');"><img src="img/banner.jpg?time=<?= time() ?>" id="stripHeader" class="navbar-fixed-top hidden-print" style=" width: 95%; margin: auto;  margin-top: 60px; display: block;" alt="" /></a>

<nav class="navbar navbar-default navbar-fixed-top" style="min-height: 50px;">
  <div class="container-fluid" style="min-height: 50px;">
<!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>

      <a class="navbar-brand" style="padding: 5px; padding-right: 160px;" href="index.php"><img class="img-responsive" style="width: 160px; position: absolute;" src="img/<?= $config['SmallLogo'] ?>" alt="" /></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" style="min-height: 50px;" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <?php
        if (basename($_SERVER['PHP_SELF']) != "index.php") {
        	if (isset($_SESSION['LastSearch'])) {
		        $url = "index.php?";
		        $url .= "sku=" . $_SESSION['LastSearch']['SKU'] . "&";
		        $url .= "line=" . $_SESSION['LastSearch']['Line'] . "&";
		        $url .= "group=" . $_SESSION['LastSearch']['Group'] . "&";
		        $url .= "wc=" . $_SESSION['LastSearch']['WC'] . "&";
		        $url .= "p=" . $_SESSION['LastSearch']['Page'];
		        if($_SESSION['LastSearch']['SKU'] != "" || $_SESSION['LastSearch']['Line'] != "" || $_SESSION['LastSearch']['Group'] != "" || $_SESSION['LastSearch']['WC'] != ""){
        ?>
        	<li><a href="<?= $url ?>">Back to Search</a></li>
        <?php
        		}else{
        ?>
        	<li><a href="index.php">Back to Search</a></li>
        <?php
        		}
        	}
        }else{
        ?>

        <li style="margin-right: -7px; margin-left: -7px;" ><a href="index.php">Clear Screen</a></li>

        <?php
			}if($isCustomer && !$detect->isMobile()){
				?>
				
				<?php

			}


        ?>

       	<?php
		if(!$isCustomer && !$detect->isMobile()){
		?>
       	<li style="font-size: 40px;" ><a href="mod.php?p=payment&a=new">💰</a></li>
		<li style="font-size: 40px;" ><a onclick="$('#navbarClearCustomer').click();">👥</a></li>
		<li style="font-size: 40px;" ><a onclick="showCatalog();"><img src="img/catalog-icon.png" style="width: 50px;" /></a></li>
		<li style="font-size: 40px;" ><a onclick="$('#deliveryModal').modal();"><img src="img/delivery_logo.png" style="width: 50px;" /></a></li>
		

		<?php
		if($salesman->AllowTruckMode){
		?>
		<li><a style="font-size: 40px;" onclick="$('#truckModeWarehouseModal').modal();"><img  style="height: 40px;" src="img/truck.png" /></a></li>
		<?php
	}
		?>

		<?php


		$query = "SELECT COUNT(PurchaseOrderID) `Count` FROM PURCHASE_ORDER_HEADER JOIN VENDOR USING(VendorID) WHERE Confirmed = 1 AND Received = 0";
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_array($result);
		if($row['Count'] > 0){

        ?>

        <li style="font-size: 40px;" ><a onclick="$('#altasModal').modal();"><img src="img/ontheway.png" style="width: 50px;"></a></li>
        <?php
			}

		}
		?>
      </ul>
      <style>
      	.catalog-menu-item{
      		padding: 2px;
      	}
      	.catalog-menu-logo{
      		height: 40px;
      	}

      </style>



      <style>
      	.promo {
      		position: fixed;
      		top: -10px;
      		right: 45%;
      	}
		.grow { transition: all .2s ease-in-out; }
		.grow:hover { transform: scale(1.2); }
      </style>


      <ul class="nav navbar-nav navbar-right">
		<div id="billboardContainer">
		</div>
		<script>
		<?php
			if ( !$detect->isMobile() ) {


		?>
			$(document).ready(function(){
				$.get('mod.php?p=billboard&a=functions&f=getNavbarBillboardsCode<?= isset($_REQUEST['splash'])?"&splash=true":"" ?>', function(data){
					$('#billboardContainer').html(data);
					$('img[id^=promoModalContent]').css('max-height', (window.innerHeight * 0.72));
					$('img[id^=promoModalContent]').css('max-width', '100%');
					$('iframe[data-type=pdf]').css('height', (window.innerHeight * 0.72));
					$('iframe[data-type=pdf]').css('width', '100%');

					var catalogs_button = window.innerWidth-$('#catalogsButton').position().left;
					//var altas_btn = window.innerWidth-$('#altasButton').position().left-$('#altasButton').width();
					var altas_btn = 0;
					var max_bb_size = (altas_btn-catalogs_button)/3;
					$('#PromoButton1').css('height', '');
					$('#PromoButton1').css('max-height', '70px');
					$('#PromoButton1').css('max-width', max_bb_size + 'px');
					$('#PromoButton1').css('right', (altas_btn - max_bb_size * 1) + "px");


					$('#PromoButton2').css('height', '');
					$('#PromoButton2').css('max-height', '70px');
					$('#PromoButton2').css('max-width', max_bb_size + 'px');
					$('#PromoButton2').css('right', (altas_btn - max_bb_size * 2) + "px");


					$('#PromoButton3').css('height', '');
					$('#PromoButton3').css('max-height', '70px');
					$('#PromoButton3').css('max-width', max_bb_size + 'px');
					$('#PromoButton3').css('right', (altas_btn - max_bb_size * 3) + "px");



					$('.dropdownModal').modal('show');
				});

			});

			function showNewPromo(ID){
				$('#promoModal'+ ID).modal('toggle');
			}

			<?php
			}
			?>
		</script>


		<?php

      	if (true) {


      ?>
		<li class=""><a onclick="$('#reorderModal').modal('toggle');return false;" href="#"><span class="glyphicon glyphicon-retweet" aria-hidden="true"></span> Reorder List</a></li>
		
      	<li id="catalogsButton" style="display:none;" class="dropdown">
      		<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?= $language=="ES"?"Catalogos":"Catalogs" ?><span class="caret"></span></a>
      		<ul class="dropdown-menu scroll-menu" role="menu">
				<?php



						$queryCatalog = "SELECT * FROM SUPPLIER_CATALOG_LINK ORDER BY Position, Name";
						$resultCatalog = mysqli_query($con, $queryCatalog);
						while($row = mysqli_fetch_array($resultCatalog, MYSQLI_ASSOC)){
				?>
					<li class="centered catalog-menu-item"><a target="<?= $row['Target'] ?>" href="<?= $row['Link'] ?>"><img src="<?= $row['ButtonImage'] ?>"  style="max-width: 200px;" alt="<?= $row['Name'] ?>" /><?= ($row['AdditionalText'])?"<br>" . $row['AdditionalText']:"" ?></a></li>
				<?php
						}
				?>
      		</ul>
      	</li>
      	<?php
      		}

      	?>
      		<li>
      			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Tools<span class="caret"></span></a>
      			<ul class="dropdown-menu multi-level" role="menu">
					<li><a href="#" onclick="$('#CustomerInformationModal').modal('toggle');">Customer Information</a></li>
					<li><a href="#" onclick="$('#SalesmanCashboxModal').modal('toggle');">My Settlement</a></li>
					<li><a onclick="orderHistoryDateSelect();">Order History</a></li>
					<?php
						if (!$isCustomer){
					?>
					<li><a onclick="invoiceHistoryDateSelect();">Invoice History</a></li>

					<li><a href="mod.php?p=payment&a=salesmancompleteaging">My Aging</a></li>
					<li><a onclick="estimateHistoryDateSelect();">Estimate History</a></li>
					<li><a onclick="$('#commissionPreviewModal').modal('toggle')">My Commission</a></li>
					<?php
						if($salesman->iPhoneConfig && $detect->isiOS()){
							echo "<li><a onclick=\"alert('The Password for opening the configuration file is:\\n\\n\\n\\n');window.open('" . $salesman->iPhoneConfig . "');\">iPhone Email Configuration</a></li>";
						}
						if($salesman->EmailPass){


						?>
					<li><a onclick="alert('Go to to your devices email app settings and add a new account.\n\nType: IMAP\n\nE-Mail/Username: <?= $salesman->Email ?>\nPassword: <?= $salesman->EmailPass ?>\n\nIncoming Server: \nPort: 993\n\nOutgoing Server: \nPort: 465');">Manual Email Settings</a></li>
						<?php
						}
						?>
					<li class="dropdown-submenu">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown">Pickups</a>
						<ul class="dropdown-menu">
							<li><a href="pickup.php">New Pickup Request</a></li>
							<li><a href="pickuphistory.php">Pickup History</a></li>
						</ul>
					</li>
					<li><a href="#" id="navbarClearCustomer">Change Customer</a></li>
					<script>
						$('#navbarClearCustomer').on('click', function(){
							$('#RequiredSetCustomerModal').modal('toggle');
							$('#RequiredSetCustomerModalCloseButton').show();
							/*
							$.get('functions.php?action=clearSetCustomer', function(data) {
								data = JSON.parse(data);
								if (data.result) {
									location.reload();
								}else {
									alert("ERROR");
								}


							});
							*/
						});

					</script>
					<li class="dropdown-submenu">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown">Payments</a>
						<ul class="dropdown-menu">
							<li><a href="mod.php?p=payment&a=new">New Payment</a></li>
							<li><a onclick="paymentHistoryDateSelect()">Payment History</a></li>
							<li><a onclick="$('#creditSearchModal').modal('toggle');">Credit Search</a></li>
						</ul>
					</li>
					<li><a href="#" onclick="$('#weeklyPlannerSelectModal').modal('toggle')">Weekly Planner</a></li>
					<li><a href="#" onclick="openMyCustomers();">Customer Last Login</a></li>
					<li><a href="#" onclick="$('#pricePlanModal').modal('toggle');">Price Plan (Calculated)</a></li>
					<li><a href="#" onclick="openSalesmanQuota();">My Quota</a></li>
					<li><a href="#" onclick="openCustomerActivity();">Customer Activity</a></li>
					<li><a href="#" onclick="window.open('mod.php?p=payment&a=printcheck','PrintCheck','toolbar=no,width=415,height=1000')">Print Check</a></li>
					
      			
				    <?php
						}else{
					?>
					<li class="dropdown-submenu">
					<a href="#" class="dropdown-toggle" data-toggle="dropdown">Payments</a>
						<ul class="dropdown-menu">
							<li><a onclick="paymentHistoryDateSelect()">Payment History</a></li>
							<li><a href="mod.php?p=payment&a=aging">Aging</a></li>
						</ul>
					</li>
					<li><a href="pickuphistory.php">Pickup History</a></li>

					<?php

						}
					?>

					<li class="dropdown-submenu">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-globe" aria-hidden="true"></span> Language</a>
						<ul class="dropdown-menu" role="menu">
							<li><a onclick="setLanguage('EN')" href="#">English</a></li>
							<li><a onclick="setLanguage('ES')" href="#">Espa&ntilde;ol</a></li>
						</ul>
					</li>
      			</ul>
      		</li>
      		<?php

      		 ?>
      	<li class="dropdown">
      		<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?= ($isCustomer)?$customer->Name:$salesman->Name ?><span class="caret"></span></a>
      		<ul class="dropdown-menu" role="menu">

      			<?php
      				if (!$isCustomer) {
      			?>

      			<?php
      				}

      			 ?>
      	        <li><a onclick="clearPasswordForm()" href="#">Change Password</a></li>
      	        <li><a href="logout.php">Logout</a></li>
				<li><a href="https://get.teamviewer.com/a3sghqu">Help</a></li>
      		</ul>
      	</li>



        <li class="<?= (basename($_SERVER['PHP_SELF']) == "cart.php")?"active":"" ?>"><a href="cart.php"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span> <span id="cartTotal"><?= moneyFormat(getCartTotal()) ?></span>
		<?php
			if(!$isCustomer && $savedCartCount > 0){
		?>
		<span style="background-color: #dc0d17;" class="badge"><?= $savedCartCount ?></span>
		<?php
			}
		?>
		</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
    </nav>

    <script type="text/javascript">
    	$('.scroll-menu').css('max-height',$(window).height()-75);
    	$('.scroll-menu').css('overflow',"scroll");

		function showAvisoImportante() {
			$("#avisoImportanteCarousel").carousel(0);
			$('#avisoImportanteModal').modal('show');
		}
		$(document).ready(function(){
			$('.combobox').combobox();
			
			if($(window).width() < 1320){
				$('#optionalPromo').hide();
			}	else{
				$('#optionalPromo').show();
			}

			$(window).resize(function(){
				if($(window).width() < 1320){
					$('#optionalPromo').hide();
				}	else{
					$('#optionalPromo').show();
				}
			});
		});

    </script>

    <?php

    include("modals.php"); ?>
    <br /><br />
	<?php

	$chatCustomers = [];
	if(isset($_SESSION['isCustomer']) && isset($customer)?in_array($customer->AccountNumber, $chatCustomers):false && false){

	?>


	<?php
	}
	?>
	<script>
		$body = $("body");
		$(document).on({
			ajaxStart: function() { $body.addClass("loading");    },
			 ajaxStop: function() { $body.removeClass("loading"); },
			 ajaxError: function() { $body.removeClass("loading"); }
		});
		$(document).ajaxError(function() { $body.removeClass("loading"); });



		//load required select customer
		<?php

		if (!$_SESSION['isCustomer']) {
     	if((isset($_SESSION['setCustomerMode']) && !$_SESSION['setCustomerMode']) || !isset($_SESSION['setCustomerMode'])) {
     	?>
		$(document).ready(function(){
			$('#RequiredSetCustomerModal').modal();
		});

		<?php
     	}
     }

		?>
		var form;
		form = document.getElementById('reorderForm');
		FormPersistence.persist(form);
		FormPersistence.load(form);
		$('#reorderForm input').on('change', function(){
				FormPersistence.save(form);
		});

	</script>
	<script src="js/jquery.fittext.js"></script>
