<?php

namespace modules\forms\models;
use frontend\models\Settings;
use modules\users\models\User;
use modules\activities\models\Activities;
use modules\custom\models\Accounts;
use yii\base\Model;
use Yii;

/**
 * This is the model class For table "dt125".
 *
 * @property integer $id
 * @property string $sheet
 * @property string $area
 * @property string $weekno
 * @property string $jobcostno
 * @property string $from
 * @property string $tmetsm
 * @property string $date
 * @property string $ellipseno
 * @property string $telno
 * @property string $stampno
 * @property string $weldersignature
 * @property string $inlineno
 * @property string $starttime
 * @property string $starttraveltime
 * @property string $finishtime
 * @property string $finishtraveltime
 * @property string $siterepsign
 * @property string $siterepprint
 * @property string $siterepdate
 * @property string $location
 * @property string $access
 * @property string $trackid
 * @property string $mileage
 * @property string $pointsno
 * @property string $grade
 * @property string $type
 * @property string $design
 * @property string $angle
 * @property string $component1
 * @property string $defecttype1
 * @property string $length1
 * @property string $width1
 * @property string $depth1
 * @property string $defectno1
 * @property string $preheattemp1
 * @property string $exclength1
 * @property string $excdepth1
 * @property string $consumetype1
 * @property string $consumedia1
 * @property string $component2
 * @property string $defecttype2
 * @property string $length2
 * @property string $width2
 * @property string $depth2
 * @property string $defectno2
 * @property string $preheattemp2
 * @property string $exclength2
 * @property string $excdepth2
 * @property string $consumetype2
 * @property string $consumedia2
 * @property string $component3
 * @property string $defecttype3
 * @property string $length3
 * @property string $width3
 * @property string $depth3
 * @property string $defectno3
 * @property string $preheattemp3
 * @property string $exclength3
 * @property string $excdepth3
 * @property string $consumetype3
 * @property string $consumedia3
 * @property string $weldcompleted
 * @property string $preheater
 * @property string $generator
 * @property string $wfu
 * @property string $dateinspected1
 * @property string $inspectedby1
 * @property string $result1
 * @property string $comments1
 * @property string $databaseno1
 * @property string $dateinspected2
 * @property string $inspectedby2
 * @property string $result2
 * @property string $comments2
 * @property string $databaseno2
 * @property string $dateinspected3
 * @property string $inspectedby3
 * @property string $result3
 * @property string $comments3
 * @property string $databaseno3
 * @property string $arcby
 * @property string $arcdate
 * @property string $insname
 * @property string $insdate
 * @property string $ellipseby
 * @property string $ellipsedate
 * @property string $formnos
 * @property integer $workid
 * @property string $databasenos
 * @property string $weldersname
 * @property string $client
 * @property string $createdon
 * @property string $createdby
 * @property string $drawingloc
 * @property string $signatureloc
 */
class Dt125 extends Model {

    public $id;
    public $sheet;
    public $area;
    public $weekno;
    public $jobcostno;
    public $_from;
    public $tmetsm;
    public $date;
    public $ellipseno;
    public $telno;
    public $stampno;
    public $weldersignature;
    public $inlineno;
    public $starttime;
    public $starttraveltime;
    public $finishtime;
    public $finishtraveltime;
    public $siterepsign;
    public $siterepprint;
    public $siterepdate;
    public $location;
    public $access;
    public $trackid;
    public $mileage;
    public $pointsno;
    public $grade;
    public $type;
    public $design;
    public $angle;
    public $component1;
    public $defecttype1;
    public $length1;
    public $width1;
    public $depth1;
    public $defectno1;
    public $preheattemp1;
    public $exclength1;
    public $excdepth1;
    public $consumetype1;
    public $consumedia1;
    public $component2;
    public $defecttype2;
    public $length2;
    public $width2;
    public $depth2;
    public $defectno2;
    public $preheattemp2;
    public $exclength2;
    public $excdepth2;
    public $consumetype2;
    public $consumedia2;
    public $component3;
    public $defecttype3;
    public $length3;
    public $width3;
    public $depth3;
    public $defectno3;
    public $preheattemp3;
    public $exclength3;
    public $excdepth3;
    public $consumetype3;
    public $consumedia3;
    public $weldcompleted;
    public $preheater;
    public $generator;
    public $wfu;
    public $dateinspected1;
    public $inspectedby1;
    public $result1;
    public $comments1;
    public $databaseno1;
    public $dateinspected2;
    public $inspectedby2;
    public $result2;
    public $comments2;
    public $databaseno2;
    public $dateinspected3;
    public $inspectedby3;
    public $result3;
    public $comments3;
    public $databaseno3;
    public $arcby;
    public $arcdate;
    public $insname;
    public $insdate;
    public $ellipseby;
    public $ellipsedate;
    public $formnos;
    public $workid;
    public $databasenos;
    public $weldersname;
    public $client;
    public $createdon;
    public $createdby;
    public $drawingloc;
    public $signatureloc;
    public $wsignatureloc;
    public $elr;
    public $mileageb;
    public $mileagec;
    public $mileaged;
    public $dt125comments;
    public $padno;
    public $account;
    public $actid;
    public $snag1;
    public $snag2;
    public $snag3;
    public $snag4;
    public $snag1id;
    public $snag2id;
    public $snag3id;
    public $snag4id;
    public $updated_at;
    public $updated_by;
    public $updated_by2;
    public $created_at;
    public $created_by;
    public $created_by2;
    public $tracktrolley;
    public $deleted;
    public $deleted_at;
    public $deleted_by;
    public $relatedid;
    public $jobno;
    public $status;
    
    public $printlayout = 'Landscape';
    public $form = 'DT125';
    public $formType = 'Arc';
    public $formLayout = '/dt125/_form.php';
    public $viewLayout = '@modules/forms/views/dt125/_view.php';
    public $createpath = '/forms/dt125/create';
    public $formname = 'Welders Work Return - Crossing Repair/Maintenance: (DT125)';
    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['jobno', 'workid'], 'integer'],
            [['snag1', 'snag1id', 'snag2', 'snag2id', 'snag3', 'snag3id', 'snag4', 'snag4id','tracktrolley', 'created_by2', 'updated_by2','deleted_by','relatedid', 'status', 'account' ], 'string'], //converts unsafe
            [['deleted'], 'boolean'],
            [['createdon', 'drawingloc', 'signatureloc', 'wsignatureloc', 'weldersname', 'actid','client'], 'safe'],
            [['sheet', 'area', 'weekno', 'jobcostno', '_from', 'tmetsm', 'date', 'ellipseno', 'telno', 'stampno', 'weldersignature', 'inlineno', 'starttime', 'starttraveltime', 'finishtime', 'finishtraveltime', 'siterepsign', 'siterepprint', 'siterepdate', 'location', 'access', 'trackid', 'mileage', 'mileageb', 'mileagec', 'mileaged', 'pointsno', 'grade', 'type', 'design', 'angle', 'component1', 'defecttype1', 'length1', 'width1', 'depth1', 'defectno1', 'preheattemp1', 'exclength1', 'excdepth1', 'consumetype1', 'consumedia1', 'component2', 'defecttype2', 'length2', 'width2', 'depth2', 'defectno2', 'preheattemp2', 'exclength2', 'excdepth2', 'consumetype2', 'consumedia2', 'component3', 'defecttype3', 'length3', 'width3', 'depth3', 'defectno3', 'preheattemp3', 'exclength3', 'excdepth3', 'consumetype3', 'consumedia3', 'weldcompleted', 'preheater', 'generator', 'wfu', 'dateinspected1', 'inspectedby1', 'result1', 'comments1', 'databaseno1', 'dateinspected2', 'inspectedby2', 'result2', 'comments2', 'databaseno2', 'dateinspected3', 'inspectedby3', 'result3', 'comments3', 'databaseno3', 'arcby', 'arcdate', 'insname', 'insdate', 'ellipseby', 'ellipsedate', 'elr', 'padno'], 'string', 'max' => 100],
            [['formnos', 'databasenos'], 'string', 'max' => 45],
            [['dt125comments'], 'string'],
			//Defaults
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			//Redundant
			
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'sheet' => 'Sheet',
            'area' => 'Area',
            'weekno' => 'Weekno',
            'jobcostno' => 'Jobcostno',
            '_from' => 'From',
            'tmetsm' => 'Tmetsm',
            'date' => 'Date',
            'ellipseno' => 'Ellipseno',
            'telno' => 'Telno',
            'stampno' => 'Stampno',
            'weldrsignature' => 'Weldersignature',
            'inlineno' => 'Inlineno',
            'starttime' => 'Starttime',
            'starttraveltime' => 'Starttraveltime',
            'finishtime' => 'Finishtime',
            'finishtraveltime' => 'Finishtraveltime',
            'siterepsign' => 'Siterepsign',
            'siterepprint' => 'Siterepprint',
            'siterepdate' => 'Siterepdate',
            'location' => 'Location',
            'access' => 'Access',
            'trackid' => 'Trackid',
            'mileage' => 'Mileage',
            'pointsno' => 'Pointsno',
            'grade' => 'Grade',
            'type' => 'Type',
            'design' => 'Design',
            'angle' => 'Angle',
            'component1' => 'Component1',
            'defecttype1' => 'Defecttype1',
            'length1' => 'Length1',
            'width1' => 'Width1',
            'depth1' => 'Depth1',
            'defectno1' => 'Defectno1',
            'preheattemp1' => 'Preheattemp1',
            'exclength1' => 'Exclength1',
            'excdepth1' => 'Excdepth1',
            'consumetype1' => 'Consumetype1',
            'consumedia1' => 'Consumedia1',
            'component2' => 'Component2',
            'defecttype2' => 'Defecttype2',
            'length2' => 'Length2',
            'width2' => 'Width2',
            'depth2' => 'Depth2',
            'defectno2' => 'Defectno2',
            'preheattemp2' => 'Preheattemp2',
            'exclength2' => 'Exclength2',
            'excdepth2' => 'Excdepth2',
            'consumetype2' => 'Consumetype2',
            'consumedia2' => 'Consumedia2',
            'component3' => 'Component3',
            'defecttype3' => 'Defecttype3',
            'length3' => 'Length3',
            'width3' => 'Width3',
            'depth3' => 'Depth3',
            'defectno3' => 'Defectno3',
            'preheattemp3' => 'Preheattemp3',
            'exclength3' => 'Exclength3',
            'excdepth3' => 'Excdepth3',
            'consumetype3' => 'Consumetype3',
            'consumedia3' => 'Consumedia3',
            'weldcompleted' => 'Weldcompleted',
            'preheater' => 'Preheater',
            'generator' => 'Generator',
            'wfu' => 'Wfu',
            'dateinspected1' => 'Dateinspected1',
            'inspectedby1' => 'Inspectedby1',
            'result1' => 'Result1',
            'comments1' => 'Comments1',
            'databaseno1' => 'Databaseno1',
            'dateinspected2' => 'Dateinspected2',
            'inspectedby2' => 'Inspectedby2',
            'result2' => 'Result2',
            'comments2' => 'Comments2',
            'databaseno2' => 'Databaseno2',
            'dateinspected3' => 'Dateinspected3',
            'inspectedby3' => 'Inspectedby3',
            'result3' => 'Result3',
            'comments3' => 'Comments3',
            'databaseno3' => 'Databaseno3',
            'arcby' => 'Arcby',
            'arcdate' => 'Arcdate',
            'insname' => 'Insname',
            'insdate' => 'Insdate',
            'ellipseby' => 'Ellipseby',
            'ellipsedate' => 'Ellipsedate',
            'formnos' => 'Formnos',
            'workid' => 'Workid',
            'databasenos' => 'Databasenos',
            'weldersname' => 'Weldersname',
            'client' => 'Client',
            'createdon' => 'Createdon',
            'createdby' => 'Createdby',
            'drawingloc' => 'Drawingloc',
            'signatureloc' => 'Signatureloc',
            'snag1' => 'snag1',
            'snag2' => 'snag2',
            'snag3' => 'snag3',
            'snag4' => 'snag4',
            'snag1id' => 'snag1id',
            'snag2id' => 'snag2id',
            'snag3id' => 'snag3id',
            'snag4id' => 'snag4id',
        ];
    }
    public function getAccountl()
    {
        return Accounts::findOne(['id' => $this->client]);
    }

    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->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 function beforeValidate(){
        $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        return parent::beforeValidate();
    }
    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;
    }
}