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

namespace wap\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use wap\controllers\Controller;
use wap\models\Task;
use wap\models\Announcement;

/**
 * wap默认控制器
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class SiteController extends Controller
{
    /**
     * 首页
     */
    public function actionIndex()
    {
        $tasks = Task::getMyTask(null, 10);
        $notReadNum = Announcement::getNotReadNum();

        return $this->render('index', [
            'tasks' => $tasks,
            'notReadNum' => $notReadNum,
        ]);
    }

    /**
     * 通知
     */
    public function actionAnnounce()
    {
        $announces = Announcement::getAnnounce();
        $notReadNum = Announcement::getNotReadNum();

        return $this->render('announce', [
            'announces' => $announces,
            'notReadNum' => $notReadNum,
        ]);
    }

    /**
     * 公共错误页面
     */
    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];
            }
        }
    }
}
