<?php

namespace app\controller;

use app\model\{Article as MArticle,
    Category
};
use page\DxtcPageA;
use think\Paginator;

class Ljct extends Base
{
    public function index()
    {
        $keyword = input('keyword/s', '');
        $categoryId = input("category_id", 0);
        if (!empty($categoryId)) {
            $category = Category::where('id', $categoryId)->find();
            $parentCategory = Category::where('id', $category['parent_id'])->find();
        } else {
            $parentCategory = Category::where('id', Category::CATEGORY_LJCT)->find();
            $category = Category::where('parent_id', Category::CATEGORY_LJCT)->order('sort asc')->find();
        }

        $categoryId = $category['id'];

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $category['seo_description'] ?: $this->system['seo_description'];
        $keywords = $category['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $category['seo_title'] ?: $category['title'] . ' | ' . $this->system['seo_title'];
        $this->setSeo($title, $keywords, $description);
        $listSort = ['a.top' => 'desc', 'a.sort' => 'desc'];

        // 自定义分页驱动
        app('think\App')->bind(Paginator::class, DxtcPageA::class);

        $items = MArticle::getList($categoryId, 6, $keyword, [], 1, $listSort, false);

        $this->data['items'] = MArticle::parseList($items);
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $category['id'];
        $this->data['bodyClass'] = 'main';

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

    //详情
    public function detail($id = 0)
    {
        if ($id <= 0) {
            return $this->error('错误页面');
        }
        $article = MArticle::getById($id);
        if (empty($article)) {
            return $this->error('无此文章');
        }

        $category = Category::where('id', $article['category_id'])->find();
        $parentCategory = Category::where('id', $category['parent_id'])->find();

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $article['seo_description'] ?: $this->system['seo_description'];
        $keywords = $article['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $article['seo_title'] ?: $article['title'] . ' | ' . $this->system['seo_title'];

        $this->setSeo($title, $keywords, $description);
        $this->data['item'] = MArticle::parseInfo($article);
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $category['id'];
        return $this->view();
    }

}