<?php

use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\data\SqlDataProvider;

use app\base\Common;
use kartik\grid\GridView;
use kartik\select2\Select2;
use kartik\widgets\DatePicker;
use rmrevin\yii\fontawesome\FA;

$this->title = Yii::t('app', Common::getMenuname());
$this->params['breadcrumbs'][] = $this->title;
$PIDs = Common::getReportsPIDs();
?>


<div class="attandance-form">
    <h1><?= Html::encode($this->title) ?></h1>
    <div class="panel panel-default">
        <div class="panel-heading">
            
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-lg-12">
                    <?php
                        // $form = ActiveForm::begin(['enableAjaxValidation' => true, 'options' => ['class' => 'form-horizontal']]);
                        $form = ActiveForm::begin(['enableAjaxValidation' => false, 'action' => ['index'], 'method' => 'get', 'options' => ['class' => 'form-horizontal']]);
                        $template = [
                            'labelOptions' => ['class' => 'form-label'],
                            'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-4">{input}{error}</div>',
                        ];
                    ?>
                    <?= $form->field($model, 'pid', $template)->widget(Select2::classname(), ['data' => $PIDs, 'options' => ['prompt' => $model->getAttributeLabel("pid")]]); ?>
                    <div class="form-group">
                        <div class="col-sm-offset-5 col-sm-7">
                            <?= Html::submitButton(Yii::t('app', 'Go'), ['class' =>'btn btn-success']) ?>
                        </div>
                    </div>
                    <?php ActiveForm::end(); ?>
                    <?php if(isset($dataProvider)) : ?>
                        <?= GridView::widget([
                            'dataProvider' => $dataProvider,
                            'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => '-', 'dateFormat' => 'dd-MM-Y','datetimeFormat' => 'dd-MM-Y hh:mm:ss'], 
                            'options'=>['style' => 'white-space:nowrap;'],                         
                            'panel' => ['type' => GridView::TYPE_PRIMARY,'afterOptions'=>['class'=>'grid_panel_remove']], 
                            'export' => [
                                'target' => GridView::TARGET_SELF,
                                'showConfirmAlert' => false,
                                'defaultPagination' => 'all'
                            ],
                           /* 'exportConfig' => [
                                GridView::CSV => [
                                    'label' => Yii::t('app', 'CSV'),
                                    'iconOptions' => ['class' => 'text-primary'],
                                    'showHeader' => true,
                                    'showPageSummary' => true,
                                    'showFooter' => true,
                                    'showCaption' => true,
                                    'filename' => Yii::t('app', 'grid-export'),
                                    'alertMsg' => Yii::t('app', 'The CSV export file will be generated for download.'),
                                    'options' => ['title' => Yii::t('app', 'Comma Separated Values')],
                                    'mime' => 'application/csv',
                                    'config' => [
                                        'colDelimiter' => ",",
                                        'rowDelimiter' => "\r\n",
                                    ],
                                ],
                            ],
							*/
                            'columns' => [
                                ['class' => 'yii\grid\SerialColumn'],
                                // 'lab_smple_part_id',
                                [
                                    'attribute' => 'departments',
                                    'label' => 'Lab Test',
                                    // 'value' => 'tests.department.lab_dept_name',
                                    'value' => function($model) {
                                        return $model->tests->department->lab_dept_name;
                                    },
                                    'group'=>true,
                                    'groupHeader'=>function ($model, $key, $index, $widget) { // Closure method
                                        return [
                                            'mergeColumns'=>[[1,6]], // columns to merge in summary
                                            'content'=>[             // content to show in each summary cell
                                                1=>$model->tests->department->lab_dept_name,
                                            ],
                                            // html attributes for group summary row
                                            'options'=>['class'=>'success','style'=>'font-weight:bold;']
                                        ];
                                    }
                                ],
                                'lab_smple_acc_no',
                                [
                                    'attribute' => 'lab_smple_tst_id',
                                    'value' => 'tests.lab_test_name',
                                ],
                                [
                                    'attribute' => 'lab_smple_spcmn_id',
                                    'value' => 'specimens.lab_spcmn_name',
                                ],
                                [
                                    'attribute' => 'lab_smple_result',
                                    // 'value' => 'testresults.lab_result_name',
                                    'value' => function($model) {
                                        if($model->tests->lab_spot_test)
                                            return $model->testresults->lab_result_name;
                                        else
                                            return $model->lab_smple_result;
                                    },
                                ],
                                [
                                    'attribute' => 'lab_smple_date',
                                   // 'format' => 'date',
                                ],
                                // [
                                //     'attribute' => 'record_date',
                                //     'format' => 'date',
                                // ],
                                // ['class' => 'app\behaviours\ActionColumns'],
                                [
                                    'class' => 'app\behaviours\ActionColumns',
                                    'visible' => (!isset(Yii::$app->request->queryParams['ajaxload']) && !isset(Yii::$app->request->queryParams['ajaxlist'])),
                                    'template' => '<div class="nowrap">{pdf}</div>',
                                    'buttons' => [
                                        'pdf' => function($url, $model, $key) {     // render your custom button
                                            $options = array_merge([
                                                'title' => Yii::t('yii', 'PDF'),
                                                'target' => '_blank',
                                                'aria-label' => Yii::t('yii', 'PDF'),
                                                'data-pjax' => '0',
                                            ]);
                                            $url = Url::toRoute(['pdf', 'id' => $model->lab_smple_acc_no, 'pid' => $model->lab_smple_part_id]);
                                            return Html::a('<span class="glyphicon glyphicon-download-alt"></span>', $url, $options);
                                        }
                                    ]
                                ],
                            ],
                        ]); ?>
                    <?php endif; ?>
                </div>
                <!-- /.col-lg-6 (nested) -->
            </div>
            <!-- /.row (nested) -->
        </div>
        <!-- /.panel-body -->
    </div>
    <!-- /.panel -->
</div>