<?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\UploadForm;
use common\components\Helper;
use common\models\User;
use common\models\Log;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','upload-image'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

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

    public function actionIndex()
    {
        return $this->render('index');
    }

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

        $model = new LoginForm();
        $model->roles = [
        		'admin',
        		'employee'
        ];
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
        	
        	$geoip = Helper::getGeoIp();
        	 
        	$user  = User::findOne(\Yii::$app->user->id);
        	$message = "Bejelentkezés:   " .$user->username. " Ip cím:".  $geoip->ip . " Város: " . $geoip->city;
        	 
        	Log::log([
        			'type' =>Log::$TYPE_LOGIN,
        			'message' => $message
        	]);
        	
        	$this->sendLoginMail();
        	
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    protected function sendLoginMail(){
    	if ( \Yii::$app->params['login_admin_email'] == true){
    		$geoip = Helper::getGeoIp();
    	
    		$user  = User::findOne(\Yii::$app->user->id);
    		$message = \Yii::$app->mailer->compose('login_admin', [
    				'model' => $user,
    				'geoip' => $geoip,
    				'company' => Helper::getCompany()
    		]);
    		 
    		$message->setFrom(  \Yii::$app->params['infoEmail'] )
    		->setTo( \Yii::$app->params['notify_mail'] )
    		->setSubject('Admin bejelentkezés - ' . $user->username )
    		->send();
    	}
    }
    
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
    
    public function actionUploadImage(){
    	\yii::$app->request->enableCsrfValidation = false;
    	$model = new UploadForm();
    	
    	return $this->render('upload', ['model' =>$model]);
    }
}
