<?php

namespace app\controllers;

use Yii;
use app\models\Voucherref;
use app\models\Motincenmaster;
use app\models\MotincenmasterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * MotincenmasterController implements the CRUD actions for Motincenmaster model.
 */
class MotincenmasterController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete', 'generate', 'pdf'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

        if(Yii::$app->request->isAjax)
            $this->layout = "base";

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

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

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

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

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

    /**
     * Deletes an existing Motincenmaster 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(['/'.Yii::$app->controller->id]);
    }

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

    public function actionGenerate()
    {
        $model = new Motincenmaster;
        $model->scenario = 'generate';

        if ($model->load(Yii::$app->request->post())) {
            $flag = false;
            $mot_inc_id = explode(',', substr(Yii::$app->request->post()['Motincenmaster']['mot_inc_id'], 0, -1));
            $VModel = Voucherref::generateVoucherno($model->mot_inc_survey, $model->mot_inc_part_id, Voucherref::INCENTIVE);
            foreach ($mot_inc_id as $key => $value) {
                $model = Motincenmaster::find()->where(["mot_inc_id" => $value])->andWhere(["mot_inc_q1" => 0])->one();
                if($model !== null) {
                    $model->mot_inc_q1 = 1;
                    $model->mot_inc_q3 = strtotime(date('Y/m/d'));
                    $model->mot_inc_q4 = $VModel->vchr_ref_loc."-".$VModel->vchr_ref_type."-".$VModel->vchr_ref_code;
                    if($model->save())
                        $flag = true;
                }
            }
            if($flag)
                $VModel->save();
            return $this->redirect(['/'.Yii::$app->controller->id]);
        }
        return $this->render('generate', ['model' => $model]);
    }

    public function actionPdf($id)
    {
        $model = $this->findModel($id);
        $pmodel = ["pid" => $model->mot_inc_part_id, "vno" => $model->mot_inc_q4, "vdt" => $model->mot_inc_q3, "amt" => $model->mot_inc_q2, "summary" => "Motivational Counseling Incentive"];
        return $content = $this->renderPartial('/layouts/print', ['model' => $pmodel]);

        $pdf = Yii::$app->pdf; // or new Pdf();
        $mpdf = $pdf->api; // fetches mpdf api
        $mpdf->WriteHtml($content); // call mpdf write html
        echo $mpdf->Output('filename.pdf', 'I'); // call the mpdf api output as needed
    }
}
