<?php
namespace modules\forms\models;
use modules\activities\models\Activities;
use modules\users\models\User;
use modules\custom\models\Accounts;
use Yii;
use Yii\base\Model;

/**
 * This is the model class For table "dt198".
 *
 * @property string $id
 * @property string $actid
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property integer $client
 * @property string $insdate
 * @property string $location
 * @property string $jobno
 * @property string $signprint
 * @property string $signed
 * @property string $signeddate
 * @property string $comments
 * @property string $seniorwelder
 * @property string $assistant1
 * @property string $assistant2
 * @property string $assistant3
 * @property integer $plannedmeterage
 * @property integer $actualmeterage
 * @property integer $shiftstart
 * @property integer $shiftend
 * @property string $railgrade
 * @property integer $curveradii
 * @property string $filename
 * @property string $filedata
 */

class Dt198 extends Model {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    public $id;
    public $actid;
    public $deleted_at;
    public $deleted_by;
    public $created_at;
    public $created_by;
    public $updated_at;
    public $updated_by;
    public $created_by2;
    public $updated_by2;
    public $client;
    public $insdate;
    public $location;
    public $jobno;
    public $signprint;
    public $signed;
    public $signeddate;
    public $comments;
    public $seniorwelder;
    public $assistant1;
    public $assistant2;
    public $assistant3;
    public $plannedmeterage;
    public $actualmeterage;
    public $shiftstart;
    public $shiftend;
    public $railgrade;
    public $curveradii;
    public $filename;
    public $filedata;
    public $padno;
    public $deleted;


    public $rowtype = [];
    public $rowdeleted = [];
    public $rowno = [];
    public $rowactid = [];
    public $rowrowno = [];
    public $rowid = [];
    public $rowrelatedid = [];
    public $rowjobno = [];
    public $rowpadno = [];
    public $rowdistance = [];
    public $rowdt198id = [];
    public $rowvoltsavg = [];
    public $rowvoltsmax = [];
    public $rowvoltsmin = [];
    public $rowvoltsvavg = [];
    public $rowvoltsvmax = [];
    public $rowvoltsvmin = [];
    public $rowampsavg = [];
    public $rowampsmax = [];
    public $rowampsmin = [];
    public $rowampsaavg = [];
    public $rowampsamax = [];
    public $rowampsamin = [];
    public $rowspeedavg = [];
    public $rowspeedmax = [];
    public $rowspeedmin = [];
    public $rowinputkjavg = [];
    public $rowinputkjmax = [];
    public $rowinputkjmin = [];
    public $rowstrikeonavg = [];
    public $rowstrikeonmax = [];
    public $rowstrikeonmin = [];
    public $rowstrikeoffavg = [];
    public $rowstrikeoffmax = [];
    public $rowstrikeoffmin = [];
    public $rowinterpassavg = [];
    public $rowinterpassmax = [];
    public $rowinterpassmin = [];
    public $rowstatus = [];
    public $rowdeleted_at = [];
    public $rowdeleted_by = [];
    public $rowupdated_at = [];
    public $rowupdated_by = [];
    public $rowupdated_at2 = [];
    public $rowupdated_by2 = [];
    public $rowcreated_at = [];
    public $rowcreated_by = [];
    public $rowcreated_by2 = [];


    public $printlayout = 'Landscape';
    public $form = 'Dt198';
    public $formname = 'Gauge Corner (DT198)';
    public $formType = 'Gauge Corner';
    
    
    public $createpath = '/forms/dt198/create';
    public $formLayout = '/dt198/_form.php';
    public $viewLayout = '@modules/forms/views/dt198/_view.php';/**
     * @inheritdoc
     */

     public function rules()
    {
        return [
            [['id', 'actid', 'client'], 'string'],
            [[ 'location',  'signprint', 'signed', 'comments', 'seniorwelder', 'assistant1', 'assistant2', 'assistant3', 'railgrade', 'filename', 'filedata'], 'string'],
            [['rowstatus', 'rowdistance', 'rowtype', 'rowno', 'rowvoltsavg', 'rowvoltsmax', 'rowvoltsmin', 'rowampsavg', 'rowampsmax', 'rowampsmin',  'rowvoltsvmax', 'rowvoltsvmin', 'rowampsaavg', 'rowampsamax', 'rowampsamin', 'rowspeedavg', 'rowspeedmax', 'rowspeedmin', 'rowinputkjavg', 'rowinputkjmax', 'rowinputkjmin', 'rowstrikeonavg', 'rowstrikeonmax', 'rowstrikeonmin', 'rowstrikeoffavg', 'rowstrikeoffmax', 'rowstrikeoffmin', 'rowinterpassavg', 'rowinterpassmax', 'rowinterpassmin', ], 'safe'],
            [['plannedmeterage', 'actualmeterage',  'curveradii'], 'integer'],
            [['insdate', 'signeddate','shiftstart','jobno', 'shiftend'], 'safe'],
					       //Defaults
        		[['deleted'], 'boolean'],
        		['deleted', 'default', 'value' => 0],
        		[['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',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'client' => 'Client',
            'insdate' => 'Insdate',
            'location' => 'Location',
            'jobno' => 'Jobno',
            'signprint' => 'Signprint',
            'signed' => 'Signed',
            'signeddate' => 'Signeddate',
            'comments' => 'Comments',
            'seniorwelder' => 'Seniorwelder',
            'assistant1' => 'Assistant1',
            'assistant2' => 'Assistant2',
            'assistant3' => 'Assistant3',
            'plannedmeterage' => 'Plannedmeterage',
            'actualmeterage' => 'Actualmeterage',
            'shiftstart' => 'Shiftstart',
            'shiftend' => 'Shiftend',
            'railgrade' => 'Railgrade',
            'curveradii' => 'Curveradii',
            'filename' => 'Filename',
            'filedata' => 'Filedata',
        ];
    }


	//defaults
	public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
	}

	public function getClientl() {
		return Accounts::findOne(['id' => $this->client]);
	}
    public function beforeValidate(){
        $this->padno = $this->padno == null ? (Yii::$app->user->isGuest == false ? User::Padno() : $this->padno) : $this->padno;
        return parent::beforeValidate();
    }

    public function init()
    {
        // Parameters 
        if((Yii::$app->controller->action->id == 'getdropbox' || Yii::$app->controller->action->id == 'create') && $this->actid !== null ){
           $act = Activities::find()->select(['jobno', 'relatedid', 'location', 'sdate'])->where(['id' => $this->actid])->asArray()->one();
           $this->jobno = $act['jobno'];
           $this->client = $act['relatedid'];
           $this->location = $act['location'];
           $this->insdate = date('d/m/Y H:i', strtotime($act['sdate']));
        }
        return parent::init();
    }

    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'],
        ];
        $buttons[$_this->formType]['items'][] = [
            'label' => 'Get Customer Data from Dropbox',
            'url' => ['/forms/getdropbox', 'actid' => $model->id],
            'linkOptions' =>  ['target'=>'_blank'],
        ];
        $buttons[$_this->formType]['items'][] = [
            'label' => 'Get Customer Data from Email',
            'url' => ['/forms/dt198mailbox', 'actid' => $model->id],
            'linkOptions' =>  ['target'=>'_blank'],
        ];
        return $buttons;
    }
}
