<?php

namespace app\controller;

use app\model\{Article as MArticle, Category};
use think\facade\View;

class Article extends Base
{
    //详情
    public function detail($id = 0)
    {

        if ($id <= 0) {
            return $this->error('错误页面');
        }
        $article = MArticle::getById($id);
        if (empty($article)) {
            return $this->error('无此文章');
        }
        MArticle::updateById($id, ['views' => $article['views'] + 1]);
        $category = Category::getById($article['category_id']);
        $prev = MArticle::getPrevArticleByIdAndCategoryId($id, $article['category_id']);
        $next = MArticle::getNextArticleByIdAndCategoryId($id, $article['category_id']);

        $keywords = $article['seo_keywords'] ? $article['seo_keywords'] : $this->system['seo_keywords'];
        $description = $article['seo_description'] ? $article['seo_description'] : $this->system['seo_description'];
        $this->setSeo($article['title'], $keywords, $description);
        $this->data['article'] = $article;
        $this->data['category'] = $category;
        $this->data['categoryId'] = $category['id'];
        $this->data['prev'] = $prev;
        $this->data['next'] = $next;
        return $this->view($category['template_detail'] ?? '');
    }

    //列表页
    public function index()
    {
        $categoryId = input('category_id/d', 0);
        if ($categoryId <= 0) {
            return $this->error('错误页面');
        }
        $category = Category::getById($categoryId);
        $page_size = 20;
        if( $category['number'])$page_size= $category['number'];

        if( $this->request->param("page_size/d")){
            $page_size= $this->request->param("page_size/d");
            cookie("article_page_size",$this->request->param("page_size/d"));
        }else{
            if( cookie("?article_page_size"))$page_size=  cookie("article_page_size");
        }

        if (empty($category)) {
            return $this->error('错误页面');
        }
        $childCategory = Category::getChildrenByParentId($categoryId);
        $description = $category['description'] ? $category['description'] : $this->system['seo_description'];
        $this->setSeo($category['title'], $this->system['seo_keywords'], $description);
        $this->data['items'] = MArticle::getListPageByCategory($categoryId, $page_size);
        $this->data['category'] = $category;
        $this->data['categoryId'] = $categoryId;
        $this->data['page_size'] = $page_size;
        $this->data['article_count'] = MArticle::getListCount($categoryId);

        return $this->view($category['template_list'] ?? '');
    }

    //查询
    public function query()
    {
        $keyword = $this->request->param("keyword/s");
        //无key值跳转到产品
        if (!$keyword) $this->redirect((string)url('/articles/12.html'));

        $where = [
            ['category_id', 'in', [5, 12, 13, 14, 15]],
            ['status', '=', 1],
        ];

        $where[] = ['title', 'like', '%' . $keyword . '%'];
        $param['keyword'] = $keyword;

        $paginate = [
            'list_rows' => 20,
            'query' => $param
        ];
        $data = MArticle::where($where)
            ->order("sort desc")
            ->paginate($paginate, false);

        View::assign("items", $data);
        View::assign("keyword", $keyword);
        $this->data['category'] = Category::getById(5);
        $this->data['categoryId'] = 5;
        return $this->view("/article/product _query");
    }

}