<?php
defined ( '_JEXEC' ) or die ();
class AdminUIHelper {
	public static $vmAdminAreaStarted = false;
	public static $backEnd = true;
	static function startAdminArea($vmView,$selectText = 'COM_VIRTUEMART_DRDOWN_AVA2ALL') {
	if (vRequest::getCmd ( 'format') =='pdf') return;
	if (vRequest::getCmd ( 'manage',false)) self::$backEnd=false;
	if(self::$vmAdminAreaStarted) return;
	self::$vmAdminAreaStarted = true;
	$admin = 'administrator/components/com_virtuemart/assets/css';
	$adminlab = 'plugins/system/vmadminui/css';
	//loading defaut admin CSS
	//vmJsApi::css('admin_ui',$admin);
	vmJsApi::css('admin.styles',$admin);
	vmJsApi::css('toolbar_images',$admin);
	vmJsApi::css('menu_images',$admin);
	vmJsApi::css('chosen');
	vmJsApi::css('vtip');
	vmJsApi::css('jquery.fancybox-1.3.4');
	vmJsApi::css('ui/jquery.ui.all');
	//loading new admin CSS
	vmJsApi::css('bootstrap.min',$adminlab);
	vmJsApi::css('font-awesome.min',$adminlab);
	vmJsApi::css('adminlab',$adminlab);
	//loading default script
	vmJsApi::addJScript('fancybox/jquery.mousewheel-3.0.4.pack');
	vmJsApi::addJScript('fancybox/jquery.easing-1.3.pack');
	vmJsApi::addJScript('fancybox/jquery.fancybox-1.3.4.pack');
	vmJsApi::addJScript('/administrator/components/com_virtuemart/assets/js/jquery.coookie.js');
	//loading new script
	vmJsApi::addJScript('/plugins/system/vmadminui/js/angular/angular.min.js');
	vmJsApi::addJScript('/plugins/system/vmadminui/js/menu/menuvm.js');
	vmJsApi::addJScript('/plugins/system/vmadminui/js/moment/moment.js');
	vmJsApi::addJScript('/plugins/system/vmadminui/js/nicescroll/jquery.nicescroll.min.js');
	VmJsApi::chosenDropDowns();
	vmJsApi::addJScript('/administrator/components/com_virtuemart/assets/js/vm2admin.js');
		$vm2string = "editImage: 'edit image',select_all_text: '".vmText::_('COM_VIRTUEMART_DRDOWN_SELALL')."',select_some_options_text: '".vmText::_($selectText)."'" ;
		vmJsApi::addJScript ('vm.remindTab', "
//<![CDATA[
		var tip_image='".JURI::root(true)."/components/com_virtuemart/assets/js/images/vtip_arrow.png';
		var vm2string ={".$vm2string."} ;
		 jQuery( function($) {                   
			jQuery('dl#system-message').hide().slideDown(400);
			if ( $('#admin-ui-tabs').length) {
				$('#admin-ui-tabs').vm2admin('tabs',virtuemartcookie);
			}
			$('#content-box [title]').vm2admin('tips',tip_image);
			$('.modal').fancybox();
			$('.reset-value').click( function(e){
				e.preventDefault();
				none = '';
				jQuery(this).parent().find('.ui-autocomplete-input').val(none);
			});
		});
//]]>
		");
		?>
		<!--[if lt IE 9]>
		<script src="//ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
		<![endif]-->
		<?php if (!self::$backEnd ){
		   //JToolBarHelper
		   $bar = JToolbar::getInstance('toolbar');
		   echo '<div class="toolbar-box" style="height: 84px;position: relative;">'.$bar->render().'</div>';
	   } ?>
	   
		<div class="virtuemart-admin-area"> 
			<div class="menu-wrapper" id="menu-wrapper">   
					<div id="admin-ui-menu" class="admin-ui-menu vmleft"> 
		<div class="vmlogo text-center">
	<?php if(!empty($vmView->langList)){ ?>
					<div class="vm-lang-list-container">
						<?php echo $vmView->langList; ?>
					</div>
				<?php } else {
					echo '<a href="index.php?option=com_virtuemart&view=virtuemart" ><img class="img-circle" src="'.JURI::root(true).'/plugins/system/vmadminui/images/vm_logo.png"></a>';
				} ?>
				
            </div>
            	<div class="vm-installed-version">
					<?php
					echo "VirtueMart ".VmConfig::getInstalledVersion();
				?>
				</div>
			
				<?php AdminUIHelper::showAdminMenu($vmView);
				?>     
					</div> 
				</div> 
				 
			<div id="admin-content" class="admin-content">
			 <header class="top-vm container-fluid">
            <button type="button" class="toggler navbar-toggle pull-left">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            
            <ul class="nav-toolbar">
			<?php            
            $db=JFactory::getDBO();
			$query="SELECT COUNT(*)	FROM #__virtuemart_orders AS o WHERE order_status='P'";
			$db->setQuery($query);
			$orders_no=$db->loadResult();
            ?>
                            	<li class="dropdown"><a href="#" data-toggle="dropdown"><i class="fa fa-line-chart"></i> <span class="badge vmbadge"><!-- počet objednávek ve stavu pending se zobrazí v seznamu zde --><?php echo $orders_no;?></span></a>
                	<div class="dropdown-menu md arrow pull-right panel panel-default arrow-top-right">
                        <div class="panel-heading">
                      <?php echo vmText::_('COM_VIRTUEMART_ORDERS')?>
                        </div>
                        <!-- posledních  5 objednávek ve stavu pending se zobrazí v seznamu zde -->
                        <div class="list-group">
                            
                            <?php
                            $query="SELECT virtuemart_order_id,order_number,order_total,order_currency,DATE(o.created_on) AS created_on,currency_code_3 
							FROM #__virtuemart_orders AS o 
							LEFT JOIN #__virtuemart_currencies AS c ON o.order_currency=c.virtuemart_currency_id
							WHERE order_status='P' ORDER BY o.created_on DESC LIMIT 5";
                            $db->setQuery($query);
                            $orders=$db->loadObjectList();
                            ?>
                            <?php foreach ($orders as $order):?>
                            <a href="index.php?option=com_virtuemart&view=orders&task=edit&virtuemart_order_id=<?php echo $order->virtuemart_order_id;?>" class="list-group-item">
                            <div class="media">
                              <div class="media-body">
                                <h5 class="media-heading"><?php echo vmText::_('COM_VIRTUEMART_ORDER')?>: <?php echo $order->order_number;?></h5>
                                <small class="text-muted"><?php echo vmText::_('COM_VIRTUEMART_CART_PRICE')?>: <?php echo round($order->order_total).$order->currency_code_3;?> <?php echo vmText::_('COM_VIRTUEMART_CREATED_ON')?>: <?php echo $order->created_on;?></small>
                              </div>
                            </div>
                            </a>
                            <?php endforeach ;?>
                            <a href="index.php?option=com_virtuemart&view=orders&order_status_code=P" class="btn btn-info btn-block vmblock"> <?php echo vmText::_('COM_VIRTUEMART_VIEW')?> <?php echo vmText::_('COM_VIRTUEMART_ALL')?></a>

                        </div>
                        
                    </div>
                </li>
                
                <li class="dropdown">
				  <a href="#" data-toggle="dropdown"><i class="fa fa-th"></i></a>
                	<div class="dropdown-menu lg pull-right arrow panel panel-default arrow-top-right">
                    	<div class="panel-heading">
                        	VirtueMart
                        </div>
                        <div class="panel-body text-center">
                        	<div class="row">                                                                                                               
                            	<div class="col-xs-6 col-sm-4"><a href="index.php?option=com_virtuemart&view=orders"><span class="h2"><i class="fa fa-line-chart"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_ORDERS')?></p></a></div>
                                <div class="col-xs-6 col-sm-4"><a href="index.php?option=com_virtuemart&view=product"><span class="h2"><i class="fa fa-barcode"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_PRODUCT_MOD')?></p></a></div>
                                <div class="col-xs-12 visible-xs-block"><hr></div>
                                <div class="col-xs-6 col-sm-4"><a href="index.php?option=com_virtuemart&view=ratings"><span class="h2"><i class="fa fa-comments-o"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_RATINGS')?></p></a></div> 
                                <div class="col-lg-12 col-md-12 col-sm-12 hidden-xs"><hr></div>
                                <div class="col-xs-6 col-sm-4"><a href="index.php?option=com_virtuemart&view=config"><span class="h2"><i class="fa fa-cog"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_CONFIGURATION')?></p></a></div>
                                <div class="col-xs-12 visible-xs-block"><hr></div>
                                <div class="col-xs-6 col-sm-4"><a href="index.php?option=com_virtuemart&view=coupon"><span class="h2"><i class="fa fa-heart-o"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_COUPON_S')?></p></a></div>
                                <div class="col-xs-6 col-sm-4"><a href="http://forum.virtuemart.net/" target="_blank"><span class="h2"><i class="fa fa-users"></i></span><p><?php echo vmText::_('COM_VIRTUEMART_COMMUNITY_FORUM')?></p></a></div>
							</div>
                        </div>
                    </div>
                </li>
            </ul>
        </header>
		  <div class="warper container-fluid">
		<?php	}
	static function endAdminArea() {
		if (!self::$backEnd) return;
		self::$vmAdminAreaStarted = false;
		if (VmConfig::get('debug') == '1') {
		//TODO maybe add debuggin again here
//		include(VMPATH_ADMIN.'debug.php');
		}
		?>      
				</div>
				</div>
		</div>

        
        
        <footer class="container-fluid footer">
            <a href="#" class="pull-right scrollTop"><i class="fa fa-chevron-up"></i></a>
        </footer>
        
    

		<div class="clear"></div>
	<?php
	    }
	static public function buildTabs($view, $load_template = array(),$cookieName='') {
		$cookieName = vRequest::getCmd('view','virtuemart').$cookieName;
		vmJsApi::addJScript ( 'vm.cookie', '
		var virtuemartcookie="'.$cookieName.'";
		');
		$html = '<div id="admin-ui-tabs">';
		foreach ( $load_template as $tab_content => $tab_title ) {
			$html .= '<div class="tabs" title="' . vmText::_ ( $tab_title ) . '">';
			$html .= $view->loadTemplate ( $tab_content );
			$html .= '<div class="clear"></div></div>';
		}
		$html .= '</div>';
		echo $html;
	}
	static function imitateTabs($return,$language = '') {
		if ($return == 'start') {
			vmJsApi::addJScript ( 'vm.cookietab','
			var virtuemartcookie="vm-tab";
			');
			$html = 	'<div id="admin-ui-tabs">
							<div class="tabs" title="'.vmText::_($language).'">';
			echo $html;
		}
		if ($return == 'end') {
			$html = '		</div>
						</div>';
			echo $html;
		}
	}
	static function _getAdminMenu($moduleId = 0) {
		$db = JFactory::getDBO ();
		$menuArr = array ();
		$filter [] = "jmmod.published='1'";
		$filter [] = "item.published='1'";
		$user = JFactory::getUser();
		if (! empty ( $moduleId )) {
			$filter [] = 'vmmod.module_id=' . ( int ) $moduleId;
		}
		$query = 'SELECT `jmmod`.`module_id`, `module_name`, `module_perms`, `id`, `name`, `link`, `depends`, `icon_class`, `view`, `task`
						FROM `#__virtuemart_modules` AS jmmod
						LEFT JOIN `#__virtuemart_adminmenuentries` AS item ON `jmmod`.`module_id`=`item`.`module_id`
						WHERE  ' . implode ( ' AND ', $filter ) . '
						ORDER BY `jmmod`.`ordering`, `item`.`ordering` ';
		$db->setQuery ( $query );
		$result = $db->loadAssocList ();
		//		echo '<pre>'.print_r($query,1).'</pre>';
		for($i = 0, $n = count ( $result ); $i < $n; $i ++) {
			$row = $result [$i];
			$menuArr [$row['module_id']] ['title'] = 'COM_VIRTUEMART_' . strtoupper ( $row['module_name'] ) . '_MOD';
			$menuArr [$row['module_id']] ['code'] = $row['module_name'];
			$menuArr [$row['module_id']] ['items'] [] = $row ;
		}
		return $menuArr;
	}
	/**
	 * Display the administrative ribbon menu.
	 * @todo The link should be done better
	 */
	static function showAdminMenu($vmView) {
		if(!isset(VmConfig::$installed)){
			VmConfig::$installed = false;
		}
		if(!VmConfig::$installed) return false;
		$document = JFactory::getDocument ();
		$moduleId = vRequest::getInt ( 'module_id', 0 );
		$user = JFactory::getUser();
		$menuItems = AdminUIHelper::_getAdminMenu ( $moduleId );
		$app = JFactory::getApplication();
		$isSite = $app->isSite();
		?>
            <nav class="vmnav">
            	<ul class="list-unstyled">
		<?php
		$modCount = 1;
		foreach ( $menuItems as $item ) {
			$html = '';
			foreach ( $item ['items'] as $link ) {
				$target='';
				if ($link ['name'] == '-') {
					// it was emtpy before
				} else {
					if (strncmp ( $link ['link'], 'http', 4 ) === 0) {
						$url = $link ['link'];
						$target='target="_blank"';
					} else {
						$url = ($link ['link'] === '') ? 'index.php?option=com_virtuemart' :$link ['link'] ;
						$url .= $link ['view'] ? "&view=" . $link ['view'] : '';
						$url .= $link ['task'] ? "&task=" . $link ['task'] : '';
						$url .= $isSite ? '&tmpl=component&manage=1':'';
						// $url .= $link['extra'] ? $link['extra'] : '';
					}
					if (/*$user->authorise('core.admin', 'com_virtuemart') or
						$user->authorise('core.manage', 'com_virtuemart') or
						($user->authorise('vm.manage', 'com_virtuemart') and $user->authorise('vm.'.$link ['view'], 'com_virtuemart'))*/
						$vmView->manager($link ['view'])
						|| $target || $link ['view']=='about' || $link ['view']=='virtuemart') {
						$html .= '
						<li>
							<a href="'.$url.'" '.$target.'>'. vmText::_ ( $link ['name'] ).'</a>
						</li>';
					}
				}
			}
			if(!empty($html)){
				switch ($item ['code']) {
					case 'product':
						$fa='fa-barcode';
						break;
					case 'order':
						$fa='fa-line-chart';
						break;
					case 'manufacturer':
						$fa='fa-building-o';
						break;
					case 'shop':
						$fa='fa-shopping-cart';
						break;
					case 'configuration':
						$fa='fa-newspaper-o';
						break;
					case 'tools':
						$fa='fa-cogs';
						break;	
					default:
						$fa='fa-bookmark-o';
						break;
				}
				?>
				<li class="vm-submenu"><a href="#"><i class="fa <?php echo $fa;?>"></i>
					<span class="leftnavmenu"><?php echo vmText::_ ( $item ['title'] )?></span>  </a>
					<ul class="list-unstyled">
						<?php echo $html ?>
					</ul>
					</li>
				<?php
				$modCount ++;
			}
		}
		?>
		</ul>
				  </nav>
		<div class="menu-notice"></div>  
	<?php
	}
}
?>