<?php

namespace backend\controllers;

use Yii;
use common\models\TicketType;
use backend\models\TicketTypeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Account;

/**
 * TicketTypeController implements the CRUD actions for TicketType model.
 */
class TicketTypeController extends \backend\controllers\BackendController
{
	
	
	public function behaviors()
	{
		return [
		'access' => [
			'class' => \yii\filters\AccessControl::className(),
				'rules' => [
					// allow authenticated users
					[
						'actions' => [ 'index','view'],
						'allow' => true,
						'roles' => ['admin','employee','reception'],
					],
					[
						'actions' => ['create' , 'update'],
						'allow' => true,
						'roles' => ['admin' ],
					],
					// everything else is denied
				],
			],
		];
	}
	

    /**
     * Lists all TicketType models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new TicketTypeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single TicketType model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new TicketType model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new TicketType();

        $model->type = TicketType::TYPE_DEFAULT;
        $model->status = TicketType::STATUS_ACTIVE;
        $model->time_unit_type = TicketType::TIME_UNIT_MONTH;
        $model->time_unit_count = 1;
        $model->max_usage_count = 0;
        $accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all();
        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_ticket_type]);
        } else {
            return $this->render('create', [
                'model' => $model,
            	'accounts' => $accounts
            ]);
        }
    }

    /**
     * Updates an existing TicketType model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $accounts = Account::find()->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $model->id_account] ])->all();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_ticket_type]);
        } else {
            return $this->render('update', [
                'model' => $model,
            	'accounts' => $accounts
            ]);
        }
    }


    /**
     * Finds the TicketType model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return TicketType the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = TicketType::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
