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

use App\Exceptions\ResponseException;
use App\Http\Controllers\Web\WebBaseController;
use App\Services\Content\HelpCategoryService;
use App\Services\Content\HelpService;
use App\Services\Content\AdService;
use Illuminate\Http\Request;


class HelpController extends WebBaseController
{
    protected $helpCategoryService;
    protected $helpService;
    protected $adService;
    protected $ad_infos;

    /**
     * HelpController constructor.
     * @param $helpCategoryService
     */
    public function __construct(HelpCategoryService $helpCategoryService, HelpService $helpService, AdService $adService)
    {
        $this->helpCategoryService = $helpCategoryService;
        $this->helpService = $helpService;
        $this->adService = $adService;
        $this->ad_infos = $this->getAdInfo();
    }


    public function index()
    {
        //获取帮助中心分类信息
        $categories = $this->helpCategoryService->getCategories(array('parent_id'=>'0'), 15);
        $return_data = array(
            'categories' => $categories,
            'ads'         => $this->ad_infos
        );

        return view('app.content.help.index', $return_data);
    }
    public function list($id)
    {
        $return_data = array();
        $helps = $this->helpService->getHelpsByType($id, '20');
        $categories = $this->helpCategoryService->noHelpCategories(array('parent_id'=>'0'), 15);
        $cate_info = $this->helpCategoryService->getOneCategory(array('id'=>$id));
        $this->putSeoData('help_cate', $cate_info);

        $return_data = array(
            'categories' => $categories,
            'helps'       => $helps,
            'cate_info'  => $cate_info,
            'ads'         => $this->ad_infos
        );
        return view('app.content.help.list', $return_data);
    }
    public function search(Request $request, $filter = '')
    {
        $return_data = array();
        $key = $filter;
        if ($request->input('key')) {
            $key = $request->input('key');
        }
        $where[] = array('title','like','%'.$key.'%');
        $helps = $this->helpService->getHelps($where, '20');
        $categories = $this->helpCategoryService->noHelpCategories(array('parent_id'=>'0'), 15);

        $return_data = array(
            'categories' => $categories,
            'helps'       => $helps,
            'key'         => $key,
            'ads'         => $this->ad_infos
        );
        return view('app.content.help.list', $return_data);
    }
    public function show($id)
    {
        $return_data = array();
        $categories = $this->helpCategoryService->noHelpCategories(array('parent_id'=>'0'), 15);
        $help_info = $this->helpService->getHelp(array('id'=>$id));
        $this->putSeoData('help', $help_info->toArray());
        $return_data = array(
            'categories' => $categories,
            'help_info'  => $help_info,
            'ads'         => $this->ad_infos
        );

        return view('app.content.help.show', $return_data);
    }
    public function getAdInfo()
    {
        //$subsite_id = 0;
        $ad_data = array(
            'theme'       => 'default',
            'org'         => 'Home',
            'alias'       => 'AIX_help_top',
            'num'         => '1',
            //'subsite_id' => $subsite_id
        );
        $ad_infos = $this->adService->getAds($ad_data);
        return $ad_infos;
    }
    public function testEmail()
    {
        $this->orderService->testEmail();
    }

}