<?php
namespace backend\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class ChangePasswordForm extends Model
{
    public $currentPassword;
    public $newPassword;
    public $newPasswordRepeat;

  //  private $_user = false;


    /**
     * @inheritdoc
     */
    public function rules()
    {
            return [
                // username and password are both required
                [['currentPassword', 'newPassword','newPasswordRepeat'], 'required'],
                // [['currentPassword'], 'validatePassword'],
                // rememberMe must be a boolean value
               // ['rememberMe', 'boolean'],
                // password is validated by validatePassword()
               // ['currentPassword', 'compare'],
                [['currentPassword,newPassword,newPasswordRepeat'],'safe'],
               [['currentPassword'],'compareCurrentPassword'],

              // [['currentPassword,newPassword,newPasswordRepeat'],'required'],
               // [['currentPassword,newPassword'], 'sameCheck'],
                [
                    'newPasswordRepeat', 'compare',
                    'compareAttribute'=>'newPassword',
                ],


            ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */





    public function compareCurrentPassword()
    {
        //echo 123;
        $user_id = Yii::$app->user->id;
        $model = User::find()->where('id='.$user_id)->one();
        $model->attributes = \Yii::$app->request->post('ChangePasswordForm');
        // echo $_POST['User']['currentPassword'];exit;
        /*if(!Yii::$app->getSecurity()->validatePassword($this->currentPassword,$model->password_hash))*/
        if(StringUtils::hashStr($this->currentPassword) !== $model ->password_hash)
        $this->addError('currentPassword', 'Current pass is invalid');

    }

    public function sameCheck()
    {
        $user_id = Yii::$app->user->id;
        $model = User::find()->where('id='.$user_id)->one();
        //$model= new ChangePasswordForm();
        $model->attributes = \Yii::$app->request->post('ChangePasswordForm');
        if($model->currentPassword == $model->newPassword)
            $this->addError('newPassword', 'Current Password and New password are the same, you need to choose different one');
    }

}
