<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\MainCategories;
use app\models\SubCategories;
use app\models\Items;
use app\models\SystemConfigurations;
use app\models\Branches;
use app\models\SpecialItems;

class MenuController extends Controller
{
    public $layout = 'menu';   
    
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function info() { $logo = SystemConfigurations::find()->where(['config_option' => 'logo'])->one()->value;
$branches = Branches::find()->all();
$twitter_link = SystemConfigurations::find()->where(['config_option' => 'twitter_link'])->one()->value;
$facebook_link = SystemConfigurations::find()->where(['config_option' => 'facebook_link'])->one()->value;
$messenger_link = SystemConfigurations::find()->where(['config_option' => 'messenger_link'])->one()->value;
$instagram_link = SystemConfigurations::find()->where(['config_option' => 'instagram_link'])->one()->value;
$whatsapp_link = SystemConfigurations::find()->where(['config_option' => 'whatsapp_link'])->one()->value;
$lang = Yii::$app->language;
if($lang == 'en')
$currency = SystemConfigurations::find()->where(['config_option' => 'currency'])->one()->value;
else
    $currency = SystemConfigurations::find()->where(['config_option' => 'currency_ar'])->one()->value;
return ['logo' => $logo, 'branches' => $branches, 'twitter_link' => $twitter_link, 'facebook_link' => $facebook_link, 'instagram_link' => $instagram_link, 'whatsapp_link' => $whatsapp_link, 'messenger_link' => $messenger_link, 'currency' => $currency,];
} public function actionHome() { $info = $this->info();
return $this->render('home', ['info' => $info,]);
} public function actionAbout() { $info = $this->info();
$restaurant_image = SystemConfigurations::find()->where(['config_option' => 'restaurant_image'])->one()->value;
$info['restaurant_image'] = $restaurant_image;
$arabic_about = SystemConfigurations::find()->where(['config_option' => 'arabic_about'])->one()->value;
$english_about = SystemConfigurations::find()->where(['config_option' => 'english_about'])->one()->value;
$info['arabic_about'] = $arabic_about;
$info['english_about'] = $english_about;
return $this->render('about', ['info' => $info]);
} public function actionCategories() { $categories = MainCategories::find()->where(['status' => 'Active'])->all();
$info = $this->info();
$info['categories'] = $categories;
return $this->render('categories', ['info' => $info]);
} public function actionSubCategories($cate_id) { $category = MainCategories::findOne($cate_id);
if ($category->status == "Active") { $subs = $category->subCategories;
$categories = MainCategories::find()->where(['status' => 'Active'])->all();
$info = $this->info();
$info['categories'] = $categories;
$info['subs'] = $subs;
$info['category'] = $category;
return $this->render('sub-categories', ['info' => $info]);
} else { return $this->redirect(['home']);
} } public function actionItems($cate_id, $sub_id = null) { $main_category = MainCategories::findOne($cate_id);
if ($main_category) { if($sub_id){ $sub_category = SubCategories::findOne($sub_id);
if ($main_category->status != "Active" || $sub_category->status != "Active") { return $this->redirect(['home']);
} } if ($main_category->status == "Active") { $special_item = SpecialItems::find()->where(['main_category_id' => $cate_id])->one();
if ($special_item) { $special_item = $special_item->item->status == 'Active' ? $special_item->item : null;
} else { $special_item = SpecialItems::find()->where(['sub_category_id' => $sub_id])->one();
if ($special_item) { $special_item = $special_item->item->status == 'Active' ? $special_item->item : null;
} } $items = Items::find()->where(['main_category_id' => $cate_id])->andwhere(['sub_category_id' => $sub_id])->andWhere(['status' => 'Active'])->orderBy(['is_new' => SORT_DESC])->all();
$categories = MainCategories::find()->where(['status' => 'Active'])->all();
$main_category = MainCategories::findOne($cate_id);
$sub_category = SubCategories::findOne($sub_id);
$info = $this->info();
$info['categories'] = $categories;
$info['items'] = $items;
$info['special_item'] = $special_item;
$info['main_category'] = $main_category;
$info['sub_category'] = $sub_category;
return $this->render('items', ['info' => $info]);
} } else { return $this->redirect(['home']);
} } 
public function actionItem($id) { 
	$item = Items::findOne($id);
	$main_category = MainCategories::findOne($item->main_category_id);
		if ($main_category) { 
			$sub_category = SubCategories::findOne($item->sub_category_id);
			if ($sub_category) {
				if ($main_category->status != "Active" || $sub_category->status != "Active") {
					return $this->redirect(['home']);
				} 
			} 
			if ($main_category->status == "Active") { 
					$categories = MainCategories::find()->where(['status' => 'Active'])->all();
					$images = $item->getItemImages()->orderBy(['sort_order' => SORT_ASC])->all();
					$info = $this->info();
					$info['categories'] = $categories;
					$info['item'] = $item;
					$info['images'] = $images;
				if($item) {
					return $this->render('item', ['info' => $info]);
				} 
			} 
		} 
		else { 
			return $this->redirect(['home']);
		} 
	} 
public function actionBranches() { $info = $this->info();
return $this->render('branches', ['info' => $info]);
}
}
