<?php
/**
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
?>

<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
	<div class="navbar-header">
		<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
			<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" href="<?=constants::url_internal?>/"><?=constants::my_name?></a>
	</div>
	<!-- /.navbar-header -->

	<ul class="nav navbar-top-links navbar-right">
		<span>
			Logged in as <a href="<?=constants::url_internal?>/settings"><?=$_user->getUsername()?></a>
			(<a href="<?=constants::url_internal?>/?logout">logout</a>)
		</span>
		<!-- /.dropdown -->
		<li class="dropdown">
			<a class="dropdown-toggle" data-toggle="dropdown" href="#">
				<i class="fa fa-tasks fa-fw"></i>  <i class="fa fa-caret-down"></i>
			</a>
			<ul class="dropdown-menu dropdown-tasks">
				<?php
				$offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= CURDATE()"));
				$list = array();
				foreach ($offers as $offer) {
					$start = BusinessAdmin::formatDate($offer->getStartDate(), false);
					$end = BusinessAdmin::formatDate($offer->getEndDate(), false);
					$since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->getStartDate();
					$total = $offer->getEndDate() - $offer->getStartDate();
					$percentage = ($total == 0) ? 100 : round($since / $total * 100);

					// We want to sort on percentage (DESC) and secondly end date (ASC) so start date (DESC)
					$list[str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->getStartDate()] = array(
						'start' => $start,
						'end' => $end,
						'id' => $offer->getId(),
						'contactClientName' => $offer->getContact()->getClient()->name,
						'percentage' => $percentage,
						'price' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL)
					);
				}
				krsort($list, SORT_STRING);
				foreach ($list as $item) {
					echo "<li>
							<a href='".constants::url_internal."/offers?id={$item['id']}'>
								<div>
									<p>
										<strong>{$item['contactClientName']}</strong> ({$item['start']} - {$item['end']}; {$item['price']})
									</p>
									<div class='progress progress-striped active' title='{$item['percentage']}% complete'>
										<div class='progress-bar progress-bar-".($item['percentage'] < 60 ? 'info' : ($item['percentage'] < 80 ? 'warning' : 'danger'))."' style='width:{$item['percentage']}%;' aria-valuemax='100' aria-valuemin='0' aria-valuenow='{$item['percentage']}' role='progressbar'></div>
									</div>
								</div>
							</a>
						</li>";
				}
				if (count($list) == 0) {
					echo "<li><a href='#'><div><p>There are no currently active offers.</p></div></a></li>";
				}
				?>
				<li>
					<a class="text-center" href="<?=constants::url_internal?>/offers">
						<strong>See All Offers</strong>
						<i class="fa fa-angle-right"></i>
					</a>
				</li>
			</ul>
			<!-- /.dropdown-tasks -->
		</li>
		<!-- /.dropdown -->
	</ul>
	<!-- /.navbar-top-links -->

	<div class="navbar-default sidebar" role="navigation">
		<div class="sidebar-nav navbar-collapse">
			<ul class="nav" id="side-menu">
				<li title="Dashboard">
					<a <?php if($_page=='/') echo 'class="active"'; ?> href="<?=constants::url_internal?>/"><i class="fa fa-dashboard fa-fw"></i> <span class="nav-title">Dashboard</span></a>
				</li>
				<li title="Clients">
					<a <?php if($_page=='/clients') echo 'class="active"'; ?> href="<?=constants::url_internal?>/clients"><i class="fa fa-institution fa-fw"></i> <span class="nav-title">Clients</span></a>
				</li>
				<li title="Contacts">
					<a <?php if($_page=='/contacts') echo 'class="active"'; ?> href="<?=constants::url_internal?>/contacts"><i class="fa fa-user fa-fw"></i> <span class="nav-title">Contacts</span></a>
				</li>
				<li title="Offers">
					<a <?php if($_page=='/offers') echo 'class="active"'; ?> href="<?=constants::url_internal?>/offers"><i class="fa fa-briefcase fa-fw"></i> <span class="nav-title">Offers</span></a>
				</li>
				<li title="Assignments">
					<a <?php if($_page=='/assignments') echo 'class="active"'; ?> href="<?=constants::url_internal?>/assignments"><i class="fa fa-check-square fa-fw"></i> <span class="nav-title">Assignments</span></a>
				</li>
				<li title="Discounts">
					<a <?php if($_page=='/discounts') echo 'class="active"'; ?> href="<?=constants::url_internal?>/discounts"><i class="fa fa-percent fa-fw"></i> <span class="nav-title">Discounts</span></a>
				</li>
				<li title="Settings">
					<a <?php if($_page=='/settings') echo 'class="active"'; ?> href="<?=constants::url_internal?>/settings"><i class="fa fa-cog fa-fw"></i> <span class="nav-title">Settings</span></a>
				</li>
				<li title="About">
					<a <?php if($_page=='/about') echo 'class="active"'; ?> href="<?=constants::url_internal?>/about"><i class="fa fa-info-circle fa-fw"></i> <span class="nav-title">About</span></a>
				</li>
			</ul>

			<a href="#" id="collapse-menu" title="Collapse menu"><i class="fa fa-fw fa-caret-square-o-left"></i></a>

			<?php if (isset($_SESSION['ba-collapse']) && $_SESSION['ba-collapse'] == true) : ?>
			<script type="text/javascript">
				$(document).ready(function(){
					$('#collapse-menu').trigger('click', { load: true });
				});
			</script>
			<?php endif; ?>
		</div>
		<!-- /.sidebar-collapse -->
	</div>
	<!-- /.navbar-static-side -->
</nav>