<?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 "dt188".
 *
 * @property string $id
 * @property string $actid
 * @property string $name
 * @property string $datetime
 * @property string $contract
 * @property string $location
 * @property string $project
 * @property string $details
 * @property string $actions
 * @property string $escalatedto
 * @property string $signature
 * @property string $date
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 */
class Dt188 extends Model {
    public $id;
    public $actid;
    public $jobno;
    public $relatedid;
    public $alertid;
    public $name;
    public $deleted;
    public $type;
    public $datetime;
    public $contract;
    public $location;
    public $project;
    public $details;
    public $actions;
    public $escalatedto;
    public $signature;
    public $date;
    public $created_at;
    public $created_by;
    public $deleted_at;
    public $deleted_by;
    public $created_by2;
    public $updated_at;
    public $updated_by2;
    public $updated_by;
    public $noncontype;
    public $status;
    public $subject;
    public $padno;
    public $form = 'dt188';
    public $formname = 'Non-Compliance Report Form (Dt188)';

    
    
    public $formLayout = '/dt188/_form.php';
    public $viewLayout = '@modules/forms/views/dt188/_view.php';
    public $formType = 'Fault';
    public $createpath = '/forms/dt188/create';
    public $printlayout = 'Portrait';
    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'dt188';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'contract', 'location', 'project', 'details', 'actions', 'escalatedto', 'signature','subject', 'padno', 'status', 'type', 'alertid', 'relatedid','noncontype', 'form', 'formname'], 'string'],
            [['actid', 'datetime', 'date'], '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',
            'name' => 'Name',
            'datetime' => 'Datetime',
            'contract' => 'Contract',
            'location' => 'Location',
            'project' => 'Project',
            'details' => 'Details',
            'actions' => 'Actions',
            'escalatedto' => 'Escalatedto',
            'signature' => 'Signature',
            'date' => 'Date',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'subject' => 'Subject',
        ];
    }
    public function beforeValidate() {
		if (Yii::$app->name !== 'app-console' ) {
			$this->datetime = date('Y-m-d h:i:s.z', strtotime(str_replace('/', '-', $this->datetime)));
            $this->date = date('Y-m-d', strtotime(str_replace('/', '-', $this->date)));
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
			return parent::beforeValidate();
		}
        return parent::beforeValidate();

	}
    public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
	}
	public function getAccount() {
		return Accounts::findOne(['accountid' => $this->company]);
	}
	public function getAlert() {
		return \backend\models\Sysalerts::findOne(['id' => $this->alertid]);
		
	}
    public function getUser() {
        return User::findOne(['id' => $this->created_by]);
    }
   
    public function setData()
    {
        if(Yii::$app->controller->action->id == 'create' && $actid = (Yii::$app->request->get('actid', null)) !== null ){
           $act = Activities::find()->select(['jobno', 'relatedid','ordertitle', 'location', 'sdate', 'elr', 'weekno', 'company'])->where(['id' => $actid])->asArray()->one();
           $this->jobno = $act['jobno'];
          // $this->client = $act['relatedid'];
           $this->location = $act['location'];
         //  $this->elr = $act['elr'];
           $this->date = date('d/m/Y', strtotime($act['sdate']));
        }
    }
    /**
     * Sets up forms buttons in Activities view
     */
    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;
    }
}

