<?php
include_once ("./static_data/code.php");
$arr_category_add1 = array();
$arr_category_add2 = array();
foreach ($arr_category1 as $key=>$val) {
    $arr_category_add1[] = array('category_code'=>$val['idx'], 'category_name'=>$val['category_name']);
}

foreach ($arr_category2 as $key=>$val) {
    $arr_category_add2[$val['category_idx']][] = array('category_code'=>$val['idx'], 'category_name'=>$val['channel_name']);
}

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



# 에디터체크
$admin_type = $this->session->userdata('Adm_Type');


$colspan = '14';

$arr_display = array();
foreach ($arr_display_count as $val) {
	$arr_display[$val['is_display']] = $val['cnt'];
}
$is_display1 = 0; $is_display2 = 0;
if(isset($arr_display['Y'])==true) 	$is_display1 = $arr_display['Y'];
if(isset($arr_display['N'])==true) 	$is_display2 = $arr_display['N'];


?>

<form name="input_form" id="input_form" method="post" action="/admin/life/life_list/5">
<input type="hidden" id="page" name="page" value="<?=$page?>">
<input type="hidden" id="search_life_type" name="search_life_type" value="<?=$search_life_type?>">
<input type="hidden" id="search_click1" name="search_click1" value="<?=$search_click1?>">
<input type="hidden" id="search_click2" name="search_click2" value="<?=$search_click2?>">
<input type="hidden" id="idx" name="idx">
<input type="hidden" id="mode" name="mode">


<div class="common_tep clearfix type01">
	<ul class="clearfix">
		<li><a href="/admin/life/life_list/5" <? if($search_life_type=='') { echo 'class="on"';}?>>전체</a></li>
	<? foreach ($def_arr_life_type as $key=>$val) {?>
        <li><a href="/admin/life/life_list/5?search_life_type=<?=$key?>" <? if($search_life_type==$key) { echo 'class="on"';}?>><?=$val?></a></li>
	<?  }?>

	</ul>
	<div class="searh_area">
		<select name="search_type" id="search_type">
			<option value="">선택</option>
			<option value="title" <? if($search_type=='title'){ echo 'selected';}?>>제목</option>
			<option value="editor_name" <? if($search_type=='editor_name'){ echo 'selected';}?>>에디터명</option>
			<option value="tag" <? if($search_type=='tag'){ echo 'selected';}?>>태그</option>
		</select> <input type="text" name="search_keyword" id="search_keyword" value="<?=$search_keyword?>" 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">
	<? if($search_type=='tag' && $search_keyword!='') {?><h2 class="subtitle"><?=$search_keyword?></h2><? }?>
	<span style="font-weight:bold">Total:</span> <?=number_format($total_count);?>
	<a href="javascript:click_search('1', 'search_click1');"><span style="font-weight:bold">오늘신규:</span> <?=number_format($new_count);?></a>
	<span style="margin-left: 5px"><input type="radio" name="sort" value="1" <? if($sort=='1') { echo 'checked';}?> onclick="check_search();"> 최신순</span>
	<span style="margin-left: 5px"><input type="radio" name="sort" value="2" <? if($sort=='2') { echo 'checked';}?> onclick="check_search();"> 조회순</span>
	<span style="margin-left: 5px"><input type="radio" name="sort" value="3" <? if($sort=='3') { echo 'checked';}?> onclick="check_search();"> 좋아요순</span>
	<span style="margin-left: 5px"><input type="radio" name="sort" value="4" <? if($sort=='4') { echo 'checked';}?> onclick="check_search();"> 댓글순</span>
	<span style="margin-left: 10px">
		<a href="javascript:click_search('1', 'search_click2');"><span class="text_blue">출력중: <?=number_format($is_display1)?></span></a>&nbsp;&nbsp;&nbsp;
		<a href="javascript:click_search('2', 'search_click2');"><span class="text_red">예약중: <?=number_format($is_display2)?></span></a>
	</span>
<? if($admin_type == '0') {?>
	<div class="searh_area">
		<select name="search_category_code1" onchange="category_search()" class="type01">
            <option value="">카테고리</option>
			<?	if(count($arr_category_add1) > 0) {
					foreach ($arr_category_add1 as $val) {?>
							<option value="<?=$val['category_code']?>" <? if($search_category_code1==$val['category_code']) { echo 'selected'; }?>><?=$val['category_name']?></option>
			<?		}
				}	?>
        </select>
        <select name="search_category_code2" onchange="category_search()" class="type01">
            <option value="">채널</option>
        <?	if(isset($arr_category_add2[$search_category_code1])==true) {
                foreach ($arr_category_add2[$search_category_code1] as $val) {
        ?>
            <option value="<?=$val['category_code']?>" <? if($search_category_code2==$val['category_code']) { echo 'selected'; }?>><?=$val['category_name']?></option>
        <?		}
            }
        ?>
        </select>

	</div>
<?}?>
</div>


<!-- 테이블 시작-->
<div class="common_table">
	<table>
		<thead>
			<th><input type="checkbox" name="allcheck" id="allcheck"></th>
			<th>번호</th>
			<th>썸네일</th>
			<th>카테고리</th>
			<th>채널명</th>
			<th>기능</th>
			<th>에디터명</th>
			<th>제목</th>
            <th>등록일시</br>(예약일시)</th>
			<th>상태</th>
			<th>조회수</th>
			<th>좋아요</th>
			<th>댓글수</th>
		<? if($search_type=='tag' && $search_keyword!='' ) {
				$colspan="15";
		?>
			<th>태그</th>
		<?	}?>
			<th>관리</th>
		</thead>
		<tbody>

<?
	if(count($list)==0) {
?>
		<tr>
			<td colspan="<?=$colspan?>">등록된 내용이 없습니다.</td>
		</tr>
<?
	} else {
		$now_date = date("Y-m-d");
	    $dataNumber = ($total_count + $limit)-($page*$limit);
		foreach($list as $val) {
            $idx            = $val['idx'];
			$title 			= $val['title'];
			$main_img       = $val['main_img'];
			$category_name1 = $val['category_name1'];
			$category_name2 = $val['category_name2'];
			$life_type 	    = $val['life_type'];
			$editor_name    = $val['editor_name'];
			$reg_date 	    = $val['reg_date'];
            $hit_count 	    = $val['hit_count'];
            $like_count 	= $val['like_count'];
            $comment_count 	= $val['comment_count'];
            $is_display 	= $val['is_display'];
            $display_date 	= $val['display_date'];
            $tag 			= $val['tag'];

			$display_date_txt = '';
			if($is_display == 'N') {
				$display_date_txt = '</br><span class="text_green">('.$display_date.')</span>';
			}

			$arr_tag = explode(" ", $tag);
			$tag_count = count($arr_tag);
			$tag_txt = '';
			foreach ($arr_tag as $key=>$val) {
				if($key > 0) $tag_txt.= ', ';
				$tag_txt.='<a href="/admin/life/life_list/5?search_type=tag&search_keyword='.$val.'" class="text_blue">'.$val.'</a>';
				if($key==2) break;
			}

			if($tag_count > 3) {
				$tag_txt.=' 외 '.($tag_count-3).'개';
			}

			$new = '';
            if($reg_date == $now_date)	$new = '<img src="/img/admin/board/icon_new.gif" style="vertical-align:middle;margin-top:-3px;" border="0"> ';

?>

			<tr>
				<td><input type="checkbox" name="is_del[]" value="<?=$idx?>"></td>
				<td><?=$dataNumber?></td>
				<td><img src="<?=$main_img?>" width="35"></td>
				<td><a href="/admin/life/life_category_list/5?search_type=category_name&search_keyword=<?=$category_name1?>"><?=$category_name1?></a></td>
				<td><a href="/admin/life/life_channel_list/5?search_type=channel_name&search_keyword=<?=$category_name2?>"><?=$category_name2?></a></td>
				<td><?=$def_arr_life_type[$life_type]?></td>
				<td><a href="/admin/life/life_list/5?search_type=editor_name&search_keyword=<?=$editor_name?>"><?=$editor_name?></a></td>
				<td style="text-align:left;" class="title"><a href="javascript:life_edit('<?=$idx?>')"><?=$new?><?=$title?></a></td>
				<td><?=$reg_date?><?=$display_date_txt?></td>
				<td>
				<? 	if($is_display == 'N') {?>
					<select name="is_display_<?=$idx?>" id="is_display_<?=$idx?>" onchange="change_is_display(this);">
						<option value="Y" <? if($is_display=='Y') { echo 'selected';}?>>출력</option>
						<option value="N" <? if($is_display=='N') { echo 'selected';}?>>예약</option>
					</select>
				<? 	} else {
						echo '출력';
					}
				?>
				</td>
				<td><?=$hit_count?></td>
				<td><?=$like_count?></td>
				<td><?=$comment_count?></td>
				<? if($search_type=='tag' && $search_keyword!='' ) {?>
					<td class="text_blue"><?=$tag_txt?></td>
				<?	}?>
				<td class="tool_btn">
					<a href="javascript:life_edit('<?=$idx?>')"><i class="fa fa-pencil"></i>수정</a>
					<a href="javascript:life_del_proc('<?=$idx?>')"><i class="fa fa-close"></i>삭제</a>
				</td>
			</tr>

<?
		    $dataNumber--;
		}
	}
?>
</tbody>
</table>
</div>
<div class="write_btn">
	<a href="javascript:life_arr_del_proc();">삭제</a>
	<a href="javascript:life_add();">라이프등록</a>
</div>

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


</form>


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

function click_search(type, id){
	$("#"+id).val(type);
	$("#input_form").submit();
}


function category_search(){
	$("#input_form").submit();
}

function life_add(){
	$("#mode").val('add');
	$("#input_form").attr("action", "/admin/life/life_reg/5");
	$("#input_form").submit();
}

// edit
function life_edit(idx){
	$("#idx").val(idx);
	$("#mode").val('edit');
	$("#input_form").attr("action", "/admin/life/life_reg/5");
	$("#input_form").submit();
}

// 삭제
function life_del_proc(idx) {
	if(confirm("글이 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
		$("#idx").val(idx);
		$("#input_form").attr("action", "/admin/life/life_reg_del_proc");
		$("#input_form").submit();
	} else {   //취소
		return;
	}
}

// 복수 삭제
function life_arr_del_proc() {
	if($("input:checkbox[name='is_del[]']").is(":checked") == false) {
		alert("삭제할 내용이 없습니다.");
		return false;
	}
	if(confirm("글이 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
		$("#input_form").attr("action", "/admin/life/life_reg_arr_del_proc");
		$("#input_form").submit();
	} else {   //취소
		return;
	}
}

// checkbox all checked/unchecked
$(function(){
	$("#allcheck").click(function(){
    	if($("#allcheck").prop("checked")) {
    		$("input:checkbox[name='is_del[]']").prop('checked', true); // 전체선택하기
		} else {
      		$("input:checkbox[name='is_del[]']").prop("checked", false);
    	}
  	})
})


function change_is_display(t){
	var val 		= t.value;
	var select_id 	= t.id;
	var idx 		= select_id.replace("is_display_", "");

	var form_data = {
		"is_display"	: val,
		"idx"			: idx
    };
    $.ajax({
        url: '/admin/life/life_is_display_update',
        sync: false,
        type: 'POST',
        dataType: 'json',
        data: form_data,
        success: function (rtn_json) {
        	if(rtn_json.result_code!='0000') {
				alert("저장에 실패했습니다.");
				return false;
			} else {
				window.location.reload(true);
			}
        },
        error: function (rtnHtml) {
            return false;
        }
    });


}





</script>