<?php
/**
 * Created by PhpStorm.
 * User: jacklu
 */

namespace app\controllers;

use app\models\Article;
use app\models\Category;
use app\models\Comment;
use yii\web\Controller;

/**
 * Class IndexController
 * @package app\controllers
 */
class IndexController extends Controller
{
    /**
     * FOR INDEX
     * @param page_num int 当前页码数
     * @param count int 总条数
     * @param type string 选择渲染不同的页面
     * @param category_id int 分类ID
     * @return string
     */
    public function actionIndex()
    {
        $page_size = \Yii::$app->params['page_size'];
        $type = \Yii::$app->request->get('type', 'index');
        $count = intval(\Yii::$app->request->get('count'));
        $page_num = intval(\Yii::$app->request->get('page_num', 1));
        $category_id = intval(\Yii::$app->request->get('category_id'));
        if ($count < $page_num * $page_size) {
            $page_num = ceil($count / $page_size);
        }
        if ($page_num <= 0) {
            $page_num = 1;
        }
        // If you click on the classification list to query the corresponding classification, not all the articles on the query.
        $article_model = new Article();
        $category_model = new Category();
        if (!$category_id) {
            $count = $article_model->ArticleCount();
            $article_list = $article_model->ArticleList($page_num, $page_size, $where = true);
        } else {
            $count = $article_model->ArticleCountByCategory($category_id);
            $article_list = $article_model->GetArticleListByCategoryId($category_id, $page_num, $page_size);
        }
        $category_list = $category_model->CategoryList();
        $recent_posts = $article_model->GetRecentPosts();
        if ($type == 'full') {
            return $this->render('full-width', [
                'article_list' => $article_list,
                'page_num' => $page_num,
                'count' => $count,
            ]);
        } else {
        return $this->render('index', [
            'article_list' => $article_list,
            'category_list' => $category_list,
            'recent_posts' => $recent_posts,
            'page_num' => $page_num,
            'count' => $count,
        ]);
        }

    }

    /**
     * @return \yii\web\Response
     */
    public function actionComment()
    {
        $model = new Article();
        $session = \Yii::$app->session;
        if (\Yii::$app->request->isPost) {
            if ($model->load(\Yii::$app->request->post()) && $model->save(true)) {
                $session->setFlash('success', 'Comment submitted successfully.', true);
                return $this->redirect(['index/detail']);
            }
        }
        return $this->redirect(['index/detail']);
    }

    /**
     * FOR SINGLE
     * @return string
     */
    public function actionSingle()
    {
        $article_model = new Article();
        $category_model = new Category();
        $comment_model = new Comment();
        $id = intval(\Yii::$app->request->get('id', 1));
        $article = Article::GetArticleDetail($id);
        $comment = $comment_model->GetAllCommentByArticle($id);
        $category_list = $category_model->CategoryList();
        $recent_posts = $article_model->GetRecentPosts();
        return $this->render('single', [
            'category_list' => $category_list,
            'recent_posts' => $recent_posts,
            'model' => $comment_model,
            'article' => $article,
            'comment' => $comment,

        ]);
    }

    /**
     * FOR 404 AND ERROR
     * @return string
     */
    public function actionNotfound()
    {
        return $this->render('notfound', [

        ]);
    }

    /**
     * FOR ABOUT
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about', [

        ]);
    }
}