<?php

namespace App\Http\Controllers\Mobile\Content;

use App\Http\Controllers\Mobile\MobileBaseController;
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 ArticleController extends MobileBaseController
{
    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 index(Request $request, $id = '1', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
    {
        $size      = 5;
        $key       = $request->input('key');
        $rst       = $this->articleService->list($key, $id, $size);
        $categorys = $this->articleCategoryService->getChildCategorys($id, $num);

        if ($request->ajax()) {
            if ($rst->lastPage() < $rst->currentPage()) {
                return response()->json(['status' => 0]);
            }
            return response()->json(['status' => 1, 'data' => view('mobile.app.content.article.ajax_article_list', ['articles' => $rst])->render()]);
        }
        $mobile_dropload = false;
        if ($rst->total() > $size) {
            $mobile_dropload = true;
        }
        $return_data = [
            'articles'        => $rst,
            'categorys'       => $categorys,
            'type_id'         => '',
            'current_url'     => \Illuminate\Support\Facades\Request::getRequestUri(),
            'wap_title'       => '职场资讯',
            'mobile_dropload' => $mobile_dropload,
            'key'             => $key,
        ];

        return view('mobile.app.content.article.index', $return_data);
    }

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

        $mobile_dropload = false;
        if ($lists->total() > $size) {
            $mobile_dropload = true;
        }
        $now_cate     = '';
        $article_cate = [];
        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 = [
            'articles'        => $lists,
            'categorys'       => $categorys,
            'key'             => $key,
            'type_id'         => $id,
            'current_url'     => \Illuminate\Support\Facades\Request::getRequestUri(),
            'mobile_dropload' => $mobile_dropload,
            'wap_title'       => '职场资讯',
            //'top_nav'=>$topNav,
            'now_cate'        => $now_cate,
        ];

        if ($request->ajax()) {
            if ($lists->lastPage() < $lists->currentPage()) {
                return response()->json(['status' => 0]);
            }
            return response()->json(['status' => 1, 'data' => view('mobile.app.content.article.ajax_article_list', ['articles' => $lists])->render()]);
        }

        return view('mobile.app.content.article.index', $return_data);
    }

    public function show($id, $parent_id = '1', $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 = [
            'categorys'       => $categorys,
            'type_id'         => $id,
            'info'            => $article_info,
            'wap_title'       => $article_info->title,
            'parent_id'       => $parent_id,
            'share_title'     => $article_info->title,
            'share_desc'      => $article_info->seo_description,
            'share_image_url' => $article_info->small_img ? storage_path($article_info->small_img) : '',
        ];
        return view('mobile.app.content.article.show', $return_data);
    }

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