<?php

namespace app\controllers;

use Yii;
use app\models\Voucherref;
use app\models\Allincvoucherreport;
use app\base\Converter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\filters\AccessControl;
use app\models\Clientreport;
use kartik\mpdf\Pdf;


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

    /**
     * Lists all Allincvoucherreport models.
     * @return mixed
     */
    
	
	public function actionIndex()
    {
        $model = new Clientreport();
        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
			//$pid=model->pid;
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                $query = Allincvoucherreport::find()->where(["pid" => $model->pid])->orderBy(["voucherno" => SORT_ASC]);
                $dataProvider = new ActiveDataProvider(['query' => $query,'pagination' => false]);
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

      

   public function actionReport()
    {
        $model = new Clientreport();

        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                 $query = Allincvoucherreport::find()->where(["pid" => $model->pid])->orderBy(["voucherno" => SORT_ASC]);
                $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false,'pagination' => false]);
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

    public function actionPdf()
    {
        $request = Yii::$app->request;
        if ($request->isGet)
            $pid = $request->get('pid');
        if ($request->isPost)
            $pid = $request->post('pid');

       
      
        // print_r($SModel);        
       

		$query = Allincvoucherreport::find()->where(["pid" =>$pid])->orderBy(["voucherno" => SORT_ASC]);
     
        $dataProvider = new ActiveDataProvider(['query' => $query,'pagination' => false]);
        
        $this->layout = "base";
        $content = $this->renderPartial('report', ['dataProvider' => $dataProvider]);
       

        $pdf = new Pdf([
            'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
            'content' => $this->renderPartial('report', ['dataProvider' => $dataProvider]),
            'options' => [
                'title' => 'Incentive Voucher Report',
                // 'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
            ],
            'methods' => [
                // 'SetHeader' => [$title_rght],
                // 'SetFooter' => ['|Page {PAGENO}|'],
            ]
        ]);
        return $pdf->render();

        $pdf = Yii::$app->pdf; // or new Pdf();
        $mpdf = $pdf->api; // fetches mpdf api
        $mpdf->SetAuthor('YRG Care');
        $mpdf->SetTitle('Expense Report');
        $mpdf->SetSubject('TCPDF Tutorial');
        $mpdf->SetKeywords('TCPDF, PDF, example, test, guide');
        
        $pdf->mode = 'c';
        $mpdf->marginTop = 5;

        // set default header data
        // $mpdf->SetHeader("<font size=\"6\">".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</font>");
        $mpdf->WriteHtml($content); // call mpdf write html
        echo $mpdf->Output('filename.pdf', 'I'); // call the mpdf api output as needed
    }
		
  
}
