<?php

namespace modules\forms\models;

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

/**
 * This is the model class For table "dtgsaf".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property string $actid
 * @property string $date
 * @property string $location
 * @property string $time
 * @property string $assessorname1
 * @property string $assessorsign1
 * @property string $assessorname2
 * @property string $assessorsign2
 * @property string $introduction
 * @property string $executivesummary
 * @property string $findings
 * @property string $conclusions
 */
class Dt194 extends Model {

    public $id;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $deleted_at;
    public $deleted_by;
    public $actid;
    public $date;
    public $location;
    public $time;
    public $assessorname1;
    public $assessorsign1;
    public $assessorname2;
    public $assessorsign2;
    public $introduction;
    public $executivesummary;
    public $findings;
    public $conclusions;
    public $deleted;
    public $created_by2;
    public $updated_by2;
    public $jobno;
    public $padno;
    public $status;
    public $relatedid;

    public $rowid = [];
    public $rowstatus = [];
    public $rowcontrolmeasure = [];
    public $rowobservation = [];
    public $rowtimescale = [];

    public $printlayout = 'portrait';
    public $form = 'dt194';
    public $formname = 'Site Assessment Form (DT194)';
    public $formType = 'Other';
    public $createpath = '/forms/dt194/create';
    
    public $formLayout = '/dt194/_form.php';
    public $viewLayout = '@modules/forms/views/dt194/_view.php';public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['location', 'time', 'assessorname1', 'assessorsign1', 'assessorname2', 'assessorsign2', 'introduction', 'executivesummary', 'findings', 'conclusions', 'status', 'jobno'], 'string'],
            [['actid','date', 'rowid', 'rowstatus', 'rowcontrolmeasure', 'rowobservation', 'rowtimescale' ], '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',
            'actid' => 'Actid',
            'date' => 'Date',
            'location' => 'Location',
            'time' => 'Time',
            'assessorname1' => 'Assessorname1',
            'assessorsign1' => 'Assessorsign1',
            'assessorname2' => 'Assessorname2',
            'assessorsign2' => 'Assessorsign2',
            'introduction' => 'Introduction',
            'executivesummary' => 'Executivesummary',
            'findings' => 'Findings',
            'conclusions' => 'Conclusions',
        ];
    }
    
   	public function beforeValidate() {
        $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 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->jobno = $act['jobno'];
           //$this->jcnno = $act['jobno'];
         //  $this->client_name = $act['relatedid'];
           $this->location = $act['location'];
         //  $this->elr = $act['elr'];
       //    $this->weekno = $act['weekno'];
           $this->date = date('d/m/Y H:i', strtotime($act['sdate']));
     //      $this->insdate = date('d/m/Y H:i', 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;
    }
}
