<?php

use modules\hse\models\{RiskCalculator};
use modules\hse\models\EmployeeDefaults;
use yii\helpers\Html;
use yii\widgets\Pjax;

/**
 * HSE Risk Index Calculator - Module View
 */

$this->title = 'HSE Risk Index Calculator - Module';

echo "<h1>HSE Risk Index Calculator - Module</h1>\n\n";

// Employee Default Settings (for display) - passed from controller
// $employeeDefaults, $datasetMap, $selectedDataset, $analysis are available as local variables

Pjax::begin(['id' => 'dataset-pjax', 'timeout' => 5000]);
echo Html::beginForm(['calculator/risk'], 'get', ['data-pjax' => 1, 'style' => 'margin:10px 0;']);
echo Html::label('Employee', 'user');
echo " ";
echo Html::dropDownList('user', $selectedUser, $users, [
    'id' => 'user',
    'prompt' => 'Select Employee',
    'class' => 'form-control',
    'onchange' => 'this.form.submit()'
]);
echo " &nbsp; ";
echo Html::label('Start Date', 'start_date');
echo " ";
echo Html::input('date', 'start_date', $startDate, ['class' => 'form-control', 'style' => 'display:inline-block; width:auto;', 'onchange' => 'this.form.submit()']);
echo " &nbsp; ";
echo Html::label('End Date', 'end_date');
echo " ";
echo Html::input('date', 'end_date', $endDate, ['class' => 'form-control', 'style' => 'display:inline-block; width:auto;', 'onchange' => 'this.form.submit()']);
echo Html::endForm();

// Display Employee Defaults
echo "<h3>Employee Default Settings:</h3>\n";
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%; margin-bottom: 20px;'>\n";
echo "<tr style='background: #e9ecef;'><th style='text-align: left;'>Setting</th><th style='text-align: left;'>Default Value</th></tr>\n";
echo "<tr><td>The workload and/or work pace of the job is typically:</td><td>{$employeeDefaults['workload_pace']}</td></tr>\n";
echo "<tr><td>The job typically requires continuous attention:</td><td>{$employeeDefaults['continuous_attention']}</td></tr>\n";
echo "<tr><td>How frequently are rest breaks typically provided OR taken?</td><td>{$employeeDefaults['rest_break_frequency']}</td></tr>\n";
echo "<tr><td>What is the typical average length of these breaks that are provided or taken?</td><td>{$employeeDefaults['rest_break_average_length']}</td></tr>\n";
echo "<tr><td>What is typically the longest period of continuous work before a break?</td><td>{$employeeDefaults['longest_continuous_work_period']}</td></tr>\n";
echo "<tr><td>What is typically the length of the break taken after this longest period of continuous work?</td><td>{$employeeDefaults['break_after_longest_period']}</td></tr>\n";
echo "<tr><td>What is the typical commuting time of employees to OR from work.</td><td>{$employeeDefaults['typical_commuting_time']}</td></tr>\n";
echo "</table>\n\n";

// Display HSE-formatted table matching screenshot
echo "<h3>HSE Risk Index Calculator - Full Report</h3>\n";
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%; font-size: 12px;'>\n";
echo "<thead style='background: #e9ecef;'>\n";
echo "<tr>\n";
echo "<th>Date</th>\n";
echo "<th>On Duty</th>\n";
echo "<th>Off Duty</th>\n";
echo "<th>Job type / breaks</th>\n";
echo "<th>Commuting Time</th>\n";
echo "<th>Duty Length</th>\n";
echo "<th>Total Duty Length</th>\n";
echo "<th>Rest Length</th>\n";
echo "<th>Average duty per day</th>\n";
echo "<th>Cumulative component</th>\n";
echo "<th>Duty timing component</th>\n";
echo "<th>Job type / Breaks component</th>\n";
echo "<th style='background: #dc3545;'>Risk Index</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";

foreach ($analysis['shift_analysis'] as $shift) {
    // Color code risk index cell
    $bgColor = '#ffffff';
    if ($shift['risk_index'] >= 15) {
        $bgColor = '#ffebee'; // Red for high risk
    } elseif ($shift['risk_index'] >= 10) {
        $bgColor = '#fff3e0'; // Orange for medium risk
    } elseif ($shift['risk_index'] >= 5) {
        $bgColor = '#fffde7'; // Yellow for moderate risk
    }
    
    // Color code total duty length cell - >14hrs RED, >12hrs AMBER
    $totalDutyBgColor = '#ffffff';
    $totalDutyTextColor = '#000000';
    // Extract hours from "Xh Ym" format
    if (preg_match('/(\d+)h/', $shift['total_duty_length'], $matches)) {
        $totalDutyHours = (int)$matches[1];
        if (preg_match('/(\d+)m/', $shift['total_duty_length'], $mmatches)) {
            $totalDutyHours += (int)$mmatches[1] / 60;
        }
        if ($totalDutyHours > 14) {
            $totalDutyBgColor = '#ffcccc';
            $totalDutyTextColor = '#cc0000';
        } elseif ($totalDutyHours > 12) {
            $totalDutyBgColor = '#ffe6cc';
            $totalDutyTextColor = '#ff8800';
        }
    }
    
    echo "<tr>\n";
    echo "<td>{$shift['date']}</td>\n";
    echo "<td>{$shift['on_duty']}</td>\n";
    echo "<td>{$shift['off_duty']}</td>\n";
    echo "<td>{$shift['job_type_breaks']}</td>\n";
    echo "<td>{$shift['commuting_time']}</td>\n";
    echo "<td>{$shift['duty_length']}</td>\n";
    echo "<td style='background: {$totalDutyBgColor}; color: {$totalDutyTextColor}; font-weight: bold;'>{$shift['total_duty_length']}</td>\n";
    echo "<td>{$shift['rest_length']}</td>\n";
    echo "<td>{$shift['average_duty_per_day']}</td>\n";
    echo "<td>{$shift['cumulative_component']}</td>\n";
    echo "<td>{$shift['duty_timing_component']}</td>\n";
    echo "<td>{$shift['job_type_breaks_component']}</td>\n";
    echo "<td style='background: {$bgColor}; font-weight: bold;'>{$shift['risk_index']}</td>\n";
    echo "</tr>\n";
}

echo "</tbody>\n";
echo "</table>\n";

Pjax::end();
