<?php
namespace modules\activities\models\Customs;
use yii;
use common\models\DynamicModel;
use modules\users\models\User;

class Fatigue extends DynamicModel{

    public static function localAttributes(){
        return [
            'user' => [
                'rules' => [['string', 'max' => 50]],
                'label' => 'User ID',
                 'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => [
                        'label' => false, 'type' => 'number', 'placeholder' => 'e.g., 20'
                    ],
                    'width' => 'col-12 d-none',
                ],
            ],
            'startdate' => [
                'rules' => [['string', 'max' => 50]],
                'label' => 'Start Time',
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'datetime', 'placeholder' => 'e.g., 20'],
                    'width' => 'col-12 col-md-6',
                ],
            ],
            'enddate' => [
                'rules' => [['string', 'max' => 50]],
                'label' => 'End Time',
                 'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'datetime', 'placeholder' => 'e.g., 20'],
                    'width' => 'col-12 col-md-6',
                ],
            ],
            'workload_pace' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is the pace of the work?"),
                'input' => [
                    'input' => 'dropdownlist',
                    'fieldclass' => null,
                    'fieldlist' => [
                        'Very undemanding, plenty of spare capacity.' => 'Very undemanding, plenty of spare capacity.',
                        'Moderately undemanding, some spare capacity.' => 'Moderately undemanding, some spare capacity.',
                        'Moderate to demanding, little spare capacity.' => 'Moderate to demanding, little spare capacity.',
                        'Very demanding, no spare capacity.' => 'Very demanding, no spare capacity.',
                    ],
                    'options' => ['label' => false, 'prompt' => 'Please Select'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'continuous_attention' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Does the work require continuous attention?"),
                'input' => [
                    'input' => 'dropdownlist',
                    'fieldclass' => null,
                    'fieldlist' => [
                        'Rarely' => 'Rarely',
                        'Some of the time' => 'Some of the time',
                        'Frequent' => 'Frequent',
                        'Continuous' => 'Continuous',
                    ],
                    'options' => ['label' => false, 'prompt' => 'Please Select'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'rest_break_frequency_hours' => [
                'rules' => [
                    ['number']
                ],
                'label' => Yii::t('app', "Rest break frequency (hours)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'step' => '0.5', 'placeholder' => 'e.g., 3.0'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'rest_break_average_length_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Rest break average length (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 25'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'longest_continuous_work_hours' => [
                'rules' => [
                    ['number']
                ],
                'label' => Yii::t('app', "Longest continuous work period (hours)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'step' => '0.5', 'placeholder' => 'e.g., 2.0'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'break_after_longest_period_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Break after longest period (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 45'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'commuting_time_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Typical commuting time (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 20'],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
            'commuting_multiplier' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Commuting multiplier"),
                'hint' => Yii::t('app', "1 = one-way trip, 2 = return trip (to and from work)"),
                'input' => [
                    'input' => 'radioList',
                    'fieldclass' => null,
                    'fieldlist' => [
                        1 => 'One-way (×1)',
                        2 => 'Return trip (×2)',
                    ],
                    'options' => ['label' => false],
                    'width' => 'col-12 col-md-6',
                ],
                'search' => false,
            ],
        ];
    }
    public static function afterLoad($data){
        $output = [];
        if(!empty($data->welder)){
            foreach(explode(',', $data->welder) as $key => $welder){
                $Model = new self();
                $jdata = json_decode($data->data, true);
                if (!empty($jdata) && (array_key_exists('Fatigue', $jdata)) && array_key_exists($welder, $jdata['Fatigue'])) {
                    $Model->load($jdata['Fatigue'][$welder], '');
                } else {
                    $Model->user = $welder;
                    $Model->startdate = $data->sdate;
                    $Model->enddate = $data->edate;
                

                }
                $user = User::find()->where(['username' => $welder])->one();
                if ($user){
                    $fatigue = $user->data['FatigueRisk'] ?? null;
                    if($fatigue){
                        empty($Model->workload_pace) ? $Model->workload_pace = $fatigue->workload_pace : false;
                        empty($Model->continuous_attention) ? $Model->continuous_attention = $fatigue->continuous_attention : false;
                        empty($Model->commuting_time_minutes) ? $Model->commuting_time_minutes = $fatigue->typical_commuting_time_minutes : false;
                        empty($Model->rest_break_frequency_hours) ? $Model->rest_break_frequency_hours = $fatigue->rest_break_frequency_hours: false;
                        empty($Model->rest_break_average_length_minutes) ? $Model->rest_break_average_length_minutes = $fatigue->rest_break_average_length_minutes: false;
                        empty($Model->longest_continuous_work_hours) ? $Model->longest_continuous_work_hours = $fatigue->longest_continuous_work_hours: false;
                        empty($Model->break_after_longest_period_minutes) ? $Model->break_after_longest_period_minutes = $fatigue->break_after_longest_period_minutes: false;
                        //empty($Model->commuting_time_minutes ) ? $Model->typical_commuting_time_minutes  = $fatigue->typical_commuting_time_minutes : false;
                        empty($Model->commuting_multiplier) ? $Model->commuting_multiplier = $fatigue->commuting_multiplier: false;

                    }
                }
                $output[$welder] = $Model;
            }
        }
        return $output;
    }
}
