<?php 
/**
* 
*/
class helpAction
{
	
	private $HEAD_CSSJS = ['css'=>['css/article.css', 'css/account.css' ], 'js'=>['js/account.js'], 'plugin'=>[]];

	function __construct()
	{
		LibTpl::Set('HEAD_CSSJS', $this->HEAD_CSSJS);
	}

	/**
	 * @method 帮助中心
	 * @author 廖
	 * @copyright 2018-1-3
	 */
	function index()
	{
		$Params = mvc::$URL_PARAMS;
		LibTpl::Set('Params', $Params);
		if(!isset($Params['catId']) && !isset($Params['artId']) && !isset($Params['keyword'])) LibTpl::Set('addList',true);
		$B2cArticle = new B2cArticle();
		//不在这一类里边
		$helpCat = array_filter(mvc::$cfg['HELP_CAT'],'LibFc::Int');
		LibTpl::Set('helpCat', $helpCat);
		$Cat = ['861'];
		LibTpl::Set('cat', $Cat);
		$helpWhere = !empty($helpCat)?sprintf(' and cat_cat_id not in(%s)',implode(',',$helpCat)):'';
		$where = !empty($Cat)?sprintf('and (cat_pcat_id in(select cat_cat_id from cm_category_tree where cat_pcat_id in(%s) %s) or cat_cat_id in(select cat_cat_id from cm_category_tree where cat_pcat_id in(%s) %s))',implode(',', $Cat),$helpWhere,implode(',', $Cat),$helpWhere):'';
		$res = $B2cArticle->get('cm_category left join cm_category_tree on cat_id=cat_cat_id',['cat_id','cat_name','cat_pcat_id','cat_path'],$where.' order by cat_seq asc');
		$catData = array_column($res['data'], null,'cat_id');
		$catIdArr = array_column($res['data'],'cat_id');
		if(!empty($res['data']))
	    {
	        $new_data = array();
	        //一级的所有数组
	        foreach ($res['data'] as $k => $v)
	        {
	            if($v['cat_pcat_id'] == 0)
	            {
	                $new_data[$v['cat_id']] = $v;
	                unset($res['data'][$k]);
	                continue;
	            }
	            $res['data'][$k]['paths'] = explode(',', $v['cat_path']);
	        }
	        //二级的所有数组
	        foreach($res['data'] as $k=>$v)
	        {
	            if(count($v['paths']) == 2)
	            {
	                $new_data[$v['paths'][1]]['son'][$v['cat_id']] = $v;
	                unset($res['data'][$k]);
	            }
	        }
	        //三级的所有数组
	        foreach($res['data'] as $k=>$v)
	        {
	            if(count($v['paths']) == 3)
	            {
	                $new_data[$v['paths'][1]]['son'][$v['paths'][2]]['son'][$v['cat_id']] = $v;
	                unset($res['data']['paths']);
	            }
	        }
	        // print_r($new_data);
			LibTpl::Set('catList', $new_data);
	    }

		//获取分类下的所有的文章
		if(LibFc::Int($Params['catId']) && !isset($Params['artId']) && !isset($Params['keyword']))
		{
			$artData = $B2cArticle->get('cm_article left join cm_article_category on art_id=arc_art_id',['art_id','art_title','arc_cat_id'],sprintf("and arc_cat_id=%d and art_status='pass' order by art_seq desc,art_id desc",$Params['catId']));
			$navId =  $Params['catId'];
			LibTpl::Set('artTitleList', $artData['data']);
		}

		//获取关键字搜索下的文章
		if(!empty(LibFc::Escape($Params['keyword'])) && !isset($Params['artId']) && !isset($Params['catId']))
		{
			$catWhere = !empty($catIdArr)?sprintf(' and art_id in(select distinct(arc_art_id) from cm_article_category where arc_cat_id in(%s))',implode(',',$catIdArr)):'';
			$artData = $B2cArticle->get('cm_article',['count(*) count'],sprintf("and (art_title like '%%%s%%' or art_keyword like '%%%s%%') and art_status='pass' %s ",$Params['keyword'],$Params['keyword'],$catWhere),true);
			$total = $artData['data']['count'];
			if($total>0)
			{
				$NowPage = isset($Params['page']) && $Params['page'] > 0 ? (int) $Params['page']:1;
				$PerPage = 20;
				$twhere .= $catWhere.sprintf(" and (art_title like '%%%s%%' or art_keyword like '%%%s%%') and art_status='pass' %s order by art_seq desc,art_id desc limit %d,%d",$Params['keyword'],$Params['keyword'], ($NowPage-1)*$PerPage, $PerPage);
				$artData = $B2cArticle->get('cm_article', ['art_id,art_title,art_txt'], $twhere);
				$artData = array_column($artData['data'],null,'art_id');
				$Page = LibPage::Show($total, $PerPage);
				LibTpl::Set('Page', $Page);
				LibTpl::Set('total', $total);
				LibTpl::Set('artSearch', $artData);
			}
		}

		//获取指定文章
		if(LibFc::Int($Params['artId']) && !isset($Params['keyword']) && !isset($Params['catId']))
		{
			$catWhere = !empty($catIdArr)?sprintf(' and arc_cat_id in(%s)',implode(',',$catIdArr)):'';
			$artData = $B2cArticle->get('cm_article left join cm_article_category on art_id=arc_art_id',['art_id','art_title','art_txt','art_add_time','art_last_time','arc_cat_id'],sprintf("and art_id=%d and art_status='pass' %s order by art_seq desc,art_id desc",$Params['artId'],$catWhere),true);
			$navId =  $artData['data']['arc_cat_id'];
			LibTpl::Set('art', $artData['data']);
		}

		//设置导航栏
		$navArr = [];
		if(!empty($navId))
		{
			$novArr = explode(',',$catData[$navId]['cat_path']);
			if(!empty($novArr))
			{
				foreach ($novArr as $key => $value) 
				{
					if(!empty($catData[$value]))
					{
						$novData[$value] = $catData[$value];
					}
				}
				if($catData[$navId])
				{
					$novData[$navId] = $catData[$navId];
					$navArr = array_merge($novArr,[$navId]);
				}
				LibTpl::Set('navList', $novData);
				LibTpl::Set('catId', $navId);
			}
		}
		LibTpl::Set('navArr', $navArr);
		$headArr = array(
			'title'=>'帮助中心 - 跑街的',	
			'keyword'=>'',	
			'des'=>''
		);
		LibTpl::Set('headArr', $headArr);
		LibTpl::Put();
	}
}