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

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

if($_REQUEST[st]=="1"){
	$order_by = " A.sort_num DESC, A.idx DESC ";
}else{
	$order_by = " A.idx DESC ";
}

//상품 리스트
$arrList = getGoodListBaseNFile(
	mysql_real_escape_string($_REQUEST[cat_no]),
	$order_by,
	mysql_real_escape_string($_REQUEST[sw]),
	mysql_real_escape_string($_REQUEST[sk]),
	$scale, $_REQUEST[offset],"");

//전체 카테고리 가져오기
$arrAllCategory = getCategoryAll();

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

$arrCategoryInfo = getCategoryInfo(mysql_real_escape_string($_REQUEST[cat_no]));

//카테고리 정보
$arrCatCode = explode("/", $arrCategoryInfo["list"][0][cat_code]);

//DB해제
SetDisConn($dblink);
?>
<div id="admin-container">
	<? include "menu_good.php"; ?>
    <div id="admin-content">
		<h2 class="admin-title">상품 관리</h2>

<script language="javascript">
function delGood(idx,cat_no,cat,cat2,cat3){
	var cfm;
	cfm =false;
	cfm = confirm("이 상품을 삭제 하시겠습니까?");
	if(cfm==true){
		document.getElementById("g_idx").value = idx;
		document.getElementById("g_cat_no").value = cat_no;
		document.getElementById("g_cat").value = cat;
		document.getElementById("g_cat2").value = cat2;
		document.getElementById("g_cat3").value = cat3;
		
		// 삭제후 검색 리스트로
		// 20100217
		/*
		document.frmListHidden.idx.value = idx;
		document.frmListHidden.cat_no.value = cat_no;
		document.frmListHidden.cat.value = cat;
		document.frmListHidden.cat2.value = cat2;
		document.frmListHidden.cat3.value = cat3;
		*/
		document.frmListHidden.submit();
	}
}
</script>

<form name="frmSort" method="get" action="<?=$_SERVER[PHP_SELF]?>">
<h3 class="admin-title-middle">상품검색</h3>
<input type="hidden" name="cat_no" id="cat_no">
<table class="admin-table-type1">
  <colgroup>
  <col width="140" />
  <col width="*" />
  </colgroup>
  <tbody>
	<tr>
		<th>분류선택</th>
		<td class="space-left">
			<select name="cat" id="cat" class="first-category depth1" depth="1">
				<option value="">==========1차분류==========</option>
				<?for($i=0;$i<$arrCategory["total"];$i++){?>
				<option value="<?=$arrCategory["list"][$i][cat_no]?>"<?=$arrCatCode[0]==$arrCategory["list"][$i][cat_no]?" selected":""?>><?=$arrCategory["list"][$i][cat_name]?></option>
				<?}?>
			</select>
			<?php
			$dblink = SetConn($_conf_db["main_db"]);
			for($i=2;$i<$_SITE["PRODUCT"]["CATEGORY_DEPTH"]+1;$i++){ //카테고리 $_SITE["PRODUCT"]["CATEGORY_DEPTH"]차까지 만듬 => 1차 초기화는 따로위에서 함
				$_dep = $i-2;
			?>
			<select name="cat<?=$i?>" id="cat<?=$i?>" class="subcategory depth<?=$i?>" depth="<?=$i?>">
				<option value="">==========<?=$i?>차분류==========</option>
				<?php
				if (strlen($arrCatCode[$_dep])) {
					$subCa = getCategoryList($arrCatCode[$_dep]);
					for ($j=0; $j<$subCa['total']; $j++) {
				?>
					<option value="<?php echo $subCa['list'][$j]['cat_no']; ?>" <?=$arrCatCode[$_dep+1]==$subCa['list'][$j]['cat_no']?" selected='selected'":""?>><?php echo $subCa['list'][$j]['cat_name']; ?></option>
				<?php
					}
				}
				?>
			</select>
			<?
			}
			?>
		</td>
	</tr>
	<tr>
	  <th>정렬</th>
	  <td class="space-left">
	    <select name="st">
		<option value="0"<?=$_REQUEST[st]=="0"?" selected":""?>>상품번호 역순</option>
		<option value="1"<?=$_REQUEST[st]=="1"?" selected":""?>>정렬번호 역순</option>
		</select>
	  </td>
	</tr>
	<tr>
	  <th>상품명</th>
	  <td class="space-left"><input type="text" name="sk" value="<?=$_REQUEST[sk]?>" class="input" style="width:300px;" /> <input type="image" src="/backoffice/images/btn_search.gif" alt="검색"/></td>
	</tr>
  </tbody>
</table>
</form>

<br />

<div class="mgb5">
	<div class="fl">&nbsp;<strong>전체 : <?=number_format($arrList['total'])?> 개</strong></div>
	<div class="fr"><!-- <a href="/backoffice/module/shop/good_to_csv.php?cat_no=&cat=&cat2=&cat3=&sk=&st=&show_yn=" target="_blank">상품목록 CSV로 받기</a> --></div>
	<div class="clear"></div>
</div>

<table class="admin-table-type1">
  <thead>
	<tr>
	  <th width="45">No.</th>
	  <th width="45">사진</th>
	  <th width="180">상품분류</th>
	  <th width="*">상품명</th>
	  <th width="65">판매가</th>
	  <!-- <th width="65">재고관리</th> -->
	  <!-- <th width="60">메인노출</th> -->
	  <th width="60">정렬번호</th>
	  <th width="120">등록일</th>
	  <th width="80">관리</th>
	</tr>
  </thead>
  <tbody>
<?if($arrList['list']['total'] > 0):?>

<?for ($i=0;$i<$arrList['list']['total'];$i++) {
	//카테고리 정보
	$arrThisCatCode = explode("/", $arrList["list"][$i][cat_code]);

	if($arrList['list'][$i]['image_s']) {
		$simg = "/uploaded/shop_good/".$arrList['list'][$i]['idx']."/".$arrList['list'][$i]['image_s'];
	} else {
		//$simg = "/images/shop/noimg.jpg";
	}
?>
<tr>
	<td><?=$arrList["total"]-$i-$_GET[offset]?></td>
	<td><!-- <a href="/shop.php?goPage=GoodDetail&idx=<?=$arrList['list'][$i]['idx']?>" target="_blank"> --><img src="<?=$simg?>" width="45"></a></td>
	<td class="space-left">
	<?for($j=0; $j <count($arrThisCatCode)-1;$j++){?>
	<!-- <a href="/shop.php?goPage=GoodList&cat_no=<?=$arrThisCatCode[$j]?>" target="_blank"> --><?=$arrAllCategory[$arrThisCatCode[$j]]?></a>
	<?=($j < count($arrThisCatCode)-2)?" ><br>":""?>
	<?}?>
	</td>
	<td class="space-left"><?=stripslashes($arrList['list'][$i]['g_name'])?></td>
	<td><?=$arrList['list'][$i]['isbn']?stripslashes($arrList['list'][$i]['isbn']):number_format($arrList['list'][$i]['price'])?></td>
	<!-- <td>
	<?if($arrList['list'][$i]['stock_type']=="2"):?>
	일반관리<br><?=number_format($arrList['list'][$i]['stock'])?> 개
	<?elseif($arrList['list'][$i]['stock_type']=="3"):?>
	연계관리<br>최소: <?=number_format($arrList['list'][$i]['min_stock'])?>
	<?else:?>
	관리안함
	<?endif;?>
	</td> -->
	<!-- <td><?=$arrList['list'][$i]['main_show']=="Y"?></td> -->
	<td><?=$arrList['list'][$i]['sort_num']?></td>
	<td><?=$arrList['list'][$i]['wdate']?></td>
	<td><a href="good_info.php?idx=<?=$arrList['list'][$i]['idx']?>&listURL=<?=urlencode($_SERVER[REQUEST_URI])?>"><img src="/backoffice/images/k_modify.gif" alt="수정" /></a> <a href="javascript:delGood('<?=$arrList['list'][$i]['idx']?>','<?=$_REQUEST[cat_no]?>','<?=$_REQUEST[cat]?>','<?=$_REQUEST[cat2]?>','<?=$_REQUEST[cat3]?>');"><img src="/backoffice/images/k_delete.gif" alt="삭제" /></a></td>
</tr>
<?}?>

<?else:?>
<tr height="100">
  <td colspan="12" >등록된 상품이 없습니다.</td>
</tr>

<?endif;?>
  </tbody>
</table>

<div class="paginate">
  <?=pageNavigation($arrList['total'],$scale,$pagescale,$_REQUEST[offset],"cat_no=".$_REQUEST[cat_no]."&sw=".$_REQUEST[sw]."&sk=".$_REQUEST[sk])?>
</div>

<form name="frmListHidden" method="post" action="good_evn.php">
<input type="hidden" name="evnMode" value="delete">
<input type="hidden" name="idx" id="g_idx">
<input type="hidden" name="cat_no" id="g_cat_no">
<input type="hidden" name="cat" id="g_cat">
<input type="hidden" name="cat2" id="g_cat2">
<input type="hidden" name="cat3" id="g_cat3">
</form>


<script language="javascript">
<?
for($i=0;$i<count($arrCatCode)-1;$i++){
?>
getCat<?=$i+1?>('<?=$arrCatCode[$i]?>','<?=$arrCatCode[$i+1]?>');
<?
}
?>
</script>
</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:{
					cat : cat,
					mode : "getsub"
				},
				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);
	});
});

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