<?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 "dt128".
 *
 * @property integer $id
 * @property string $date
 * @property string $location
 * @property string $jcn
 * @property string $Mileage
 * @property string $Road
 * @property string $weldersname
 * @property string $welderssignature
 * @property string $stampno
 * @property string $printname
 * @property string $ncrdescription
 * @property string $ncrclientrep
 * @property string $ncrclientsignature
 * @property string $actioncode
 * @property string $esrrequired
 * @property string $defclientrep
 */
class Dt128 extends Model {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    
    public $id;
    public $actid;
    public $date;
    public $client;
    public $location;
    public $jcn;
    public $mileage;
    public $mileageb;
    public $mileagec;
    public $mileaged;
    public $road;
    public $weldersname;
    public $welderssignature;
    public $stampno;
    public $printname;
    public $ncrdescription;
    public $ncrclientrep;
    public $ncrclientsignature;
    public $actioncode;
    public $esrrequired;
    public $defclientrep;
    public $defclientsignature;
    public $rail;
    public $wsignatureloc;
    public $signatureloc;
    public $signature3loc;
    public $weld;
    public $injobno;
    public $padno;
    public $deleted;


    public $tracktrolley;
    public $defclientremail;
    public $updated_by2;
    public $created_by2;
    public $updated_at;
    public $created_at;
    public $deleted_at;
    public $updated_by;
    public $created_by;
    public $deleted_by;

    public $printlayout = 'portrait';
    public $form = 'DT128';
    public $formname = 'Non-Conformance Form (DT128)';

    
    public $formLayout = '/dt128/_form.php';
    public $viewLayout = '@modules/forms/views/dt128/_view.php';
    public $createform = '/dt128/_view.php';
    
    public $formType = 'Fault';
    public $createpath = '/forms/dt128/create';
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['signatureloc', 'wsignatureloc', 'signature3loc',  'actid', 'weldersname'], 'safe'],
            [['ncrdescription'], 'string'],
            [['client', ], 'string'],
            [['date', 'location', 'jcn', 'mileage', 'road', 'stampno',
             'printname', 'ncrclientrep', 'ncrclientsignature', 'actioncode', 
             'esrrequired', 'defclientrep', 'rail', 'weld', 'injobno', 
             'mileageb', 'mileagec', 'mileaged', 'padno', 'defclientremail'], 'string', 'max' => 100],
			 //Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'date' => 'Date',
            'location' => 'Location',
            'jcn' => 'Job Cost Number',
            'mileage' => 'Mileage',
            'road' => 'Track ID',
            'weldersname' => 'Welders Name',
            'welderssignature' => 'Welders Signature',
            'stampno' => 'Stampno',
            'printname' => 'Printname',
            'ncrdescription' => 'NCR Description',
            'ncrclientrep' => 'Client Repesentative',
            'ncrclientsignature' => 'Ncrclientsignature',
            'actioncode' => 'Minimum action code applied',
            'esrrequired' => 'ESR Required',
            'defclientrep' => 'Client repesentative',
            'defclientsignature' => 'Defclientsignature',
            'rail' => 'Rail',
        ];
    }
    public function getAccount() {
        return Accounts::findOne(['id' => $this->client]);
    }

    Public function getUser() {
        return User::findOne(['id' => $this->created_by]);
    }
	public function beforeValidate() {
        $this->weldersname = is_array($this->weldersname) ?  implode(",", $this->weldersname) : Yii::$app->user->identity->username;
        $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        return parent::beforeValidate();
    }
    
	public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
		}
    public function getWas() {
		return Dt_was::findOne(['actid' => $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','ordertitle', 'location', 'sdate', 'elr', 'weekno', 'company'])->where(['id' => $actid])->asArray()->one();
           $this->injobno = $act['jobno'];
           $this->jcn = $act['ordertitle'];
           $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;
    }
}