<?php

namespace modules\forms\models;

use Yii;
/**
 * This is the model class For table "dt127".
 *
 * @property string $id
 * @property string $actid
 * @property string $date
 * @property string $location
 * @property integer $heldby
 * @property string $subjects
 * @property string $welders
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 */
class Dt127 extends Yii\base\Model {
   
    public $relatedid;
    public $jobno;
    public $status;
    public $description;


    public $id;
    public $actid;
    public $padno;
    public $date;
    public $title;
    public $location;
    public $heldby;
    public $subjects;
    public $welders;
    public $created_at;
    public $created_by;
    public $created_by2;
    public $updated_at;
    public $updated_by;
    public $updated_by2;
    public $deleted_at;
    public $deleted_by;
    public $deleted;
    public $rowdata;
    public $name = [];
    public $signature = [];
    public $position = [];
    public $notes = [];
    public $rowstatus = [];

    public $form = 'DT127';
    public $printlayout = 'Portrait';
    public $formname = 'Briefing Form (DT127)';
    public $createpath = '/forms/dt127/create';
    public $formType = 'Other';
    
    public $formLayout = '/dt127/_form.php';
    public $viewLayout = '@modules/forms/views/dt127/_view.php';public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['location', 'subjects', 'welders', 'padno', 'title'], 'string'],
            [['name', 'signature', 'position', 'notes', 'rowstatus'], 'safe'],
            [['heldby'], 'safe'],
            [['actid', 'date', 'rowdata'], 'safe'],
			//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',
            'title' => 'Title',
            'actid' => 'Actid',
            'date' => 'Date',
            'location' => 'Location',
            'heldby' => 'Heldby',
            'subjects' => 'Subjects',
            'welders' => 'Welders',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
        ];
    }
    
    public function getUser(){
        return is_integer($this->heldby) ? User::findOne(['_id' => $this->heldby]) : User::findOne(['username' => $this->heldby]);
    }

    public function getUpload(){
        return Uploads::findOne(['actid' => $this->id]);
    }
    public function getSignatures(){
        return Signature::find()->where(['relatedid' => $this->id, 'status' => ['Active', null]])->all();
    }
	
    public function beforeValidate(){      
        if (Yii::$app->name !== 'app-console' ) {
            $this->date = date('Y-m-d', strtotime(str_replace('/', '-', $this->date)));
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        }
        return parent::beforeValidate();
    }

    public function UpdateSignatures(){
        foreach($this->name as $i => $value){
            if ($this->rowstatus[$i] !== null) {
                $sign = new \frontend\models\Signature([
                    'id' => \common\models\Base::Tempid(),
                    'form' => 'dt127',
                    'relatedid' => $this->id,
                    'name' => $this->name[$i],
                    'signature' => $this->signature[$i],
                    'position' => $this->position[$i],
                    'sequence' => $i,
                    'note' => empty($this->notes) == false && array_key_exists($i, $this->notes) ? $this->notes[$i] : null,
                    'status' => empty($this->status) == false && array_key_exists($i, $this->status) ? $this->status[$i] : null,
                ]);
                $sign->save();
            } 
        }
    }

    public function init(){
        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;
    }
}
