<?php

namespace modules\forms\models;

use Yii;
use modules\users\models\User;
use modules\forms\models\Dt135rows;

use modules\activities\models\Activities;
use modules\custom\models\Accounts;
use yii\base\Model;
/**
 * This is the model class For table "dt135".
 *
 * @property string $id
 * @property string $actid
 * @property string $position
 * @property string $jobno
 * @property string $location
 * @property string $description
 * @property string $date
 * @property string $operatorname
 * @property string $osign
 * @property string $hseqname
 * @property string $hseqsign
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 */
class Dt135 extends Model {

    public $id;
    public $actid;
    public $position;
    public $jobno;
    public $location;
    public $description;
    public $date;
    public $operatorname;
    public $osign;
    public $hseqname;
    public $hseqsign;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $deleted_at;
    public $deleted_by;

    public $updated_by2;
    public $created_by2;
    public $deleted;


	public $rowid = [];
    public $rowdt135id = [];
    public $rowactid = [];
    public $rowjobno = [];
    public $rowpadno = [];
    public $rowstatus = [];
    public $rowtooltype = [];
    public $rowrunningtotal = [];
    public $rowpph = [];
    public $rowtooltypeid = [];
    public $rowToolname = [];
    public $rowtimestarted = [];
    public $rowtimestopped = [];
    public $rowminutes = [];
    public $rowhours = [];
    public $rowOperator = [];
    public $rowpoints = [];
    public $rowtooltypetemp = [];
    public $rowdeleted = [];
    public $rowcreated_at = [];
    public $rowcreated_by = [];
    public $rowupdated_at = [];
    public $rowupdated_by = [];
    public $rowdeleted_at = [];
    public $rowdeleted_by = [];
    public $rowupdated_by2 = [];
    public $rowcreated_by2 = [];
    public $rowpph_old = [];
    public $rowpoints_old = [];
    public $rowrunningtotal_old= [];
    public $rowrelatedid= [];
    public $rowtoolms2= [];
    public $rowEAV = [];
    public $rowELV = [];
    public $rowMagnitudeHSE = [];
    public $rowMagnitudeUser = [];
    public $rowSign = [];


    public $rowdata = [];
    public $padno;
    public $reg;
    public $form = 'DT135';
    public $printlayout = 'Landscape';
    public $formname = 'Hand Arm Vibration – Tool Usage Assessment Form (DT135)';
    public $formType = 'HSEQ';
    public $formLayout = '/dt135/_form.php';
    public $viewLayout = '@modules/forms/views/dt135/_view.php';
    public $createpath = '/forms/dt135/create';
    public $subclass = 'Dt135rows';

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position', 'jobno', 'location', 'description', 'operatorname', 'osign', 'hseqname', 'hseqsign', 'padno'], 'string'],
            [['rowid','rowdt135id','rowtooltype','rowactid','rowrelatedid', 'rowtoolms2', 'rowjobno', 'rowpadno', 'rowrunningtotal','rowstatus', 'rowpph','rowpph_old', 'rowpoints_old', 'rowrunningtotal_old', 'rowtooltypeid','rowtimestarted','rowtimestopped',
            'rowhours','rowminutes','rowpoints','rowtooltypetemp','rowdeleted','rowcreated_at','rowcreated_by','rowupdated_at','rowupdated_by','rowdeleted_at','rowdeleted_by','rowupdated_by2','rowcreated_by2'], 'safe'],
            [['actid', 'date', 'rowdata'], 'safe'],				
            [['rowToolname','rowEAV','rowELV','rowMagnitudeHSE','rowMagnitudeUser','rowOperator','rowSign'], 'safe'],	
			//Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by', 'id'],'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'actid' => 'Actid',
            'position' => 'Position',
            'jobno' => 'Jobno',
            'location' => 'Location',
            'description' => 'Description',
            'date' => 'Date',
            'operatorname' => 'Operatorname',
            'osign' => 'Osign',
            'hseqname' => 'Hseqname',
            'hseqsign' => 'Hseqsign',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
        ];
    }



	public function getItems() {
        return $this->datarows;
	}
    
	
    public function getActivity() {
        return Activities::findOne(['id' => $this->actid]);
	}
	
    public function getUser() {
        return User::findOne(['id' => $this->created_by]);
    }
    public function getAccount()
	{
        return Accounts::findOne(['accountid' => $this->company]);
    } 
    
    public function beforeValidate(){
        if ($this->date) {$this->date = date('Y-m-d', strtotime(str_replace('/', '-', $this->date)));} else {$this->date = null;}
        $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        return parent::beforeValidate();
    }

    public function init(){
        parent::init();
        if($this->actid !== null){
            $activity = Activities::findOne(['id' => $this->actid]);
            $this->jobno = $activity->jobno;
            $this->location = $activity->location;
            $this->description = "Casting Aluminothermic Weld - Cutting welding gap with rail mounted disc cutter, descalling, cutting riser off & profile grinding";
            $this->equipment($activity);
        }
    }

    public function equipment($activity) : void {
        $welders = explode(',', $activity->welder);
        foreach($welders as $welder){
            foreach($this->getEquipment() as $equipment){
                $this->rowstatus[$welder.'_'.$equipment['id']] = 'Active';
                $this->rowhours[$welder.'_'.$equipment['id']] = 0;
                $this->rowminutes[$welder.'_'.$equipment['id']] = 0;
                $this->rowpoints[$welder.'_'.$equipment['id']] = 0;
                $this->rowtooltypeid[$welder.'_'.$equipment['id']] = $equipment['id'];
                $this->rowToolname[$welder.'_'.$equipment['id']] = $equipment['name'];
                $this->rowOperator[$welder.'_'.$equipment['id']] = $welder;
                $this->rowpph[$welder.'_'.$equipment['id']] = $equipment['pph'];
                $this->rowEAV[$welder.'_'.$equipment['id']] = $equipment['EAV'];
                $this->rowELV[$welder.'_'.$equipment['id']] = $equipment['ELV'];
                $this->rowMagnitudeHSE[$welder.'_'.$equipment['id']] = 0;
                $this->rowMagnitudeUser[$welder.'_'.$equipment['id']] = $equipment['magnitude_user'];
            
            }   
        }
    }

    public function getEquipment(){
        return [
            [
                'id' => '1',
                'name' => 'Angle Grinder - 180mm',
                'magnitude_user' => 9.5,
                'pph' => 181,
                'EAV' => '00:33',
                'ELV' => '02:12'
            ],
            [
                'id' => '2',
                'name' => 'Hilti TE300 with needle attachment',
                'magnitude_user' => 13.3,
                'pph' => 354,
                'EAV' => '00:16',
                'ELV' => '01:07'
            ],
            [
                'id' => '3',
                'name' => 'Rail mounted disc cutter',
                'magnitude_user' => 6.3,
                'pph' => 79,
                'EAV' => '01:15',
                'ELV' => '05:02'
            ],
            [
                'id' => '4',
                'name' => 'Profiel grinding with ROBEL',
                'magnitude_user' => 2.5,
                'pph' => 13,
                'EAV' => '08:00',
                'ELV' => '24:00'
            ],
            [
                'id' => '5',
                'name' => 'Angle Grinder - 230mm',
                'magnitude_user' => 7.2,
                'pph' => 104,
                'EAV' => '00:57',
                'ELV' => '03:51'
            ],
        ];
    }
    
    public static function adminFormButtons($buttons, $model){
      $_this = new self();
        $buttons[$_this->formType]['items'][] = [
            'label' => $_this->formname,
            'url' => [$_this->createpath, 'actid' => $model->id],
            'linkOptions' =>  ['target'=>'_blank'],
        ];
        return $buttons;
    }
}
