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

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



<form name="input_form" id="input_form" method="post" action="/admin/etc_section/today_word_list/6" enctype="multipart/form-data">
<input type="hidden" id="idx" name="idx">
<input type="hidden" id="mode" name="mode">
<input type="hidden" id="search_type" name="search_type" value="word">


<div class="common_table 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">
<span><b>Total:</b> <?=number_format($total_count);?></span>
<table>
	<thead>
	<tr>
		<th><input type="checkbox" name="allcheck" id="allcheck"></th>
		<th>번호</th>
		<th>성경구절</th>
		<th>성경본문</th>
		<th>노출일</th>
		<th>출력상태</th>
		<th>관리</th>

	</tr>
	</thead>
	<tbody>

<?
	if(count($list)==0) {
		$i =0;
?>
		<tr>
			<td colspan="7">등록된 내용이 없습니다.</td>
		</tr>
<?
	} else {

		$i=1;
	    $dataNumber = ($total_count + $limit)-($page*$limit);
		foreach($list as $val) {
            $idx            = $val['idx'];
			$word 	   		= $val['word'];
			$title			= $val['title'];
			$display_date 	= $val['display_date'];
			$is_display		= $val['is_display'];

			$is_display_txt = '';
			if($is_display == 'Y')	$is_display_txt = '<span class="text_blue">ON</span>';

?>

				<tr>
					<td><input type="checkbox" name="is_del[]" value="<?=$idx?>"></td>
					<td><?=$dataNumber?></td>
                    <td><?=$word?></td>
                    <td><?=$title?></td>
                    <td><input type="text" name="display_date_<?=$idx?>" id="display_date_<?=$i?>" style="width:100px" size="12" value="<?=$display_date?>" /></td>
                    <td><?=$is_display_txt?></td>
					<td class="tool_btn">
						<a href="javascript:today_word_edit('<?=$idx?>')"><i class="fa fa-pencil"></i>수정</a>
						<a href="javascript:today_word_del_proc('<?=$idx?>')"><i class="fa fa-close"></i>삭제</a>
					</td>

				</tr>

<?
			$i++;
		    $dataNumber--;
		}
	}
?>

</tbody>
</table>
<div class="write_btn">
	<a href="javascript:today_word_arr_del_proc();">삭제</a>
	<a href="javascript:today_word_add();">오늘의말씀등록</a>
</div>

<div class="pasing_wrap">
	<?=$pagination?>
</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>
				<input type="text" name="word" id="word" class="input00" size="30">
			</td>
		</tr>
		<tr>
			<th>성경본문</th>
			<td>
				<input type="text" name="title" id="title" class="input00" size="30">
			</td>
		</tr>
		<tr>
			<th>노출일시</th>
			<td class="tl">
				<input type="text" name="display_date" id="display_date" class="type02" size="12" />
			</td>
		</tr>
	</table>
	<div class="write_btn tc">
		<a href="javascript:today_word_reg_proc('add')" id="btn_add" style="display:none;">신규등록</a>
		<a href="javascript:today_word_reg_proc('edit')" id="bnt_edit" style="display:none;">수정</a>
		<a href="javascript:commonLayerClose('reg_layer')">취소하기</a>
	</div>
</div>
</div>


<!--// 카테고리 등록/수정 layer -->


</form>


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

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


// 등록 - layer 열기
function today_word_add() {
	form_reset();
    $("#btn_add").show();
    commonLayerOpen('reg_layer');
}

// form 초기화
function form_reset(){
	$("#bnt_edit").hide();
	$("#btn_add").hide();

	$("#idx").val('');
	$("#word").val('');
	$("#title").val('');
	$("#display_date").val('');
}

// 수정 - layer열기
function today_word_edit(idx){
	form_reset();

	$("#bnt_edit").show();

	var form_data = {
		"idx"					: idx
    };
    $.ajax({
        url: '/admin/etc_section/today_word_detail',
        sync: false,
        type: 'POST',
        dataType: 'json',
        data: form_data,
        success: function (rtn_json) {
        	console.log(rtn_json);
			if(rtn_json.result_code=='0000') {
				var result = rtn_json.result_data;
				$("#idx").val(idx);
				$("#word").val(result.word);
				$("#title").val(result.title);
				$("#display_date").val(result.display_date);

				commonLayerOpen('reg_layer');
			} else {
				alert("데이터로드에 실패했습니다.");
				return false;
			}
        },
        error: function (rtnHtml) {
            return false;
        }
    });

}


// 저장
function today_word_reg_proc(mode){
	if($("#word").val()=='') {
		alert("어플명을 입력해주세요.");
		return false;
	}
	if($("#market_url").val()=='') {
		alert("마켓주소를 입력해주세요.");
		return false;
	}

	if($("#display_date").val()!='') {
		var display_date = new Date($('#display_date').val());
		var now_date = new Date();
		now_date.setHours(0, 0, 0, 0);
		if (display_date < now_date){
			alert('노출일을 이전으로 등록할수 없습니다.');
			return false;
		}
	}
    $("#mode").val(mode);

	$("#input_form").attr("action", "/admin/etc_section/today_word_reg_proc");
	$("#input_form").submit();

}

// 삭제
function today_word_del_proc(idx) {
	if(confirm("오늘의말씀이 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
		$("#idx").val(idx);
		$("#input_form").attr("action", "/admin/etc_section/today_word_del_proc");
		$("#input_form").submit();
	} else {   //취소
		return;
	}
}

// 복수 삭제
function today_word_arr_del_proc() {
	if($("input:checkbox[name='is_del[]']").is(":checked") == false) {
		alert("삭제할 내용이 없습니다.");
		return false;
	}
	if(confirm("오늘의말씀이 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
		$("#input_form").attr("action", "/admin/etc_section/today_word_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);
    	}
  	})
})

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

});
for(z=1;z<"<?=$i?>";z++) {
	var datepicker_id = "display_date_"+z;
	$("#"+datepicker_id).datepicker({
		inline: true,
		dateFormat: 'yy-mm-dd',
		buttonImage:"/img/btn_date.png",
		showOn: "both",
		buttonImageOnly: true,
		onSelect: function (dateText, inst) {
			change_display_date(dateText, inst)
		}

	});

}

function change_display_date(dateText, inst){
	var datepicker_id = inst.id;
	name = $('#'+datepicker_id).attr('name');
	var idx = name.replace("display_date_", "");

	var form_data = {
		"display_date"	: dateText,
		"idx"			: idx
    };
    $.ajax({
        url: '/admin/etc_section/today_word_display_date_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>