<?php

namespace modules\forms\models;

use Yii;
use Yii\base\Model;
use modules\activities\models\Activities;
use modules\users\models\User;
use modules\custom\models\Accounts;


/**
 * This is the model class For table "dt132".
 *
 * @property string $id
 * @property string $actid
 * @property string $dt131id
 * @property string $contract
 * @property string $ref
 * @property string $incidentdetails
 * @property string $eventdetails
 * @property string $eventdate
 * @property string $eventtime
 * @property string $eventlocation
 * @property string $witnessname
 * @property string $witnesstitle
 * @property string $witnessemployer
 * @property string $intername
 * @property string $intertitle
 * @property string $interemployer
 * @property string $prevention
 * @property string $statement
 * @property string $statementsign
 * @property string $statementdate
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 */
class Dt132 extends  Model {

    public $id;
    public $actid;
    public $dt131id;
    public $contract;
    public $ref;
    public $incidentdetails;
    public $eventdetails;
    public $eventdate;
    public $eventtime;
    public $eventlocation;
    public $witnessname;
    public $witnesstitle;
    public $witnessemployer;
    public $intername;
    public $intertitle;
    public $interemployer;
    public $prevention;
    public $statement;
    public $statementsign;
    public $statementdate;
    public $deleted_at;
    public $deleted_by;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $padno;
    public $jobno;
    public $relatedid;
    public $status;
    
    public $created_by2;
    public $updated_by2;
    public $deleted;
    
    public $printlayout = 'Portrait';
    public $form = 'DT132';
    public $formname = 'Witness Statement Form (DT132)';
    public $createpath = '/forms/dt132/create';
    public $formType = 'Accidents';
    
    public $formLayout = '/dt132/_form.php';
    public $viewLayout = '@modules/forms/views/dt132/_view.php';public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid','dt131id'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'dt132';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'contract', 'ref', 'incidentdetails', 'eventdetails', 'eventtime', 'eventlocation', 'witnessname', 'witnesstitle', 'witnessemployer', 'intername', 'intertitle', 'interemployer', 'prevention', 'statement', 'statementsign'], 'string'],
            [['actid', 'dt131id','eventdate', 'statementdate'], 'safe'],
            					//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',
            'actid' => 'Actid',
            'dt131id' => 'Dt131id',
            'contract' => 'Contract',
            'ref' => 'Ref',
            'incidentdetails' => 'Incidentdetails',
            'eventdetails' => 'Eventdetails',
            'eventdate' => 'Eventdate',
            'eventtime' => 'Eventtime',
            'eventlocation' => 'Eventlocation',
            'witnessname' => 'Witnessname',
            'witnesstitle' => 'Witnesstitle',
            'witnessemployer' => 'Witnessemployer',
            'intername' => 'Intername',
            'intertitle' => 'Intertitle',
            'interemployer' => 'Interemployer',
            'prevention' => 'Prevention',
            'statement' => 'Statement',
            'statementsign' => 'Statementsign',
            'statementdate' => 'Statementdate',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
        ];
    }
    public function beforeValidate()
    {
		if (Yii::$app->name !== 'app-console' ) {
			$this->eventdate = str_replace('/', '-', $this->eventdate);
			$this->statementdate = str_replace('/', '-', $this->statementdate);
			$this->eventdate = date('Y-m-d', strtotime($this->eventdate));
            $this->statementdate = date('Y-m-d', strtotime($this->statementdate));
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
            
		} else {
        }
		return parent::beforeValidate();		
	}
    
    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 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;
    }
}
