<?php

use \common\widgets\GridView;

?>
<div class="calculator-index">

    <h1>HSE Calculator</h1>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'format' => 'raw', 
                'header' => '',
                'value' => function($model) { 
                    return Html::A(Html::tag('span', '', ['class' => 'fa fa-eye']), ['activities/view', 'id' => is_array($model) ? $model['id'] : $model->id], ['target' => '_blank']); 
                }, 
        ],
            'date',
            'start_time',
            'end_time',
            [
                'attribute' => 'duty_length',
                'label' => 'Total Duty Length (Hrs)',
                'value' => function($model) {
                    // Handle both array and object access
                    $dutyHours = is_array($model) ? ($model['duty_length'] ?? 0) : ($model->duty_length ?? 0);
                    $commuteMinutes = is_array($model) ? ($model['commutetime'] ?? 0) : ($model->commutetime ?? 0);
                    $commuteMinutes = empty($commuteMinutes) ? 0 : $commuteMinutes;
                    // Add commute time * 2 (to/from work) converted to hours
                    $totalHours = $dutyHours + (($commuteMinutes * 2) / 60);
                    
                    // Format as hours and minutes
                    $hours = floor($totalHours);
                    $minutes = round(($totalHours - $hours) * 60);
                    
                    return $hours . 'h ' . $minutes . 'm';
                },
                'contentOptions' => function($model) {
                    // Handle both array and object access
                    $dutyHours = is_array($model) ? ($model['duty_length'] ?? 0) : ($model->duty_length ?? 0);
                    $commuteMinutes = is_array($model) ? ($model['commutetime'] ?? 0) : ($model->commutetime ?? 0);
                    $commuteMinutes = empty($commuteMinutes) ? 0 : $commuteMinutes;
                    // Add commute time * 2 (to/from work) converted to hours
                    $totalHours = $dutyHours + (($commuteMinutes * 2) / 60);
                    
                    // Color coding: >14hrs RED, >12hrs AMBER
                    if ($totalHours > 14) {
                        return ['style' => 'background-color: #ffcccc; color: #cc0000; font-weight: bold;'];
                    } elseif ($totalHours > 12) {
                        return ['style' => 'background-color: #ffe6cc; color: #ff8800; font-weight: bold;'];
                    }
                    return [];
                },
            ],
            'work_intensity',
            'commutetime',
        ],
    ]); ?>
</div>