<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */
namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\AdminForm;
use backendApi\modules\v1\models\LoginForm;
use backendApi\modules\v1\models\User;
use common\helpers\Form;
use common\helpers\LoggerTool;
use Yii;
use yii\web\HttpException;

class OauthController extends BaseController
{
    public $modelClass = User::class;

    public function actionMenu(){
        $menu = require Yii::getAlias('@backendApi/config/menu.php');
        return $this->_childMenu($menu);
    }

    private function _childMenu($parentArray){
        $menuResult = [];
        foreach($parentArray as $key => $parentMenu){
            // 菜单是否显示
            if(isset($parentMenu['show']) && !$parentMenu['show']){
                continue;
            }
            // 子菜单同样设置
            if(isset($parentMenu['child']) && !empty($parentMenu['child'])){
                $parentMenu['child'] = $this->_childMenu($parentMenu['child']);
            }
            $menuResult[] = $parentMenu;
        }
        return $menuResult;
    }

    public function actionInfo(){
        $userInfo = User::find()->where(['ID'=>Yii::$app->user->id])->asArray()->one();
        unset($userInfo['PASSWORD_HASH'], $userInfo['PASSWORD_RESET_TOKEN'], $userInfo['AUTH_KEY']);
        return static::notice($userInfo);
    }

    /**
     * 登录
     * @return mixed
     * @throws HttpException
     * @throws \yii\base\Exception
     */
    public function actionLogin() {
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            LoggerTool::debug(json_encode(['actionLoginSuccess', Yii::$app->request->post(), $_SERVER]));
            $token = Yii::$app->getUser()->getToken();
            return static::notice($token);
        } else {
            LoggerTool::debug(json_encode(['actionLoginFailed', Yii::$app->request->post(), $model->getErrors(), $_SERVER]));

            $firstError = $model->getFirstError('LoginForm');
            if( $firstError === LoginForm::ERROR_IS_MODIFY_PASSWORD ) {
                return static::notice(LoginForm::ERROR_IS_MODIFY_PASSWORD, 403);
            }

            return static::notice(Form::formatErrorsForApi($model->getErrors()), 401);
        }

    }

    public function actionNoLoginModifyPassword() {
        $form = new AdminForm();
        $form->scenario = 'noLoginModifyPassword';

        if(Yii::$app->request->isPost && $form->load(Yii::$app->request->post(), '') && $result = $form->edit()){
            // Log::adminHandle('管理员'.$result->ADMIN_NAME.'重置密码');
            return static::notice('重置密码成功');
        } else {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
    }

    /**
     * 用refreshToken刷新accessToken和refreshToken
     * @return mixed
     * @throws HttpException
     */
    public function actionRefreshToken(){
        $refreshToken = Yii::$app->request->get('refresh-token');
        Yii::$app->user->refreshToken($refreshToken);
        $token = Yii::$app->getUser()->getToken();
        if($token){
            return static::notice($token);
        } else {
            return static::notice('更新Token失败', 401);
        }
    }

    /**
     * 用refreshToken刷新accessToken
     * @return mixed
     * @throws HttpException
     */
    public function actionRefreshAccessToken(){
        $refreshToken = Yii::$app->request->get('refresh-token');
        Yii::$app->user->refreshAccessToken($refreshToken);
        $token = Yii::$app->getUser()->getToken();
        if($token){
            return static::notice($token);
        } else {
            return static::notice('更新Token失败', 401);
        }
    }

    /**
     * 用refreshToken刷新refreshToken
     * @return mixed
     * @throws HttpException
     */
    public function actionRefreshRefreshToken(){
        $refreshToken = Yii::$app->request->get('refresh-token');
        Yii::$app->user->refreshRefreshToken($refreshToken);
        $token = Yii::$app->getUser()->getToken();
        if($token){
            return static::notice($token);
        } else {
            return static::notice('更新Token失败', 401);
        }
    }

    public function actionTest(){
        return static::notice(['test'=>Yii::$app->getUser()->id]);
    }
}