<?php

namespace modules\forms\models;

use Yii;
use modules\activities\models\Activities;
use modules\users\models\User;
use modules\custom\models\Accounts;
use yii\base\Model;
/**
 * This is the model class For table "dt058".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property string $timesheetid
 * @property string $actid
 * @property string $site
 * @property integer $clientid
 * @property integer $employeename1
 * @property integer $employeename2
 * @property integer $employeename3
 * @property string $excesscode1
 * @property string $excesscode2
 * @property string $excesscode3
 * @property string $requestor1
 * @property string $requestor2
 * @property string $requestor3
 * @property string $methodofrequest1
 * @property string $methodofrequest2
 * @property string $methodofrequest3
 * @property string $reasoncode1
 * @property string $reasoncode2
 * @property string $reasoncode3
 * @property string $likelihood1a
 * @property string $likelihood2a
 * @property string $likelihood3a
 * @property string $likelihood1b
 * @property string $likelihood2b
 * @property string $likelihood3b
 * @property string $severity1a
 * @property string $severity2a
 * @property string $severity3a
 * @property string $severity1b
 * @property string $severity2b
 * @property string $severity3b
 * @property string $riskfactor1a
 * @property string $riskfactor2a
 * @property string $riskfactor3a
 * @property string $riskfactor1b
 * @property string $riskfactor2b
 * @property string $riskfactor3b
 * @property string $control1
 * @property string $control2
 * @property string $control3
 * @property string $clientsiterepprint
 * @property string $date
 * @property string $clientsiterepsign
 */
class Dt058 extends Model {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid', 'timesheetid'];
    public $securefields = [];
	public $data;
    public $id;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $timesheetid;
    public $actid;
    public $site;
    public $clientid;
    public $employeename1;
    public $employeename2;
    public $employeename3;
    public $excesscode1;
    public $excesscode2;
    public $excesscode3;
    public $requestor1;
    public $requestor2;
    public $requestor3;
    public $methodofrequest1;
    public $methodofrequest2;
    public $methodofrequest3;
    public $reasoncode1;
    public $reasoncode2;
    public $reasoncode3;
    public $likelihood1a;
    public $likelihood2a;
    public $likelihood3a;
    public $likelihood1b;
    public $likelihood2b;
    public $likelihood3b;
    public $severity1a;
    public $severity2a;
    public $severity3a;
    public $severity1b;
    public $severity2b;
    public $severity3b;
    public $riskfactor1a;
    public $riskfactor2a;
    public $riskfactor3a;
    public $riskfactor1b;
    public $riskfactor2b;
    public $riskfactor3b;
    public $control1;
    public $control2;
    public $control3;
    public $clientsiterepprint;
    public $date;
    public $clientsiterepsign;
    public $clientrepsignrepsign;
    public $tspadno;
    public $padno;
    public $jobno;
    public $deleted;
    public $relatedid;
    public $employee1id;
    public $employee2id;
    public $employee3id;
    public $deleted_at;
    public $deleted_by;
    public $status;
    public $risk;
    public $created_by2;
    public $updated_by2;

    public $printlayout = 'Portrait';
    public $form = 'DT058';
    public $formname = 'Excess Hours Form (DT058)';
    
    public $createpath = '/forms/dt058/create';
    public $formType = 'Other';
    public $formLayout = '/dt058/_form.php';
    public $viewLayout = '@modules/forms/views/dt058/_view.php';/**
     * @inheritdoc
     */

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['site', 'excesscode1', 'excesscode2', 'excesscode3', 'requestor1', 'requestor2', 'requestor3', 'methodofrequest1', 'methodofrequest2', 'methodofrequest3', 'reasoncode1', 'reasoncode2', 'reasoncode3', 'likelihood1a', 'likelihood2a', 'likelihood3a', 'likelihood1b', 'likelihood2b', 'likelihood3b', 'severity1a', 'severity2a', 'severity3a', 'severity1b', 'severity2b', 'severity3b', 'riskfactor1a', 'riskfactor2a', 'riskfactor3a', 'riskfactor1b', 'riskfactor2b', 'riskfactor3b', 'control1', 'control2', 'control3', 'clientsiterepprint', 'padno', 'tspadno'], 'string'],
            [['clientid', 'employeename1', 'employeename2', 'employeename3'], 'string'],
            [['date','timesheetid', 'actid', 'clientrepsignrepsign'], 'safe'],
            ['site', 'required'],
			//Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			//Redundant
			[['created_by2', 'updated_by2'], 'integer'],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'timesheetid' => 'Timesheetid',
            'actid' => 'Actid',
            'site' => 'Site',
            'clientid' => 'Clientid',
            'employeename1' => 'Employeename1',
            'employeename2' => 'Employeename2',
            'employeename3' => 'Employeename3',
            'excesscode1' => 'Excesscode1',
            'excesscode2' => 'Excesscode2',
            'excesscode3' => 'Excesscode3',
            'requestor1' => 'Requestor1',
            'requestor2' => 'Requestor2',
            'requestor3' => 'Requestor3',
            'methodofrequest1' => 'Methodofrequest1',
            'methodofrequest2' => 'Methodofrequest2',
            'methodofrequest3' => 'Methodofrequest3',
            'reasoncode1' => 'Reasoncode1',
            'reasoncode2' => 'Reasoncode2',
            'reasoncode3' => 'Reasoncode3',
            'likelihood1a' => 'Likelihood1a',
            'likelihood2a' => 'Likelihood2a',
            'likelihood3a' => 'Likelihood3a',
            'likelihood1b' => 'Likelihood1b',
            'likelihood2b' => 'Likelihood2b',
            'likelihood3b' => 'Likelihood3b',
            'severity1a' => 'Severity1a',
            'severity2a' => 'Severity2a',
            'severity3a' => 'Severity3a',
            'severity1b' => 'Severity1b',
            'severity2b' => 'Severity2b',
            'severity3b' => 'Severity3b',
            'riskfactor1a' => 'Riskfactor1a',
            'riskfactor2a' => 'Riskfactor2a',
            'riskfactor3a' => 'Riskfactor3a',
            'riskfactor1b' => 'Riskfactor1b',
            'riskfactor2b' => 'Riskfactor2b',
            'riskfactor3b' => 'Riskfactor3b',
            'control1' => 'Control1',
            'control2' => 'Control2',
            'control3' => 'Control3',
            'clientsiterepprint' => 'Clientsiterepprint',
            'date' => 'Date',
            'clientsiterepsign' => 'Clientsiterepsign',
        ];
    }
    public function beforeValidate(){
        if ($this->date) {$this->date = date('Y-m-d', strtotime(str_replace('/', '-', $this->date)));}
        $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        return parent::beforeValidate();
    }
    
    public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
	}
	
	public function getClientl() {
		return Accounts::findOne(['id' => $this->clientid]);
		
    } 
    
    public function getUser() {
        return user::findOne(['id' => $this->created_by]);
    }
    public function getEmp1() {
        return empty($this->employeename1) == false  && is_integer($this->employeename1) == true ? user::findOne(['_id' => $this->employeename1]) : user::findOne(['username' => $this->employeename1]);
    }
    public function getEmp2() {
        return empty($this->employeename2) == false  && is_integer($this->employeename2) == true ? user::findOne(['_id' => $this->employeename2]) : user::findOne(['username' => $this->employeename2]);
        
    }
    public function getEmp3() {
        return empty($this->employeename3) == false  && is_integer($this->employeename3) == true ? user::findOne(['_id' => $this->employeename3]) : user::findOne(['username' => $this->employeename3]);
    }
        
    public function setData()
    {
        if(Yii::$app->controller->action->id == 'create' && $actid = (Yii::$app->request->get('actid', null)) !== null ){
           $act = Activities::find()->select(['jobno', 'relatedid', 'location', 'sdate', 'elr', 'weekno', 'company'])->where(['id' => $actid])->asArray()->one();
           //$this->jcnno = $act['jobno'];
           $this->clientid = $act['relatedid'];
           $this->site = $act['location'];
         //  $this->elr = $act['elr'];
           $this->date = date('d/m/Y', strtotime($act['sdate']));
        }
    }
    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;
    }



    public static function excessCodes() : array {
        return [
            'A' => 'More than thirteen turns of duty in any fourteen day period.',
            'B' => 'More than twelve hours of work per turn of duty (Including Travel Time)',
            'C' => 'More than seventy-two hours of work per week',
            'D' => 'Less than twelve hours between booking off from a turn of duty and booking on for the next turn of duty',
            'E' => 'For regular shift patterns which rotate or alternate on a weekly basis, with less than eight hours of rest during the weekly shift changeover',
        ];
    }
    public static function methodOfRequest() : array {
        return [
            'W' => 'Written (letter,Text, Email etc)',
            'O' => 'Oral',
            'R' => 'Retrospective',
        ];
    }
    public static function reasonCodes() : array {
        return [
            '1' => 'Adverse weather conditions.',
            '2' => 'Equipment failure.',
            '3' => 'Emergency Site Requirements',
            '4' => 'Accident or Incident.',
        ];
    }
}
