<?php

namespace backend\controllers;

use backend\models\DoorMoveForm;
use common\components\Helper;
use common\models\Card;
use common\models\Ticket;
use Yii;
use common\models\DoorLog;
use backend\models\DoorLogSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * DoorLogController implements the CRUD actions for DoorLog model.
 * 
 * TODO: FIX ACCESS
 */
class DoorLogController extends Controller
{
	public function behaviors()
	{
		return [
				'access' => [
						'class' => \yii\filters\AccessControl::className(),
						'rules' => [
								// allow authenticated users
								[
										'actions' => [  ],
										'allow' => true,
										'roles' => ['admin','employee','reception'],
								],
								// everything else is denied
						],
				],
		];
	}

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

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

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

    public function actionIn( )
    {
        $model = new DoorMoveForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate() ) {
            $log = new DoorLog();
            //5559	9719

            $log->id_card = 9719;
            $log->id_customer = 5559;
            $log->id_ticket_current = $model->id_ticket;
            $log->direction = 3;
            $log->id_key = 1;
            $log->type = 0;
            $log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN;
            $log->id_account = null;
            $log->card_flag = 0;
            $log->flag_out = 0;

            $log->save(false);
            Helper::flash("success", "Belépett " .$model->id_ticket);
            return $this->redirect(['in']);
        }else{
            $model->id_ticket = $this->findTicket();
            return $this->render('move', [
                'model' => $model
            ]);
        }
    }

    public function actionOut()
    {
        $model = new DoorMoveForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate() ) {
            $log = new DoorLog();
            $log->id_card = 9719;
            $log->id_customer = 5559;
            $log->id_ticket_current = $model->id_ticket;
            $log->direction = 1;
            $log->id_key = null;
            $log->type = 0;
            $log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN;
            $log->id_account = null;
            $log->card_flag = 0;
            $log->flag_out = 0;

            $log->save(false);
            Helper::flash("success", "Kilépett " .$model->id_ticket );
            return $this->redirect(['out']);
        }else{
            $model->id_ticket = $this->findTicket();
            return $this->render('move', [
                'model' => $model
            ]);
        }
    }

    private function findTicket(){
        $idCard = 9719;
        $card = Card::findOne($idCard);
        $tickets = Ticket::readActive($card);
        if ( count($tickets) > 0 ){
            return $tickets[0]->id_ticket;
        }
        return "";
    }

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

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

    /**
     * Updates an existing DoorLog 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);

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

    /**
     * Deletes an existing DoorLog model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

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

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