<?
# session
$SES_MEMBER_ID              = $this->session->userdata('C_Member_Id');
$SES_MEMBER_NICNAME         = $this->session->userdata('C_Member_Nicname');

$controller = $this->uri->segment(2);

$ci=& get_instance();

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

# 채널
$req_data = array();
if(get_cookie("pre_page") != '' && get_cookie("pre_page") != null){
	$req_data['content_type']       =  get_cookie("pre_page");
}
else{
	$req_data['content_type']       = '3';
}
$c_type =$req_data['content_type'];


$req_data['offset']             = 0;
$req_data['limit']              = 10;
$limit = $req_data['limit'];
$req_data['member_id']          = $SES_MEMBER_ID;

# model



?>
<script>
	function setCookie(cname, cvalue, exdays) {
		var d = new Date();
		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
		var expires = "expires="+d.toUTCString();
		document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
	}

	function getCookie(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1);
			}
			if (c.indexOf(name) == 0) {
				return c.substring(name.length, c.length);
			}
		}
		return "";
	}

	function checkCookie() {
		var user = getCookie("username");
		if (user != "") {
			alert("Welcome again " + user);
		} else {
			user = prompt("Please enter your name:", "");
			if (user != "" && user != null) {
				setCookie("username", user, 365);
			}
		}
	}

</script>


<div class="mypage_top">
	<a href="/m/mypage/qna_add" class="pens"><img src="/img/mobile/penicon.png"></a>
	<div class="mypage_logo"><a href="/m"><img src="/img/mobile/logo_cm.jpg" alt="logo"></a>
	</div>
	<div class="mypage_title"><a href="/m/mypage/req_password">마이페이지</a>
	</div>
	<div class="mypage_menu"><a href=""><img src="/img/mobile/menu_list.gif" alt="menu"></a>
	</div>
</div>
<div class="mypage_header">
	<h3 class="p_title3 fl"><strong><?=$SES_MEMBER_NICNAME?></strong> 님의 배달함</h3>
	<span class="btn-info fr"><a  href="javascript:change_help()"><img src="/img/mobile/btn-info.png"></a></span>
</div>
<div class="mypage_submenu">
		<span class="fl submenu_cate">
			 <a onclick="javascript:change_delivery_type('3')" href="#3"><span class="<?=$req_data['content_type'] == 3 ? "current": "" ?>" id="delivery_menu3">채널</span></a>  |
			 <a onclick="javascript:change_delivery_type('2')" href="#2"><span class="<?=$req_data['content_type'] == 2 ? "current": "" ?>" id="delivery_menu2">칼럼</span></a>  |
			 <a onclick="javascript:change_delivery_type('9')" href="#9"><span class="<?=$req_data['content_type'] == 9 ? "current": "" ?>" id="delivery_menu9">태그</span></a>
		</span>
	<span class="fr"><a href="javascript:delivery_arr_del()"><img src="/img/mobile/img_trash.gif"></a></span>
</div>
<div class="mypage_cont">
	<div class="popupbody3">
		<div class="pop_tit">
			<span id="delivery_text">배달받는 채널 수</span> <span class="beachblue" id="delivery_count"><?//number_format($total_count)?></span>
		</div>
		<div id="delivery_list">
		</div>
	</div>

		<div class="delivery_info3" id="delivery_help" style="z-index:9999;width:200px;display:none">
			<p class="d_info_tit" style="text-align:center; margin:10px 0">배달함 도움말</p>
			<p class="d_info_txt" style="margin-bottom: 15px;display: inline-block;"> 채널 배달받기를 하고 있는 나만의배달리스트를 관리 할수 있습니다. </p>
		</div>


</div>




<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
	// 스크롤 고정 함수.
	function scroll_fix3(news_nav) {

		$( document ).ready( function() {

			var jbOffset = $( '.'+news_nav ).offset(); // 고정하고자 하는 클래스의 offset.

			$( window ).scroll( function() {
				/*스크롤탑(현재스크롤위치) > news_nav : 상단 아래로 내렸을때 상단 고정*/
				if ( $( document ).scrollTop() > jbOffset.top ) {
					$( '.'+news_nav ).addClass( 'jbFixed2' );
					$( '.'+news_nav ).css('position','fixed');
				}
				/*스크롤탑(현재스크롤위치) <= news_nav : 원래*/
				else {
					$( '.'+news_nav ).removeClass( 'jbFixed2' );
					$( '.'+news_nav ).css('position','relative');
				}

				console.log($( document ).scrollTop());
			});


			$(".pop_cont_info .fr").click(function(e){
				e.preventDefault();
				$(this).next().show();

			});
		} );
	}
	//

	scroll_fix3('mypage_header'); // 상단 고정하고 싶은 클래스 설정.
</script>





<script>
	window.onpageshow = function (event) {
		if (event.persisted) {
			console.log('BFCahe로부터 복원됨');
		}
		else {
			console.log('새로 열린 페이지');
		}
	};


	// 레이어 이외의 공간 클릭시 close
	$(document).mouseup(function (e){
		var container = $("#delivery_help");
		if( container.has(e.target).length === 0)   container.hide();
		container = $(".info_popup");
		if( container.has(e.target).length === 0)   container.hide();


	});

	$(".mask").on('click', function(event){
		$(event.target).parent().hide();

	});

	var help_type = 1;
	function change_help(){

		if(help_type ==1) {
			$("#delivery_help").css("display","block");
			help_type = 2;
		} else {
			$("#delivery_help").css("display","none");
			help_type = 1;
		}


	}


	var delivery_content_type = '<?=$c_type?>';


	function change_delivery_type(content_type) {
		$('#delivery_menu3').removeClass('current');
		$('#delivery_menu2').removeClass('current');
		$('#delivery_menu9').removeClass('current');
		$('#delivery_menu'+content_type).addClass('current');

		delivery_page = 1;
		delivery_content_type = content_type;
		delivery_list(content_type);

		var text = '배달받는 채널 수';
		if(content_type == '2') {
			text = '배달받는 칼럼 수';
		} else if(content_type == '3') {
			text = '배달받는 채널 수';
		} else if(content_type == '9') {
			text = '배달받는 태그 수';
		}
		$("#delivery_text").html(text);

	}


	var delivery_page = 1;


	function delivery_list(content_type){
		if(content_type == '') content_type = delivery_content_type;

		console.log(content_type);
		var form_data = {
			"page"          : delivery_page,
			"content_type"  : content_type,
			"limit" : <?=$limit?>
		};
		$.ajax({
			url: '/m/mypage/delivery_list',
			async: false,
			type: 'POST',
			dataType: 'HTML',
			data: form_data,
			success: function (rtnHtml) {
				if(rtnHtml=='') return false;
				var arrHtml = rtnHtml.split("|||||");
				if(arrHtml[0].trim()=='0000') {
					if(delivery_page == 1) {
						$("#delivery_list").html(arrHtml[1]);
					} else {
						$("#delivery_list").append(arrHtml[1]);
					}
					var total_count = setComma(arrHtml[2]);
					$("#delivery_count").html(total_count);

					if (arrHtml[3] == "N") {
						$("#delivery_more_btn").hide();
					} else {
						$("#delivery_more_btn").show();
					}
					delivery_page++;

				} else {
					alert(arrHtml[1]);
					return false;
				}
			},
			error: function (rtnHtml) {
				alert('데이터 가져오기에 실패했습니다.');
				return false;
			}
		});
	}

	function delivery_del(idx){
		var form_data = {
			"idx"			: idx
		};
		$.ajax({
			url: '/web/comm/delivery_del_proc',
			async: false,
			type: 'POST',
			dataType: 'JSON',
			data: form_data,
			success: function (rtnJson) {
				if (rtnJson.code == "0000") {
					delivery_page = 1;
					delivery_list(delivery_content_type);
				} else {
					alert(rtnJson.msg);
					return false;
				}
			},
			error: function (rtnJson) {
				alert('삭제에 실패했습니다.');
				return false;
			}
		});

	}



	function delivery_arr_del(){
		if(confirm("배달함이 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
			var checkbox_del = [];
			$("input[name='is_del[]']").each(function(idx){
				checkbox_del.push($(this).val());
			});
			var form_data = {
				"idxs"			: checkbox_del
			};
			$.ajax({
				url: '/web/comm/delivery_arr_del_proc',
				async: false,
				type: 'POST',
				dataType: 'JSON',
				data: form_data,
				success: function (rtnJson) {
					if (rtnJson.code == "0000") {
						delivery_page = 1;
						delivery_list(delivery_content_type);
					} else {
						alert(rtnJson.msg);
						return false;
					}
				},
				error: function (rtnJson) {
					alert('삭제에 실패했습니다.');
					return false;
				}
			});



		} else {   //취소
			return false;
		}


	}



	function copy_delivery_url(idx){
		$('#copy_delivery_url'+idx).select();
		try {
			document.execCommand('copy');
			alert("주소가 복사되었습니다.")
		} catch (err) {
			alert('이 브라우저는 지원하지 않습니다.')
		}
	}


	$(document).ready(function(){
		delivery_list(delivery_content_type);
	});


</script>
