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

use App\Http\Controllers\Mobile\MobileBaseController;
use App\Models\Policy;
use App\Models\PolicyCategory;
use App\Services\Content\ArticleService;
use App\Services\Content\ArticleCategoryService;
use App\Services\Content\AdService;
use App\Services\Content\NavigationService;
use App\Services\Content\PolicyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;


class PolicyController extends MobileBaseController
{
    protected $articleService;
    protected $articleCategoryService;
    protected $adService;
    protected $navigationService;
    protected $policycategoryService;


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

    public function index(Request $request, $id = '1', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
    {
        return redirect(route('mobile.policy.list',['id'=>72]));
        $size = 5;
        $key = $request->input('key');
        $rst = $this->policycategoryService->policy_list($key, $id, '10');
        $categorys_root = $this->policycategoryService->getChildCategorys(0);//一级分类
        $categorys = $this->policycategoryService->getChildCategorys(1);//一级分类

        if ($request->ajax()) {
            if ($rst->lastPage() < $rst->currentPage()) {
                return response()->json(['status'=>0]);
            }
            return response()->json(['status'=>1,'data'=>view('mobile.app.content.policy.ajax_policy_list', ['articles'=>$rst])->render()]);
        }
        $mobile_dropload = false;
        if ($rst->total() > $size) {
            $mobile_dropload = true;
        }
        $return_data = array(
            'articles'           => $rst,
            'categorys'          => $categorys,
            'categorys_root'          => $categorys_root,
            'type_id'            => 9,
            'roottype_id'            => 1,
            'current_url'        => \Illuminate\Support\Facades\Request::getRequestUri(),
            'wap_title'          => '人才政策',
            'mobile_dropload'    => $mobile_dropload
        );

        return view('mobile.app.content.policy.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')?$request->input('key'):'';
        $category=PolicyCategory::where(array('id'=>$id))->first();
        $lists = $this->policycategoryService->policy_list($key, $id, '10');
        $categorys_root = $this->policycategoryService->getChildCategorys(0);//一级分类
        $search_id=$category->parent_id!=0?$category->parent_id:$id;
        $categorys = $this->policycategoryService->getChildCategorys($search_id);

        $mobile_dropload = false;
        if ($lists->total() > $size) {
            $mobile_dropload = true;
        }
        $now_cate = '';
        $article_cate = array();
        foreach ($categorys as $k => $v) {
            if ($v->id == $id) {
                $now_cate = $v->categoryname;
                $article_cate = $v;
            }
        }
        $this->putSeoData('article_cate', $article_cate);

        $return_data = array(
            'articles'=>$lists,
            'categorys'=>$categorys,
            'categorys_root'=>$categorys_root,
            'key'=>$key,
            'type_id'=>$id,
            'roottype_id' => $search_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.policy.ajax_policy_list', ['articles'=>$lists])->render()]);
        }

        return view('mobile.app.content.policy.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 =Policy::where(array('id'=>$id))->first();
        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,
            'wap_title' => $article_info->title,
            'parent_id' => $parent_id
        );
        return view('mobile.app.content.policy.show', $return_data);
    }

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