<?php

namespace modules\forms\models;
use Yii;
use yii\base\Model;
use modules\activities\models\Activities;
use modules\custom\models\{Accounts, Contacts};
use modules\users\models\User;
/**
 * This is the model class For table "dt_was".
 *
 * @property integer $id
 * @property string $cay
 * @property string $cate
 * @property integer $week
 * @property string $jcnno
 * @property string $location
 * @property string $postcode
 * @property string $possessiontime
 * @property string $nowelds
 * @property string $railsection
 * @property string $railgrade
 * @property string $comments
 * @property string $name1
 * @property string $name2
 * @property string $name3
 * @property string $name4
 * @property string $name5
 * @property string $company1
 * @property string $compamy2
 * @property string $company3
 * @property string $company4
 * @property string $company5
 * @property string $grade1
 * @property string $grade2
 * @property string $grade3
 * @property string $grade4
 * @property string $grade5
 * @property string $contactno1
 * @property string $contactno2
 * @property string $contactno3
 * @property string $contactno4
 * @property string $contactno5
 * @property string $oncallmanager
 * @property string $oncallmanagerno
 */
class Dtwas extends Model {


    public $id;
    public $cay;
    public $cate;
    public $week;
    public $jcnno;
    public $location;
    public $postcode;
    public $possessiontime;
    public $nowelds;
    public $railsection;
    public $railgrade;
    public $comments;
    public $name1;
    public $name2;
    public $name3;
    public $name4;
    public $name5;
    public $name6;
    public $name7;
    public $name8;
    public $name9;
    public $name10;
    public $company1;
    public $company2;
    public $compamy2;
    public $company3;
    public $company4;
    public $company5;
    public $company6;
    public $company7;
    public $company8;
    public $company9;
    public $company10;
    public $grade1;
    public $grade2;
    public $grade3;
    public $grade4;
    public $grade5;
    public $grade6;
    public $grade7;
    public $grade8;
    public $grade9;
    public $grade10;
    public $contactno1;
    public $contactno2;
    public $contactno3;
    public $contactno4;
    public $contactno5;
    public $contactno6;
    public $contactno7;
    public $contactno8;
    public $contactno9;
    public $contactno10;
    public $oncallmanager;
    public $oncallmanagerno;
    public $actid;
    public $padno;
    public $relatedid;
    public $created_at;
    public $updated_at;
    public $deleted_at;
    public $created_by;
    public $updated_by;
    public $deleted_by;
    public $created_by2;
    public $updated_by2;
    public $status;
    public $deleted;
    
    public $day;
    public $date;
    public $starttime;
    public $finishtime;
    public $company;
    public $siteinstructions;
    public $furtherinfo;
    public $sign;
    public $planneddate;
    public $amendmentdate;
    public $signdate;
    public $hscontact;
    public $oncallman;
    public $emails;
    public $email1;
    public $email2;
    public $email3;
    public $email4;
    public $email5;
    public $email6;
    public $email7;
    public $email8;
    public $email9;
    public $email10;
    public $pcemails;
    public $pcemail1;
    public $pcemail2;
    public $pcemail3;
    public $pcemail4;
    public $pcemail5;
    public $pcemail6;
    public $pcemail7;
    public $pcemail8;
    public $pcemail9;
    public $pcemail10;
    public $usernames;
    public $username1;
    public $username2;
    public $username3;
    public $username4;
    public $username5;
    public $username6;
    public $username7;
    public $username8;
    public $username9;
    public $username10;
    public $jobno;
    public $companyint;
    public $facilities_provider;
    public $facilities_notes;


    public $printlayout = 'Portrait';
    public $form = 'dtwas';
    public $formname = 'Welders Arrangement Sheet';
    public $formLayout = '/dtwas/_form.php';
    public $viewLayout = '@modules/forms/views/dtwas/_view.php';
    public $formType = 'Administration';
    public $createpath = '/forms/dtwas/create';


    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    /**
     * @inheritdoc
     */

     

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['day', 'comments', 'furtherinfo', 'siteinstructions'], 'string'],
            [['date', 'actid', 'planneddate', 'amendmentdate','signdate', 'sign'], 'safe'],
            [[ 'name1', 'name2', 'week', 'hscontact', 'oncallman', 'companyint'], 'integer'],
            [['facilities_provider', 'facilities_notes'], 'string'],
            [['jcnno', 'location', 'postcode', 'starttime', 'finishtime', 'nowelds', 'railsection', 'railgrade','name3', 'name4', 'name5', 'company', 'company2', 'company3', 'company4', 'company5', 'grade1', 'grade2', 'grade3', 'grade4', 'grade5', 'contactno1', 'contactno2', 'contactno3', 'contactno4', 'contactno5', 'oncallmanager', 'oncallmanagerno', 'starttime', 'finishtime' ], 'string', 'max' => 45],
            [['company6', 'company7', 'company8', 'company9', 'company10', 'name6', 'name7', 'name8', 'name9', 'name10', 'grade6', 'grade7', 'grade8', 'grade9', 'grade10', 'contactno6', 'contactno7', 'contactno8', 'contactno9', 'contactno10', 'email1', 'email2', 'email3', 'email4', 'email5', 'email6', 'email7', 'email8', 'email9', 'email10', 'username1', 'username2', 'username3', 'username4', 'username5', 'username6', 'username7', 'username8', 'username9', 'username10', ], 'string', 'max' => 100],
            [['pcemails', 'emails' ], 'string', 'max' => 1000],
            ['name1', 'default', 'value' => null],
            [['hscontact', 'oncallman'], 'required'],
			[['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',
            'day' => 'Day',
            'date' => 'Date',
            'week' => 'Week',
            'jcnno' => 'Jcnno',
            'location' => 'Location',
            'postcode' => 'Postcode',
            'possessiontime' => 'Possessiontime',
            'nowelds' => 'Nowelds',
            'railsection' => 'Railsection',
            'railgrade' => 'Railgrade',
            'comments' => 'Comments',
            'name1' => 'Name1',
            'name2' => 'Name2',
            'name3' => 'Name3',
            'name4' => 'Name4',
            'name5' => 'Name5',
            'company' => 'Company1',
            'company2' => 'Company2',
            'company3' => 'Company3',
            'company4' => 'Company4',
            'company5' => 'Company5',
            'grade1' => 'Grade1',
            'grade2' => 'Grade2',
            'grade3' => 'Grade3',
            'grade4' => 'Grade4',
            'grade5' => 'Grade5',
            'contactno1' => 'Contactno1',
            'contactno2' => 'Contactno2',
            'contactno3' => 'Contactno3',
            'contactno4' => 'Contactno4',
            'contactno5' => 'Contactno5',
            'oncallmanager' => 'On Call Manager',
            'hscontact' => 'H&S Contact',
            'oncallmanagerno' => 'On Callmanager Number',
            'starttime' => 'Start Time', 
            'finishtime' => 'Finish Time',
            'facilities_provider' => 'Welfare Facilities Provider',
            'facilities_notes' => 'Welfare Facilities Notes',
        ];
    }
       
    public function getAccount() {
        return Accounts::findOne(['account_name' => $this->client]);
    }
    public function getCompanyl1() {
        return Accounts::findOne(['id' => $this->company]);
    }
    public function getNamel1() {
        
       if (empty($this->name1) == false) {
           return Contacts::findOne(['_id' => $this->name1]);
        } else {
            return null;
        }
    }
    public function getCompanyl2() {
        return Accounts::findOne(['id' => $this->company2]);
    }
    public function getNamel2() {
        return Contacts::findOne(['_id' => $this->name2]);
    }
    public function getCompanyl3() {
        return Accounts::findOne(['accountid' => $this->company3]);
    }
    public function getNamel3() {
		return User::findOne(['id' => $this->name3]);
    }
	public function getCompanyl4() {
        return Accounts::findOne(['accountid' => $this->company4]);
    }
	public function getNamel4() {
       if ($this->name4 != '') {} 
        return User::findOne(['_id' => $this->name4]);			
 	} 	
    
	public function getCompanyl5() {
        return Accounts::findOne(['accountid' => $this->company5]);
    }
	public function getNamel5() {
		return User::findOne(['_id' => $this->name5]);
    }
 
    public function getNamel6() {
        return User::findOne(['_id' => $this->name6]);
    }
     public function getNamel7() {
    		return User::findOne(['_id' => $this->name7]);
	}
     public function getNamel8() {
    		return User::findOne(['_id' => $this->name8]);
	}
     public function getNamel9() {
    		return User::findOne(['_id' => $this->name9]);
	}
	
     public function getNamel10() {
    		return User::findOne(['_id' => $this->name10]);
	}
	Public function getLogo() {
		return ArrayHelper::map(Settings::find()->where("id = '1'")->all(), 'logo', 'logo');
	}
    public function getActivity() {
		return Activities::findOne(['id' => $this->actid]);
	}
	 public function getJob() {
		return Activities::findOne(['id' => $this->actid]);
	}

	public function getEmailsubject() {
		return "Inline Work Instruction and Technical Review - ".$this->location;
	}

	function getDtwps1() {
		return Dtwps::findOne(['id' => "weldprocess1"])
        ->viaTable('activities', ['id' => $this->actid]);
			
	}
	function getDtwps2() {
		return Dtwps::findOne(['id' => "weldprocess2"])
			->viaTable('activities', ['id' => $this->actid]);
	}
	function getDtwps3() {
		return Dtwps::findOne(['id' => "weldprocess3"])
			->viaTable('activities', ['id' => $this->actid]);
	}
		function getOcm() {
		return User::findOne(['_id' => $this->oncallman]);
	}
		function getHscontactl() {
		return User::findOne(['_id' => $this->hscontact]);
	}
	
    public function beforeValidate() {
        if (Yii::$app->name !== 'app-console' ) {
           // $this->name1 = empty($this->name1) == true ?  1100 : $this->name1;
            $this->planneddate = is_integer($this->planneddate) == false ? strtotime($this->planneddate) : $this->planneddate;
            $this->amendmentdate = is_integer($this->amendmentdate) == false ? strtotime($this->amendmentdate) : $this->amendmentdate;
            $this->signdate = is_integer($this->signdate) == false ? strtotime($this->signdate) : $this->signdate;
            $this->company2 = empty($this->name2) == false ?  $this->company : null;
        }
        return parent::beforeValidate();
    }
    public function afterValidate(){
        parent::afterValidate();
        Yii::error($this->errors);
        return ;
    }
    public function init()
    {
        if($this->actid !== null) {
            $act = Activities::findOne(['id' => $this->actid]);
            $this->company = $act ?  $act->relatedid : null;
            //$this->companyint = $act ?? $act->account?->accountid;
        };        
        return true;
    }
    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;
    }
}
