<?
# 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();
$req_data['content_type']       = '3';
$req_data['offset']             = 0;
$req_data['limit']              = 15;
$req_data['member_id']          = $SES_MEMBER_ID;

# model
$list           = $ci->m_delivery->delivery_join_channel_list($req_data);
$total_count    = $ci->m_delivery->delivery_join_channel_list_total_count($req_data);

?>

<div id="delivery_layer" style="display:none;">
    <div class="popup_wrap2_d" style="z-index:9998;">
		<div class="popupbox2" style="width:810px;">
			<div class="popuptop3_d">
				<h3 class="p_title2"><strong><?=$SES_MEMBER_NICNAME?></strong> 님의 배달함 <a href="javascript:change_delivery_type('3')"><span style="padding-left:40px;" class="current" id="delivery_menu3">채널</span></a>  | <a href="javascript:change_delivery_type('2')"><span id="delivery_menu2">칼럼</span></a>  | <a href="javascript:change_delivery_type('9')"><span id="delivery_menu9">태그</span></a></h3>
				<span class="btn-info"><a href="javascript:change_help()"><img src="/img/web/btn-info.png"></a></span>
				<span class="btn-close"><a href="javascript:change_toggle('delivery_layer')"><img src="/img/web/btn-close2.png"></a></span>
			</div>
		</div>

		<div class="popupbody3_d">
            <div class="pop_tit">
				<span id="delivery_text">배달받는 채널 수</span> <span class="beachblue" id="delivery_count"><?=number_format($total_count)?></span>
				<span class="fr pop_box_d"><a href="javascript:delivery_arr_del()">전체삭제</a></span>
			</div>
            <div id="delivery_list">
        <?
            foreach ($list as $val) {
                $idx                    = $val['idx'];
                $article_count      	= $val['article_count'];
                $delivery_count         = $val['delivery_count'];
                $reg_date               = $val['reg_date'];
                $main_idx               = $val['main_idx'];
                $content_type         	= $val['content_type'];

                $now_date               = date("Y-m-d").' 00:00:00';


                $delivery_title         = $val['channel_name'];
                $main_img           	= $val['channel_icon'];
                $req_data = array();
                $req_data['channel_idx']    = $val['main_idx'];
                $delivery_row    = $ci->m_life->life_where_channel_idx_list_limit1($req_data);
                $display_date           = $delivery_row['display_date'];
                $delivery_row_display_date_txt  = substr($display_date, 0, 16);

                $article_view_url = '/web/life/life_view/'.$delivery_row['idx'];
                $view_url  = '/web/life/channel/'.$main_idx;

                $req_data = array();
                $req_data['channel_idx']    = $val['main_idx'];
                $req_data['now_date']    = $now_date;

                $delivery_today_count       = $ci->m_life->life_where_display_date_channel_idx_count($req_data);


        ?>
                <div class="pop_cont_box_d">
                    <input type="text" name="copy_delivery_url<?=$idx?>" id="copy_delivery_url<?=$idx?>" style="position:absolute;top:-9999em;" value="<?=WWW_CROSSMAP?><?=$view_url?>">
                    <input type="hidden" name="is_del[]" value="<?=$idx?>">
                    <div class="pop_cont_title_d">
                        <a href="<?=$article_view_url?>"><?=$delivery_row['title']?></a>
                        <p class="popup_date_d"><?=$delivery_row_display_date_txt?></p>
                    </div>
                    <div class="pop_cont_txt">
                        <span class="pop_cont_img_d" style="padding-top: 10px">
                            <div onclick="location.href='<?=$view_url?>'" style="border-radius:50px;cursor:pointer;width:50px;height:50px;background:url('<?=$main_img?>') no-repeat center; background-size:cover;"></div>
                        </span>
                        <div class="pop_cont_txt_box_d">
                            <p class="t_black"><a href="<?=$view_url?>"><?=$delivery_title?></a> <? if($delivery_today_count>0) {?><span class="pop_cont_like_d"><?=number_format($delivery_today_count)?>+</span><? }?></p>
                            <p><span class="beachblue"><?=number_format($article_count)?></span>개의 글이 있습니다.   <span style="padding:0 10px;">|</span>  <span class="beachblue"><?=number_format($delivery_count)?></span>명에게 배달 중</p>
                        </div>
                        <div class="pop_cont_info_d">
                            <span class="fr"><a href="javascript:change_toggle('delivery_info<?=$idx?>')"><img src="/img/web/pop_info.png"></a></span>
                            <ul class="info_popup_d" id="delivery_info<?=$idx?>" style="display:none">
                                <li><a href="javascript:delivery_del('<?=$idx?>');">삭제하기</a></li>
                                <li style="border-top:1px solid #ededed"><a href="javascript:copy_delivery_url('<?=$idx?>');">링크복사</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
        <?  }?>
            </div>

        <?  $more_btn_style = 'style="display:block"';
            if($total_count <= count($list)) {
                $more_btn_style = 'style="display:none"';
                echo '<div style="height:50px"></div>';
            }
        ?>
            <div class="btn btn-default btn_life" id="delivery_more_btn" <?=$more_btn_style?>>
                <a href="javascript:delivery_list('');">더보기 </a><img src="/img/web/img_replyopen.gif">
            </div>
        </div>
	</div>
    <div class="mask" style="z-index: 9997"></div>
</div>


<div class="delivery_info3" id="delivery_help" style="z-index:9999;top:195px;left:895px;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>




<script>
// 레이어 이외의 공간 클릭시 close
$(document).mouseup(function (e){
    var container = $("#delivery_help");
    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 = '3';
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 = 2;
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
	};
	$.ajax({
        url: '/web/comm/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('이 브라우저는 지원하지 않습니다.')
    }
}



</script>
