<?php
namespace app\controller;

use app\model\{Category, Block, Article, History, Model};

class Page extends Base
{
    // 默认单页页面
    public function index($categoryId)
    {
        $category = Category::getById($categoryId);
        if ($category) {
            $description = $category['description'] ? $category['description'] : $this->system['seo_description'];
            $this->setSeo($category['title'], $this->system['seo_keywords'], $description);
        } else {
            return $this->error('页面错误');
        }

        $this->data['categoryId'] = $categoryId;
        $this->data['category'] = $category;
        $this->data['blocks'] = Block::getByCategoryId($categoryId);
        $this->templateDetailAssign($category);

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

    private function templateDetailAssign($category)
    {
        $template = $category['template_detail'] ?? '';
        $TopCId = Category::firstGradeById($category['id']);
        if($TopCId == $category['id']) {
            $topCategory = $category;
        } else {
            $topCategory = Category::getById($TopCId);
        }
        $childCategory = Category::getChildrenByParentId($topCategory['id']);

        $this->data['topCategory'] = $topCategory;
        $this->data['childCategory'] = $childCategory;
        switch ($template) {
            case 'about' :
                $this->assignAbout($childCategory);
                break;
            case 'service' :
                $this->assignService($childCategory);
                break;
            case 'marketing' :
                $this->assignMarketing($childCategory);
                break;
            case 'contact' :
                $this->assignContact($childCategory);
                break;
            default :
                $this->data['blocks'] = Block::getByCategoryId($category['id']);
        }
    }

    // 获取单页栏目IDs
    private function getBlockCateIds($categoryItems)
    {
        $blockCateIds = [];
        foreach ($categoryItems as $cate) {
            if($cate['model_id'] == Model::PAGE) {
                $blockCateIds[] = $cate['id'];
            }
        }
        return $blockCateIds;
    }

    // 走进超宇
    private function assignAbout($childCategory)
    {
        $honorTopCId = Category::$CIdList['honors_manage'] ?? 0;
        $historyCId = Category::$CIdList['history_manage'] ?? 0;
        $historyCate = Category::getById($historyCId);
        $honors = [];
        $blocks = [];
        $blockCateIds = $this->getBlockCateIds($childCategory);
        if($honorTopCId) {
            $honors = Category::getChildrenByParentId($honorTopCId);
            foreach ($honors as &$honor) {
                $honor['items'] = Article::getListByCategoryIds([$honor['id']], $honor['number'] ? $honor['number'] : 20, '', [], 1);
            }
            unset($honor);
        }
        $blockList = Block::getByCategoryIds($blockCateIds);
        $aboutChildrenFlip = array_flip(Category::$CIdList['about_children']);
        foreach ($childCategory as $cate) {
            $blocks[$aboutChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
        }

        $this->data['blocks'] = $blocks;
        $this->data['honors'] = $honors;
        $this->data['historyList'] = array_reverse(History::getByCategoryId($historyCId, true, $historyCate['number'] ?? -1));
    }

    // 品质与服务
    private function assignService($childCategory)
    {
        $blocks = [];
        $blockCateIds = $this->getBlockCateIds($childCategory);
        $blockList = Block::getByCategoryIds($blockCateIds);
        $serviceChildrenFlip = array_flip(Category::$CIdList['service_children']);
        foreach ($childCategory as $cate) {
            $blocks[$serviceChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
        }

        $this->data['blocks'] = $blocks;
    }

    // 营销网络
    private function assignMarketing($childCategory)
    {
        $blocks = [];
        $blockCateIds = $this->getBlockCateIds($childCategory);
        $blockList = Block::getByCategoryIds($blockCateIds);
        $marketingChildrenFlip = array_flip(Category::$CIdList['marketing_children']);
        foreach ($childCategory as $cate) {
            $blocks[$marketingChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
        }
        $achievementCate = Category::getById(Category::$CIdList['achievement_manage']);
        $achievementList = [];
        if ($achievementCate) {
            $achievementList = Article::getLatestByCategory($achievementCate['id'], $achievementCate['number'] ? $achievementCate['number'] : 10, 1);
        }
        $this->data['blocks'] = $blocks;
        $this->data['achievementList'] = $achievementList;
    }

    // 联系我们
    private function assignContact($childCategory)
    {
        $blocks = [];
        $blockCateIds = $this->getBlockCateIds($childCategory);
        $blockList = Block::getByCategoryIds($blockCateIds);
        $contactChildrenFlip = array_flip(Category::$CIdList['contact_children']);
        foreach ($childCategory as $cate) {
            $blocks[$contactChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
        }
        $jobsCate = Category::getById(Category::$CIdList['jobs_manage']);
        $jobList = Article::getLatestByCategory($jobsCate['id'], $jobsCate['number'] ? $jobsCate['number'] : 10, 1);
        $this->data['blocks'] = $blocks;
        $this->data['jobList'] = $jobList;
    }
}