<?php
$arr_category2_add = array();
foreach ($arr_category2 as $key=>$val) {
	foreach ($val as $val1) {
		$arr_category2_add[] = array('p_category_code' => $key, 'category_code' => $val1['category_code'], 'category_name' => $val1['category_name']);
	}
}

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


?>


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



<div class="common_table clearfix type01">
	<div class="searh_area">
		<input type="text" name="search_keyword" id="search_keyword" value="<?=$search_keyword?>" onkeypress="if( event.keyCode==13 ){check_search();}" placeholder="업체명">
		<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">
	<span><b>Total:</b> <?=number_format($total_count);?></span>
	<div class="searh_area">
		<select name="search_category_code1" id="search_category_code1" onchange="change_category(1)">
			<option value="">카테고리 1</option>
	<? 	foreach ($arr_category1 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" id="search_category_code2" onchange="change_category(2)">
		<option value="">카테고리 2</option>
	<?
	if($search_category_code1!='') {
		foreach ($arr_category2[$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>
	<tr>
		<th><input type="checkbox" name="allcheck" id="allcheck"></th>
		<th>번호</th>
		<th>카테고리</th>
		<th>로고</th>
		<th>업체명</th>
		<th>링크연결</th>
		<th>조회수</th>
		<th>등록일</th>
		<th>출력순서</th>
		<th>출력상태</th>
		<th>관리</th>
	</tr>
	</thead>
	<tbody>


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

	    $dataNumber = ($total_count + $limit)-($page*$limit);
		foreach($list as $val) {
            $idx            = $val['idx'];
			$company_name 	= $val['company_name'];
			$category_name1 = $val['category_name1'];
			$category_name2 = $val['category_name2'];
			$logo_img		= $val['logo_img'];
			$homepage		= $val['homepage'];
			$hit_count		= $val['hit_count'];
			$reg_date 	    = $val['reg_date'];
			$display_sort	= $val['display_sort'];
			$is_direct		= $val['is_direct'];
			$category_code1	= $val['category_code1'];
			$category_code2	= $val['category_code2'];

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

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

?>

				<tr>
					<td><input type="checkbox" name="is_del[]" value="<?=$idx?>"></td>
					<td><?=$dataNumber?></td>
					<td><a href="/admin/etc_section/main_category_list/6?search_category_code1=<?=$category_code1?>"><?=$category_name1?></a> > <a href="/admin/etc_section/main_category_list/6?search_category_code1=<?=$category_code1?>&search_category_code2=<?=$category_code2?>"><?=$category_name2?></a></td>
					<td><img src="<?=$logo_img?>" width="75"></td>
                    <td><?=$company_name?></td>
                    <td><a href="<?=$homepage?>" target="_blank"><?=$homepage?></a></td>
                    <td><?=$hit_count?></td>
                    <td><?=$reg_date?></td>
                    <td><?=$display_sort?></td>
                    <td><?=$is_direct_txt?></td>
					<td class="tool_btn">
						<a href="javascript:company_edit('<?=$idx?>')"><i class="fa fa-pencil"></i>수정</a>
						<a href="javascript:company_del_proc('<?=$idx?>')"><i class="fa fa-close"></i>삭제</a>
					</td>
				</tr>
<?
		    $dataNumber--;
		}
	}
?>
	</tbody>
</table>
<div class="write_btn">
	<a href="javascript:company_arr_del_proc();">삭제</a>
	<a href="javascript:company_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 class="tl">
				<select name="category1" id="category1" onchange="change_category1(this.value);">
							<option value="" selected>선택</option>
					<?  foreach ($arr_category1 as $val) { ?>
                            <option value="<?=$val['category_code']?>" <? if($search_category_code1==$val['category_code']) { echo 'selected';}?>><?=$val['category_name']?></option>
                    <?  }?>
						</select>
						<select name="category2" id="category2">
							<option value="" selected>선택</option>
				<?  if($search_category_code1!='') {
                        foreach($arr_category2_add as $val) {
                            if($val['p_category_code'] == $search_category_code1) {
                ?>
                                <option value="<?=$val['category_code']?>" <? if($search_category_code2==$val['category_code']) { echo 'selected';}?>><?=$val['category_name']?></option>
                <?           }
                        }
                    }
                ?>
						</select>
			</td>
		</tr>
		<tr>
			<th>업체명</th>
			<td class="tl">
				<input type="text" name="company_name" id="company_name" class="input00" size="30">
			</td>
		</tr>
		<tr>
			<th>홈페이지</th>
			<td class="tl">
				<input type="text" name="homepage" id="homepage" class="input00" size="30">
			</td>
		</tr>
		<tr>
			<th>송출관리</th>
			<td class="tl">
				<input type="checkbox" name="is_direct" id="is_direct" class="input00" value="Y"> 바로가기에 노출
			</td>
		</tr>
		<tr>
			<th>출력순서</th>
			<td class="tl">
				<input type="text" name="display_sort" id="display_sort" class="input00" size="4">
			</td>
		</tr>
		<tr>
			<th>로고첨부</th>
			<td class="tl">
				<span id="load_img"></span>
				<input type="hidden" name="org_logo_img" id="org_logo_img">
				<input type="file" name="logo_img" id="logo_img" class="input00">
			</td>
		</tr>

	</table>
	<div class="write_btn tc">
		<a href="javascript:company_reg_proc('add')" id="btn_add" style="display:none;">신규등록</a>
		<a href="javascript:company_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 company_add() {
	form_reset();
    $("#btn_add").show();
    commonLayerOpen('reg_layer');
}

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

	console.log("<?=$search_category_code2?>");

	$("#category1").val("<?=$search_category_code1?>").prop("selected", true);
	$("#category2").val("<?=$search_category_code2?>").prop("selected", true);

	$("#idx").val('');
	$("#company_name").val('');
	$("#homepage").val('');
	$("#display_sort").val('');
	$("input:checkbox[id='is_direct']").attr("checked", false);
	$("#org_logo_img").val('');
	$('#load_img').empty();
}

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

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

	var form_data = {
		"idx"					: idx
    };
    $.ajax({
        url: '/admin/etc_section/main_category_company_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);
				$("#category1").val(result.category_code1).prop("selected", true);
				change_category1(result.category_code1);
				$("#category2").val(result.category_code2).prop("selected", true);

				$("#company_name").val(result.company_name);
				$("#homepage").val(result.homepage);
				$("#display_sort").val(result.display_sort);
				if(result.is_direct=='Y') {
					$("input:checkbox[id='is_direct']").attr("checked", true);
				}
				if(result.logo_img!= '') {
					$("#org_logo_img").val(result.logo_img);
					$('#load_img').html('<img src="' + result.logo_img + '" width="100"><br /><input type="checkbox" name="is_del_img" value="Y"> 이미지삭제 ');
				}
				commonLayerOpen('reg_layer');
			} else {
				alert("데이터로드에 실패했습니다.");
				return false;
			}
        },
        error: function (rtnHtml) {
            return false;
        }
    });

}


// 저장
function company_reg_proc(mode){
	if($("#category1").val()=='') {
		alert("카테고리1를 선택해주세요.");
		return false;
	}

	if($("#category2").val()=='') {
		alert("카테고리2를 선택해주세요.");
		return false;
	}
	
	if($("#company_name").val()=='') {
		alert("업체명을 입력해주세요.");
		return false;
	}
    $("#mode").val(mode);

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

}


function change_category(depth){
	if(depth=='1')	$("#search_category_code2").val("").prop("selected", true);
	$("#input_form").submit();

}

var json_category = '<?=json_encode($arr_category2_add)?>';
json_category = eval(json_category);
function change_category1(val){

    $('#category2').empty();
    var option = '';

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

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

        }
    }

}


// 삭제
function company_del_proc(idx) {
	if(confirm("업체가 삭제됩니다. 삭제하시겠습니까?") == true) {    //확인
		$("#idx").val(idx);
		$("#input_form").attr("action", "/admin/etc_section/main_category_company_del_proc");
		$("#input_form").submit();
	} else {   //취소
		return;
	}
}

// 복수 삭제
function company_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/main_category_company_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);
    	}
  	})
})




</script>