<?php

namespace modules\forms\models;

use Yii;
use modules\users\models\User;
use modules\activities\models\Activities;
use modules\custom\models\Accounts;
use yii\base\Model;
/**
 * This is the model class For table "dttmz".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property integer $date
 * @property string $deliveryunit
 * @property string $sitelocation
 * @property string $route
 * @property string $line
 * @property string $miles
 * @property string $yards
 * @property string $pointno
 * @property string $crossingmanufacturer
 * @property string $crossingno
 * @property string $legendfr
 * @property string $type
 * @property string $angle
 * @property string $legendlhrh
 * @property string $mlength
 * @property string $mdepth
 * @property string $mwidth
 * @property string $mtlength
 * @property string $mtdepth
 * @property string $mtwidth
 * @property string $encrouch5mm
 * @property string $distancefromtmwnorm
 * @property string $distancefromtmwmang
 * @property string $overall
 * @property string $depth
 * @property string $nglength
 * @property string $ngdepth
 * @property string $ngwidth
 * @property string $electrodeused
 * @property string $processmma
 * @property string $processsemiauto
 * @property string $processfullyauto
 * @property string $weldersname
 * @property string $weldersstamp
 * @property string $supervisorname
 * @property string $signature
 */
class Dt192 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 $date;
    public $deliveryunit;
    public $sitelocation;
    public $route;
    public $line;
    public $miles;
    public $yards;
    public $pointno;
    public $crossingmanufacturer;
    public $crossingno;
    public $legendfr;
    public $type;
    public $angle;
    public $legendlhrh;
    public $mlength;
    public $mdepth;
    public $mwidth;
    public $mtlength;
    public $mtdepth;
    public $mtwidth;
    public $encrouch5mm;
    public $distancefromtmwnorm;
    public $distancefromtmwmang;
    public $overall;
    public $depth;
    public $nglength;
    public $ngdepth;
    public $ngwidth;
    public $electrodeused;
    public $processmma;
    public $processsemiauto;
    public $processfullyauto;
    public $weldersname;
    public $weldersstamp;
    public $supervisorname;
    public $signature;
    public $deleted;
    public $defectno;
    public $printlayout = 'Portrait';
    public $form = 'dt192';
    public $formType = 'Arc';
    public $formname = 'TRI-METAL WELD AND ZONE REPAIRS (DT192)';
    
    public $formLayout = '/dt192/_form.php';
    public $viewLayout = '@modules/forms/views/dt192/_view.php';
    public $createpath = '/forms/dt192/create';
    public $updated_by2;
    public $created_by2;
    public $padno;
    

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
     
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['deliveryunit', 'sitelocation','defectno', 'route', 'line', 'miles', 'yards', 'pointno', 'crossingmanufacturer', 'crossingno', 'legendfr', 'type', 'angle', 'legendlhrh', 'mlength', 'mdepth', 'mwidth', 'mtlength', 'mtdepth', 'mtwidth', 'encrouch5mm', 'distancefromtmwnorm', 'distancefromtmwmang', 'overall', 'depth', 'nglength', 'ngdepth', 'ngwidth', 'electrodeused', 'processmma', 'processsemiauto', 'processfullyauto', 'weldersname', 'weldersstamp', 'supervisorname', 'signature'], 'string'],
            [['actid','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',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'date' => 'Date',
            'deliveryunit' => 'Delivery Unit',
            'sitelocation' => 'Location',
            'route' => 'Route',
            'line' => 'Line',
            'miles' => 'Miles',
            'yards' => 'Yards',
            'pointno' => 'Pointno',
            'crossingmanufacturer' => 'Crossingmanufacturer',
            'crossingno' => 'Crossingno',
            'legendfr' => 'Legendfr',
            'type' => 'Type',
            'angle' => 'Angle',
            'legendlhrh' => 'Legendlhrh',
            'mlength' => 'Mlength',
            'mdepth' => 'Mdepth',
            'mwidth' => 'Mwidth',
            'mtlength' => 'Mtlength',
            'mtdepth' => 'Mtdepth',
            'mtwidth' => 'Mtwidth',
            'encrouch5mm' => 'Encrouch5mm',
            'distancefromtmwnorm' => 'Distancefromtmwnorm',
            'distancefromtmwmang' => 'Distancefromtmwmang',
            'overall' => 'Overall',
            'depth' => 'Depth',
            'nglength' => 'Nglength',
            'ngdepth' => 'Ngdepth',
            'ngwidth' => 'Ngwidth',
            'electrodeused' => 'Electrodeused',
            'processmma' => 'Processmma',
            'processsemiauto' => 'Processsemiauto',
            'processfullyauto' => 'Processfullyauto',
            'weldersname' => 'Welders Name',
            'weldersstamp' => 'Welders Stamp',
            'supervisorname' => 'Supervisor Name',
            'signature' => 'Signature',
            'defectno' => 'Defect Number',
        ];
    }
    
    public function beforeValidate() {
		if (Yii::$app->name != 'app-console' ) {
            $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 getAccount() {
		return $this->hasOne(Accounts::class, ['AccountID' => $this->company])
		->Via('activity');
	}
    public function getUser() {
        return $this->hasOne(User::class, ['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', 'location', 'sdate', 'elr', 'weekno', 'company'])->where(['id' => $actid])->asArray()->one();
           //$this->jobno = $act['jobno'];
        //   $this->inlineno = $act['jobno'];
           //$this->jcnno = $act['jobno'];
           //$this->client = $act['relatedid'];
           $this->sitelocation = $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;
    }
}
