<?php

namespace modules\forms;

use Yii;
use yii\base\Event;

/**
 * HSE Fatigue & Risk Calculator Module
 *
 * This module provides HSE-compliant fatigue and risk index calculations
 * for shift work patterns. It can be integrated into any Yii2 application.
 */
class Module extends \yii\base\Module
{
    public $controllerNamespace = 'modules\forms\controllers';

    public function init()
    {
        parent::init();

        // Register translations if needed
        if (!isset(Yii::$app->i18n->translations['forms*'])) {
            Yii::$app->i18n->translations['forms*'] = [
                'class' => 'yii\i18n\PhpMessageSource',
                'sourceLanguage' => 'en-US',
                'basePath' => '@modules/forms/messages',
            ];
        }
        \Yii::$app->urlManager->addRules([
            'forms/<form:[\w\-]+>/<action:[\w\-]+>' => 'forms/forms/<action>',
            '<form:[dt133|dt203]+>/<action:[\w\-]+>' => 'forms/forms/<action>',
            'forms/<action:addsignature>' => 'forms/forms/<action>',
            '<form:[^(?!import).*$]>' => 'forms/index',
        ]);
        Event::on(
            'yii\\web\\View',
            'afteradminButtonsCollect',
            function ($event) {
                $buttons = $event->buttons;
                $buttons['Gauge Corner'] = ['label' => 'Gauge Corner'];
                $buttons['Grinding'] = ['label' => 'Grinding'];
                $buttons['Thermic'] = ['label' => 'Thermic'];
                $buttons['Arc'] = ['label' => 'Arc'];
                $buttons['Inspection'] = ['label' => 'Inspection'];
                $buttons['Other'] = ['label' => 'Other'];
                $buttons['Fault'] = ['label' => 'Fault'];
                $buttons['HSEQ'] = ['label' => 'HSEQ'];
                $buttons['Accidents'] = ['label' => 'Accidents'];
                $buttons['Medical'] = ['label' => 'Medical'];
                $buttons['Administration'] = ['label' => 'Administration'];
                // $event->buttons is a reference, so you can add items:
                foreach(\yii\helpers\FileHelper::findFiles(__DIR__ . '/models', ['only' => ['*.php']]) as $file){
                    $pathinfo = pathinfo($file);
                    $class = "modules\\forms\\models\\{$pathinfo['filename']}";
                    if(method_exists($class, 'adminFormButtons')){
                        $buttons = $class::adminFormButtons($buttons, $event->model);
                    }                   
                }
                $buttons['Accidents']['items'][] = [
                    'label' => 'PDF',
                    'url' => ['/activities/pdfincidentview', 'id' => $event->model->id ],
                    'linkOptions' =>  ['target'=>'_blank'],
                ];
                $buttons['Accidents']['items'][] = [
                    'label' => 'PDF and Send',
                    'url' => ['/activities/pdfincident', 'id' => $event->model->id ],
                    'linkOptions' =>  ['target'=>'_blank'],
                ];
                if(Yii::$app->user->identity->ingroup('Global Admins')){
                    $buttons['Admin'] = [
                        'label' => 'Admin',
                        'items' => [
                            [
                                'label' => 'Send all PDF Jobs',
                                'url' => ['/activities/apdf', 'id' => $event->model->id],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'PDF all Sheets',
                                'url' => ['/activities/apdfview', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'PDF my Sheets',
                                'url' => ['/activities/pdfview', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'Clone',
                                'url' => ['/activities/clone', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'Clone (New Job No)',
                                'url' => ['/activities/clonenewjobno', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'PDF all WITRs',
                                'url' => ['/activities/awasview', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'Send WITR to Client',
                                'url' => ['/activities/awassend', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],
                            [
                                'label' => 'Send WITR to Welders',
                                'url' => ['/activities/awassendwelders', 'id' => $event->model->id ],

                            ],
                            [
                                'label' => 'Cancel Job',
                                'url' => ['/activities/cancelled', 'id' => $event->model->id ],

                            ],
                            [
                                'label' => 'Update Related Jobs',
                                'url' => ['/activities/superupdate', 'id' => $event->model->id ],
                                'linkOptions' =>  ['target'=>'_blank'],
                            ],

                        ]
                        ];
                }
                $event->buttons = $buttons;
            }
        );
    }
}