<?php
namespace frontend\components;

use Yii;
use common\models\Order;
use yii\helpers\Html;
use common\models\MoneyMovement;
use yii\db\Query;
use common\models\AccountState;
use backend\models\AccountSearch;
use common\models\Account;
use common\components\Helper;

class FrontendMenuStructure{
	
	public $menuItems;
	public $start;//start date and time
	public $tomorrow;//tomorrow date and time
	
	public $startDate;//start date
	public $tomorrowDate;//tomorrow date 
	public $yesterDay;//yesterday date
	
	public function __construct(){
		$this->menuItems = [];
		
		$this->yesterDay = \Yii::$app->formatter->asDatetime( strtotime('yesterday UTC') );
		$this->start =  \Yii::$app->formatter->asDatetime( strtotime('today UTC') );
		$this->tomorrow =  Yii::$app->formatter->asDatetime( strtotime('tomorrow UTC') );
		$this->startDate =  Yii::$app->formatter->asDate(  strtotime('today UTC') );
		$this->tomorrowDate =  Yii::$app->formatter->asDate( strtotime('tomorrow UTC') );
		
		Yii::info("Start date is : ". $this->start);
		
		
		
		if ( $this->isLogged()  ){
			$lastAccountState = AccountState::find()->andWhere(['id_user' => Yii::$app->user->id])->andWhere(['type' =>  AccountState::TYPE_OPEN ])->orderBy(['account_state.created_at' => SORT_DESC])->limit(1)->one();
			if ( isset($lastAccountState) ){
				$this->start =  Yii::$app->formatter->asDatetime(strtotime( $lastAccountState->created_at . ' UTC' ));
			}
		}

	}
	
	
	
	protected function can($authItem){
		$result = false;
		if (\Yii::$app->user->can($authItem)) {
			$result = true;
		}
		return $result;
	}
	
	
	protected function isLogged(){
		return !Yii::$app->user->isGuest;
	}
	
	
	protected function addRecepcio(){
		if ( $this->isLogged() ){
			
		$isadmin =Yii::$app->user->can('admin') ||Yii::$app->user->can('employee');
			
			$this->menuItems[]  = ['label' => Yii::t('frontend/customer','Reception'), 'url' => ['/customer/reception'] ];
			
			$items = [
							['label' =>  Yii::t('frontend/account-state','Default account'), 'url' => ['/account/select'] ],
							['label' =>  Yii::t('frontend/account-state', 'Account states'), 'url' => ['/account-state/index' ,'AccountstateSearch[id_account]' => Account::readDefault(), 'AccountstateSearch[start]' => $this->yesterDay] ],
							['label' =>  Yii::t('frontend/account-state','Open account state'), 'url' => ['/account-state/open'] ],
							['label' =>  Yii::t('frontend/account-state','Close account state'), 'url' => ['/account-state/close'] ],
							
							['label' =>  Yii::t('frontend/money-movement','Money movements'), 'url' => [ '/money-movement/index', 'MoneyMovementSearch[start]' => $this->start, 'MoneyMovementSearch[end]' => $this->tomorrow ] ],
							
					];
			
			if ( Helper::isCompanyMovar() ){
				$items[] = ['label' =>  Yii::t('frontend/transfer','Daily transfers'), 'url' => [ '/transfer/list', 'TransferListSearch[id_account]' => Account::readDefault(), 'TransferListSearch[start]' => $this->start, 'TransferListSearch[end]' => $this->tomorrow ] ];
				$items[] = ['label' =>  Yii::t('frontend/transfer','Sales detailed'), 'url' => [ '/transfer/sale', 'TransferSaleSearch[id_user]' =>\Yii::$app->user->id, 'TransferSaleSearch[id_account]' =>  Account::readDefault(), 'TransferSaleSearch[start]' => $this->start, 'TransferSaleSearch[end]' => $this->tomorrow ] ];
				$items[] = ['label' =>  Yii::t('frontend/transfer','Ticket sale detailed'), 'url' => [ '/transfer/tickets','TransferTicketSearch[id_user]' =>\Yii::$app->user->id, 'TransferTicketSearch[id_account]' =>  Account::readDefault(),  'TransferTicketSearch[start]' => $this->start, 'TransferTicketSearch[end]' => $this->tomorrow ] ];
			}
			
// 			if ( $isadmin ||  Yii::$app->user->can('reception.transfers') ){
				$items[] = ['label' =>  Yii::t('frontend/transfer','Transfers'), 'url' => ['/transfer/index', 'TransferSearch[id_user]' =>\Yii::$app->user->id, 'TransferSearch[id_account]' =>  Account::readDefault(), 'TransferSearch[start]' => $this->start, 'TransferSearch[end]' => $this->tomorrow ] ];
				//$items[] = ['label' =>  Yii::t('frontend/collection','Collections'), 'url' => ['/collection/index' , 'CollectionSearch[start]' =>$this->start,'CollectionSearch[end]' => $this->tomorrow ] ];
// 			}
			
			$items[] = ['label' =>  Yii::t('frontend/card','Vendégek'), 'url' => [ '/card/index' ] ];
// 			$items[] = ['label' =>  Yii::t('frontend/card','Leltár'), 'url' => [ '/product/inventory' ] ];
// 			$items[] = ['label' =>  Yii::t('frontend/card','Leltár'), 'url' => [ '/inventory/index' ] ];
				
			
			$this->menuItems[]  = ['label' => Yii::t('frontend/account', 'Account'),   
					'items' => $items
					
						
						 	
							
			];
		}
	}
	
	
	protected function addLoginMainMenu(){
		if (Yii::$app->user->isGuest) {
			$mainMenuItem= ['label' => Yii::t('frontend/site','Login'), 'url' => ['/site/login']];
		} else {
			$mainMenuItem= [
					'label' => Yii::t('frontend/transfer','Logout'). '(' . Yii::$app->user->identity->username . ')',
					'url' => ['/site/logout'],
					'linkOptions' => ['data-method' => 'post']
			];
		}
		$this->menuItems[] = $mainMenuItem;
	}
	
	
	public function run(){
		$this->addRecepcio();
		$this->addLoginMainMenu();
		return $this->menuItems;
	}
	
	
}