<?php

namespace app\controllers;

use Yii;
use app\models\Branches;
use app\models\BranchesSearch;
use app\models\Users;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

class BranchesController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => false,
                        'roles' => ['?']
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index', 'create', 'update', 'delete', 'view', 'change-branch-admin'],
                        'roles' => ['Admin']
                    ]
                ],
                'denyCallback' => function(){
                    if(Yii::$app->user->isGuest){
                        return Yii::$app->response->redirect(['/users/login']);
                    }
                    else{
                        return Yii::$app->response->redirect(['/dashboard/index']);
                    }
                }
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionIndex()
    {
        $searchModel = new BranchesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $branches_admins = Users::find()
            ->innerJoin('auth_assignment as a', 'a.user_id = users.id')
            ->where(['a.item_name' => 'Branch Admin'])
            ->orWhere(['a.item_name' => 'Admin'])
            ->all();

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

    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    public function actionCreate()
    {
        $model = new Branches();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        $branches_admins = Users::find()
            ->innerJoin('auth_assignment as a', 'a.user_id = users.id')
            ->where(['a.item_name' => 'Branch Admin'])
            ->orWhere(['a.item_name' => 'Admin'])
            ->all();

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

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        $branches_admins = Users::find()
            ->innerJoin('auth_assignment as a', 'a.user_id = users.id')
            ->where(['a.item_name' => 'Branch Admin'])
            ->orWhere(['a.item_name' => 'Admin'])
            ->all();

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

    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    protected function findModel($id)
    {
        if (($model = Branches::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

    public function actionChangeBranchAdmin($id){
        if(Yii::$app->request->isAjax){
            $data = Yii::$app->request->get();
            $branch_admin = $data['Branches']['branch_user_id'];
            $model = $this->findModel($id);
            if($model){
                $model->branch_user_id = $branch_admin;
                if($model->save()){
                    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                    return ['status' => true, 'data' => Yii::t('app', 'The branch admin has changed successfully')];
                }
                else{
                    var_dump($model->getErrors());
                }
            }
            return ['status' => true, 'error' => Yii::t('app', 'No Such Branch')];
        }
    }
}
