<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\SqlDataProvider;
use yii\data\ActiveDataProvider;

use app\base\Converter;
use app\models\Settings;
use app\models\Labsamplerequest;
use app\models\Clientreport;

use kartik\mpdf\Pdf;

/**
 * LabtestreportController implements the CRUD actions for Clientreport model.
 */
class LabtestreportController 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 Arvmaster models.
     * @return mixed
     */
    public function actionIndex()
    {
        $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 = Labsamplerequest::find()->where(["IS NOT", "lab_smple_resultdate", NULL])->andWhere(["lab_smple_part_id" => $model->pid])->orderBy(["length(lab_smple_part_id)" => SORT_ASC, "lab_smple_part_id" => SORT_ASC, "record_date" => SORT_ASC]);
                $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => 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 = Labsamplerequest::find()->where(["corfrm_q12" => $model->reptype]);
                $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]);
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

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

        // $query = Labsamplerequest::find()->where(["IS NOT", "lab_smple_resultdate", NULL])->andWhere(["lab_smple_acc_no" => $id, "lab_smple_part_id" => $pid])->orderBy(["length(lab_smple_part_id)" => SORT_ASC, "lab_smple_part_id" => SORT_ASC, "record_date" => SORT_ASC]);
        $SModel = Settings::find()->one();
        // print_r($SModel);
        
        $stngs_app_name = $SModel->stngs_app_name;
        $stngs_org_name = $SModel->stngs_org_name;
        $stngs_org_addrs = $SModel->stngs_org_addrs;
        $stngs_org_phone = $SModel->stngs_org_phone;
        $stngs_org_mail = $SModel->stngs_org_mail;
        $stngs_org_website = $SModel->stngs_org_website;

        $query = Labsamplerequest::find()->where(["lab_smple_acc_no" => $id, "lab_smple_part_id" => $pid])->orderBy(["length(lab_smple_part_id)" => SORT_ASC, "lab_smple_part_id" => SORT_ASC, "record_date" => SORT_ASC]);
        // $query = Labsamplerequest::find()->where(["corfrm_q12" => $model->reptype]);
        $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]);
        // return $content = $this->renderPartial('/layouts/print', ['model' => $model]);
        $this->layout = "base";
        $content = $this->renderPartial('report', ['dataProvider' => $dataProvider]);
        // return $content;
        // echo count($model);

        $pdf = new Pdf([
            'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
            'content' => $this->renderPartial('report', ['dataProvider' => $dataProvider]),
            'options' => [
                'title' => 'Lab 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
    }

}
