<?php
namespace modules\hse\controllers;

use Yii;
use yii\web\Controller;
use modules\forms\models\Forms;
use yii\data\ArrayDataProvider;

/**
 * ExceedanceController
 *
 * Lightweight admin to list exceedances created by the ComplianceChecker.
 */
class ExceedanceController extends Controller
{
    public function actionIndex()
    {
        // Load latest DT058 form entries (virtual model stored in `forms` table)
        $dt058 = Forms::find()->where(['form' => 'dt058'])->orderBy(['created_at' => SORT_DESC])->limit(200)->all();

        // Map DT058 forms into arrays for ArrayDataProvider
        $dtRows = [];
        foreach ($dt058 as $f) {
            $data = is_object($f->data) ? $f->data : null;
            $dtRows[$f->id] = $data;
        }

        $dtProvider = new ArrayDataProvider([
            'allModels' => $dtRows,
            //'sort' => ['attributes' => ['id','date','employee','code']],
            'pagination' => ['pageSize' => 50],
        ]);

        return $this->render('@modules/hse/views/exceedance/index', [
            'dtProvider' => $dtProvider,
        ]);
    }
}
