<?php

namespace app\controller;

use app\model\{Category, Block, History, Honour, Model as MModel, Article as MArticle, PositionModel};
use think\response\View;

class Page extends Base
{
    /**
     * @return View|void
     */
    public function index($categoryId = 0)
    {
        $category = Category::getById($categoryId);
        if (!empty($category)) {
            $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);
        } else {
            $this->redirect('/404.html');
        }

        $childCategory   = Category::getChildrenByParentId($category['id']);
        $parentCategory  = Category::getById($category['parent_id']);
        $brotherCategory = Category::getChildrenByParentId($category['parent_id']);
        $blocks          = Block::getByCategoryId($category['id']);
        $blocks          = Block::convertValue($blocks);

        // 父级单页配置的公共信息
        $parentBlocks = [];
        if (!empty($parentCategory) && $parentCategory['model_id'] == MModel::MODEL_PAGE) {
            $parentBlocks = Block::getByCategoryId($parentCategory['id']);
            $parentBlocks = Block::convertValue($parentBlocks);
        }

        $this->data['categoryPath']    = Category::categoryPath($category['path']);
        $this->data['topCategoryId']   = Category::firstGradeId($category['path'], $categoryId);
        $this->data['categoryId']      = $category['id'];
        $this->data['category']        = $category;
        $this->data['childCategory']   = $childCategory;
        $this->data['blocks']          = $blocks;
        $this->data['parentBlocks']    = $parentBlocks;
        $this->data['parentCategory']  = $parentCategory;
        $this->data['brotherCategory'] = $brotherCategory;
        $this->data['bodyClass']       = 'main';
        $this->setExtraBaseConfig();

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