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


<form name="input_form" id="input_form" method="post" action="/admin/cs/cs_faq_category_list/7">
<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="search_type" name="search_type" value="category_name">


<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 style="font-weight:bold">Total:</span> <?=number_format($total_count);?>
	<table>
		<thead>
			<th>번호</th>
			<th>카테고리</th>
			<th>생성일자</th>
			<th>관리</th>

		</thead>
		<tbody>

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

	    $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'];
            $depth 			= $val['depth'];
            $reg_date 	    = $val['reg_date'];

			$del_key = $p_category_code;
            $reply_icon = '';
			$search_category_code = $category_code;
            if($depth == 2) {
                $reply_icon ='<img src="/img/admin/board/icon_reply.gif" style="vertical-align:middle;margin-top:-3px;" border="0"> ';
				$del_key = $idx;
				$search_category_code = $p_category_code.$category_code;
            }

?>

				<tr>
					<td><?=$dataNumber?></td>
                    <td style="text-align: left;">
                        <input type="hidden" name="category_name_<?=$idx?>" id="category_name_<?=$idx?>" value="<?=$category_name?>">
                        &nbsp;&nbsp;<?=$reply_icon?><a href="/admin/cs/cs_faq_list/7?search_category_code=<?=$search_category_code?>"><?=$category_name?></a>
                        <?  if($depth == 1) {?>
                       <button type="button" class="btn_s_blue" style="width:110px" onclick="category_add('<?=$category_code?>');" style="cursor: pointer;">2차 카테고리등록</button>
                        <?  }?>
                    </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>

<div class="write_btn">
	<a href="javascript:category_add('');">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>

	</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>

	

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

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


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

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

    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();

	$("#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){
	var category_name 	    = $("#category_name").val();
    var p_category_code 	= $("#p_category_code").val();
	var category_idx 		= $("#category_idx").val();

	if(category_name=='') {
		alert("카테고리명을 입력해주세요.");
		return false;
	}
	var form_data = {
		"mode"					: mode,
		"category_name"		    : category_name,
        "p_category_code"		: p_category_code,
		"category_idx"			: category_idx

    };
    $.ajax({
        url: '/admin/cs/cs_faq_category_reg_proc',
        sync: false,
        type: 'POST',
        dataType: 'html',
        data: form_data,
        success: function (rtnHtml) {
			if(rtnHtml.trim()=='0000') {
				alert("저장되었습니다.");
				window.location.reload(true);

			} else {
				alert("등록에 실패했습니다.");
				commonLayerClose('category_reg_layer');
				return false;
			}
        },
        error: function (rtnHtml) {
            return false;
        }
    });
}

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


}

</script>