<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Settings;
use app\models\Users;
use yii\filters\AccessControl;

class SettingsController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => false,
                        'roles' => ['?']
                    ],
                    [
                        'allow' => true,
                        'roles' => ['Admin']
                    ],
                ],
                'denyCallback' => function(){
                    if(Yii::$app->user->isGuest){
                        return Yii::$app->response->redirect(['/users/login']);
                    }
                    else{
                        return Yii::$app->response->redirect(['/dashboard/index']);
                    }
                }
            ],
        ];
    }

    public function actionIndex(){
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            if(isset($data['Settings'])){
                $settings = $data['Settings'];
                foreach($settings as $name => $value){
                    $setting_model = Settings::find()->where(['name' => $name])->one();
                    if($setting_model){
                        $setting_model->value = $value;
                        if(! $setting_model->save()){
                            return var_dump($setting_model->getErrors());
                        }
                    }
                }

                Yii::$app->session->setFlash('settings_saved', Yii::t('app', 'Settings Saved'));
                return $this->redirect(['index']);
            }
        }
        
        $settings = Settings::getSettingsArray();
        $model = new Settings();
        $model->capability_to_add_old_interviews = $settings['capability_to_add_old_interviews'];
        $model->capability_to_update_interview_before_hours_number = $settings['capability_to_update_interview_before_hours_number'];
        $model->sending_emails = $settings['sending_emails'];
        $model->training_days_number = $settings['training_days_number'];
        $model->first_weekend_day = $settings['first_weekend_day'];
        $model->second_weekend_day = $settings['second_weekend_day'];
        $model->login_attempt_count = $settings['login_attempt_count'];
        $model->login_attempt_duration_in_seconds = $settings['login_attempt_duration_in_seconds'];
        
        return $this->render('index', [
            'settings' => $settings,
            'model' => $model
        ]);
    }

    public function actionAddUsers(){
        $model = new Users();
        $model->scenario = Users::SCENARIO_ADD_USER;

        if($model->load(Yii::$app->request->post())){
            if($model->save()){
                $role = Yii::$app->request->post()['Users']['_role'];
                $auth = Yii::$app->authManager;
                $auth->assign($auth->getRole($role), $model->id);
                Yii::$app->session->setFlash('user_added', Yii::t('app', 'The user has been added successfully'));
                return $this->redirect(['add-users']);
            }
        }

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

}
