<?
include $_SERVER[DOCUMENT_ROOT] . "/backoffice/header.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/product/product.lib.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/category/category.lib.php";
include $_SERVER[DOCUMENT_ROOT] . "/common/fckeditor/fckeditor.php";
if(!in_array("product_manage",$_SESSION[$_SITE["DOMAIN"]]["ADMIN"]["AUTH"]) && $_SESSION[$_SITE["DOMAIN"]]["ADMIN"]["GRADE"]!="ROOT"):
	jsMsg("권한이 없습니다.");
	jsHistory("-1");
endif;

//DB연결
$dblink = SetConn($_conf_db["main_db"]);

//제품분류 리스트
$arrCategory = getCategoryList(0);//1차카테고리

$catrs = mysql_query("select * from tbl_category order by cat_code");


//_DEBUG($arrList);
//DB해제
SetDisConn($dblink);
?>

<div id="admin-container">
	<? include "menu.php"; ?>
    <div id="admin-content">
		<h2 class="admin-title">제품 등록</h2>

			<form name="frmInfo" method="post" action="product_evn.php" ENCTYPE="multipart/form-data" >
			<input type="hidden" name="evnMode" value="insert">


			<!-- 기본정보 -->
			<h3 class="admin-title-middle">기본정보</h3>
			<table class="admin-table-type1">
			  <colgroup>
			  <col width="140" />
			  <col width="*" />
			  </colgroup>
			  <tbody>
				<tr>
					<th>제품분류</th>
					<td class="space-left">
						<input type="hidden" name="cat_no" id="cat_no">
						<select name="cat" id="cat" class="first-depth" depth="1">
						<option value="">==========1차분류==========</option>
						<?for($i=0;$i<$arrCategory["total"];$i++){?>
						<option value="<?=$arrCategory["list"][$i][cat_no]?>"><?=$arrCategory["list"][$i][cat_name]?></option>
						<?}?>
						</select>

						<?
						for($i=2;$i<$_SITE["PRODUCT"]["CATEGORY_DEPTH"]+1;$i++){ //카테고리 5차까지 만듬 => 1차 초기화는 따로위에서 함
						?>
						<select name="cat<?=$i?>" id="cat<?=$i?>" class="subcategory depth<?=$i?>" depth="<?=$i?>">
						<option value="">==========<?=$i?>차분류==========</option>
						</select>
						<?
						}
						?>
					</td>
				</tr>
				<!-- <tr>
					<th>추가분류</th>
					<td class="space-left">
						<select name="addcaList">
							<option value="">선택</option>
							<?php
							for ($i=0; $row = mysql_fetch_assoc($catrs); $i++) {
								unset($space);
								for ($j=0; $j<$row['cat_depth']; $j++) {
									$space .= "&nbsp;&nbsp;&nbsp;";
								}
							?>
							<option value="<?php echo $row['cat_no']; ?>"><?php echo $space.$row['cat_name']; ?></option>
							<?php
							}
							?>
						</select>
						<button value="추가" type="button" class="addcat">추가</button>
						<button value="삭제" type="button" class="delcat">삭제</button>
						<select multiple="multiple" name="rel_category[]" style="width:30%;height:150px;"></select>
					</td>
				</tr> -->
				<tr>
					<th>정렬순서</th>
					<td class="space-left"><input type="text" name="sort_num" style="width:100" maxlength="8" class="input" /> (숫자가 높을수록 앞쪽에 위치함)</td>
				</tr>
				<!-- <tr>
					<th>메인표시</th>
					<td class="space-left"><input type="checkbox" name="show_main" value="Y"<?=$arrInfo["list"][0]["show_main"]=="Y"?" checked":""?>> 표시함</td>
				</tr> -->
				<tr>
					<th>제품명</th>
					<td class="space-left"><input type="text" name="p_name" style="width:50%" maxlength="200" class="input" /></td>
				</tr>
				<tr>
					<th>옵션1</th>
					<td class="space-left"><input type="text" name="etc_1" style="width:50%" value="<?=stripslashes($arrInfo["list"][0]["etc_1"])?>" class="input" /></td>
				</tr>
				<tr>
					<th>옵션2</th>
					<td class="space-left"><input type="text" name="etc_2" style="width:50%" value="<?=stripslashes($arrInfo["list"][0]["etc_2"])?>" class="input" /></td>
				</tr>
				<tr>
					<th>옵션3</th>
					<td class="space-left"><input type="text" name="etc_3" style="width:50%" value="<?=stripslashes($arrInfo["list"][0]["etc_3"])?>" class="input" /></td>
				</tr>
				<tr>
					<th>옵션4</th>
					<td class="space-left"><input type="text" name="etc_4" style="width:50%" value="<?=stripslashes($arrInfo["list"][0]["etc_4"])?>" class="input" /></td>
				</tr>
				<tr>
					<th>옵션5</th>
					<td class="space-left"><input type="text" name="etc_5" style="width:50%" value="<?=stripslashes($arrInfo["list"][0]["etc_5"])?>" class="input" /></td>
				</tr>
				<!-- <tr>
					<th>제품간단설명</th>
					<td class="space-left">
						<textarea name="memo" id="memo" cols="150" rows="5"><?=stripslashes($arrInfo["list"][0]["memo"])?></textarea>
					</td>
				</tr> -->
				<!-- <tr>
					<th>카다로그</th>
					<td class="space-left"><input type="file" name="catalog_file[]" style="width:50%" class="input" /></td>
				</tr> -->
				<!-- <tr>
					<th>카다로그</th>
					<td class="space-left"><input type="file" name="catalog_file1[]" style="width:50%" class="input" /></td>
				</tr>
				<tr>
					<th>카다로그</th>
					<td class="space-left"><input type="file" name="catalog_file2[]" style="width:50%" class="input" /></td>
				</tr>
				<tr>
					<th>카다로그</th>
					<td class="space-left"><input type="file" name="catalog_file3[]" style="width:50%" class="input" /></td>
				</tr>
				-->
				<!-- <tr>
					<th>관련제품</th>
					<td class="space-left">
						<select name="caList">
							<option value="">선택</option>
							<?php
							//DB연결
							$dblink = SetConn($_conf_db["main_db"]);
							$catrs = mysql_query("select * from tbl_category order by cat_code");
							for ($i=0; $row = mysql_fetch_assoc($catrs); $i++) {
								unset($space);
								for ($j=0; $j<$row['cat_depth']; $j++) {
									$space .= "&nbsp;&nbsp;&nbsp;";
								}
							?>
							<option value="<?php echo $row['cat_no']; ?>"><?php echo $space.$row['cat_name']; ?></option>
							<?php
							}
							?>
						</select>
						<select multiple="multiple" name="pickList" style="width:30%;height:150px;"></select>
						<button value="추가" type="button" class="addrel">추가</button>
						<button value="삭제" type="button" class="delrel">삭제</button>
						<select multiple="multiple" name="rel_product[]" style="width:30%;height:150px;"></select>
					</td>
				</tr> -->
			  </tbody>
			</table>

			<br />


			<!-- 제품 사진 -->
			<h3 class="admin-title-middle">제품 사진</h3>
			<table class="admin-table-type1">
			  <colgroup>
			  <col width="140" />
			  <col width="*" />
			  </colgroup>
			  <tbody>
			<?
			for($i=0;$i<$_SITE["PRODUCT"]["IMAGE_COUNT"];$i++){
			?>
			<tr>
			  <th>사진 <?=$i+1?></th>
			  <td class="space-left"><input type="file" name="photo_file[]" style="width:50%" class="input" /> <input type="radio" name="p_image" value="<?=$i?>" id="idPhoto<?=$i?>"<?=$i==0?" checked":""?>><label for="idPhoto<?=$i?>">대표이미지</label></td>
			</tr>
			<?
			}
			?>
			  <tbody>
			</table>


			<br>

			<!-- 제품 설명 -->
			<h3 class="admin-title-middle">상세정보</h3>
			<table class="admin-table-type1">
			  <colgroup>
			  <col width="140" />
			  <col width="*" />
			  </colgroup>
			  <tbody>
				<tr>
					<th>상세설명</th>
					<td class="space-left">
						<?
						$edit_height="240";
						include $_SERVER[DOCUMENT_ROOT] . "/webedit/Editor.html";
						?>
					</td>
				</tr>
				<!-- <tr>
					<th>상세설명2</th>
					<td class="space-left">
						<?php
						$edit_name = "contents1";
						$edit_height="240";
						//include $_SERVER[DOCUMENT_ROOT] . "/webedit/Editor.html";
						?>
					</td>
				</tr> -->
			  </tbody>
			</table>

			<div class="admin-buttons">
				<div class="cen">
					<span class="btn_pack xlarge icon"><span class="check"></span><input type="submit" value="제품 등록" style="font-weight:bold" /></span>
				</div>
			</div>
		</form>
	</div>
</div>

<script language="javascript">
jQuery(function($) {
	function reset_category(obj) {
		$(obj).empty().append("<option value=\"\">=========="+$(obj).attr("depth")+"차분류==========</option>");
	}
	function category_set(obj) {
		var cat = $(obj).val();
		if (cat.length) {
			$("#cat_no").val(cat);
			$.ajax({
				type:"POST",
				url:"/module/category/ajax.category.php",
				data:{
					mode : "getsub",
					cat : cat
				},
				datatype:"json",
				success: function(msg){
					//if (!$("#debug").length) $("body").prepend("<div id='debug' style='position:absolute;width:80%;50%;background-color:#fff;left:10%;top:10%;z-index:9999;'></div>");
					//$("#debug").html(msg);
					data = JSON.parse(msg);
					if (data.success) {
						var target, idx;
						idx = parseInt($(obj).attr("depth")) + 1;
						target = ".depth"+idx;
						if ($(target).length) {
							reset_category(target);
							for(var i=0; i<data.calist.length; i++) {
								$(target).append("<option value=\""+data.calist[i].cat_no+"\">"+data.calist[i].cat_name+"</option>");
							}
						}
					}
				},
				error:function(msg){
					alert("로드에러");
					document.reload();
				},
				complete : function(){
				},
				beforeSend : function() {
				}
			});
		}
	}

	// 초기화
	$(".subcategory").each(function(idx) {
		reset_category(this);
		$(this).bind("change",function() {
			category_set(this);
		});
	});

	$("#cat").change(function() {
		category_set(this);
	});

	$("select[name=caList]").change(function() {
		if ($(this).val() != "") {
			var cat = $(this).val();
			$.ajax({
				type:"POST",
				url:"/module/product/ajax.product.php",
				data:{
					mode : "getproduct",
					cat : cat
				},
				datatype:"json",
				success: function(msg){
					//if (!$("#debug").length) $("body").prepend("<div id='debug' style='position:absolute;width:80%;50%;background-color:#fff;left:10%;top:10%;z-index:9999;'></div>");
					//$("#debug").html(msg);
					data = JSON.parse(msg);
					if (data.success) {
						var target;
						target = "select[name=pickList]";
						if ($(target).length) {
							$(target).empty();
							for(var i=0; i<data.result.length; i++) {
								$(target).append("<option value=\""+data.result[i].idx+"\">"+data.result[i].p_name+"</option>");
							}
						}
					}
				},
				error:function(msg){
					alert("로드에러");
					document.reload();
				},
				complete : function(){
				},
				beforeSend : function() {
				}
			});
		}
	});

	$(".addcat").click(function() {
		if ($("select[name=addcaList] option:selected").length) {
			$("select[name=addcaList] option:selected").each(function(idx) {
				var obj = this;
				$("select[name='rel_category[]'] option").each(function() {
					if ($(this).val() == $(obj).val()) {
						$(this).remove();
					}
				});
				$("select[name='rel_category[]']").append("<option value='"+$(obj).val()+"'>"+$(obj).text()+"</option>");
			});
		} else {
			alert("분류를 선택해주세요");
			return false;
		}
	});
	$(".delcat").click(function() {
		if ($("select[name='rel_category[]'] option:selected").length) {
			$("select[name='rel_category[]'] option:selected").each(function(idx) {
				$(this).remove();
			});
		} else {
			alert("분류를 선택해주세요");
			return false;
		}
	});

	$(".addrel").click(function() {
		if ($("select[name=pickList] option:selected").length) {
			$("select[name=pickList] option:selected").each(function(idx) {
				var obj = this;
				$("select[name='rel_product[]'] option").each(function() {
					if ($(this).val() == $(obj).val()) {
						$(this).remove();
					}
				});
				$("select[name='rel_product[]']").append("<option value='"+$(obj).val()+"'>"+$(obj).text()+"</option>");
			});
		} else {
			alert("제품을 선택해주세요");
			return false;
		}
	});
	$(".delrel").click(function() {
		if ($("select[name='rel_product[]'] option:selected").length) {
			$("select[name='rel_product[]'] option:selected").each(function(idx) {
				$(this).remove();
			});
		} else {
			alert("제품을 선택해주세요");
			return false;
		}
	});

	$("form[name=frmInfo]").submit(function() {
		if ($("input[name=cat_no]").val() == ""){
			alert("제품 분류를 선택해 주십시요.");
			$("input[name=cat_no]").focus();
			return false;
		}
		if ($("input[name=p_name]").val() == ""){
			alert("제품명을 입력해 주십시요.");
			$("input[name=cat_no]").focus();
			return false;
		}

		try{ contents.outputBodyHTML(); } catch(e){ }

		$("select[name='rel_product[]'] option").each(function(idx) {
			$(this).attr("selected","selected");
		});
		$("select[name='rel_category[]'] option").each(function(idx) {
			$(this).attr("selected","selected");
		});
	});
});

</script>

<?
include $_SERVER[DOCUMENT_ROOT] . "/backoffice/footer.php" ;
?>