<?php

namespace App\Http\Controllers;

use App\Models\Advert;
use App\Models\Article;
use App\Models\Category;
use App\Models\Link;
use Illuminate\Http\Request;

class IndexController extends Controller
{

    /**
     * Notes: 首页
     *
     * @Author: 玄尘
     * @Date  : 2020/6/1 9:11
     */
    public function index()
    {
        $nyydtCate = Category::find(7);//能源院动态
        $mtbdCate  = Category::find(31);//媒体报道
        $dzjsCate  = Category::find(28);//党政建设
        $xxydCate  = Category::find(9);//学习园地

        $notices = Article::shown()
            ->ByCategory(6)
            ->bysort()
            ->take(2)
            ->get();//两个通知公告


        $mtbdArticles = Article::shown()
            ->byCategory(31)
            ->bysort()
            ->take(2)
            ->get();


        $nyydtimg = Article::shown()
            ->byCategory(7)
            ->whereNotNull('cover')
            ->bysort()
            ->take(2)
            ->get();

        $nyydt = Article::shown()
            ->whereNotIn('id', $nyydtimg->pluck('id'))
            ->byCategory(7)
            ->bysort()
            ->take(4)
            ->get();

        $dzjsimg = Article::shown()
            ->byCategory($dzjsCate->id)
            ->whereNotNull('cover')
            ->bysort()
            ->first();

        $dzjsArticles = Article::shown()
            ->where('id', '<>', $dzjsimg->id)
            ->byCategory($dzjsCate->id)
            ->bysort()
            ->take(6)
            ->get();

        $xxydimg = Article::shown()
            ->byCategory($xxydCate->id)
            ->whereNotNull('cover')
            ->bysort()
            ->first();

        $xxydArticles = Article::shown()
            ->where('id', '<>', $xxydimg->id)
            ->byCategory($xxydCate->id)
            ->bysort()
            ->take(6)
            ->get();

        $advert = Advert::query()->where('category_id', 36)->first();

        $categorys = Category::oldest('order')->find([20, 21, 24]);//成果
        $categorys = $categorys->map(function ($info) {
            $imgArticle         = Article::shown()
                ->byCategory($info->id)
                ->whereNotNull('cover')
                ->bysort()
                ->first();
            $info->imgArticle   = $imgArticle;
            $info->articleLists = Article::shown()
                ->where('id', '<>', $imgArticle->id)
                ->byCategory($info->id)
                ->bysort()
                ->take(4)
                ->get();
            return $info;
        });


        $data = compact(
            'categorys',
            'xxydCate',
            'mtbdCate',
            'nyydtCate',
            'dzjsCate',
            'notices',
            'mtbdArticles',
            'xxydArticles',
            'dzjsArticles',
            'nyydt',
            'dzjsimg',
            'xxydimg',
            'advert',
            'nyydtimg'
        );
        return view('index.index', $data);
    }

    /**
     * Notes: 搜索
     *
     * @Author: 玄尘
     * @Date: 2022/6/29 10:14
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function search(Request $request)
    {
        $title    = $request->title;
        $articles = Article::shown()->latest()->where('title', 'like', "%{$title}%")->paginate();

        return view('index.search', compact('articles'));
    }


}
