<?php
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;
use backend\models\Profile;
use common\models\User;
use frontend\models\Option;
use yii\data\ActiveDataProvider;
use yii\validators\BooleanValidator;
use yii\validators\StringValidator;
/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['login', 'error'],
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@']
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                    'delete-req' => ['post']
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex()
    {
/*
        if (Yii::$app->user->can('site/index'))
        {
    	   
        }
        else
        {
            Yii::$app->getSession()->setFlash('error', 'Доступ закрыт..');
    
            return $this->redirect('/');
        }
*/
        return $this->render('index');
    }

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

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
    

    /**
     * Displays Profile page.
     *
     * @return mixed
     */
    public function actionProfile()
    { 
        $model = new Profile();
        
        $model->saveProfile(); 

        return $this->render('profile', [
            'model' => $model,
        ]);
    }
 
    public function actionRequests($id = 0) {
    	if($id != 0) {
    		Option::markOld($id);
    		$this->redirect(['site/requests']);
    	}
    	$query = Option::find()->where(['model' => 'Feedback', 'model_id' => 1, 'parent_id' => null]);
    	$provider = new ActiveDataProvider([
    		'query' => $query,
    		'pagination' => [
    			'pageSize' => 2,	
    		],
    		'sort' => [
    			'defaultOrder' => [
    				'created_at' => SORT_DESC
    			]	
    		]
    	]);
    	$data = $provider->getModels();
    	return $this->render('requests', [
    		'dataProvider' => $provider
    	]);
    }
    
    public function actionDeleteReq($id) {
    	$model = Option::findOne($id);
    	if(!is_null($model) && $model->delete()) {
    		return $this->redirect(['site/requests']);
    	} else{
    		Yii::$app->session->setFlash('post_error', $model->getFirstErrors());
    		return $this->redirect('[site/requests]');
    	}		
    }
    
}
