<?php

namespace modules\cors\models;

use Yii;
use backend\models\Datestrowsbe;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use modules\users\models\User;
/**
 * This is the model class For table "datests".
 *
 * @property string $id
 * @property string $created_by
 * @property string $updated_by
 * @property string $deleted_by
 * @property string $actid
 * @property string $relatedid
 * @property string $tester
 * @property string $employeewitness
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $deleted_at
 * @property integer $jobno
 * @property integer $date
 * @property string $status
 * @property string $padno
 * @property string $result
 * @property string $comments
 */
class Datestsbe extends \common\models\Base
{
	public $dates = ['created_at', 'updated_at', 'deleted_at', 'date'];
	public $uniqueidentifiers = ['employee', 'relatedid','actid','tester', 'witness'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'datests';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['id', 'created_by', 'updated_by', 'deleted_by', 'actid', 'relatedid', 'tester', 'employee','witness', 'status', 'padno', 'result', 'comments'], 'string'],
            [['created_at', 'updated_at', 'deleted_at', 'jobno'], 'integer'],
            [['id'], 'unique'],
            [['date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'deleted_by' => 'Deleted By',
            'actid' => 'Actid',
            'relatedid' => 'Relatedid',
            'tester' => 'Tester',
            'employee' => 'Employee',
            'witness' => 'witness',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
            'jobno' => 'Jobno',
            'date' => 'Date',
            'status' => 'Status',
            'padno' => 'Padno',
            'result' => 'Result',
            'comments' => 'Comments',
        ];
    }
    public function getRows() {
    if (Yii::$app->controller->action->id == 'create') {
		$tempid = Yii::$app->session->get('dateststempid');
			return Datestrowsbe::find()->where(['relatedid' => "$tempid"])->all();
		} else {
			return $this->hasMany(Datestrowsbe::class, ['relatedid' => 'id']);	
		}
	}
	public function getFiles() {
		return $this->hasMany(\backend\models\Uploadsbe::class, ['relatedid' => 'id']);
	}
    public function getEmployeel() {
		return $this->hasOne(User::class, ['id' => 'employee']);
	}
	public function getTesterl() {
		return $this->hasOne(User::class, ['id' => 'tester']);
	}	
    public function getWitnessl() {
		return $this->hasOne(User::class, ['id' => 'witness']);
	}
}
