<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model modules\forms\models\Dt135 */

$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'DT135', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<style>
.alert {
    color: red;
}
</style>
<div class="dt135-view">
    <table class="table table-borderless">
        <thead>
            <tr>
                <td colspan="8" rowspan="2" class="p-0" style="padding:none;">
                    <?= $this->render('//common/_viewheader', ['title' => 'Hand Arm Vibration – Tool Usage Assessment Form DT135']) ?>
                </td>
                <td colspan="3" class="DarkerBlue">Pad no</td>
            </tr>
            <tr>
                <td colspan="3"><?= $model->padno ?></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td colspan="4" class="DarkerBlue">Name:</td>
                <td colspan="4" class="DarkerBlue">Position:</td>
                <td colspan="3" class="DarkerBlue">In Job No:</td>
            </tr>
            <tr>
                <td colspan="4"><?= $model->user ? $model->user->username : false ?></td>
                <td colspan="4"><?= $model->position ?></td>
                <td colspan="3"><?= $model->jobno ?></td>
            </tr>
            <tr>
                <td colspan="4" class="DarkerBlue">Location:</td>
                <td colspan="4" class="DarkerBlue">Works Description:</td>
                <td colspan="3" class="DarkerBlue">Date:</td>
            </tr>
            <tr>
                <td colspan="4" ><?= !empty($model->location) ? $model->location : '&nbsp;' ?></td>
                <td colspan="4" ><?= $model->description ?></td>
                <td colspan="3" ><?= $model->date ?></td>
            </tr>
            <tr>
                <tr>
                    <th rowspan="2"  class="DarkerBlue">Operator</th>
                    <th rowspan="2" class="DarkerBlue">Tool</th>
                    <th colspan="2" class="DarkerBlue">Vibration Magnitude m/s² A(8)</th>                    
                    <th rowspan="2" class="DarkerBlue">Task Points per Hour</th>
                    <th rowspan="2" class="DarkerBlue">Time to reach EAV hh:mm</th>
                    <th rowspan="2" class="DarkerBlue">Time to reach ELV hh:mm</th>
                    <th colspan="2" class="DarkerBlue">Exposure Duration</th>
                    <th rowspan="2" class="DarkerBlue">Partial Exposure m/s² A(8)</th>
                    <th rowspan="2" class="DarkerBlue">Partial Exposure Points</th>
                </tr>
                <tr>
                    <th class="DarkerBlue">HSE</th>
                    <th class="DarkerBlue">User</th>
                    <th class="DarkerBlue">Hours</th>
                    <th class="DarkerBlue">Minutes</th>
                </tr>
            </tr>
<?php 
                        $currentOperator = null;
                        $operatorTotal = 0;
                        $grandTotal = 0;
                        foreach($model->rowOperator as $x => $array) { 
                            $operator = $model->rowOperator[$x];
                            
                            // If operator changed, show subtotal for previous operator
                            if ($currentOperator !== null && $currentOperator !== $operator) {
                                ?>
                                <tr class="table-secondary">
                                    <td rowspan="2" colspan="9" class="text-right font-weight-bold">Subtotal for <?= $currentOperator ?>:</td>
                                    <th>Daily Exposure m/s² A(8)</th>
                                    <th>Daily Exposure points</th>
                                <tr class="table-secondary">
                                    <td class="font-weight-bold operator-exposure-subtotal" data-operator="<?= htmlspecialchars($currentOperator) ?>">0.0</td>
                                    <td class="font-weight-bold operator-subtotal" data-operator="<?= htmlspecialchars($currentOperator) ?>"><?= $operatorTotal ?></td>
                                </tr>
                                <?php
                                $operatorTotal = 0;
                            }
                            
                            $currentOperator = $operator;
                        ?>
                            <tr class="datarows" data-operator="<?= htmlspecialchars($operator) ?>">
                                <td>
                                    <?= $operator ?>
                                </td>
                                <td>
                                    <?= $model->rowToolname[$x] ?>
                                </td>
                                <td>
                                    <?= $model->rowMagnitudeHSE[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowMagnitudeUser[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowpph[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowEAV[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowELV[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowhours[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowminutes[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowtoolms2[$x] ?> 
                                </td>
                                <td>
                                    <?= $model->rowpoints[$x] ?> 
                                </td>
                            </tr>
                            <?php 
                            $points = isset($model->rowpoints[$x]) ? $model->rowpoints[$x] : 0;
                            $operatorTotal += $points;
                            $grandTotal += $points;
                        } 
                        
                        // Show subtotal for last operator
                        if ($currentOperator !== null) {
                            ?>
                            <tr class="table-secondary">
                                <td colspan="9" class="font-weight-bold">Subtotal for <?= $currentOperator ?>:</td>
                                <th>Daily Exposure m/s² A(8)</th>
                                <th>Daily Exposure points</th>
                            </tr>
                            <tr class="table-secondary">
                                <td colspan="2"> Operator Signature </td>
                                <td colspan="7"><?= Html::Img($model->rowSign[$x]) ?></td>
                                <td class="font-weight-bold operator-exposure-subtotal" data-operator="<?= htmlspecialchars($currentOperator) ?>">0.0</td>
                                <td class="font-weight-bold operator-subtotal" data-operator="<?= htmlspecialchars($currentOperator) ?>"><?= $operatorTotal ?></td>
                            </tr>
                        <?php } ?>
            <tr>
                <td colspan="3"  class="DarkerBlue text-right">Exposure Calculation by:</td>
                <td colspan="3" class="text-center">G Warren & S Thomson</td>
                <td rowspan="2" colspan="3" class="DarkerBlue">Calculation Date</td>
                <td rowspan="2" colspan="2"><?= $model->created_at ?></td>
            </tr>
            <tr>
                <td colspan="3" class="DarkerBlue text-right">Job Role:</td>
                <td colspan="3" class="text-center">HSQE & Compliance Manager & MD</td>
            </tr>
        </tbody>
    </table>
</div>
<style>
.dt135-view td, dt135-view th{
    padding: 5px 9px;
}    
</style>