<?php
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use backend\models\ChangePasswordForm;
use backend\models\User;
use yii\filters\VerbFilter;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','changepassword'],
                        '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();
        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();
    }
    public function actionChangepassword()
    {
        $model=new ChangePasswordForm();
        if(isset($_POST['ChangePasswordForm']))
        {
            $currentPassword=$_POST['ChangePasswordForm']['currentPassword'];
            $newPassword=$_POST['ChangePasswordForm']['newPassword'];
            $user_id = Yii::$app->user->id;
            $ha = User::find()->where('id ='.$user_id)->one();
            if(Yii::$app->getSecurity()->validatePassword($currentPassword,$ha->password_hash))
            {
                $hash= Yii::$app->getSecurity()->generatePasswordHash($currentPassword);
                if(!Yii::$app->getSecurity()->validatePassword($newPassword,$hash))
                {
                    $ha->password_hash = Yii::$app->getSecurity()->generatePasswordHash($newPassword);
                    $ha->save(false);
                    Yii::$app->user->logout();
                    $this->redirect('login');
                }
                else
                {
                    Yii::$app->getSession()->setFlash('error', '<p style="color:red">'.'New Password same Current Password!'.'</p>');
                    return $this->redirect('changepassword');
                }
            }
            else
            {
                Yii::$app->getSession()->setFlash('error', '<p style="color:red">'.'Incorrect Password!'.'</p>');
                return $this->redirect('changepassword');
            }
        }
        return $this->render('changepassword', [
            'model' => $model,
        ]);
    }
}
