<?php

namespace modules\custom\controllers;

use Yii;
use modules\custom\models\Dt078be;
use modules\custom\models\Dt078beSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Mpdf\Mpdf;
/**
 * Dt078Controller implements the CRUD actions for dt078be model.
 */
class Dt078Controller extends Controller
{
    /**
     * @inheritdoc
     */

   public function behaviors()
    {
        return [
			'access' => [
                'class' => \yii\filters\AccessControl::class,
                'only' => ['create', 'update', 'view', 'pdf'],
                'rules' => [
                
                    [
                        'actions' => ['create', 'update', 'view', 'pdf'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                            return yii::$app->user->identity->useradv->accesslevel >= 2;
                        }
                    ],
                    [
                        'actions' => ['index', 'deleted'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                            return yii::$app->user->identity->useradv->accesslevel >= 7;
                        }
                    ],
                ],
				'denyCallback' => function ($rule, $action) {
					return	$this->redirect(['site/accessdenied']);
			   
			}
            ],
			'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    
	public function beforeAction($action) {
   		\frontend\models\Audit::qcreate(['id' => yii::$app->request->get('id', null), 'controller' =>  Yii::$app->controller->id,  'action' => Yii::$app->controller->action->id,  'attributes' => null]);
        return parent::beforeAction($action);
	}
    /**
     * Lists all dt078be models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new dt078besearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('@backend/views/dt078/index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
     /**
     * Lists Deleted dt078be models.
     * @return mixed
     */
    public function actionDeleted() {
        $searchModel = new dt078besearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

        return $this->render('@backend/views/dt078/index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

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

	public function actionPdf($id) {
	 	$pdf = new mPDF([
	        'mode' => 'utf-8', //Pdf::MODE_CORE, // leaner size using standard fonts
	    	'format' => 'A4', 
	        'margin_top' => 7.5,
	        'margin_header' => 0,
	        'margin_left' => 5,
	        'margin_right' => 5,
	        'margin_footer' => 0,
	        'margin_bottom' => 1,
	        'default_font_size' => 9,
	        'default_font' => "Gill Sans MT",      
	    ]);
	    $stylesheet = file_get_contents('css/PDF179.css');
	    $pdf->setTitle('Inline Track Welding Limited - Medication Declaration Form (DT078)');
	    $pdf->setSubject('DT078');
		$pdf->WriteHTML($stylesheet,1);
	    $pdf->WriteHTML($this->renderPartial('@backend/views/dt078/_view', ['model' => $this->findModel($id)]));
	    return $pdf->output();
	}
    /**
     * Creates a new dt078be model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new dt078be();
        $model->status = 'Active';
		$employees = \frontend\models\User::Employees();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = Dt078be::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
        
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('@backend/views/dt078/create', [
                'model' => $model,
               	'employees' => $employees,
            ]);
        }
    }

    /**
     * Updates an existing dt078be model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
		$employees = \frontend\models\User::Employees();

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

    /**
     * Deletes an existing dt078be model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id) {
		$model = $this->findModel($id);
		$model->status = 'Deleted';
		$model->deleted_at = strtotime('now');
		$model->save();
        return $this->redirect(['index']);
    }

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