<?php

namespace backend\controllers;

use Yii;
use common\models\TicketInstallmentRequest;
use backend\models\TicketInstallmentRequestSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\TicketInstallmentRequestSearchPending;
use backend\models\TicketInstallmentMarkForSendForm;
use backend\models\TicketInstallmentRequestSearchDownloadGiro;
use backend\models\GiroKotegForm;

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

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

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    
    /**
     * Lists pending TicketInstallmentRequest models.
     * @return mixed
     */
    public function actionPending()
    {
    	$model = new TicketInstallmentMarkForSendForm();
    	if ($model->load(Yii::$app->request->post()) ) {
    		$model->markForSend();
    	}
    	
        $searchModel = new TicketInstallmentRequestSearchPending();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index_pending', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    
    
    public function actionAccept( $id )
    {
    	$model = $this->findModel($id);
    	
    	if ( !$model->isStatusAccepted() ){
    		$model->applyStatus(TicketInstallmentRequest::$STATUS_ACCEPTED_MANUAL,true);
    		\Yii::$app->session->setFlash('success',"Megbízás teljesítve");
    	}
// else{
    		
//     	}
    	
//     	echo "asdff";
    	
    	return $this->redirect(['ticket-installment-request/view', 
    			'id' => $model->id_ticket_installment_request,
    	]);
    }
    /**
     * Lists pending TicketInstallmentRequest models.
     * @return mixed
     */
    public function actionDownloadGiro()
    {
        $model = new GiroKotegForm();
        
        if ($model->load(Yii::$app->request->post()) && $model->validate() ) {
        	$model->createKoteg();
        	return $this->redirect(['ugiro/view', 'id' => $model->koteg->id_ugiro]);
        }
    	
        $searchModel = new TicketInstallmentRequestSearchDownloadGiro();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        
		$model->action = "create";
        return $this->render('index_download_giro', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        	'model' => $model,
        ]);
    }

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

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

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

    /**
     * Updates an existing TicketInstallmentRequest 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_ticket_installment_request]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing TicketInstallmentRequest 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']);
    }
    
    public function actionTest( )
    {
    
    	return $this->render('test');
    }

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