<?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 "dt133".
 *
 * @property string $id
 * @property string $actid
 * @property string $contract
 * @property string $refno
 * @property string $detailsname
 * @property string $detailstitle
 * @property string $detailsmobile
 * @property string $detailsaddress1
 * @property string $detailsaddress2
 * @property string $detailstown
 * @property string $detailscounty
 * @property string $detailspostcode
 * @property string $detailstelno
 * @property string $detailsemployer
 * @property string $detailssign
 * @property string $detailssigndate
 * @property string $eventdate
 * @property string $eventtime
 * @property string $eventlocation
 * @property integer $nrcontrolled
 * @property string $elr
 * @property string $mileage
 * @property string $about
 * @property string $equipment
 * @property string $witnesses
 * @property string $immediateaction
 * @property string $confirmedname
 * @property string $confirmedtitle
 * @property string $confirmedsign
 * @property integer $riddor95
 * @property integer $f2508
 * @property string $findings
 * @property string $conclusions
 * @property string $immediatecause
 * @property string $underlyingcause
 * @property string $remedialactions
 * @property string $recommedations
 * @property string $lmsigned
 * @property string $lmname
 * @property string $lmdate
 * @property string $hseqsign
 * @property string $hseqprint
 * @property string $hseqdate
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 */
class Dt133 extends Model {

    public $id;
public $actid;
public $contract;
public $refno;
public $detailsname;
public $detailstitle;
public $detailsmobile;
public $detailsaddress1;
public $detailsaddress2;
public $detailstown;
public $detailscounty;
public $detailspostcode;
public $detailstelno;
public $detailsemployer;
public $detailssign;
public $detailssigndate;
public $eventdate;
public $eventtime;
public $eventlocation;
public $nrcontrolled;
public $elr;
public $mileage;
public $about;
public $equipment;
public $witnesses;
public $immediateaction;
public $confirmedname;
public $confirmedtitle;
public $confirmedsign;
public $riddor95;
public $f2508;
public $findings;
public $conclusions;
public $immediatecause;
public $underlyingcause;
public $remedialactions;
public $recommedations;
public $lmsigned;
public $lmname;
public $lmdate;
public $hseqsign;
public $hseqprint;
public $hseqdate;
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 $padno;
public $jobno;
public $relatedid;
public $status;

public $printlayout = 'Portrait';
public $form = 'DT133'; 
public $formname = 'Incident Form(DT133)';
public $formLayout = '/dt133/_form.php';
public $viewLayout = '@modules/forms/views/dt133/_view.php';
public $createpath = '/forms/dt133/create';
public $formType = 'Accidents';

public $hseqconfirmed;
public $hseqposition;
public $lmsigndate2;


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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contract', 'refno', 'detailstitle', 'detailsmobile', 'detailsaddress1', 'detailsaddress2', 'detailstown', 'detailscounty', 'detailspostcode', 'detailstelno', 'detailsemployer', 'detailssign', 'eventtime', 'eventlocation', 'elr', 'mileage', 'about', 'equipment', 'witnesses', 'immediateaction', 'confirmedname', 'confirmedtitle', 'confirmedsign', 'findings', 'conclusions', 'immediatecause', 'underlyingcause', 'remedialactions', 'recommedations', 'lmsigned', 'lmname', 'hseqsign', 'hseqprint','hseqposition','hseqconfirmed'], 'string'],
            [['actid', 'detailssigndate', 'eventdate', 'lmdate', 'lmsigndate2',  'detailsname', 'hseqdate'], 'safe'],
            [['nrcontrolled', 'riddor95', 'f2508'], 'integer'],	
			//Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by', 'id'],'safe'],
			
			[['detailsname'],'required'],
			//Redundant
			[['created_by2', 'updated_by2'], 'integer'],
			
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'actid' => 'Actid',
            'contract' => 'Contract',
            'refno' => 'Refno',
            'detailsname' => 'Detailsname',
            'detailstitle' => 'Detailstitle',
            'detailsmobile' => 'Detailsmobile',
            'detailsaddress1' => 'Detailsaddress1',
            'detailsaddress2' => 'Detailsaddress2',
            'detailstown' => 'Detailstown',
            'detailscounty' => 'Detailscounty',
            'detailspostcode' => 'Detailspostcode',
            'detailstelno' => 'Detailstelno',
            'detailsemployer' => 'Detailsemployer',
            'detailssign' => 'Detailssign',
            'detailssigndate' => 'Detailssigndate',
            'eventdate' => 'Eventdate',
            'eventtime' => 'Eventtime',
            'eventlocation' => 'Eventlocation',
            'nrcontrolled' => 'Nrcontrolled',
            'elr' => 'Elr',
            'mileage' => 'Mileage',
            'about' => 'About',
            'equipment' => 'Equipment',
            'witnesses' => 'Witnesses',
            'immediateaction' => 'Immediateaction',
            'confirmedname' => 'Confirmedname',
            'confirmedtitle' => 'Confirmedtitle',
            'confirmedsign' => 'Confirmedsign',
            'riddor95' => 'Riddor95',
            'f2508' => 'F2508',
            'findings' => 'Findings',
            'conclusions' => 'Conclusions',
            'immediatecause' => 'Immediatecause',
            'underlyingcause' => 'Underlyingcause',
            'remedialactions' => 'Remedialactions',
            'recommedations' => 'Recommedations',
            'lmsigned' => 'Lmsigned',
            'lmname' => 'Lmname',
            'lmdate' => 'Lmdate',
            'hseqsign' => 'Hseqsign',
            'hseqprint' => 'Hseqprint',
            'hseqdate' => 'Hseqdate',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
        ];
    }
    
    public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
	}
	public function getAccount()
	{
		return Accounts::findOne(['accountid' => $this->company]);
	} 
	
    public function getUser() {
        return User::findOne(['id' => $this->created_by]);
    }
    
    public function beforeValidate() {
		if (Yii::$app->name !== 'app-console' ) {
			$this->lmsigndate2 = $this->lmsigndate2 !== '' ? $this->lmsigndate2 : null;
			$this->detailssigndate = $this->detailssigndate !== '' ? $this->detailssigndate : null;
			$this->eventdate = $this->eventdate !== '' ? $this->eventdate : null;
            $this->hseqdate = $this->hseqdate !== '' ? $this->hseqdate : null;
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
                
	    }
        return parent::beforeValidate();		
	}

	public function init() {
		if (Yii::$app->name !== 'app-console' ) {
		}
        return parent::init();		
	}
	        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;
    }
}
