<?php

namespace app\models;

use dektrium\user\helpers\Password;
use dektrium\user\models\Token;
use Yii;



class User extends \dektrium\user\models\User
{

    public $last_login_at;

    public function scenarios()
    {
        return [
            'register' => ['username', 'email', 'password', 'fio', 'org_type', 'member_phone', 'role', 'fax', 'at_org'],
            'connect'  => ['username', 'email'],
            'create'   => ['role','username', 'email', 'password'],
            'update'   => ['username', 'email', 'password'],
            'settings' => ['username', 'email', 'password'],
        ];
    }

    public function rules()
    {
        return [
            'roleRequired' => ['role', 'required'],
            'usernameRequired' => ['username', 'required', 'on' => ['register', 'connect', 'create', 'update']],
            'usernameMatch' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'],
            'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 150],
            'usernameUnique' => ['username', 'unique'],
            'usernameTrim' => ['username', 'trim'],
            'emailRequired' => ['email', 'required', 'on' => ['register', 'connect', 'create', 'update']],
            'emailPattern' => ['email', 'email'],
            'emailLength' => [['email', 'at_org'], 'string', 'max' => 255],
            'emailUnique' => ['email', 'unique'],
            'emailTrim' => ['email', 'trim'],
            'passwordRequired' => ['password', 'required', 'on' => ['register']],
            'passwordLength' => ['password', 'string', 'min' => 6, 'on' => ['register', 'create']],
            'passwordMatch' => ['repeatpassword', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match"],

            ['verifyCode','captcha', 'captchaAction'=>'auth/open/captcha']

        ];
    }

    public function attributeLabels()
    {
        return [
            'fio'               => \Yii::t('app', 'FIO'),
            'role'              => \Yii::t('app', 'Role'),
            'role_user'         => \Yii::t('app', 'Role'),
            'username'          => \Yii::t('app', 'Username'),
            'email'             => \Yii::t('app', 'Email'),
            'registration_ip'   => \Yii::t('app', 'Registration ip'),
            'unconfirmed_email' => \Yii::t('app', 'New email'),
            'password'          => \Yii::t('app', 'Password'),
            'repeatpassword'    => \Yii::t('app', 'Repeat Password'),
            'created_at'        => \Yii::t('app', 'Registration time'),
            'confirmed_at'      => \Yii::t('app', 'Confirmation time'),
            'fax'               => \Yii::t('app', 'fax'),
            'captcha'           => \Yii::t('app', 'captcha'),
            'at_org'            => Yii::t('app', 'Company full name')
        ];
    }

    public function getProfile(){
        return $this->hasOne(Profile::className(), ['user_id' => 'id']);
    }

    public function getAt_org(){
        return $this->profile->at_org;
    }


}
