<?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 mypage extends cm_Controller
{

	function fn_qna(){
		# Request
		$page                   = trim($this->input->get_post('page', TRUE));
		$search_type            = trim($this->input->get_post('search_type', TRUE));
		$search_keyword         = trim($this->input->get_post('search_keyword', TRUE));
		$SES_MEMBER_ID          = $this->session->userdata('C_Member_Id');
		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		$search_type = isset($search_keyword) ? "total" : "";
		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}


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

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

		# 1:1문의
		$req_data = array();
		$req_data['search_type']        = $search_type;
		$req_data['search_keyword']     = $search_keyword;
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;
		$req_data['member_id']          = $SES_MEMBER_ID;

		# model - list
		$this->load->model('web/m_bbs_question');
		$result         = $this->m_bbs_question->bbs_question_where_member_id_list($req_data);
		$total_count    = $this->m_bbs_question->bbs_question_where_member_id_list_total_count($req_data);

		# page
		$max_page   = ceil($total_count / $limit);
		$pagination =  gf_paging($limit, $page, $max_page, '/m/mypage/qna?search_type='.$search_type.'&search_keyword='.$search_keyword.'&page=');


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

		# view data
		$return_data = array();
		$return_data['list']            = $result;
		$return_data['total_count']     = $total_count;
		$return_data['data_num']        = $data_num;
		$return_data['pagination']      = $pagination;
		$return_data['search_type']     = $search_type;
		$return_data['search_keyword']  = $search_keyword;




		return $this->load->view("/m/mypage/qna",$return_data,true);


	}



	function fn_qna_arr_del_proc() {
		# request
		$arr_idx        = $this->input->get_post('is_del', TRUE);   // 삭제할 idx
		$SES_MEMBER_ID  = $this->session->userdata('C_Member_Id');

		if($SES_MEMBER_ID == '') {
			gf_alert("회원로그인 후에 가능합니다.", '/m/mypage/qna');
			exit;
		}

		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}

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

		$now_date   = date("Y-m-d H:i:s");
		foreach ($arr_idx as $key=>$val) {
			$req_data = array();
			$req_data['idx']        = $val;
			$req_data['write_id']   = $SES_MEMBER_ID;

			$count   = $this->m_bbs_question->bbs_question_write_id_idx_count($req_data);
			if($count > 0) {
				$arr_update_data1 = array();
				$arr_update_data1[$key]['idx']           = $val;
				$arr_update_data1[$key]['is_del']        = 'Y';
				$arr_update_data1[$key]['update_id']     = $SES_MEMBER_ID;
				$arr_update_data1[$key]['update_date']   = $now_date;
				$this->m_bbs_question->bbs_question_is_del_update_batch($arr_update_data1);

				$arr_update_data2 = array();
				$arr_update_data2[$key]['p_idx']         = $val;
				$arr_update_data2[$key]['is_del']        = 'Y';
				$arr_update_data2[$key]['update_id']     = $SES_MEMBER_ID;
				$arr_update_data2[$key]['update_date']   = $now_date;
				$this->m_bbs_question->bbs_question_p_idx_is_del_update_batch($arr_update_data2);

			}

		}


		gf_alert("삭제되었습니다.", '/m/mypage/qna');

	}
	function fn_qna_add() {

		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}



		# view
		return $this->load->view('/m/mypage/qna_add', '', true);

	}
	function fn_qna_add_proc() {
		# Request
		$title              = trim($this->input->get_post('title', TRUE));
		$content            = trim($this->input->get_post('content', TRUE));
		$SES_MEMBER_ID      = $this->session->userdata('C_Member_Id');
		$SES_MEMBER_NICNAME = $this->session->userdata('C_Member_Nicname');

		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}


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

		# max idx 가져오기
		$result         = $this->m_bbs_question->bbs_question_max_idx_select();

		$max_idx    = $result['max_idx']+1;
		$now_date   = date("Y-m-d H:i:s");

		$req_data = array();
		$req_data['title']      = $title;
		$req_data['content']    = $content;
		$req_data['idx']        = $max_idx;
		$req_data['p_idx']      = $max_idx;
		$req_data['s_idx']      = '1';
		$req_data['member_id']  = $SES_MEMBER_ID;
		$req_data['write_id']   = $SES_MEMBER_ID;
		$req_data['write_name'] = $SES_MEMBER_NICNAME;
		$req_data['reg_date']   = $now_date;
		$req_data['update_id']  = $SES_MEMBER_ID;
		$req_data['update_date']= $now_date;

		# insert
		$this->m_bbs_question->bbs_question_insert($req_data);

		gf_alert("등록되었습니다.", '/m/mypage/qna');

	}

	function fn_bookmark() {

		# Request
		$search_keyword_txt         = trim($this->input->get('search_keyword', TRUE));
		$page = trim($this->input->get('page', TRUE));

		if($page=='')           $page = '1';
		$limit  = 20;
		$offset = ($page-1) * $limit;



		$SES_MEMBER_ID          = $this->session->userdata('C_Member_Id');
		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}

		# 북마크
		$req_data = array();
		$req_data['search_type']        = 'total';
		$req_data['search_keyword']     = isset($search_keyword_txt) ?$search_keyword_txt :"";
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;
		$req_data['member_id']          = $SES_MEMBER_ID;

		# model - list
		$this->load->model('web/m_bookmark');

		$total_count    = $this->m_bookmark->bookmark_join_vw_content_list_total_count($req_data);

		# paging 설정
		$offset = ($page-1) * $limit;
		$max_page   = ceil($total_count / $limit);
		$result         = $this->m_bookmark->bookmark_join_vw_content_list($req_data);

		$pagination =  gf_paging($limit, $page, $max_page, '/m/mypage/bookmark?search_keyword='.$search_keyword_txt.'&page=');

		# view data
		$return_data = array();
		$return_data['list']            = $result;
		$return_data['limit']           = $limit;
		$return_data['total_count']     = $total_count;
		$return_data['search_keyword']  = $search_keyword_txt;
		$return_data['pagination'] = $pagination;

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

	}

	function fn_delivery(){

		$SES_MEMBER_ID          = $this->session->userdata('C_Member_Id');
		$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}

		return $this->load->view('/m/mypage/delivery',null,true);
	}

	#---------------------------------------------------------------------------
	# PURPOSE :	delivery list
	# CREATE  :	2018-05-07
	#---------------------------------------------------------------------------
	function fn_ajax_delivery_list() {
		# request

		$page           = trim($this->input->get_post('page', TRUE));
		$content_type   = trim($this->input->get_post('content_type', TRUE));

		$SES_MEMBER_ID  = $this->session->userdata('C_Member_Id');


		$limit  = 15;
		$offset = ($page-1) * $limit;


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



		# 채널
		$req_data = array();
		$req_data['content_type']       = $content_type;
		$req_data['offset']             = $offset < 0 ? 0 : $offset;

		$req_data['limit']              = $limit;
		$req_data['member_id']          = $SES_MEMBER_ID;

		if($content_type == '2') {
			# model
			$list           = $this->m_delivery->delivery_join_partner_list($req_data);
			$total_count    = $this->m_delivery->delivery_join_partner_list_total_count($req_data);

		} else if($content_type == '3') {
			# model
			$list           = $this->m_delivery->delivery_join_channel_list($req_data);
			$total_count    = $this->m_delivery->delivery_join_channel_list_total_count($req_data);
		} else if($content_type == '9') {
			# model
			$list           = $this->m_delivery->delivery_join_tag_list($req_data);
			$total_count    = $this->m_delivery->delivery_join_tag_list_total_count($req_data);
		}



		$more_list = 'N';
		if(($total_count-($page*$limit)) > 0){
			$more_list = 'Y';
		}


		$return_data['result_code']         = '0000';
		$return_data['total_count']         = $total_count;
		$return_data['list']                = $list;
		$return_data['more_list']           = $more_list;


		$this->load->view("/m/mypage/delivery_list", $return_data);

	}

	function fn_req_password() {
		return $this->load->view("/m/mypage/req_password",null,true);

	}

	function fn_member_edit() {

		$this->load->helper('cookie');
		$crossmap_passwd_check 		= get_cookie('crossmap_passwd_check');
		if($crossmap_passwd_check != 'Y') {
			gf_alert("잘못된 접근입니다.", '/m/mypage/req_password');
			exit;
		}
//		set_cookie('crossmap_passwd_check', 'N', 0);

		$SES_MEMBER_ID              = $this->session->userdata('C_Member_Id');
		$SES_MEMBER_GROUP_IDX       = $this->session->userdata('C_Member_Group');
		$SES_MEMBER_GROUP_Name      = $this->session->userdata('C_Member_Group_Name');
		if($SES_MEMBER_GROUP_IDX == '') {
			$SES_MEMBER_GROUP_IDX   ='8';
			$SES_MEMBER_GROUP_Name  = '비회원';
		}

		# 해당회원이 사용가능한지 확인
		$req_data = array();
		$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
		$req_data['menu_code']              = '3';

		# model
		$this->load->model('web/m_member_group_menu_auth');
		$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
		if($count > 0) {
			gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
			exit;
		}


		# model - list
		$this->load->model('web/m_member');
		$result         = $this->m_member->member_where_member_id_select($SES_MEMBER_ID);

		if(count($result) == 0){
			gf_alert("잘못된 접근입니다.2", '/m');
			exit;
		}

		# view data
		$return_data = array();
		$return_data['result']                      = $result;


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

	}


	function fn_user_comment(){

		if(!isset($_GET['id'])){
			$SES_MEMBER_ID          = $this->session->userdata('C_Member_Id');
			$SES_MEMBER_GROUP_IDX   = $this->session->userdata('C_Member_Group');
			$SES_MEMBER_GROUP_Name  = $this->session->userdata('C_Member_Group_Name');
			if($SES_MEMBER_GROUP_IDX == '') {
				$SES_MEMBER_GROUP_IDX   ='8';
				$SES_MEMBER_GROUP_Name  = '비회원';
			}

			# 해당회원이 사용가능한지 확인
			$req_data = array();
			$req_data['member_group_idx']       = $SES_MEMBER_GROUP_IDX;
			$req_data['menu_code']              = '3';

			# model
			$this->load->model('web/m_member_group_menu_auth');
			$count = $this->m_member_group_menu_auth->member_group_menu_auth_count($req_data);
			if($count > 0) {
				gf_alert($SES_MEMBER_GROUP_Name."은 해당 메뉴를 사용하실수 없습니다.", 'back');
				exit;
			}
		}



		return $this->load->view('/m/mypage/user_comment_inc',null, true);
	}

	#---------------------------------------------------------------------------
	# PURPOSE :	user_comment list
	# CREATE  :	2018-05-08
	#---------------------------------------------------------------------------
	function fn_ajax_user_comment_list() {
		# request
		$page           = trim($this->input->get_post('page', TRUE));
		$content_type   = trim($this->input->get_post('content_type', TRUE));
		$write_id       = trim($this->input->get_post('write_id', TRUE));
		$sort           = trim($this->input->get_post('sort', TRUE));

		switch ($sort) {
			case '4': $sort_column = 'reply_count'; $sort_type = 'desc'; break;
			case '3': $sort_column = 'reg_date';    $sort_type = 'asc'; break;
			case '2': $sort_column = 'reg_date';    $sort_type = 'desc'; break;
			case '1': $sort_column = 'like_count';  $sort_type = 'desc'; break;
			default : $sort_column = 'reg_date';    $sort_type = 'desc'; break;
		}

		$this->load->helper('cookie');
		$comment_sns_id 		= get_cookie('crossmap_comment_sns_id');


		$limit  = 15;
		$offset = ($page-1) * $limit;

		$is_comment_view = 'N';
		if($comment_sns_id != '') {
			if($content_type =='3') {
				if($write_id==$comment_sns_id)   $is_comment_view = 'Y';
			} else {
				$is_comment_view = 'Y';
			}
		}

		$total_count = 0;
		$list = array();
		if($is_comment_view == 'Y') {
			# 댓글
			$this->load->model('web/m_comment');

			$req_data = array();
			$req_data['content_type']   = $content_type;
			$req_data['write_id']       = $write_id;

			# 총댓글수 + 답글
			$total_count    = $this->m_comment->comment_join_vw_content_where_write_id_list_count($req_data);

			# 댓글 리스트
			$req_data = array();
			$req_data['content_type']   = $content_type;
			$req_data['offset']         = $offset;
			$req_data['limit']          = $limit;
			$req_data['sort_column']    = $sort_column;
			$req_data['sort_type']      = $sort_type;
			$req_data['write_id']       = $write_id;

			# list
			$list         = $this->m_comment->comment_join_vw_content_where_write_id_list($req_data);
		}



		$more_list = 'N';
		if(($total_count-($page*$limit)) > 0){
			$more_list = 'Y';
		}

		$return_data = array();
		$return_data['result_code']         = '0000';
		$return_data['total_count']         = $total_count;
		$return_data['list']                = $list;
		$return_data['more_list']           = $more_list;


		# view
		return $this->load->view("/m/mypage/user_comment_list", $return_data);

	}





	#---------------------------------------------------------------------------
	# PURPOSE :	댓글 삭제
	# CREATE  :	2018-05-07
	#---------------------------------------------------------------------------
	function fn_ajax_user_comment_del_proc() {
		# request
		$idx            = trim($this->input->get_post('idx', TRUE));

		$this->load->helper('cookie');
		$comment_sns_id 		= get_cookie('crossmap_comment_sns_id');
		if($comment_sns_id == '')    throw new Cm_Exception('json', '9999', '회원로그인 후에 가능합니다.');

		# model
		$this->load->model('web/m_comment');
		$this->load->model('web/m_comment_setting');


		$req_data = array();
		$req_data['idx']        = $idx;
		$req_data['write_id']   = $comment_sns_id;
		$arr_comment   = $this->m_comment->comment_member_id_idx_select($req_data);

		if(count($arr_comment) == 0) {
			throw new Cm_Exception('json', '9999', '삭제할수 없는 댓글입니다.');
		} else {

			$content_type   = $arr_comment['content_type'];
			$comment_org    = $arr_comment['comment'];

			$arr_setting   = $this->m_comment_setting->comment_setting_where_content_idx_select($content_type);

			$comment = '작성자에 의해 삭제된 댓글입니다.';
			if(isset($arr_setting['self_del_contnet'])==true) {
				$comment = $arr_setting['self_del_contnet'];
			}

			$req_data = array();
			$req_data['idx']            = $idx;
			$req_data['comment']        = $comment;
			$req_data['comment_org']    = $comment_org;
			$req_data['del_date']       = date("Y-m-d H:i:s");
			$req_data['is_del']         = 'Y';
			$req_data['comment_status'] = '4';

			$result = $this->m_comment->comment_is_del_update($req_data);
		}

		if($result) {
			$return_data['result'] = array('code' => '0000');
		} else {
			$return_data['result'] = array('code' => '9999');
		}

		# view
		return $this->load->view('/web/comm/result_json', $return_data);

	}


	function fn_ajax_comment_reply_list() {
		# request
		$p_idx                  = trim($this->input->get_post('comment_idx', TRUE));
		$page                   = trim($this->input->get_post('page', TRUE));
		$sort                   = trim($this->input->get_post('sort', TRUE));
		$type                   = trim($this->input->get_post('type', TRUE));

		switch ($sort) {
			case '4': $sort_column = 'like_count'; $sort_type = 'desc'; break;
			case '3': $sort_column = 'reg_date';    $sort_type = 'asc'; break;
			case '2': $sort_column = 'reg_date';    $sort_type = 'desc'; break;
			case '1': $sort_column = 'like_count';  $sort_type = 'desc'; break;
			default : $sort_column = 'reg_date';    $sort_type = 'desc'; break;
		}

		# paging 설정
		if($page == '') $page = 1;
		$limit  = 10;
		if($type == 'user_comment') $limit  = 50;
		$offset = ($page-1) * $limit;

		# 검색
		$req_data = array();
		$req_data['p_idx']              = $p_idx;
		$req_data['sort_column']        = $sort_column;
		$req_data['sort_type']          = $sort_type;
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;

		# model - list
		$this->load->model('web/m_comment');

		# 총댓글수
		$total_count    = $this->m_comment->comment_where_p_idx_count($p_idx);
		$arr_comment    = $this->m_comment->comment_child_list($req_data);

		$more_list = 'N';
		if(($total_count-($page*$limit)) > 0){
			$more_list = 'Y';
		}

		# view data
		$return_data = array();
		$return_data['result_code']         = '0000';
		$return_data['total_count']         = $total_count;
		$return_data['arr_comment']         = $arr_comment;
		$return_data['more_list']           = $more_list;

		# view
		if($type == 'user_comment') {
			return $this->load->view('/m/mypage/user_comment_reply_list', $return_data);
		} else {
			return $this->load->view('/m/mypage/comment_reply_list', $return_data);
		}



	}
}
