<?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 "dtccrc".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property integer $name
 * @property string $location
 * @property string $date
 * @property string $time
 * @property string $issuetype
 * @property string $type
 * @property string $description
 * @property string $action
 * @property string $issuelocation
 * @property string $engineersign
 * @property string $clientsign
 */
class Dt195 extends Model {

    public $id;
    public $actid;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $deleted_at;
    public $deleted_by;
    public $name;
    public $location;
    public $date;
    public $time;
    public $issuetype;
    public $type;
    public $description;
    public $action;
    public $issuelocation;
    public $engineersign;
    public $clientsign;
    public $deleted;
    public $created_by2;
    public $updated_by2;
    public $padno;
    public $status;

    public $printlayout = 'Landscape';
    public $form = 'dt195';
    public $formname = 'Close Call Card (DT195)';
    public $formType = 'HSEQ';


    
    public $formLayout = '/dt195/_form.php';
    public $viewLayout = '@modules/forms/views/dt195/_view.php';
    public $createpath = '/forms/dt195/create';
    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['location', 'issuetype', 'type', 'description', 'action', 'issuelocation', 'engineersign', 'clientsign','name'], 'string'],
            [['date', 'time','actid'], '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',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'name' => 'Name',
            'location' => 'Location',
            'date' => 'Date',
            'time' => 'Time',
            'issuetype' => 'Issuetype',
            'type' => 'Type',
            'description' => 'Description',
            'action' => 'Action',
            'issuelocation' => 'Issuelocation',
            'engineersign' => 'Engineersign',
            'clientsign' => 'Clientsign',
        ];
    }
	public function beforeValidate() {
		if (Yii::$app->name !== 'app-console' ) {
			$this->time !== null ? $this->time = "1970-01-01 ".$this->time.":00.000" : null;
			//$this->time = date('Y-m-d h:i:s.z', strtotime($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 $this->hasOne(Activities::class, ['id' => $this->actid]);
	}
    public function getUser() {
        return $this->hasOne(User::class, ['id' => $this->created_by]);
    }
    public function getAccount() {
		return $this->hasOne(Accounts::class, ['accountid' => $this->company])
		->joinwith('activity');
	}


        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->injobno = $act['jobno'];
         //  $this->client = $act['relatedid'];
           $this->location = $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;
    }
}
