<?php

namespace modules\forms\models;


use Yii;
use modules\activities\models\Activities;
use modules\custom\models\Accounts;
use modules\users\models\User;
use yii\base\Model;

/**
 * This is the model class For table "dt203".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $created_by
 * @property string $updated_by
 * @property string $actid
 * @property string $location
 * @property integer $date
 * @property string $elr
 * @property string $trackid
 * @property string $mileagem
 * @property string $mileagey
 * @property string $pointsno
 * @property string $type
 * @property string $crossingangle
 * @property string $construction
 * @property string $design
 * @property string $installationtype
 * @property string $steeltype
 * @property integer $datemanufactured
 * @property integer $dateinstalled
 * @property string $manufacturer
 * @property string $castno
 * @property string $inspectedbyname
 * @property string $inspectedbysignature
 * @property integer $inspectedbydate
 * @property string $smtname
 * @property string $smtsignature
 * @property integer $smtdate
 * @property string $inputername
 * @property string $inputersignature
 * @property integer $inputerdate
 * @property string $sectionplannername
 * @property string $sectionplannersignature
 * @property integer $sectionplannerdate
 * @property string $twrrecommendation
 * @property string $twrrepairtime
 * @property string $twrstaged
 * @property string $prrecommendation
 * @property string $prrepairtime
 * @property string $prstaged
 * @property string $rrecommendation
 * @property string $rrepairtime
 * @property string $rstaged
 * @property string $ultrasonics
 * @property string $addcomments
 * @property integer $sitevisitperformed
 * @property string $sectionmanname
 * @property string $sectionmansignature
 * @property integer $sectionmandate
 * @property string $trackmanname
 * @property string $trackmansignature
 * @property integer $trackmandate
 * @property string $repairzones
 * @property string $railfefect1
 * @property string $railfefect2
 * @property string $railfefect3
 * @property string $actions1
 * @property string $actions2
 * @property string $actions3
 * @property string $trackconditions1
 * @property string $trackconditions2
 * @property string $trackconditions3
 * @property string $actions4
 * @property string $actions5
 * @property string $actions6
 * @property string $rdmsno1
 * @property string $rdmsno2
 * @property string $rdmsno3
 */
class Dt203 extends Model {

    public $id;
    public $created_at;
    public $updated_at;
    public $created_by;
    public $updated_by;
    public $actid;
    public $location;
    public $date;
    public $elr;
    public $trackid;
    public $mileagem;
    public $mileagey;
    public $pointsno;
    public $type;
    public $crossingangle;
    public $construction;
    public $design;
    public $installationtype;
    public $steeltype;
    public $datemanufactured;
    public $dateinstalled;
    public $manufacturer;
    public $castno;
    public $inspectedbyname;
    public $inspectedbysignature;
    public $inspectedbydate;
    public $smtname;
    public $smtsignature;
    public $smtdate;
    public $inputername;
    public $inputersignature;
    public $inputerdate;
    public $sectionplannername;
    public $sectionplannersignature;
    public $sectionplannerdate;
    public $twrrecommendation;
    public $twrrepairtime;
    public $twrstaged;
    public $prrecommendation;
    public $prrepairtime;
    public $prstaged;
    public $rrecommendation;
    public $rrepairtime;
    public $rstaged;
    public $ultrasonics;
    public $addcomments;
    public $sitevisitperformed;
    public $sectionmanname;
    public $sectionmansignature;
    public $sectionmandate;
    public $trackmanname;
    public $trackmansignature;
    public $trackmandate;
    public $repairzones;
    public $railfefect1;
    public $railfefect2;
    public $railfefect3;
    public $actions1;
    public $actions2;
    public $actions3;
    public $trackconditions1;
    public $trackconditions2;
    public $trackconditions3;
    public $actions4;
    public $actions5;
    public $actions6;
    public $rdmsno1;
    public $rdmsno2;
    public $rdmsno3;
    public $image0;
    public $image1;

    public $response1a, $response1b, $response1c;
    public $response2a, $response2b, $response2c;
    public $response3a, $response3b, $response3c;
    public $response4a, $response4b, $response4c;
    public $response5a, $response5b, $response5c;
    public $response6a, $response6b, $response6c;
    public $response7a, $response7b, $response7c;
    public $response8a, $response8b, $response8c;
    public $response9a, $response9b, $response9c;
    public $response10a, $response10b, $response10c;
    public $response11a, $response11b, $response11c;
    public $response12a, $response12b, $response12c;
    public $response13a, $response13b, $response13c;
    public $response14a, $response14b, $response14c;
    public $response15a, $response15b, $response15c;
    public $response16a, $response16b, $response16c;
    public $response17a, $response17b, $response17c;
    public $response18a, $response18b, $response18c;
    public $response19a, $response19b, $response19c;
    public $response20a, $response20b, $response20c;
    public $response21a, $response21b, $response21c;
    public $response22a, $response22b, $response22c;
    public $response23a, $response23b, $response23c;
    public $response24a, $response24b, $response24c;
    public $response25a, $response25b, $response25c;
    public $response26a, $response26b, $response26c;
    public $response27a, $response27b, $response27c;
    public $response28a, $response28b, $response28c;
    public $response29a, $response29b, $response29c;
    public $response30a, $response30b, $response30c;
    public $response31a, $response31b, $response31c;
    public $response32a, $response32b, $response32c;
    public $response33a, $response33b, $response33c;
    public $response34a, $response34b, $response34c;
    public $response35a, $response35b, $response35c;
    public $response36a, $response36b, $response36c;
    public $response37a, $response37b, $response37c;
    public $response38a, $response38b, $response38c;
    public $response39a, $response39b, $response39c;
    public $response40a, $response40b, $response40c;
    public $response41a, $response41b, $response41c;
    public $response42a, $response42b, $response42c;
    public $response43a, $response43b, $response43c;
    public $response44a, $response44b, $response44c;
    public $response45a, $response45b, $response45c;
    public $response46a, $response46b, $response46c;
    public $response47a, $response47b, $response47c;


    public $deleted_by;
    public $deleted_at;
    public $relatedid;
    public $deleted;
    public $jobno;
    public $padno;
    public $status;
    public $client;


    public $layout = 'portrait';
    public $form = 'dt203';
    public $formname = 'Crossing Inspection Report (DT203)';
    public $formType = 'Grinding';
    
    public $formLayout = '/dt203/_form.php';
    public $viewLayout = '@modules/forms/views/dt203/_view.php';
    public $createpath = '/forms/dt203/create';
    public $printlayout = 'portrait';
    
    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];


    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['location', 'elr', 'trackid', 'mileagem', 'mileagey', 'pointsno',  'crossingangle', 'manufacturer', 'castno', 'inspectedbyname', 'inspectedbysignature', 'smtname', 'smtsignature', 'inputername', 'inputersignature', 'sectionplannername', 'sectionplannersignature', 'twrrecommendation', 'twrrepairtime', 'twrstaged', 'prrecommendation', 'prrepairtime', 'prstaged', 'rrecommendation', 'rrepairtime', 'rstaged', 'ultrasonics', 'addcomments', 'sectionmanname', 'sectionmansignature', 'trackmanname', 'trackmansignature', 'image0','image1', 'railfefect1', 'railfefect2', 'railfefect3', 'actions1', 'actions2', 'actions3', 'trackconditions1', 'trackconditions2', 'trackconditions3', 'actions4', 'actions5', 'actions6', 'rdmsno1', 'rdmsno2', 'rdmsno3'], 'string'],
            [['response1a', 'response1b', 'response1c','response2a', 'response2b', 'response2c','response3a', 'response3b', 'response3c','response4a', 'response4b', 'response4c','response5a', 'response5b', 'response5c','response6a', 'response6b', 'response6c','response7a', 'response7b', 'response7c','response8a', 'response8b', 'response8c','response9a', 'response9b', 'response9c','response10a', 'response10b', 'response10c','response11a', 'response11b', 'response11c','response12a', 'response12b', 'response12c','response13a', 'response13b', 'response13c','response14a', 'response14b', 'response14c','response15a', 'response15b', 'response15c','response16a', 'response16b', 'response16c','response17a', 'response17b', 'response17c','response18a', 'response18b', 'response18c','response19a', 'response19b', 'response19c','response20a', 'response20b', 'response20c','response21a', 'response21b', 'response21c','response22a', 'response22b', 'response22c','response23a', 'response23b', 'response23c','response24a', 'response24b', 'response24c','response25a', 'response25b', 'response25c','response26a', 'response26b', 'response26c','response27a', 'response27b', 'response27c','response28a', 'response28b', 'response28c','response29a', 'response29b', 'response29c','response30a', 'response30b', 'response30c','response31a', 'response31b', 'response31c','response32a', 'response32b', 'response32c','response33a', 'response33b', 'response33c','response34a', 'response34b', 'response34c','response35a', 'response35b', 'response35c','response36a', 'response36b', 'response36c','response37a', 'response37b', 'response37c','response38a', 'response38b', 'response38c','response39a', 'response39b', 'response39c','response40a', 'response40b', 'response40c','response41a', 'response41b', 'response41c','response42a', 'response42b', 'response42c','response43a', 'response43b', 'response43c','response44a', 'response44b', 'response44c','response45a', 'response45b', 'response45c','response46a', 'response46b', 'response46c','response47a', 'response47b', 'response47c',], 'string'],
            [['created_at', 'updated_at', 'deleted_at'], 'safe'],
            [['construction', 'type', 'design', 'installationtype','steeltype' ], 'safe'],
            [['date', 'datemanufactured', 'dateinstalled', 'inspectedbydate', 'smtdate', 'inputerdate', 'sectionplannerdate',  'sectionmandate', 'trackmandate'], 'safe'],
        	[['id', 'created_by', 'deleted_by', 'updated_by', 'actid'],'safe'],
        	[['padno', 'client', 'status'],'string'],
        	[['sitevisitperformed',],'boolean']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'actid' => 'Actid',
            'location' => 'Location',
            'date' => 'Date',
            'elr' => 'Elr',
            'trackid' => 'Trackid',
            'mileagem' => 'Mileagem',
            'mileagey' => 'Mileagey',
            'pointsno' => 'Pointsno',
            'type' => 'Type',
            'crossingangle' => 'Crossingangle',
            'construction' => 'Construction',
            'design' => 'Design',
            'installationtype' => 'Installationtype',
            'steeltype' => 'Steeltype',
            'datemanufactured' => 'Datemanufactured',
            'dateinstalled' => 'Dateinstalled',
            'manufacturer' => 'Manufacturer',
            'castno' => 'Castno',
            'inspectedbyname' => 'Inspectedbyname',
            'inspectedbysignature' => 'Inspectedbysignature',
            'inspectedbydate' => 'Inspectedbydate',
            'smtname' => 'Smtname',
            'smtsignature' => 'Smtsignature',
            'smtdate' => 'Smtdate',
            'inputername' => 'Inputername',
            'inputersignature' => 'Inputersignature',
            'inputerdate' => 'Inputerdate',
            'sectionplannername' => 'Sectionplannername',
            'sectionplannersignature' => 'Sectionplannersignature',
            'sectionplannerdate' => 'Sectionplannerdate',
            'twrrecommendation' => 'Twrrecommendation',
            'twrrepairtime' => 'Twrrepairtime',
            'twrstaged' => 'Twrstaged',
            'prrecommendation' => 'Prrecommendation',
            'prrepairtime' => 'Prrepairtime',
            'prstaged' => 'Prstaged',
            'rrecommendation' => 'Rrecommendation',
            'rrepairtime' => 'Rrepairtime',
            'rstaged' => 'Rstaged',
            'ultrasonics' => 'Ultrasonics',
            'addcomments' => 'Addcomments',
            'sitevisitperformed' => 'Sitevisitperformed',
            'sectionmanname' => 'Sectionmanname',
            'sectionmansignature' => 'Sectionmansignature',
            'sectionmandate' => 'Sectionmandate',
            'trackmanname' => 'Trackmanname',
            'trackmansignature' => 'Trackmansignature',
            'trackmandate' => 'Trackmandate',
            'railfefect1' => 'Railfefect1',
            'railfefect2' => 'Railfefect2',
            'railfefect3' => 'Railfefect3',
            'actions1' => 'Actions1',
            'actions2' => 'Actions2',
            'actions3' => 'Actions3',
            'trackconditions1' => 'Trackconditions1',
            'trackconditions2' => 'Trackconditions2',
            'trackconditions3' => 'Trackconditions3',
            'actions4' => 'Actions4',
            'actions5' => 'Actions5',
            'actions6' => 'Actions6',
            'rdmsno1' => 'Rdmsno1',
            'rdmsno2' => 'Rdmsno2',
            'rdmsno3' => 'Rdmsno3',
            'image0' => 'Image 0',
            'image1' => 'Image 1',
            //Cast Monobloc, Cast Centre or Cast Vee Within fishplate limits
            'response1a'=>'Transverse crack emanating from behind fishplates',
            'response1b'=>'response1b',
            'response2a'=>'Horizontal cracking in the upper or lower fillet radius of a casting',
            'response2b'=>'response2b',
            'response3a'=>'Transverse cracking of casting extending from running surface',
            'response3b'=>'response3b',
            'response4a'=>'Longitudinal vertical cracking of casting',
            'response4b'=>'response4b',
            'response5a'=>'Longitudinal crack in the apron',
            'response5b'=>'response5b',
            'response6a'=>'Transverse crack in the foot',
            'response6b'=>'response6b',
            'response7a'=>'Voiding of 7mm or greater at joint',
            'response7b'=>'response7b',
            'response8a'=>'response8a',
            'response8b'=>'response8b',
            'response9a'=>'response9a',
            'response9b'=>'response9b',
            //Leg ends
            'response10a'=>'Transverse cracking of casting extending from running surface',
            'response10b'=>'response10b',
            'response11a'=>'Transverse crack in the foot',
            'response11b'=>'response11b',
            'response12a'=>'Surface breaking defect associated with the tri-metal weld (particular attention shall be paid to the underside of the foot and foot tip areas)',
            'response12b'=>'response12b',
            'response13a'=>'response13a',
            'response13b'=>'response13b',
            'response14a'=>'response14a',
            'response14b'=>'response14b',

            //Anywhere within casting
            'response15a'=>'Cracking in web of casting, not covered elsewhere',
            'response15b'=>'response15b',
            'response16a'=>'Lipping on casting',
            'response16b'=>'response16b',
            'response17a'=>'Profile variation across tri metal zone',
            'response17b'=>'response17b',
            'response18a'=>'Transverse defect from RCF in a casting',
            'response18b'=>'response18b',
            'response19a'=>'Squat on a casting',
            'response19b'=>'response19b',
            'response20a'=>'Wheel burn on a casting',
            'response20b'=>'response20b',
            'response21a'=>'Rail end batter',
            'response21b'=>'response21b',
            'response22a'=>'response22a',
            'response22b'=>'response22b',
            'response23a'=>'response23a',
            'response23b'=>'response23b',
            'response24a'=>'response24a',
            'response24b'=>'response24b',
            //Change of section
            'response25a'=>'Transverse cracking of casting elsewhere in the box section from the running surface',
            'response25b'=>'response25b',
            'response26a'=>'Transverse crack in the foot',
            'response26b'=>'response26b',
            'response27a'=>'response27a',
            'response27b'=>'response27b',
            'response28a'=>'response28a',
            'response28b'=>'response28b',
            'response29a'=>'response29a',
            'response29b'=>'response29b',
            //Monobloc
            'response30a'=>'Crack on one foot extending into a core pocket and to a position up the side wall (from the underside of foot)',
            'response30b'=>'response30b',
            'response31a'=>'Crack on foot extending across full width of casting and to a position on any one side (from underside of foot)',
            'response31b'=>'response31b',
            'response32a'=>'Transverse cracking of casting in the crossing vee',
            'response32b'=>'response32b',
            'response33a'=>'Transverse cracking of casting in the wing rail',
            'response33b'=>'response33b',
            'response34a'=>'Horizontal cracking of the flangeway wall or wing rail',
            'response34b'=>'response34b',
            'response35a'=>'Longitudinal cracking of the base of the flangeway',
            'response35b'=>'response35b',
            'response36a'=>'Depression of the running surface, other than in the wheel transfer area',
            'response36b'=>'response36b',
            'response37a'=>'Flaking or shelling of the wheel contact surface',
            'response37b'=>'response37b',
            'response38a'=>'Wear / damage in wheel transfer area (state dimensions)',
            'response38b'=>'response38b',
            'response39a'=>'Voiding of 7mm or greater in the wheel transfer area',
            'response39b'=>'response39b',
            'response40a'=>'Metal breaking out in the wheel transfer area',
            'response40b'=>'response40b',
            'response41a'=>'response41a',
            'response42a'=>'response42a',
            'response43a'=>'response43a',
            'response44a'=>'response44a',
            //Rolled Steel Crossing Components Wheel transfer area
            'response45a'=>'Wear / damage in wheel transfer area (state dimensions)',
            'response45b'=>'response45b',
            'response46a'=>'Voiding of 7mm or greater in the wheel transfer area	',
            'response46b'=>'response46b',
            'response47a'=>'Lipping',
            'response47b'=>'response47b',
        ];
    }

    public function beforeSave($insert) {
		if (Yii::$app->controller->id == 'sync') {
			return parent::beforeSave($insert);
		} else {
			//if ($this->client == '') {$this->client = null;}	
			if ($this->date) {$this->date = strtotime(str_replace('/','-',$this->date));}	
			if ($this->datemanufactured) {$this->datemanufactured = strtotime(str_replace('/','-',$this->datemanufactured));}	
			if ($this->dateinstalled) {$this->dateinstalled = strtotime(str_replace('/','-',$this->dateinstalled));}	
			if ($this->inspectedbydate) {$this->inspectedbydate = strtotime(str_replace('/','-',$this->inspectedbydate));}	
			if ($this->smtdate) {$this->smtdate = strtotime(str_replace('/','-',$this->smtdate));}	
			if ($this->inputerdate) {$this->inputerdate = strtotime(str_replace('/','-',$this->inputerdate));}	
			if ($this->sectionplannerdate) {$this->sectionplannerdate = strtotime(str_replace('/','-',$this->sectionplannerdate));}	
			if ($this->sectionmandate) {$this->sectionmandate = strtotime(str_replace('/','-',$this->sectionmandate));}	
			if ($this->trackmandate) {$this->trackmandate = strtotime(str_replace('/','-',$this->trackmandate));}	
			if (Is_array($this->construction)) {$this->construction = implode(',', $this->construction);}
			if (Is_array($this->type)) {$this->type = implode(',', $this->type);}
			if (Is_array($this->design)) {$this->design = implode(',', $this->design);}
			if (Is_array($this->installationtype)) {$this->installationtype = implode(',', $this->installationtype);}
			if (Is_array($this->steeltype)) {$this->steeltype = implode(',', $this->steeltype);}
			
			return parent::beforeSave($insert);
		}
	}

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

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