<?php defined('BASEPATH') OR exit('No direct script access allowed');
header("Content-Type: text/html; charset=UTF-8");
/**
 * 인클루드 class
 *
 * cm_controller: 부모 Class(CI_Controller 확장한 custome class)
 */
include_once(APPPATH.'core/m/cm_controller.php');
class board extends cm_Controller
{


	function fn_notice(){

		$this->load->model('/web/m_bbs_notice');

		$current_page= (isset($_GET['page'])? $_GET['page'] : 1);
		$limit=10;


		$req_data['offset'] = ( ($current_page-1)  * $limit);
		$req_data['limit'] = $limit;

		if($this->input->get('search_keyword')){
			$req_data['search_type'] = 'total';
			$req_data['search_keyword'] = $this->input->get('search_keyword');
		}
		else{
			$req_data['search_type'] = null;
			$req_data['search_keyword'] = null;
		}


		$board_data = $this->m_bbs_notice->bbs_notice_list($req_data);
		$board_count = $this->m_bbs_notice->bbs_notice_list_total_count($req_data);


		$data = array(
			"board_data" => $board_data,
			"board_count" => $board_count,
			"current_page"=> $current_page,
			"search_keyword"=> $req_data['search_keyword'],
			"total_count"=> $board_count,
			"limit" => $limit
		);



		return $this->load->view('/m/board/notice',$data,true);
	}



	#---------------------------------------------------------------------------
	# PURPOSE : faq
	# CREATE  :	2018-05-04
	#---------------------------------------------------------------------------
	function fn_faq() {
		# Request
		$page               = trim($this->input->get_post('page', TRUE));
		$search_keyword     = trim($this->input->get_post('search_keyword', TRUE));
		$search_category1   = trim($this->input->get_post('search_category1', TRUE));
		$search_category2   = trim($this->input->get_post('search_category2', TRUE));

		# default setting
		if($page=='')           $page = '1';


		# 카테고리 가져오기
		$this->load->model('web/m_bbs_faq_category');
		$arr_category   = $this->m_bbs_faq_category->bbs_faq_category_all_list();
		$arr_category1 = array();   $arr_category2 = array();
		foreach ($arr_category as $val) {
			if($val['depth']=='1')  $arr_category1[] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'idx'=> $val['idx']);
			if($val['depth']=='2')  $arr_category2[$val['p_category_code']][$val['category_code']] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'idx'=> $val['idx'] , 'list'=>array());
		}
		if($search_category1 == '') $search_category1 = $arr_category1[0]['category_code'];


		# paging 설정
		$limit  = 20;
		$offset = ($page-1) * $limit;

		# faq
		$req_data = array();
		$req_data['search_type']        = 'title';
		$req_data['search_keyword']     = $search_keyword;
		$req_data['faq_category_code1'] = $search_category1;
		$req_data['faq_category_code2'] = $search_category2;
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;

		# model - list
		$this->load->model('web/m_bbs_faq');
		$result         = $this->m_bbs_faq->bbs_faq_list($req_data);
		$total_count    = $this->m_bbs_faq->bbs_faq_list_total_count($req_data);


		# page
		$max_page   = ceil($total_count / $limit);
		$pagination =  gf_paging($limit, $page, $max_page, '/web/cs/faq?search_keyword='.$search_keyword.'&search_category1='.$search_category1.'&search_category2='.$search_category2.'&page=');

		$data_num = ($total_count + $limit)-($page*$limit);


		foreach($result as $data){
			$arr_category2[$data['faq_category_code1']][$data['faq_category_code2']]['list'][] = $data;

		}


		# view data
		$return_data = array();
		$return_data['total_count']                 = $total_count;
		$return_data['search_keyword']              = $search_keyword;
		$return_data['search_category1']            = $search_category1;
		$return_data['search_category2']            = $search_category2;
		$return_data['list']                        = $result;
		$return_data['arr_category1']               = $arr_category1;
		$return_data['arr_category2']               = $arr_category2;
		$return_data['pagination']	                = $pagination;
		$return_data['data_num']	                = $data_num;

//		var_dump($arr_category2);


		# view
		return $this->load->view('/m/board/faq', $return_data, true);


	}


	#---------------------------------------------------------------------------
	# PURPOSE : faq 뷰
	# CREATE  :	2018-04-16
	#---------------------------------------------------------------------------
	function fn_faq_view() {
		# Request
		$idx                = $this->uri->segment(4);

		$_SESSION['read'][$idx] = true;
		$search_keyword     = trim($this->input->get_post('search_keyword', TRUE));
		$search_category1   = trim($this->input->get_post('search_category1', TRUE));
		$search_category2   = trim($this->input->get_post('search_category2', TRUE));
		$data_num           = trim($this->input->get_post('data_num', TRUE));

		# 카테고리 가져오기
		$this->load->model('web/m_bbs_faq_category');
		$arr_category   = $this->m_bbs_faq_category->bbs_faq_category_all_list();
		$arr_category1 = array();   $arr_category2 = array();
		foreach ($arr_category as $val) {
			if($val['depth']=='1')  $arr_category1[] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'idx'=> $val['idx']);
			if($val['depth']=='2')  $arr_category2[$val['p_category_code']][] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'idx'=> $val['idx']);
		}
		if($search_category1 == '') $search_category1 = $arr_category1[0]['category_code'];

		# model - list
		$this->load->model('web/m_bbs_faq');
		$result         = $this->m_bbs_faq->bbs_faq_select($idx);

		# faq
		$req_data = array();
		$req_data['search_type']        = 'title';
		$req_data['search_keyword']     = $search_keyword;
		$req_data['faq_category_code1'] = $search_category1;
		$req_data['faq_category_code2'] = $search_category2;
		$req_data['offset']             = 0;
		$req_data['limit']              = 4;
		$req_data['idx']                = $idx;

		$next_list      = $this->m_bbs_faq->bbs_faq_other_list($req_data);
		$total_count    = $this->m_bbs_faq->bbs_faq_list_total_count($req_data);


		# view data
		$return_data = array();
		$return_data['idx']                     = $idx;
		$return_data['result']                  = $result;
		$return_data['next_list']               = $next_list;
		$return_data['search_keyword']          = $search_keyword;
		$return_data['search_category1']        = $search_category1;
		$return_data['search_category2']        = $search_category2;
		$return_data['arr_category1']           = $arr_category1;
		$return_data['arr_category2']           = $arr_category2;
		$return_data['total_count']             = $total_count;
		$return_data['data_num']                = $data_num;


		# view
		return $this->load->view('/m/board/faq_view', $return_data, true);


	}



	function fn_ajax_new_notice() {
		# Request
		$this->load->model('/web/m_bbs_notice');
		echo ($this->m_bbs_notice->bbs_notice_new() > 0 ) ? true : false;
		exit();
	}


}
