<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/13
 * Time: 上午11:22
 */

namespace backend\models\forms;

use backend\models\LoginApi;
use yii\base\Model;

class PwdForm extends Model
{
    public $username;
    public $type;

    public function rules()
    {
        return [
            [['username'], 'required'],
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'email'],
            [['type'], 'integer'],
            [['type'], 'default', 'value' => 1],
        ];
    }

    public function changePwd()
    {
        if ($this->validate()) {
            $data = [];
            $data['username'] = $this->username;
            $data['type'] = $this->type;

            $api = new LoginApi();
            $result = $api->pwd1($data);
            if ($result['code'] == 1) {
                return true;
            } else {
                if (is_array($result['message'])) {
                    $this->addErrors($result['message']);
                } else {
                    $this->addError('username', $result['message']);
                }
            }
        }
        return !$this->hasErrors();
    }
}
