<?php
namespace App\Http\Controllers\Web\Content;

use App\Http\Controllers\Web\WebBaseController;
use App\Services\Content\ArticleService;
use App\Services\Content\ArticleCategoryService;
use App\Services\Content\AdService;
use App\Services\Content\NavigationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;


class ServiceareaController extends WebBaseController
{
    protected $articleService;
    protected $articleCategoryService;
    protected $adService;
    protected $navigationService;

    /**
     * ArticleController constructor.
     * @param $articleService
     * @param $articleCategoryService
     */
    public function __construct(ArticleService $articleService, ArticleCategoryService $articleCategoryService, AdService $adService, NavigationService $navigationService)
    {
        $this->articleService = $articleService;
        $this->articleCategoryService = $articleCategoryService;
        $this->adService = $adService;
        $this->navigationService = $navigationService;
    }

    public function list(Request $request, $id, $parent_id = '38', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
    {
        $key = $request->input('key');
        $lists = $this->articleService->list($key, $id, '10');
        $categorys = $this->articleCategoryService->getChildCategorys($parent_id, $num);

        //获取后台职场资讯名称
        $where = array(
            'alias'=>'AIX_top',
            'pagealias' => 'AIX_news',
            'display'=>'1'
        );
        $topNav = $this->navigationService->getTopNav($where);
        $now_cate = '';
        $article_cate = array();
        foreach ($categorys as $k => $v) {
            if ($v->id == $id) {
                $now_cate = $v->category_name;
                $article_cate = $v;
            }
        }
        $this->putSeoData('article_cate', $article_cate);

        $return_data = array(
            'articles'=>$lists,
            'categorys'=>$categorys,
            'key'=>$key,
            'type_id'=>$id,
            'top_nav'=>$topNav,
            'now_cate'=>$now_cate
        );

        return view('app.content.servicearea.list', $return_data);
    }

    public function show($id, $parent_id = '38', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
    {
        $categorys = $this->articleCategoryService->getChildCategorys($parent_id, $num);

        $article_info = $this->articleService->getArticleInfo($id);
        if (!$article_info) {
            $back_url = \Illuminate\Support\Facades\URL::previous();
            return $this->showMessage('资讯不存在', $back_url, true, '上一页', '3');
        }

        $this->putSeoData('article', $article_info);

        $return_data = array(
            'categorys'=>$categorys,
            'type_id'=>$id,
            'info'=>$article_info,
            'parent_id'=>$parent_id
        );

        return view('app.content.servicearea.show', $return_data);
    }

    public function click($id)
    {
        $rst = $this->articleService->incrementData(array('id'=>$id), 1, 'click');
        $data = array('status'=>0);
        if ($rst) {
            $data = array('status'=>1);
        }
        return response()->json($data);
    }
}