<?php

namespace modules\forms\models;

use Yii;
use Yii\base\Model;

use modules\activities\models\Activities;
use modules\users\models\User;
use modules\custom\models\Accounts;

/**
 * This is the model class For table "dt129".
 *
 * @property integer $id
 * @property string $date
 * @property string $location
 * @property string $client
 * @property string $weldersname
 * @property string $welderssignature
 * @property string $starttime
 * @property string $finishtime
 * @property string $injobno
 * @property string $issueno1
 * @property string $timereported1
 * @property string $description1
 * @property string $timelost1
 * @property string $issueno2
 * @property string $timereported2
 * @property string $description2
 * @property string $timelost2
 * @property string $issueno3
 * @property string $timereported3
 * @property string $description3
 * @property string $timelost3
 * @property string $issueno4
 * @property string $timereported4
 * @property string $description4
 * @property string $timelost4
 * @property string $issueno5
 * @property string $timereported5
 * @property string $description5
 * @property string $timelost5
 * @property string $issueno6
 * @property string $timereported6
 * @property string $description6
 * @property string $timelost6
 * @property string $issueno7
 * @property string $timereported7
 * @property string $description7
 * @property string $timelost7
 * @property string $issueno8
 * @property string $timereported8
 * @property string $description8
 * @property string $timelost8
 * @property string $issueno9
 * @property string $timereported9
 * @property string $description9
 * @property string $timelost9
 * @property string $issueno10
 * @property string $timereported10
 * @property string $description10
 * @property string $timelost10
 * @property string $totaltimelost
 * @property string $clientname
 * @property string $clientsignature
 */
class Dt129 extends Model {

    public $id;
public $date;
public $location;
public $client;
public $weldersname;
public $welderssignature;
public $starttime;
public $finishtime;
public $injobno;
public $issueno1;
public $timereported1;
public $description1;
public $timelost1;
public $issueno2;
public $timereported2;
public $description2;
public $timelost2;
public $issueno3;
public $timereported3;
public $description3;
public $timelost3;
public $issueno4;
public $timereported4;
public $description4;
public $timelost4;
public $issueno5;
public $timereported5;
public $description5;
public $timelost5;
public $issueno6;
public $timereported6;
public $description6;
public $timelost6;
public $issueno7;
public $timereported7;
public $description7;
public $timelost7;
public $issueno8;
public $timereported8;
public $description8;
public $timelost8;
public $issueno9;
public $timereported9;
public $description9;
public $timelost9;
public $issueno10;
public $timereported10;
public $description10;
public $timelost10;
public $totaltimelost;
public $clientname;
public $clientsignature;

public $created_at;
public $updated_at;
public $deleted_at;
public $deleted_by;
public $created_by;
public $updated_by;
public $tracktrolley;
public $relatedid;
public $status;

public $wsignatureloc;
public $signatureloc;
public $jobno;
public $padno;
public $deleted;
public $created_by2;
public $updated_by2;
public $actid;

    public $printlayout = 'Portrait';
    public $form = 'DT129';
    public $formname = 'SITE REPORT FORM (DT129)';

    
    public $createpath = '/forms/dt129/create';
    public $formType = 'Other';
    public $formLayout = '/dt129/_form.php';
    public $viewLayout = '@modules/forms/views/dt129/_view.php';public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['weldersname', 'date', 'wsignatureloc', 'signatureloc','actid' ], 'safe'],
            [['location', 'client', 'welderssignature', 'starttime', 'finishtime', 'injobno', 'issueno1', 'timereported1', 'description1', 'timelost1', 'issueno2', 'timereported2', 'description2', 'timelost2', 'issueno3', 'timereported3', 'description3', 'timelost3', 'issueno4', 'timereported4', 'description4', 'timelost4', 'issueno5', 'timereported5', 'description5', 'timelost5', 'issueno6', 'timereported6', 'description6', 'timelost6', 'issueno7', 'timereported7', 'description7', 'timelost7', 'issueno8', 'timereported8', 'description8', 'timelost8', 'issueno9', 'timereported9', 'description9', 'timelost9', 'issueno10', 'timereported10', 'description10', 'timelost10', 'totaltimelost', 'clientname', 'clientsignature', 'padno'], 'string'],
        					//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',
            'date' => 'Date',
            'location' => 'Location',
            'client' => 'Client',
            'weldersname' => 'Welders Name',
            'welderssignature' => 'Welders Signature',
            'starttime' => 'Start Time',
            'finishtime' => 'Finish Time',
            'injobno' => 'Job No',
            'issueno1' => 'Issueno1',
            'timereported1' => 'Timereported1',
            'description1' => 'Description1',
            'timelost1' => 'Timelost1',
            'issueno2' => 'Issueno2',
            'timereported2' => 'Timereported2',
            'description2' => 'Description2',
            'timelost2' => 'Timelost2',
            'issueno3' => 'Issueno3',
            'timereported3' => 'Timereported3',
            'description3' => 'Description3',
            'timelost3' => 'Timelost3',
            'issueno4' => 'Issueno4',
            'timereported4' => 'Timereported4',
            'description4' => 'Description4',
            'timelost4' => 'Timelost4',
            'issueno5' => 'Issueno5',
            'timereported5' => 'Timereported5',
            'description5' => 'Description5',
            'timelost5' => 'Timelost5',
            'issueno6' => 'Issueno6',
            'timereported6' => 'Timereported6',
            'description6' => 'Description6',
            'timelost6' => 'Timelost6',
            'issueno7' => 'Issueno7',
            'timereported7' => 'Timereported7',
            'description7' => 'Description7',
            'timelost7' => 'Timelost7',
            'issueno8' => 'Issueno8',
            'timereported8' => 'Timereported8',
            'description8' => 'Description8',
            'timelost8' => 'Timelost8',
            'issueno9' => 'Issueno9',
            'timereported9' => 'Timereported9',
            'description9' => 'Description9',
            'timelost9' => 'Timelost9',
            'issueno10' => 'Issueno10',
            'timereported10' => 'Timereported10',
            'description10' => 'Description10',
            'timelost10' => 'Timelost10',
            'totaltimelost' => 'Totaltimelost',
            'clientname' => 'Clientname',
            'clientsignature' => 'Clientsignature',
            'createdon' => 'created On',
            'createdby' => 'created By',
        ];
    }
    public function getAccount()
    {
        return Accounts::findOne(['id' => $this->client]);
    }
    public function getActivity()
    {
		return Activities::findOne(['id' => $this->actid]);
	}
    public function getUser() {
        return User::findOne(['id' => $this->created_by]);
    }

    public function beforeValidate(){      
        if (Yii::$app->name !== 'app-console' ) {
            $this->weldersname = is_array($this->weldersname) ?  implode(",", $this->weldersname) : $this->weldersname;
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        }
        return parent::beforeValidate();
    }

    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->injobno = $act['jobno'];
           $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;
    }
}