<?php
    
    namespace common\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;

    /**
     * This is the model class for table "visit".
     *
     * @property int    $id
     * @property string $name
     * @property string $phone
     * @property string $message
     * @property string $entity
     * @property int    $entity_id
     */
    class Visit extends \yii\db\ActiveRecord
    {
        const DURING_30_MIN = '1';
    
        const FROM_9_12 = '2';
    
        const FROM_12_18 = '3' ;
    
        public $agree;
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                    'updatedAtAttribute' => false,
                ],
            ];
        }
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'visit';
        }


	    /**
	     * Метод, который нужен для универсальных тайтлов для временных меток
	     *
	     * @param int $inputMode
	     * @return string
	     */
	    public static function getTimeTitles(int $inputMode): string
	    {
		    $resultTimeMessage = '';
		    switch ($inputMode) {
			    case 1:
				    $resultTimeMessage = "В течение 30 минут";
				    break;
			    case 2:
				    $resultTimeMessage = 'с 9:00 до 12:00';
				    break;
			    case 3:
				    $resultTimeMessage = ' с 12:00 до 18:00';
				    break;
			    default:
				    $resultTimeMessage = ' Время не указано';

		    }
		    return $resultTimeMessage;
	    }






        
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                  [
                      'phone'
                  ],
                  'required'
                ],
                [
                    [
                        'status'
                    ],
                    'boolean'
                ],
                [
                    [
                        'email'
                    ],
                    'email'
                ],
                [
                    [ 'message' ],
                    'string',
                ],
                [
                    [ 'entity_id' ],
                    'default',
                    'value' => null,
                ],
                [
                    [ 'entity_id', 'created_at' ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'phone',
                        'entity',
	                    'email',
	                    'request_webpage'
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'agree',
                    ],
                    'in',
                    'range'      => [ 1 ],
                    'allowArray' => false,
                ],
                [
                    [ 'time' ], 'integer'
                ],
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function attributeLabels()
        {
            return [
                'id'        => Yii::t('app', 'ID'),
                'name'      => Yii::t('app', 'Name'),
                'phone'     => Yii::t('app', 'Phone'),
                'message'   => Yii::t('app', 'Message'),
                'entity'    => Yii::t('app', 'Entity'),
                'entity_id' => Yii::t('app', 'Entity ID'),
                'email'     => Yii::t('app', 'Email'),
	            'request_webpage' => Yii::t('app', 'Request_webpage'),
            ];
        }
        
        public function getEntity(){
            if ($this->entity !== null){
                return $this->hasOne($this->entity, ['id' => 'entity_id']);
            }
            return null;
        }
    }
