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

use App\Http\Controllers\Web\WebBaseController;
use App\Models\Policy;
use App\Models\PolicyCategory;
use App\Models\PolicyProperty;
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 WebBaseController
{
    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 = '2', $recommend = '7', $other_num = '5')
    {
        $key = $request->input('key');
        $categorys = $this->policycategoryService->getChildCategorys($id);
        $search_id = '';
        if ($categorys) {
            //$search_id = $categorys[0]->id;
            $search_id = $id;
            $search_where = array();
            $search_where['id'] = $search_id;
            if ($id != '1') {
                $search_where['parent_id'] = $id;
            }
            if ($num != '8') {
                $search_where['num'] = $num;
            }
            if ($focus != '2') {
                $search_where['focus'] = $focus;
            }
            if ($recommend != '7') {
                $search_where['recommend'] = $recommend;
            }
            if ($other_num != '5') {
                $search_where['other_num'] = $other_num;
            }
            return redirect()->route('policy.list', $search_where);
        } else {
            $rst = $this->articleService->list($key, '', 10);
        }

        $focus_articles = $this->policycategoryService->getFocosPolicys($focus, $other_num); //省级
        $recommend_articles = $this->policycategoryService->getFocosPolicys($recommend, $other_num); //市级
        //顶部广告位
        $ad_data = array(
            'theme'       => 'default',
            'org'         => 'Home',
            'alias'       => 'AIX_news_index_top',
            'num'         => '1'
        );
        $top_ads = $this->adService->getAds($ad_data);

        $return_data = array(
            'articles'           => $rst,
            'categorys'          => $categorys,
            'focus_articles'     => $focus_articles,
            'recommend_articles' => $recommend_articles,
            'key'                  => $key,
            'top_ads'              => $top_ads
        );
        return view('app.content.policy.index', $return_data);
    }

    public function list(Request $request, $id, $parent_id = '1', $num = '8', $focus = '2', $recommend = '7', $other_num = '5')
    {

        $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);
        $focus_articles = $this->policycategoryService->getFocosPolicys($focus, $other_num); //省级
        $recommend_articles = $this->policycategoryService->getFocosPolicys($recommend, $other_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,
            'categorys_root'=>$categorys_root,
            'focus_articles'=>$focus_articles,
            'recommend_articles'=>$recommend_articles,
            'key'=>$key,
            'type_id'=>$id,
            'parent_id'=>$category->parent_id,
            'top_nav'=>$topNav,
            'now_cate'=>$now_cate
        );

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

    public function show($id, $parent_id = '1', $num = '8', $focus = '2', $recommend = '7', $other_num = '5')
    {

        $policy =Policy::where(array('id'=>$id))->first();
        $categorys = $this->policycategoryService->getChildCategorys($policy->parentid);
        $categorys_root = $this->policycategoryService->getChildCategorys(0);//一级分类
        $focus_articles = $this->policycategoryService->getFocosPolicys($focus, $other_num); //省级
        $recommend_articles = $this->policycategoryService->getFocosPolicys($recommend, $other_num); //市级
        $policy_info = $this->policycategoryService->getPolicyInfo($id);

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

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

        $return_data = array(
            'categorys'=>$categorys,
            'categorys_root'=>$categorys_root,
            'focus_articles'=>$focus_articles,
            'recommend_articles'=>$recommend_articles,
            'type_id'=>$id,
            'info'=>$policy_info,
            'parent_id'=>$parent_id
        );

        return view('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);
    }
}