<?php

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

/**
 * This is the model class For table "dt197".
 *
 * @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 integer $target
 */
class Dt197 extends Model {

    public $id;
    public $actid;
    public $created_at;
    public $created_by;
    public $deleted_at;
    public $deleted_by;
    public $updated_at;
    public $updated_by;
    public $relatedid;
    public $client;
    public $status;
    public $insdate;
    public $location;
    public $jobno;
    public $signprint;
    public $signed;
    public $signeddate;
    public $comments;
    public $target;
    public $deleted;
    public $created_by2;
    public $updated_by2;
    public $padno;
    public $confirmation;
    public $response1a, $response1b;
    public $response2a, $response2b;
    public $response3a, $response3b;
    public $response4a, $response4b;
    public $response5a, $response5b;
    public $response6a, $response6b;
    public $response7a, $response7b;
    public $response8a, $response8b;
    public $response9a, $response9b;
    public $response10a, $response10b;
    public $response11a, $response11b;
    public $response12a, $response12b;
    public $response13a, $response13b;
    public $response14a, $response14b;
    public $response15a, $response15b;
    public $response16a, $response16b;
    public $response17a, $response17b;
    public $response18a, $response18b;
    public $response19a, $response19b;
    public $response20a, $response20b;
    public $response21a, $response21b;
    public $response22a, $response22b;
    public $response23a, $response23b;
    public $response24a, $response24b;
    public $response25a, $response25b;
    public $response26a, $response26b;
    public $response27a, $response27b;
    public $response28a, $response28b;
    public $response29a, $response29b;
    public $response30a, $response30b;
    public $response31a, $response31b;
    public $response32a, $response32b;
    public $response33a, $response33b;
    public $response34a, $response34b;
    public $response35a, $response35b;
    public $response36a, $response36b;
    public $response37a, $response37b;
    public $response38a, $response38b;
    public $response39a, $response39b;
    public $response40a, $response40b;
    public $datarows = [];
    public $datarowsa = [];

    public $printlayout = 'Portrait';
    public $form = 'dt197';
    public $formname = 'Gauge Corner Daily Checksheet (Dt197)';
    public $formType = 'Gauge Corner';

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['actid', 'location', 'jobno', 'signprint', 'signed', 'comments', 'client'], 'string'],
            [
                [
                    'response1a', 'response1b','response2a', 'response2b','response3a', 'response3b',
                    'response4a', 'response4b','response5a', 'response5b','response6a', 'response6b',
                    'response7a', 'response7b','response8a', 'response8b','response9a', 'response9b',
                    'response10a', 'response10b','response11a', 'response11b','response12a', 'response12b',
                    'response13a', 'response13b','response14a', 'response14b','response15a', 'response15b',
                    'response16a', 'response16b','response17a', 'response17b','response18a', 'response18b',
                    'response19a', 'response19b','response20a', 'response20b','response21a', 'response21b',
                    'response22a', 'response22b','response23a', 'response23b','response24a', 'response24b',
                    'response25a', 'response25b','response26a', 'response26b','response27a', 'response27b',
                    'response28a', 'response28b','response29a', 'response29b','response30a', 'response30b',
                    'response31a', 'response31b','response32a', 'response32b','response33a', 'response33b',
                    'response34a', 'response34b','response35a', 'response35b','response36a', 'response36b',
                    'response37a', 'response37b','response38a', 'response38b','response39a', 'response39b',
                    'response40a', 'response40b','target',
                ],
                'string'
            ],
            [['confirmation', ],'boolean'],
            [['insdate', 'signeddate'], 'safe'],
				//Defaults
			[['deleted'], 'boolean'],
			['deleted', 'default', 'value' => 0],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by', 'id'],'safe'],

        ];
    }

    /**
     * @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',
            'target' => 'Target',
            //post Welds
            'response1a'=>'Has equipment strip down process been carried out correctly?',
            'response1b'=>'response1b',
            'response2a'=>'Are all cables accounted for?',
            'response2b'=>'response2b',
            'response3a'=>'Are all small tools accounted for?',
            'response3b'=>'response3b',
            'response4a'=>'Has the welding area been cleared of all debris?',
            'response4b'=>'response4b',
            'response5a'=>'Has all dry space and exclusion zones been dismantled?',
            'response5b'=>'response5b',
            'response6a'=>'Have machines been placed back into the vehicles & secured correctly?',
            'response6b'=>'response6b',
            'response7a'=>'Has the welding been checked for quality?',
            'response7b'=>'response7b',
            'response8a'=>'Has a walkthrough of the welding area been carried out?',
            'response8b'=>'response8b',
            'response9a'=>'Has fire checks been carried out of the welding site & the surrounding areas?',
            'response9b'=>'response9b',
            'response10a'=>'response10a',
            'response10b'=>'response10b',
            'response11a'=>'response11a',
            'response11b'=>'response11b',
            'response12a'=>'response12a',
            'response12b'=>'response12b',
            'response13a'=>'response13a',
            'response13b'=>'response13b',
            'response14a'=>'response14a',
            'response14b'=>'response14b',
            'response15a'=>'response15a',
            'response15b'=>'response15b',
            'response16a'=>'response16a',
            'response16b'=>'response16b',
            'response17a'=>'response17a',
            'response17b'=>'response17b',
            'response18a'=>'response18a',
            'response18b'=>'response18b',
            'response19a'=>'response19a',
            'response19b'=>'response19b',
            'response20a'=>'response20a',
            'response20b'=>'response20b',
            //PRE Welds
            'response21a'=>'Are both keeper rails free from obstruction?',
            'response21b'=>'response21b',
            'response22a'=>'Has Dye Penetrant Inspection been carried out?',
            'response22b'=>'response22b',
            'response23a'=>'Is there any Plated or Glued joints in welding area?',
            'response23b'=>'response23b',
            'response24a'=>'Are there any other welds in the welding area?',
            'response24b'=>'response24b',
            'response25a'=>'Are there any visual Rail defects in welding area?',
            'response25b'=>'response25b',
            'response26a'=>'Have the vehicles been positioned safely?',
            'response26b'=>'response26b',
            'response27a'=>'Are all operatives wearing the correct PPE?',
            'response27b'=>'response27b',
            'response28a'=>'Are all cables complete and free from damage?',
            'response28b'=>'response28b',
            'response29a'=>'Are all calibrated items in date?',
            'response29b'=>'response29b',
            'response30a'=>'Is welding Tip & Nozzle free from damage or wear?',
            'response30b'=>'response20b',
            'response31a'=>'Are stems on Seam Probes free from damage?',
            'response31b'=>'response31b',
            'response32a'=>'Are RED, YELLOW & BLUE mains plugs & sockets free from damage?',
            'response32b'=>'response32b',
            'response33a'=>'Is earth plug free from damage?',
            'response33b'=>'response33b',
            'response34a'=>'Are Rail Heaters free from damage including BLUE plugs?',
            'response34b'=>'response34b',
            'response35a'=>'Is Diesel, Oil & Water levels at the correct requirements for use on the Generator?',
            'response35b'=>'response35b',
            'response36a'=>'Is the YELLOW & RED sockets free from damage on the Generator?',
            'response36b'=>'response36b',
            'response37a'=>'Is the +/- Terminal leads free from damage on the Generator?',
            'response37b'=>'response37b',
            'response38a'=>'Is the dry space structure set up correctly?',
            'response38b'=>'response38b',
            'response39a'=>'response39a',
            'response39b'=>'response39b',
            'response40a'=>'response40a',
            'response40b'=>'response40b',

        ];
    }

	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 ? User::Padno() : $this->padno;
        return parent::beforeValidate();
    }
    public function init()
    {
        // Parameters 
        if(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'],
        ];
        return $buttons;
    }
}
