<?php

$arr_category_add1 = array();
$arr_category_add2 = array();
$arr_category_add3 = array();
foreach ($arr_category as $key=>$val) {
	if($val['depth'] == '1') {
		$arr_category_add1[] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'p_category_code'=> $val['p_category_code']);
	} else if($val['depth'] == '2') {
		$arr_category_add2[] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'p_category_code'=> $val['p_category_code']);
	} else if($val['depth'] == '3') {
		$arr_category_add3[] = array('category_code'=>$val['category_code'], 'category_name'=>$val['category_name'], 'p_category_code'=> $val['p_category_code']);
	}
}

$menu_code = '22';
require_once (APPPATH.'views/skin/admin/_content_comm.php');
?>



<form name="input_form" id="input_form" method="post" action="/admin/news/news_section_list/2">
<input type="hidden" id="p_category_code" name="p_category_code">
<input type="hidden" id="category_idx" name="category_idx">
<input type="hidden" id="del_key" name="del_key">
<input type="hidden" id="depth" name="depth">
<input type="hidden" id="mode" name="mode">
<input type="hidden" id="org_category_code" name="org_category_code">
<input type="hidden" id="search_type" name="search_type" value="category_name">


<div class="common_tep clearfix type01">
	<div class="searh_area">
		<input type="text" name="search_keyword" id="search_keyword" value="<?=$search_keyword?>" placeholder="뉴스섹션" onkeypress="if( event.keyCode==13 ){check_search();}">
		<a href="javascript:check_search();" class="btn"><i class="fa moving_all fa-search" style="margin-left:8px;"></i></a>
	</div>
</div>

<div class="common_table"  style="margin-top: 30px">
<?
foreach ($arr_depth_count as $val) {
?>
	<span style="margin-right:10px;"><span style="font-weight:bold"><?=$val['depth']?>차: </span> <?=number_format($val['cnt']);?></span>

<?
}
?>

</div>


<div class="common_table">
	<span style="margin-right:10px;"><span style="font-weight:bold">Total:</span> <?=number_format($total_count);?></span>
	<span style="margin-right:10px;"><span style="font-weight:bold">전체글수:</span> <?=number_format($article_count);?></span>
	<span style="margin-right:10px;"><span style="font-weight:bold" class="text_blue">오늘글수:</span> <?=number_format($today_article_count);?></span>
</div>



	

<!-- 테이블 시작-->
<div class="common_table">
	<table>
		<thead>
			<th>번호</th>
			<th>뉴스섹션</th>
			<th>전체글수</th>
			<th>오늘의글수</th>
			<th>출력순위</th>
			<th>생성일자</th>
			<th>관리</th>
		</thead>
		<tbody>
<?
	if(count($list)==0) {
?>
		<tr>
			<td colspan="7">등록된 내용이 없습니다.</td>
		</tr>
<?
	} else {

		$ci=& get_instance();
		$ci->load->model('admin/news/m_news');

	    $dataNumber = ($total_count + $limit)-($page*$limit);
		foreach($list as $val) {
            $idx            = $val['idx'];
            $category_code 	= $val['category_code'];
			$category_name 	= $val['category_name'];
			$p_category_code= $val['p_category_code'];
            $display_sort	= $val['display_sort'];
            $total_count 	= $val['total_count'];
            $today_count 	= $val['today_count'];
            $depth 			= $val['depth'];
            $reg_date 	    = $val['reg_date'];

			$reg_date 		= substr($reg_date, 0, 10);

			$category_code1 = substr($category_code, 0, 2);
			$category_code2 = substr($category_code, 0, 4);
			$category_code3 = substr($category_code, 0, 6);

			$del_key = $category_code;
            $reply_icon = '';
            if($depth != 1) {
            	for($i=1;$i<=$depth;$i++) {
            		$reply_icon.= '&nbsp;&nbsp;&nbsp;&nbsp;';
				}
                $reply_icon.='<img style="vertical-align:middle;margin-top:-3px;" src="/img/admin/board/icon_reply.gif" border="0"> ';
            }

            # 전체 글수
			$req_data = array();
			$req_data['category_code']      = ${"category_code".$depth};
			$total_count	= $ci->m_news->news_section_code_count($req_data);


			# 오늘글
			$req_data = array();
			$req_data['category_code']      = ${"category_code".$depth};
			$req_data['display_date']      	= date("Y-m-d").' 00:00:00';
			$today_count	= $ci->m_news->news_section_code_count($req_data);


			

?>

				<tr>
					<td><?=$dataNumber?></td>
                    <td style="text-align: left">
                        <input type="hidden" name="category_name_<?=$idx?>" id="category_name_<?=$idx?>" value="<?=$category_name?>">
						<input type="hidden" name="display_sort_<?=$idx?>" id="display_sort_<?=$idx?>" value="<?=$display_sort?>">
                        <a href="/admin/news/news_list/2?search_category_code1=<?=$category_code1?>&search_category_code2=<?=$category_code2?>&search_category_code3=<?=$category_code3?>"><?=$reply_icon?><?=$category_name?></a>
                        <?  if($depth != 3) {?>
                       <button type="button" class="btn_s_blue" style="width:110px" onclick="category_add('<?=$depth?>', '<?=$category_code?>');" style="cursor: pointer;"><?=$depth+1?>차 섹션등록</button>
                        <?  }?>
                    </td>
					<td><?=$total_count?><button type="button" class="btn_s_blue" style="width:40px" onclick="category_article_move('<?=$category_code?>')" style="cursor: pointer;">이동</button></td>
					<td><?=$today_count?></td>
					<td><?=$display_sort?></td>
                    <td><?=$reg_date?></td>
					<td class="tool_btn">
						<a href="javascript:category_edit('<?=$idx?>')"><i class="fa fa-pencil"></i>수정</a>
						<a href="javascript:category_del_proc('<?=$depth?>', '<?=$del_key?>', '<?=$category_code?>')"><i class="fa fa-close"></i>삭제</a>
					</td>
				</tr>

<?
		    $dataNumber--;
		}
	}
?>
	</tbody>
</table>
<div class="write_btn">
	<a href="javascript:category_add('0', '');">1차 섹션등록</a>
</div>

<div class="pasing_wrap">
	<?=$pagination?>
</div>



<div id="category_reg_layer" style="display:none">
<div class="black_board" style="display:block"></div>
<div class="pop_con common_table moving_all" style="display:block;top:100px;">
   <a href="javascript:commonLayerClose('category_reg_layer')" class="pop_close">x</a>
	<h2>섹션등록</h2>
	<table>
		<tr>
			<th>섹션명</th>
			<td><input type="text" name="category_name" id="category_name"></td>
		</tr>
		<tr>
			<th>노출순서</th>
			<td><input type="text" name="display_sort" id="display_sort"></td>
		</tr>
	</table>
	<div class="write_btn tc">
		<a href="javascript:category_reg_proc('add')" id="btn_category_add">신규등록</a>
		<a href="javascript:category_reg_proc('edit')" id="bnt_category_edit">수정</a>
		<a href="javascript:commonLayerClose('category_reg_layer')">취소하기</a>
	</div>
</div>
</div>


<div id="reg_layer" style="display:none">
<div class="black_board" style="display:block"></div>
<div class="pop_con common_table moving_all" style="display:block;top:100px;">
   <a href="javascript:commonLayerClose('reg_layer')" class="pop_close">x</a>
	<h2>섹션이동</h2>
	<table>
		<tr>
			<th>이동할 섹션</th>
			<td class="tl">
			<? 	if(count($arr_category_add1)>0){?>
					<select name="category1" id="category1" onchange="change_category(1, this.value)" class="type01">
						<option value="">선택</option>
			<? 		foreach ($arr_category_add1 as $val) {?>
						<option value="<?=$val['category_code']?>"><?=$val['category_name']?></option>
			<?		}?>

					</select>

			<?	}?>
					<select name="category2" id="category2" onchange="change_category(2, this.value)" class="type01">
						<option value="">선택</option>
					</select>
					<select name="category3" id="category3" class="type01">
						<option value="">선택</option>
					</select>
			</td>
		</tr>

	</table>
	<div class="write_btn tc">
		<a href="javascript:category_article_move_proc('add')"">이동</a>
		<a href="javascript:commonLayerClose('reg_layer')">취소하기</a>
	</div>
</div>
</div>




</form>



<script type="text/javascript">
function check_search(){
    if($("#search_type").val()=="" && $("#search_keyword").val()!=""){
        alert("검색할 항목을 선택해주세요.");
        return false;
    }
    $("#input_form").submit();
}


// 등록 - layer 열기
function category_add(depth, p_category_code) {
    $("#bnt_category_edit").hide();
    $("#btn_category_add").show();

	$("#depth").val('');
	$("#category_idx").val('');
    $("#p_category_code").val('');
    $("#category_name").val('');

	$("#depth").val(depth);
    if(p_category_code != '') {
        $("#p_category_code").val(p_category_code);
    }
    commonLayerOpen('category_reg_layer');
}

// 등록 - layer열기
function category_edit(idx){
	$("#bnt_category_edit").show();
    $("#btn_category_add").hide();

	$("#depth").val('');
	$("#category_idx").val('');
    $("#p_category_code").val('');
    $("#category_name").val('');

	$("#category_idx").val(idx);
	$("#category_name").val($("#category_name_"+idx).val());

	commonLayerOpen('category_reg_layer');
}


// 카테고리 저장
function category_reg_proc(mode){
	$("#mode").val(mode);
	if($("#category_name").val()=='') {
		alert("카테고리명을 입력해주세요.");
		return false;
	}

	$("#input_form").attr("action", "/admin/news/news_section_reg_proc");
	$("#input_form").submit();

}

// 삭제
function category_del_proc(depth, del_key, category_code) {
	var form_data = {
		"depth"					: depth,
		"category_code"		    : category_code
    };
	$.ajax({
        url: '/admin/news/news_section_code_count',
        sync: false,
        type: 'POST',
        dataType: 'html',
        data: form_data,
        success: function (rtnHtml) {
        	if(rtnHtml == 0) {
        		if(depth == '3') {
					var msg = '카테고리가 삭제됩니다. 삭제하시겠습니까?';
				} else {
					var msg = '하위카테고리까지 모두 삭제됩니다. 삭제하시겠습니까?';
				}
				if(confirm(msg) == true) {    //확인
					$("#del_key").val(del_key);
					$("#input_form").attr("action", "/admin/news/news_section_del_proc");
					$("#input_form").submit();
				} else {   //취소
					return;
				}
			} else if(rtnHtml > 0) {
				alert("등록되어있는 글이 있으면 삭제할수 없습니다.");
				return false;
			} else {
				alert("삭제에 실패했습니다. 다시시도해주세요.");
				return false;
			}
        },
        error: function (rtnHtml) {
            return false;
        }
    });


}


function category_article_move(org_category_code){
	$("#org_category_code").val(org_category_code);
	commonLayerOpen('reg_layer');
}

function category_article_move_proc(){
	if(confirm("등록된 글이 모두 이동됩니다. 진행하시겠습니까?") == true) {    //확인
		$("#input_form").attr("action", "/admin/news/news_section_article_move_proc");
		$("#input_form").submit();
	} else {   //취소
		return;
	}
}



function change_category(depth, val){
	if(depth == '1') {
		$('#category2').empty();
		$('#category3').empty();

		var option = "<option value=''>선택</option>";
		$('#category2').append(option);
		$('#category3').append(option);

		var json_category = '<?=json_encode($arr_category_add2)?>';
		json_category = eval(json_category);

		for(key in json_category) {
			if(json_category[key]['p_category_code'] == val) {
				option = $("<option value='"+json_category[key]['category_code']+"'>"+json_category[key]['category_name']+"</option>");
				$('#category2').append(option);
			}
		}


	} else if(depth == '2') {
		$('#category3').empty();

		var option = "<option value=''>선택</option>";
		$('#category3').append(option);

		var json_category = '<?=json_encode($arr_category_add3)?>';
		json_category = eval(json_category);

		for(key in json_category) {
			if(json_category[key]['p_category_code'] == val) {
				option = $("<option value='"+json_category[key]['category_code']+"'>"+json_category[key]['category_name']+"</option>");
				$('#category3').append(option);
			}
		}

	}
}

</script>