<?php
/**
 * Created by PhpStorm.
 * Author: NODELOG
 * DateTime: 2017/3/8 17:46
 * Description:
 */

namespace api\modules\v1\models;

use api\common\models\User;
use Yii;
use yii\base\Model;
use yii\web\ServerErrorHttpException;


class LoginForm extends Model
{
    //user
    public $id;
    public $companyname;
    public $mobile;
    public $email;
    public $type;

    //profile
    public $address;
    public $contact;
    public $logo;

    public $rememberMe = true;
    /* @var $_user \common\modules\user\models\User */
    private $_user;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id'], 'required'],
            [['address', 'companyname', 'contact', 'email', 'logo'], 'string'],
            [['id', 'type'], 'integer'],
            [['mobile'], 'safe'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'companyname' => '公司名称',
            'contact' => '联系人',
            'address' => '地址',
            'email' => '邮箱',
            'mobile' => '手机号',
            'logo' => 'LOGO',
            'type' => '类型',
        ];
    }


    /**
     * Logs in a user using the provided openid and password.
     *
     * @return bool whether the user is logged in successfully
     * @throws \yii\base\InvalidConfigException
     * @author nodelog
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), 3600 * 2);//2小时
        } else {
            return false;
        }
    }


    /**
     * @return User|array|\common\modules\user\models\User|\yii\db\ActiveRecord|null
     * @throws ServerErrorHttpException
     * @throws \yii\base\InvalidConfigException
     * @author nodelog
     */
    protected function getUser()
    {
        //查询用户
        if ($this->_user === null) {
            $this->_user = User::find()->where(['jucai_id' => $this->id, 'type' => $this->type])->one();
            if ($this->_user && $this->_user->getIsBlocked()) {
                throw new ServerErrorHttpException('用户已被禁用');
            }
        }

        //注册用户
        if ($this->_user === null) {
            /** @var User $model */
            $model = \Yii::createObject([
                'class' => User::className(),
                'scenario' => 'create',
            ]);
            $model->jucai_id = $this->id;
            $model->nickname = $this->companyname;
            $model->tel = $this->mobile;
            $model->email = $this->email;
            $model->type = $this->type;
            $model->username = Yii::$app->security->generateRandomString();

            if ($model->create()) {
                $this->_user = $model;
            } else {
                throw new ServerErrorHttpException(current($model->getErrors())[0]);
            }
        } else {
            $this->_user->updateAttributes(['nickname' => $this->companyname, 'tel' => $this->mobile, 'email' => $this->email]);
        }

        if ($this->_user != null) {
            $this->_user->profile->updateAttributes([
                'avatar' => $this->logo,
                'address' => $this->address,
                'contact' => $this->contact,
            ]);
        } else {
            throw new ServerErrorHttpException('登录失败');
        }

        return $this->_user;
    }
}
