<?php

namespace App\Http\Controllers;

use App\Models\Advert;
use App\Models\Article;
use App\Models\Category;
use App\Models\Leader;

class CategoryController extends Controller
{

    /**
     * 显示分类
     *
     * @param  Category  $category  [description]
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View [type]             [description]
     */
    public function show(Category $category)
    {
        //跳转地址
        if ($category->type == Category::TYPE_LINK && $category->uri) {
            return redirect()->away($category->uri);
        }

        $topCate  = $category->getTopCategory();
        $template = 'list';
        if ($category->template) {
            $template = $category->template;
        }

        $parent = $category;
        if ($category->children->isEmpty() && $category->parent) {
            $parent = $category->parent;
        }

        $articles = Article::shown()->ByCategory($category->getAllChildrenId())
            ->where('status', 1)
            ->Bysort()
            ->paginate();

        $data = [
            'articles' => $articles,
            'category' => $category,
            'parent'   => $parent,
            'topCate'  => $topCate,
        ];

        if ($category->id == 2) {
            $article = Article::shown()->ByCategory(2)->Bysort()->first();
            $data    = array_merge($data, [
                'article' => $article,
            ]);
        }

        if ($category->id == 18) {
            $cgCate     = Category::find(24);
            $hjCate     = Category::find(20);
            $zlCate     = Category::find(21);
            $cgArticles = Article::shown()->ByCategory($cgCate->id)->Bysort()->take(3)->get();
            $hjArticles = Article::shown()->ByCategory($hjCate->id)->Bysort()->take(3)->get();
            $zlArticles = Article::shown()->ByCategory($zlCate->id)->Bysort()->take(6)->get();

            $data = array_merge($data, [
                'cgCate'     => $cgCate,
                'hjCate'     => $hjCate,
                'zlCate'     => $zlCate,
                'cgArticles' => $cgArticles,
                'hjArticles' => $hjArticles,
                'zlArticles' => $zlArticles,
            ]);
        }

        if ($category->id == 3) {
            $articles = Article::shown()->ByCategory($category->getAllChildrenId())
                ->where('status', 1)
                ->Bysort()
                ->get();

            $data = array_merge($data, [
                'articles' => $articles,
            ]);
        }

        if ($category->id == 5) {
            $tzCate    = Category::find(6);
            $nyydtCate = Category::find(7);
            $mtbdCate  = Category::find(31);

            $tzArticles    = Article::shown()->ByCategory($tzCate->id)->Bysort()->take(4)->get();
            $nyydtArticles = Article::shown()->ByCategory($nyydtCate->id)->Bysort()->take(3)->get();
            $mtbdArticles  = Article::shown()->ByCategory($mtbdCate->id)->Bysort()->take(3)->get();
            $data          = array_merge($data, [
                'tzCate'        => $tzCate,
                'nyydtCate'     => $nyydtCate,
                'mtbdCate'      => $mtbdCate,
                'tzArticles'    => $tzArticles,
                'nyydtArticles' => $nyydtArticles,
                'mtbdArticles'  => $mtbdArticles,
            ]);
        }

        if ($category->id == 11) {
            $yjCate = Category::find(12);
            $zdCate = Category::find(16);
            $ljCate = Category::find(29);

            $yjArticles = Article::shown()->ByCategory($yjCate->id)->Bysort()->take(3)->get();
            $ljLeaders  = Leader::shown()->where('category_id', $ljCate->id)->Bysort()->take(3)->get();
            $data       = array_merge($data, [
                'yjCate'     => $yjCate,
                'zdCate'     => $zdCate,
                'ljCate'     => $ljCate,
                'yjArticles' => $yjArticles,
                'ljLeaders'  => $ljLeaders,
            ]);
        }

        if ($category->id == 32) {

            $nghjxhArticle = Article::shown()->ByCategory($category->id)->Bysort()->first();
            $xhdtCate      = Category::find(33);
            $xhdtArticles  = Article::shown()->ByCategory($xhdtCate->id)->Bysort()->get();

            $data = array_merge($data, [
                'nghjxhArticle' => $nghjxhArticle,
                'xhdtCate'      => $xhdtCate,
                'xhdtArticles'  => $xhdtArticles,
            ]);
        }

        return view('category.'.$template, $data);
    }

}
