<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\controllers;

use Yii;
use common\models\LoginForm;
use yii\console\Request;
use common\models\User;
use qcbase\models\UserInfo;
use home\models\UserPortlet;
use home\modules\receivable\models\Plan;

/**
 * Site controller
 */
class SiteController extends Controller
{

    public function actionNotice()
    {
        echo '<span style="font-size:20px;color:red;">系统升级更新中...</span>';
        if (Yii::$app->request->get('test')) return SiteController::actionIndex();
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [];
    }

    /**
     * 工作台首页
     */
    public function actionIndex()
    {
        //回款计划消息提示
        Plan::checkPrompt();

        $portlets = UserPortlet::getUserPortlet();

        //$navTree = $model -> getAdminMenu();
        //return $this ->renderPartial('index', ['navTree' => $navTree]);
        return $this->render('index', [
            'portlets' => $portlets,
        ]);
    }

    /**
     * 公共错误页面
     */
    public function actionError()
    {
        $request = Yii::$app->request;
        $this->layout = '//main';
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            $errorMsg = '出错了！' . $exception->getMessage();
            if (!$request->isAjax) {
                return $this->flash($errorMsg, 'error', null, 0);
            } else {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return ['status' => 0, 'info' => $errorMsg];
            }
        }
    }

    /**
     * 公共提示页面
     * @param string $info 提示信息 也可以是数组['info' => '操作成功']
     * @param string $type 提示类型 success error
     * @param string $url 要跳转的地址 默认为返回上一页
     * @return type
     */
    public function actionFlash($info = '', $type = '', $url = '')
    {
        $this->layout = '//main';
        return $this->flash($info, $type, $url);
    }

    /**
     * 修改密码
     */
    public function actionRepwd()
    {
        $this->layout = '//modal';
        $model = new User;
        $model = User::findOne(Yii::$app->user->identity->user_id);
        if (Yii::$app->request->isPost) {
            $model->scenario = 'repwd';
            $model->load(Yii::$app->request->post());
            if ($model->validate() && $model->save()) {
                Yii::$app->getSession()->setFlash('success', '密码修改成功');
            }
        }
        return $this->render('repwd', [
                'model' => $model,
                'action' => ['site/repwd'],
        ]);
    }

    /**
     * 修改信息
     */
    public function actionInfo()
    {
        $this->layout = '//modal';
        $model = new User;
        $model->info = new UserInfo;
        $model = User::find()
            ->where(['user_id' => Yii::$app->user->identity->user_id])
            ->with('info')
            ->one();
        if (Yii::$app->request->isPost) {
            $model->scenario = 'info';
            $model->load(Yii::$app->request->post());
            $model->user_id = Yii::$app->user->identity->user_id;
            if ($model->validate() && $model->save()) {
                Yii::$app->getSession()->setFlash('success', '个人信息更新成功');
            } else {
                Yii::$app->getSession()->setFlash('error', '个人信息更新失败');
            }
        }
        return $this->render('info', [
                'model' => $model,
                'action' => ['site/info'],
        ]);
    }
}
