<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "training_request".
 *
 * @property int $id
 * @property string $full_name
 * @property string $mother_name
 * @property string $gender
 * @property string $residence_place
 * @property int $phone
 * @property int $mobile
 * @property int $alternative_phone_number
 * @property string $educational_degree
 * @property string $university_name
 * @property string $faculty
 * @property string $year_of_study
 * @property string $university_training_request
 * @property string $personal_photo
 * @property string $identity
 * @property string $training_request_status
 * @property string $create_date
 * @property string $serial_number
 * @property int $trainee_user_id
 *
 * @property Evaluation $evaluation
 * @property Branches $branch
 */
class TrainingRequests extends \yii\db\ActiveRecord
{
    const UPDATE_ENGLISH_FULL_NAME  = 'UPDATE_ENGLISH_FULL_NAME';
    const UPDATE_STATUS             = 'UPDATE_STATUS';

    public $from_date;
    public $to_date;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'training_requests';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['full_name', 'full_name_english', 'mother_name', 'gender', 'residence_place', 'phone', 'mobile', 'educational_degree', 'university_name', 'faculty', 'year_of_study', 'university_training_request', 'personal_photo', 'identity', 'create_date', 'serial_number'], 'required'],
            [['gender', 'training_request_status'], 'string'],
            [['trainee_user_id'], 'integer'],
            [['create_date'], 'safe'],
            [['full_name', 'year_of_study'], 'string', 'max' => 300],
            [['mother_name'], 'string', 'max' => 200],
            [['residence_place', 'serial_number'], 'string', 'max' => 250],
            [['educational_degree', 'university_name', 'faculty'], 'string', 'max' => 500],
            [['identity', 'university_training_request', 'personal_photo'], 'image', 'extensions' => 'jpg, jpeg, png'],
            [['full_name', 'mother_name', 'residence_place', 'educational_degree', 'university_name', 'faculty'], 'match', 'pattern' => "/^([\x{0600}-\x{06ff}]|[\x{0750}-\x{077f}]|[\x{fb50}-\x{fbc1}]|[\x{fbd3}-\x{fd3f}]|[\x{fd50}-\x{fd8f}]|[\x{fd92}-\x{fdc7}]|[\x{fe70}-\x{fefc}]|[\x{fdf0}-\x{fdfd}]|[' '])*$/u", 'message' => Yii::t('app', '{attribute} has to be written in Arabic')],
            [['full_name_english'], 'match', 'pattern' => "/^([A-Za-z]|[' '])*$/u", 'message' => Yii::t('app', '{attribute} has to be written in English')],
            [['phone'], 'match', 'pattern' => "/^[0-9]{7,10}$/", 'message' => Yii::t('app', '{attribute} should be valid number')],
            [['mobile', 'alternative_phone_number'], 'match', 'pattern' => "/^09[0-9]{8}$/", 'message' => Yii::t('app', '{attribute} should be valid number')]
        ];
    }

    public function scenarios(){
        $scenarios = parent::scenarios();
        $scenarios[self::UPDATE_ENGLISH_FULL_NAME] = ['full_name_english'];
        $scenarios[self::UPDATE_STATUS] = ['training_request_status'];
        return $scenarios;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'full_name' => Yii::t('app', 'Full Name'),
            'full_name_english' => Yii::t('app', 'Full Name English'),
            'mother_name' => Yii::t('app', 'Mother Name'),
            'gender' => Yii::t('app', 'Gender'),
            'residence_place' => Yii::t('app', 'Residence Place'),
            'phone' => Yii::t('app', 'Phone'),
            'mobile' => Yii::t('app', 'Mobile'),
            'alternative_phone_number' => Yii::t('app', 'Alternative Phone Number'),
            'educational_degree' => Yii::t('app', 'Educational Degree'),
            'university_name' => Yii::t('app', 'University Name'),
            'faculty' => Yii::t('app', 'Faculty'),
            'year_of_study' => Yii::t('app', 'Year Of Study'),
            'university_training_request' => Yii::t('app', 'University Training Request'),
            'personal_photo' => Yii::t('app', 'Personal Photo'),
            'identity' => Yii::t('app', 'Identity'),
            'training_request_status' => Yii::t('app', 'Training Request Status'),
            'create_date' => Yii::t('app', 'Create Date'),
            'serial_number' => Yii::t('app', 'Serial Number'),
            'trainee_user_id' => Yii::t('app', 'Trainee User'),
            'from_date' => Yii::t('app', 'From Date'),
            'to_date' => Yii::t('app', 'To Date'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEvaluation()
    {
        return $this->hasOne(Evaluations::className(), ['training_request_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRejections()
    {
        return $this->hasMany(RejectedRequestsCauses::className(), ['training_request_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getInterviews()
    {
        return $this->hasMany(Interviews::className(), ['training_request_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTrainings()
    {
        return $this->hasMany(Trainings::className(), ['training_request_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(Users::className(), ['id' => 'trainee_user_id']);
    }
}
