<?php


namespace modules\forms\models;


use Yii;
use Yii\base\Model;
use yii\helpers\Html;
use modules\activities\models\Activities;
use modules\users\models\User;
use modules\forms\models\Forms;
use modules\custom\models\Accounts;

use modules\forms\models\Dt188;
class Dt134 extends Model {

public $id;
public $date;
public $created_at;
public $updated_at;
 public $created_by;
 public $updated_by;
 public $deleted_by;
 public $deleted_at;
 public $deleted;
 public $actid;
 public $status;
 public $driver;
 public $vehiclereg;
 public $vehicleid;
 public $startmileage;
 public $finishmileage;
 public $question1;
 public $question2;
 public $question3;
 public $question4;
 public $question5;
 public $question6;
 public $question7;
 public $question8;
 public $question9;
 public $question10;
 public $question11;
 public $question12;
 public $question13;
 public $question14;
 public $question15;
 public $question16;
 public $question17;
 public $question18;
 public $question19;
 public $question20;
 public $question21;
 public $question22;
 public $tyredepthin1;
 public $tyredepthin2;
 public $tyredepthin3;
 public $tyredepthin4;
 public $tyredepthin5;
 public $tyredepthout1;
 public $tyredepthout2;
 public $tyredepthout3;
 public $tyredepthout4;
 public $tyredepthout5;
 public $tyrepressure1;
 public $tyrepressure2;
 public $tyrepressure3;
 public $tyrepressure4;
 public $tyrepressure5;
 public $image;
 public $signature1;
 public $signature2;
 public $comments;
 public $correctivemeasures;
 public $result;

 public $padno;
 public $jobno;
 public $relatedid;
 
 public $created_by2;
 public $updated_by2;
 
 public $printlayout = 'Landscape';
 public $form = 'DT134';
 public $formname = 'Vehicle Checklist (DT134)';

    public $formType = 'Other';
    public $createpath = '/forms/dt134/create';

    public $formLayout = '/dt134/_form.php';
    public $viewLayout = '@modules/forms/views/dt134/_view.php';
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'created_by', 'updated_by', 'deleted_by', 'actid', 'relatedid', 'status', 'driver', 'vehiclereg', 'vehicleid', 'question1', 'question2', 'question3', 'question4', 'question5', 'question6', 'question7', 'question8', 'question9', 'question10', 'question11', 'question12', 'question13', 'question14', 'question15', 'question16', 'question17', 'question18', 'question19', 'question20', 'question21', 'question22', 'image', 'signature1', 'signature2', 'comments', 'result','correctivemeasures'], 'string'],
            [['startmileage', 'finishmileage', 'jobno'], 'integer'],
            [['tyredepthin1', 'tyredepthin2', 'tyredepthin3', 'tyredepthin4', 'tyredepthin5', 'tyredepthout1', 'tyredepthout2', 'tyredepthout3', 'tyredepthout4', 'tyredepthout5', 'tyrepressure1', 'tyrepressure2', 'tyrepressure3', 'tyrepressure4', 'tyrepressure5'],'number'],
            [['created_at', 'updated_at', 'deleted_at', 'date'], 'safe'],
            [['deleted' ], 'boolean'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'deleted_by' => 'Deleted By',
            'deleted_at' => 'Deleted At',
            'deleted' => 'Deleted',
            'actid' => 'Actid',
            'status' => 'Status',
            'driver' => 'Driver',
            'vehiclereg' => 'Vehiclereg',
            'vehicleid' => 'Vehicleid',
            'startmileage' => 'Startmileage',
            'finishmileage' => 'Finishmileage',
            'question1' => 'Question1',
            'question2' => 'Question2',
            'question3' => 'Question3',
            'question4' => 'Question4',
            'question5' => 'Question5',
            'question6' => 'Question6',
            'question7' => 'Question7',
            'question8' => 'Question8',
            'question9' => 'Question9',
            'question10' => 'Question10',
            'question11' => 'Question11',
            'question12' => 'Question12',
            'question13' => 'Question13',
            'question14' => 'Question14',
            'question15' => 'Question15',
            'question16' => 'Question16',
            'question17' => 'Question17',
            'question18' => 'Question18',
            'question19' => 'Question19',
            'question20' => 'Question20',
            'question21' => 'Question21',
            'question22' => 'Question22',
            'tyredepthin1' => 'Tyredepthin1',
            'tyredepthin2' => 'Tyredepthin2',
            'tyredepthin3' => 'Tyredepthin3',
            'tyredepthin4' => 'Tyredepthin4',
            'tyredepthin5' => 'Tyredepthin5',
            'tyredepthout1' => 'Tyredepthout1',
            'tyredepthout2' => 'Tyredepthout2',
            'tyredepthout3' => 'Tyredepthout3',
            'tyredepthout4' => 'Tyredepthout4',
            'tyredepthout5' => 'Tyredepthout5',
            'tyrepressure1' => 'Tyrepressure1',
            'tyrepressure2' => 'Tyrepressure2',
            'tyrepressure3' => 'Tyrepressure3',
            'tyrepressure4' => 'Tyrepressure4',
            'tyrepressure5' => 'Tyrepressure5',
            'image' => 'Image',
            'signature1' => 'Signature1',
            'signature2' => 'Signature2',
            'comments' => 'Comments',
        ];
    }
    
	public function getVehiclel() {
        return \modules\cors\models\Vehiclebe::findOne(['id' => $this->vehicleid] );
    }
    
    public function getDriverl() {
        return User::findOne(['id' => $this->driver] );
		
    }
    
    public function getNoncoms(){
        return Forms::find()
            ->where(['relatedid' => $this->id, 'form' => 'DT188' ])
            ->one();

    }
    public function beforeValidate(){
        $this->padno = $this->padno == null ? User::Padno() : $this->padno;

        return parent::beforeValidate();
    }

    public function afterValidate(){

        if(is_object($this->noncoms) == true){
            $form = new Forms(['id' => \common\models\Base::Tempid(), 'form' => 'DT188', 'language' => 'en', 'relatedid' => $this->id ]);
            $dt188 = new Dt188(['id' => \common\models\Base::Tempid(), ]);
            $dt188->relatedid = $this->id;
            $dt188->name = $this->driver;
            $dt188->datetime = date('d/m/Y H:i', strtotime('now'));
            $dt188->date = date('d/m/Y H:i', strtotime('now'));
            if (
                $this->tyredepthin1 <=  1.6 || 
                $this->tyredepthin2 <=  1.6 || 
                $this->tyredepthin3 <=  1.6 ||
                $this->tyredepthin4 <=  1.6 ) 
            { 
                $dt188->details = 'Illegal Thread on '.$this->vehiclel->reg.' see the following defect sheet '. Html::A('DT134', ['/forms/dt134/view','id' => $this->id]);
            } elseif (
                ($this->tyredepthin1 >  1.6 && $this->tyredepthin1 <=  2.0) ||
                ($this->tyredepthin2 >  1.6 && $this->tyredepthin2 <=  2.0) ||
                ($this->tyredepthin3 >  1.6 && $this->tyredepthin3 <=  2.0) ||
                ($this->tyredepthin4 >  1.6 && $this->tyredepthin4 <=  2.0) 
            ){
                $dt188->details = 'Low Thread on '.$this->vehiclel->reg.' see the following defect sheet';
            }
            $form->data = $dt188;
            $form->save();
        }   

        return parent::afterValidate();
    }

    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;
    }
}