<?php
include_once ("./static_data/code.php");
$arr_theme_reg = array();
for($i=11;$i<=21;$i++) {
	$arr_theme_reg[$i] = '';
}


$arr_section_reg = array();
for($i=1;$i<=4;$i++) {
	$arr_section_reg[$i] = '';
}


$admin_type = $this->session->userdata('Adm_Type');
$admin_id	= $this->session->userdata('Adm_Id');
if($mode=='edit') {
	// 칼럼리스트는 자기 글만 삭제 가능
	$write_id			= $result['write_id'];
	if($admin_type == '1') {
		if($write_id != $admin_id) {
			gf_alert("잘못된 접근입니다.", "/admin/main");
			exit;
		}
	}

    $title      		= $result['title'];
	$title 				= htmlspecialchars($title);

    $content    		= htmlentities($result['content']);
    $category_code1  	= $result['category_idx'];
    $category_code2  	= $result['channel_idx'];
    $category_code3  	= $result['partner_idx'];
    $head_title 	    = $result['head_title'];
	$head_title 		= htmlspecialchars($head_title);
    $sub_title    		= $result['sub_title'];
	$sub_title 			= htmlspecialchars($sub_title);
    $tag    			= $result['tag'];
    $org_url    		= $result['org_url'];
    $hit_count    		= $result['hit_count'];
    $like_count  	    = $result['like_count'];
    $display_date		= $result['display_date'];
    $is_display			= $result['is_display'];
	$org_main_img		= $result['main_img'];
	$life_type			= $result['life_type'];
	$main_img			= $result['main_img'];
	$video_url			= $result['video_url'];
	if($video_url !='')	$video_url = str_replace('https://www.youtube.com/embed/','', $video_url);
	
	$arr_display_date  	= explode(' ', $display_date);
	$display_date		= $arr_display_date[0];
	$arr_display_time  	= explode(':', $arr_display_date[1]);
	$display_hour		= $arr_display_time[0];
	$display_min		= $arr_display_time[1];
	$display_sec		= $arr_display_time[2];

	$tag		= str_replace(' ', ',', $tag);

	foreach ($arr_theme as $val) {
		$arr_theme_reg[$val['theme_type']] = $val['rank'];
	}

	foreach ($arr_section as $val) {
		$arr_section_reg[$val['etc_section_type']] = $val['rank'];
	}


} else {
    $main_img       = '';
	$category_code1	= '';
	$category_code2	= '';
	$category_code3	= '';
	$title          = '';
	$head_title     = '';
	$content        = '';
	$sub_title      = '';
	$tag            = '';
	$org_url        = '';
	$hit_count		= 0;
	$like_count	    = 0;
	$video_url	    = '';

	$display_date	= '';
	$display_hour	= '';
	$display_min	= '';
	$display_sec	= '';
	$life_type		= $search_life_type;
	if($life_type == '')	$life_type = '1';

}


?>
<link rel="stylesheet" href="<?=DIR_CSS?>/jquery-ui.min.css" />
<script src="<?=DIR_JS?>/jquery-ui.min.js"></script>
<script src="<?=DIR_JS?>/admin/jquery.multiselect.js"></script>
<link href="<?=DIR_CSS?>/admin/jquery.multiselect.css" rel="stylesheet">
<script src="/ckeditor/ckeditor.js"></script>


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




<form name="input_form" id="input_form" action="/admin/life/life_reg_proc" method="post" enctype="multipart/form-data">
<input type="hidden" name="idx" id="idx" value="<?=$idx?>">
<input type="hidden" id="search_type" name="search_type" value="<?=$search_type?>">
<input type="hidden" id="search_keyword" name="search_keyword" value="<?=$search_keyword?>">
<input type="hidden" id="search_life_type" name="search_life_type" value="<?=$search_life_type?>">
<input type="hidden" id="search_category_code1" name="search_category_code1" value="<?=$search_category_code1?>">
<input type="hidden" id="search_category_code2" name="search_category_code2" value="<?=$search_category_code2?>">
<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="sort" name="sort" value="<?=$sort?>">
<input type="hidden" id="page" name="page" value="<?=$page?>">
<input type="hidden" id="mode" name="mode" value="<?=$mode?>">
<input type="hidden" id="img_path" name="img_path">



<div class="common_table news">
<table>
<tr>
	<th>기능</th>
	<td class="tl">
        <? foreach ($def_arr_life_type as $key=>$val) {?>
            <input type="radio" name="life_type" value="<?=$key?>" <? if($life_type==$key) { echo 'checked';}?>> <?=$val?>&nbsp;&nbsp;
        <?  }?>
	</td>
</tr>
<tr>
	<th>카테고리</th>
	<td class="tl">
		<select name="category1" id="category1" onchange="change_category(1, this.value)" class="type01">
			<option value="">선택</option>
	<?	if(count($arr_category1) > 0) {
			foreach ($arr_category1 as $val) {?>
					<option value="<?=$val['idx']?>" <? if($category_code1==$val['idx']) { echo 'selected'; }?>><?=$val['category_name']?></option>
	<?		}
		}	?>
		</select>
	</td>
</tr>
<tr>
	<th>채널</th>
	<td class="tl">
		<select name="category2" id="category2" onchange="change_category(2, this.value)" class="type01">
			<option value="">선택</option>
	<?	if(count($arr_category2) > 0) {
			foreach ($arr_category2 as $val) {?>
					<option value="<?=$val['idx']?>" <? if($category_code2==$val['idx']) { echo 'selected'; }?>><?=$val['channel_name']?></option>
	<?		}
		}	?>
		</select>
	</td>
</tr>
<tr>
	<th>에디터</th>
	<td class="tl">
		<select name="category3" id="category3" class="type01">
			<option value="">선택</option>
	<?	if(count($arr_category3) > 0) {
			foreach ($arr_category3 as $val) {?>
					<option value="<?=$val['idx']?>" <? if($category_code3==$val['idx']) { echo 'selected'; }?>><?=$val['partner_name']?></option>
	<?		}
		}	?>
		</select>

	</td>
</tr>
<tr>
	<th>말머리</th>
	<td class="tl">
		<input type="text" name="head_title" id="head_title" class="type02" value="<?=$head_title?>">
	</td>
</tr>
<tr>
	<th>제목</th>
	<td class="tl">
		<input type="text" name="title" id="title" value="<?=$title?>">
	</td>
</tr>
<tr>
	<th>부제목</th>
	<td class="tl">
		<input type="text" name="sub_title" id="sub_title" value="<?=$sub_title?>">
	</td>
</tr>
<tr>
	<th>메인이미지</th>
	<td class="tl">

		<input type="file" name="main_img" id="main_img" class="input00" size="100">

    <? 	if($main_img!='') { ?>
		<input type="checkbox" name="is_del_img" value="Y">삭제
		<img src="<?=$main_img?>" width="100px"></br>
		<?	} else {
				$main_img = '';
			}
		?>
		<input type="hidden" name="org_main_img" id="org_main_img" value="<?=$main_img?>">

	</td>
</tr>
<tr>
	<th>태그</th>
	<td class="tl">
		<input type="text" name="tag" id="tag" class="type01" value="<?=$tag?>"> 최대 5개까지 입력 가능(,로 구분해주세요.)
	</td>
</tr>
<tr>
	<th>원본주소</th>
	<td class="tl">
		기사의 원본 주소를 입력하면 기사 클릭시 기사원본의 주소로 이동합니다.</br>
		<input type="text" name="org_url" id="org_url" class="input00" value="<?=$org_url?>">
	</td>
</tr>
<tr>
	<th>youtube 주소</th>
	<td class="tl">
		https://youtu.be/<input type="text" name="video_url" id="video_url" class="input00" value="<?=$video_url?>" style="width:200px"> (키만 넣어주세요.)
	</td>
</tr>
<tr>
	<th>본문</th>
	<td>
		<!-- <textarea id="content" name="content"><?=$content?></textarea> -->
		<textarea name="content" id="ir1" style="width:100%; height:500px;" /><?=$content?></textarea>
	</td>
</tr>
<tr>
	<th>조회수</th>
	<td class="tl">
		<input type="text" name="hit_count" id="hit_count" class="type03" value="<?=$hit_count?>"> (관리자가 임의적으로 조회수를 설정할 수 있습니다.)
	</td>
</tr>
<tr>
	<th>좋아요수</th>
	<td class="tl">
        <input type="text" name="like_count" id="like_count" class="type03" value="<?=$like_count?>"> (관리자가 임의적으로 좋아요수를 설정할 수 있으나 조회수 이상이 될 수는 없습니다)
	</td>
</tr>
</table>

<? if($admin_type=='0') {?>

<h2 class="subtitle"><span>PC</span>테마설정</h2>
<table>
<? foreach($def_arr_life_theme as $key=>$val) {
        if($key == '16') break;
        $add_txt = '';
        $width  = "300px";
        if($key == '13' || $key == '14') {
            $add_txt = '</br>(모바일공통)';
            $width="400px";
        }

?>

<tr>
	<th><?=$val['name']?><?=$add_txt?></td>
	<td class="tl">

		<div class="opt_box">
			<select name="theme_type<?=$key?>" class="type01">
				<option value="">순위설정</option>
		<? for($i=1;$i<=$def_arr_life_theme[$key]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_theme_reg[$key] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
		</div>
		<img src="/img/admin/life<?=$key?>.png" width="<?=$width?>">
		<? if($key==13){ ?><img src="/img/admin/1.png" width="300px"><? }?>
		<? if($key==14){ ?><img src="/img/admin/2.png" width="250px"><? }?>
		<? if($key==15){ ?><img src="/img/admin/3.png" width="400px"><? }?>

	</td>
</tr>


<? }?>
</table>


<h2 class="subtitle"><span>모바일</span>테마설정</h2>
<table>
<? for($key=16;$key<=21;$key++){
        $add_txt = '라이프</br>';
        if($key <= '19') {
            $add_txt = '은혜</br>';

        }

?>

<tr>
	<th><?=$add_txt?><?=$def_arr_life_theme[$key]['name']?></td>
	<td class="tl">

		<div class="opt_box">
			<select name="theme_type<?=$key?>" class="type01">
				<option value="">순위설정</option>
		<? for($i=1;$i<=$def_arr_life_theme[$key]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_theme_reg[$key] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
		</div>
		<img src="/img/admin/life<?=$key?>.png" width="350px">



	</td>
</tr>


<? }?>


</table>

<h2 class="subtitle">기타섹션<span>(모바일공통)</span></h2>
<table>
	<tr>
		<th>인기영상</td>
		<td class="tl">
			<div class="opt_box">
			<select name="section_type1" class="type01">
				<option value="">순위선택</option>
		<? for($i=1;$i<=$def_arr_etc_section[1]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_section_reg[1] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
			</div>
			<img src="/img/admin/section1.png" width="300px">

		</td>
	</tr>
	<tr>
		<th>인기포토</td>
		<td class="tl">
			<div class="opt_box">
			<select name="section_type2" class="type01">
				<option value="">순위설정</option>
		<? for($i=1;$i<=$def_arr_etc_section[2]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_section_reg[2] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
			</div>
			<img src="/img/admin/section1.png" width="300px">


		</td>
	</tr>
	<tr>
		<th>은혜로운영상<br />(모바일전용)</td>
		<td class="tl">
			<div class="opt_box">
			<select name="section_type3" class="type01">
				<option value="">순위설정</option>
		<? for($i=1;$i<=$def_arr_etc_section[3]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_section_reg[3] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
			</div>
			<img src="/img/admin/section2.png" width="250px">
		</td>
	</tr>
	<tr>
		<th>카드뉴스</td>
		<td class="tl">
			<div class="opt_box">

			<select name="section_type4" class="type01">
				<option value="">순위설정</option>
		<? for($i=1;$i<=$def_arr_etc_section[4]['count'];$i++){?>
				<option value="<?=$i?>" <? if($arr_section_reg[4] == $i) { echo 'selected';}?>><?=$i?>위</option>
		<?	}?>
			</select>
			</div>
			<img src="/img/admin/news_10.png" width="250px">

		</td>
	</tr>
</table>

<?}?>


<h2 class="subtitle">등록일시설정</h2>
<table>
	<tr>
		<th>등록일시</th>
		<td class="tl">
		<? if($mode=='add') {?>
			<input type="text" name="display_date" id="display_date" value="<?=$display_date?>" class="type02" size="10" placeholder="예약일">
			<input type="text" name="display_hour" id="display_hour" value="<?=$display_hour?>" class="type03" size="3" placeholder="시">:<input type="text" name="display_min" id="display_min" value="<?=$display_min?>" class="type03" size="3" placeholder="분">:<input type="text" name="display_sec" id="display_sec" value="<?=$display_sec?>" class="type03" size="3" placeholder="초">
		<?} else {?>
			<? if(true) {?>
				<input type="text" name="display_date" id="display_date" value="<?=$display_date?>" class="type02" size="10" placeholder="예약일">
				<input type="text" name="display_hour" id="display_hour" value="<?=$display_hour?>" class="type03" size="3" placeholder="시">:<input type="text" name="display_min" id="display_min" value="<?=$display_min?>" class="type03" size="3" placeholder="분">:<input type="text" name="display_sec" id="display_sec" value="<?=$display_sec?>" class="type03" size="3" placeholder="초">

			<? } else {?>

				<input type="hidden" name="display_date" value="<?=$display_date?>">
				<input type="hidden" name="display_hour" value="<?=$display_hour?>">
				<input type="hidden" name="display_min" value="<?=$display_min?>">
				<input type="hidden" name="display_sec" value="<?=$display_sec?>">

				<?=$display_date?> <?=$display_hour?>:<?=$display_min?>:<?=$display_sec?>

		<?		}

			}
		?>
		</td>
	</tr>
</table>
</div>
<div class="write_btn tc">
	<a href="javascript:life_reg_proc();">저장하기</a>
    <a href="/admin/life/life_list/2/<?=$page?>?search_type=<?=$search_type?>&search_keyword=<?=$search_keyword?>&search_category_code1=<?=$search_category_code1?>&search_category_code2=<?=$search_category_code2?>&search_life_type=<?=$search_life_type?>&sort=<?=$sort?>&search_click1=<?=$search_click1?>&search_click2=<?=$search_click2?>">취소하기</a>
</div>
</form>


<script type="text/javascript">
var oEditors = [];
var oEditors2 = [];

$(function(){
      nhn.husky.EZCreator.createInIFrame({
          oAppRef: oEditors,
          elPlaceHolder: "ir1",
          sSkinURI: "/smarteditor/SmartEditor2Skin.html",  
          htParams : {
              bUseToolbar : true,             
              bUseVerticalResizer : true,     
              bUseModeChanger : true,         
              fOnBeforeUnload : function(){
              }
          }, 
          fOnAppLoad : function(){
          },
          fCreator: "createSEditor2"
      });
      
      nhn.husky.EZCreator.createInIFrame({
          oAppRef: oEditors2,
          elPlaceHolder: "ir2", 
          sSkinURI: "/smarteditor/SmartEditor2Skin.html",  
          htParams : {
              bUseToolbar : true,             
              bUseVerticalResizer : true,     
              bUseModeChanger : true,         
              fOnBeforeUnload : function(){
              }
          }, 
          fOnAppLoad : function(){
          },
          fCreator: "createSEditor2"
      });

});
</script>

<script type="text/javascript">
$(document).ready(function() {
    /*
	CKEDITOR.replace('content', {
        height: '400px'
    });
	*/
});


function life_reg_proc(){
	//CKEDITOR.instances.content.updateElement();
	oEditors.getById["ir1"].exec("UPDATE_CONTENTS_FIELD", []);
	//oEditors2.getById["ir2"].exec("UPDATE_CONTENTS_FIELD", []);

    if($("#category1").val()=='') {
		alert("카테고리를 선택해주세요.");
		return false;
	}
	if($("#category2").val()=='') {
		alert("채널을 선택해주세요.");
		return false;
	}

	if($("#category3").val()=='') {
		alert("에디터를 선택해주세요.");
		return false;
	}

	if($("#title").val()=='') {
		alert("제목을 입력해주세요.");
		return false;
	}

	if($("#main_img").val()=='' && $("#org_main_img").val()=='') {
		alert("메인이미지를 입력해주세요.");
		return false;
	}

	/*
	if($("#content").val()=='') {
		alert("내용을 입력해주세요.");
		return false;
	}
	*/

	if($("#ir1").val()=='' || $("#ir1").val()=='null' || $("#ir1").val()=='&nbsp;' || $("#ir1").val()=='<p>&nbsp;</p>') {
		alert("내용을 입력해주세요.");
		oEditors.getById["ir1"].exec("focus");
		return false;
	}

	var hit_count 	= parseInt($("#hit_count").val());
    var like_count  = parseInt($("#like_count").val());

	if(like_count > hit_count) {
		alert("좋아요수가 조회수 이상이 될수 없습니다.");
		return false;
	}

	$("#input_form").submit();

}


function change_category(depth, val){
	if(depth ==  1) {
		var json_category = '<?=json_encode($arr_category2)?>';
		json_category = eval(json_category);

		$('#category2').empty();
		$('#category3').empty();

		var option = "<option value=''>선택</option>";
		$('#category2').append(option);
		$('#category3').append(option);
		for(key in json_category) {
			if(json_category[key]['category_idx'] == val) {
				option = $("<option value='"+json_category[key]['idx']+"'>"+json_category[key]['channel_name']+"</option>");
				$('#category2').append(option);

			}
		}

	} else {
		
		var json_category = <?=json_encode($arr_category3)?>;

		$('#category3').empty();

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

		for(key in json_category) {
			if(json_category[key]['channel_idx'] == val) {
				option = $("<option value='"+json_category[key]['idx']+"'>"+json_category[key]['partner_name']+"</option>");
				$('#category3').append(option);

			}
		}

	}
}


// jquery datepicker
$("#display_date").datepicker({
	inline: true,
	dateFormat: 'yy-mm-dd',
	buttonImage:"/img/admin/btn_date.jpg",
	showOn: "both",
	buttonImageOnly: true

});
$('img.ui-datepicker-trigger').css({'cursor':'pointer',  'width':'30px'});  //아이콘(icon) 위치

</script>