<?php
	session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	
	if ($_GET['ls_msg']==""){
		$ls_msg="产品类别管理操作";
	}else{
		$ls_msg=$_GET['ls_msg'];	
	}

	$ls=$_GET['ls'];
	$id=$_GET['id'];
	$nownum=$_GET['Num'];
	$whichcate=$_GET['whichcate'];

	
	
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	
	/**************** 更新添加连接的状态 ****************/
	switch($_POST['ls']){
		case 'upadd':
			$add_prolinkid = $_POST['add_prolinkid'];
			$add_prolink_sql = 'UPDATE `ls_products` SET `addProLink`=NULL';
			if($opendb->query($add_prolink_sql)){
				$add_prolink_sql = 'UPDATE `ls_products` SET `addProLink`=1 WHERE `ID`='.$add_prolinkid;
				if($opendb->query($add_prolink_sql)){exit;}
			}
			break;
		case 'deladd':
			$del_prolink_sql = 'UPDATE `ls_products` SET `addProLink`=NULL';
			if($opendb->query($del_prolink_sql)){exit;}
			break;
		case 'upaddnew':
			$add_newprolinkid = $_POST['add_newprolinkid'];
			$add_prolink_sql = 'UPDATE `ls_products` SET `addNewProLink`=NULL';
			if($opendb->query($add_prolink_sql)){
				$add_prolink_sql = 'UPDATE `ls_products` SET `addNewProLink`=1 WHERE `ID`='.$add_newprolinkid;
				if($opendb->query($add_prolink_sql)){exit;}
			}
			break;
		case 'deladdnew':
			$del_newprolink_sql = 'UPDATE `ls_products` SET `addNewProLink`=NULL';
			if($opendb->query($del_newprolink_sql)){exit;}
			break;
	}
	/************************************************************************/
	switch ($ls)
	{
		case  "sort":
			$operationId=$_GET['opid'];
			$targetId=$_GET['tgid'];
			
			$operationSql="select ID,Num from ls_products where ID=".$operationId;
			$targetSql="select ID,Num from ls_products where ID=".$targetId;
			$operationQuery=$opendb->query($operationSql);
			$targetQuery=$opendb->query($targetSql);
			$operationArray=mysql_fetch_assoc($operationQuery);
			$targetArray=mysql_fetch_assoc($targetQuery);
			$operationNum=$targetArray['Num'];
			$targetNum=$operationArray['Num'];
			$operationUpdateSql="update ls_products set Num=".$operationNum." where ID=".$operationId;
			$targetUpdateSql="update ls_products set Num=".$targetNum." where ID=".$targetId;
			$updateQuery=$opendb->query($operationUpdateSql);
			$updateQuery=$opendb->query($targetUpdateSql);
			
			$ls_msg="排序操作成功！";
			break;
	 	case "unenable":
			$unenable_sql="update ls_products set enable=0 where id=".$id." ";
			//echo $unenable_sql;
			$unenable_query=$opendb->query($unenable_sql);
			$ls_msg="不显示操作成功！";
			break;
		case "enable":
			$enable_sql="update ls_products set enable=1 where id=".$id." ";
			//echo $enable_sql;
			$enable_query=$opendb->query($enable_sql);
			$ls_msg="显示操作成功！";
			break;
		case 'scan':
			scancates();
			$ls_msg="扫描与修复完成！";
			break;
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/admin.css" rel="stylesheet" type="text/css">
<?php
	/**************** 创建添加连接脚本用到的JS变量 ****************/
	$add_prolink_sql = 'SELECT `ID` FROM `ls_products` WHERE `addProLink`=1';
	$add_prolink=$opendb->query($add_prolink_sql);
	if(mysql_num_rows($add_prolink)>0){
		if($add_prolink=mysql_fetch_array($add_prolink)){
			echo '<script type="text/javascript">';
			echo 'var add_prolink_ID='.$add_prolink['ID'];
			echo '</script>';
		}
	}
	$add_newprolink_sql = 'SELECT `ID` FROM `ls_products` WHERE `addNewProLink`=1';
	$add_newprolink=$opendb->query($add_newprolink_sql);
	if(mysql_num_rows($add_newprolink)>0){
		if($add_newprolink=mysql_fetch_array($add_newprolink)){
			echo '<script type="text/javascript">';
			echo 'var add_newprolink_ID='.$add_newprolink['ID'];
			echo '</script>';
		}
	}
	/************************************************************************/
?>
<script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/ls.js"></script>
<script type="text/javascript" src="js/add_prolink.js"></script>
<title>无标题文档</title>
</head>
<body>
<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
	<tr>
		<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>产品类别管理</strong></font></div></td>
	</tr>
	<tr>
		<td height="213" valign="top" bgcolor="#dee7ff"><br>
			<table border="0" width="90%" align="center">
				<tr>
					<td width="32%">提示：<font color="#FF0000"><?php echo $ls_msg; ?></font></td>
					<td align="right" width="30%"><img src="images/shao.gif"><a href="cate.php?ls=scan"><u>重新排序</u></a></td>
					<td align="right" width="16%"><img src="images/shao.gif">&nbsp;<a href="addcate.php"><u>添加大类</u></a></td>
					<td align="right" width="22%"><input  type="button" style="cursor:hand;color:#0000FF;" value="生成全部产品页" onclick="location='temp_index.php?ls=products&productsall=ok'"></td>
				</tr>
			</table>
			<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
				<tr bgcolor="#B9DAFF">
					<td align="center" height="25" width="7%">序号</td>
					<td align="center" height="25" width="50%">类别名称 （鼠标经过时可绑定 到主菜单 Products 项）</td>
					<td align="center" colspan="2">排序</td>
					<td align="center" colspan="3">操作选项</td>
					<td align="center" width="60">批量导入</td>
					<td align="center" width="60">是否显示</td
						>
				</tr>
				<?php
						$i=1;
							$cate_sql="select * from ls_products where subcateName is null and subcateFolders is null and subcateBrief is null and subcateID is null and productPic is null and productItemNo is null and productName is null and midcateName is null and productSpecification is null and productID is null order by Num asc";
							$cate_query=$opendb->query($cate_sql);
							$cate_myrows=mysql_num_rows($cate_query); 
							
							//缓存一个用于指示排序参数的数组
							if($cate_myrows!=0){
								$topSign=0;
								$cateNumArray=array();
								while($cateArray=mysql_fetch_assoc($cate_query)){
									if($topSign==0){
										$prevNumId=$cateArray['ID'];
										$topSign++;
										continue;
									}
									$cateNumArray[$prevNumId]['down']=$cateArray['ID'];
									$cateNumArray[$cateArray['ID']]['up']=$prevNumId;
									$prevNumId=$cateArray['ID'];
									$topSign++;
								}
								mysql_data_seek($cate_query,0);		//缓存结束，重置指针
							}
							
							while($cate_rows=mysql_fetch_assoc($cate_query))
							 {
							  echo "<tr bgcolor='#A4B6D7'";
							echo "onMouseOver=";
								echo " 	this.style.backgroundColor='#CCFFCC' ";
								echo "onMouseOut=";
								echo " 	this.style.backgroundColor='#A4B6D7' ";
								echo ">";
							  echo "	<td  align=\"center\">";
							  echo 			"B-".$cate_rows['Num'];
							  echo "	</td>";
							  echo "	<td rel={$cate_rows[ID]}>";
							  echo "		<img src='images/tree_cate.gif'>";
							  echo 			$cate_rows['cateName'];
							  echo "	</td>";
							  echo "	<td align='center'> ";
							  if($i!=1)
							  {
								echo "		<a href='?ls=sort&opid=".$cate_rows['ID']."&tgid=".$cateNumArray[$cate_rows['ID']]['up']."' title='向上移动'><img src='images/up.gif' border='0'></a>";
								}
								echo "  </td>";
								echo "	<td align='center'> ";
								if($i!=$cate_myrows)
							  {
								echo "	 <a href='?ls=sort&opid=".$cate_rows['ID']."&tgid=".$cateNumArray[$cate_rows['ID']]['down']."' title='向下移动'><img src='images/down.gif' border='0'></a>";
								}
								
								$i++;
								echo "  </td>";
								echo "	<td align='center'> ";
								if($cate_rows['enable']==0)
								{
								echo "		<img src='images/b_add.gif' border='0'>";
								}
								if($cate_rows['enable']==1)
								{
								echo "		<a href='addmidcate.php?id=".$cate_rows['ID']." ' title='添加产品中类'><img src='images/b_add.gif' border='0'></a>";
								}
								echo "  </td>";
								echo "	<td align='center'> ";
								echo "		<a href='modicate.php?id=".$cate_rows['ID']."' title='编辑'><img src='images/b_edit.png' border='0'></a>";
								echo "  </td>";
								echo "	<td align='center'> ";
								
								$midcate_sql="select * from ls_products where cateName is null and cateFolders is null and cateBrief is null and subcateName is null and subcateFolders is null and subcateBrief is null and subcateID is null and productPic is null and productItemNo is null  and productName is null and productSpecification is null and productID is null and midcateID=".$cate_rows['ID']." order by Num asc";
								$midcate_query=$opendb->query($midcate_sql);
								$midcate_myrows=mysql_num_rows($midcate_query); 
								if($midcate_myrows==0)
								{
									$midcate2_sql="select * from ls_products where productID=".$cate_rows['ID']." order by Num desc";
									$midcate2_query=$opendb->query($midcate2_sql);
									$midcate2_myrows2=mysql_num_rows($midcate2_query);
									if($midcate2_myrows2==0)
									{
									echo "		<a href='savecate.php?id=".$cate_rows['ID']."&ls=del' onclick='return ConfirmDelCate();' title='删除'><img src='images/b_drop.png' border='0'></a>";
									}
									else
									{
									echo "		<img src='images/b_undrop.png' border='0' alt='大类下面有产品。&#10请先删除产品。'>";
									}
								}
								else
								{
								echo "		<img src='images/b_undrop.png' border='0' alt='大类下面还有中类。&#10请先删除中类。'>";
								}
								
								echo "  </td>";
								echo "	<td align='center'>";
						  
								  if($midcate_myrows==0)
									{
										echo "<a href=batchadd.php?id=".$cate_rows['ID']."&name=".urlencode($cate_rows['cateName'])."&ls=batchadd title='导入'><=</a>";
									}
															
								echo "	</td>";
								echo "	<td align='center'>";
						  
								  if($cate_rows['enable']==1)
									{
										echo "<a href='?id=".$cate_rows['ID']."&ls=unenable' title='显示'><img src='images/enable.gif' border='0'></a>";
									}
								  if($cate_rows['enable']==0)
								  	{
										echo "<a href='?id=".$cate_rows['ID']."&ls=enable' title='不显示'><img src='images/unenable.gif' border='0'></a>";
									}
															
								echo "	</td>";
							  echo "</tr>";
							  
							  
							  if($cate_rows['enable']==1)
									{
													$j=1;
													
													//缓存一个中类的用于指示排序参数的数组
													if($midcate_myrows!=0){
														$topSignMid=0;
														$midCateNumArray=array();
														while($midCateArray=mysql_fetch_assoc($midcate_query)){
															if($topSignMid==0){
																$prevNumIdMid=$midCateArray['ID'];
																$topSignMid++;
																continue;
															}
															$midCateNumArray[$prevNumIdMid]['down']=$midCateArray['ID'];
															$midCateNumArray[$midCateArray['ID']]['up']=$prevNumIdMid;
															$prevNumIdMid=$midCateArray['ID'];
															$topSignMid++;
														}
														mysql_data_seek($midcate_query,0);		//缓存结束，重置指针
													}
									
										  			while($midcate_rows=mysql_fetch_assoc($midcate_query))
													  {
															  echo "<tr bgcolor='#f4f4f4'";
															echo "onMouseOver=";
																echo " 	this.style.backgroundColor='#CCFFCC' ";
																echo "onMouseOut=";
																echo " 	this.style.backgroundColor='#f4f4f4' ";
																echo ">";
							 									echo "	<td  align=\"center\">";
							 									echo 			" M-".$midcate_rows['Num'];
							  									echo "	</td>";
															  echo "	<td style='padding-left:25px;' rel={$midcate_rows[ID]}>";
															  echo "		<img src='images/tree_cate.gif'>";
															  echo 			$midcate_rows['midcateName'];
															  echo "	</td>";
															  echo "	<td align='center'> ";
															  if($j!=1)
															  {
																echo "		<a href='?ls=sort&opid=".$midcate_rows['ID']."&tgid=".$midCateNumArray[$midcate_rows['ID']]['up']."' title='向上移动'><img src='images/up.gif' border='0'></a>";
																}
																echo "  </td>";
																echo "	<td align='center'> ";
															if($j!=$midcate_myrows)
															  {
																echo "	 <a href='?ls=sort&opid=".$midcate_rows['ID']."&tgid=".$midCateNumArray[$midcate_rows['ID']]['down']."' title='向下移动'><img src='images/down.gif' border='0'></a>";
																}
																$j++;

																echo "  </td>";
																echo "	<td align='center'> ";
																echo "		<a href='addsubcate.php?id=".$midcate_rows['ID']." ' title='添加产品小类'><img src='images/b_add.gif' border='0'></a>";
																echo "  </td>";
																echo "	<td align='center'> ";
																echo "		<a href='modimidcate.php?id=".$midcate_rows['ID']."' title='编辑'><img src='images/b_edit.png' border='0'></a>";
																echo "  </td>";
																echo "	<td align='center'> ";
																
																$subcate_sql="select * from ls_products where cateName is null and cateFolders is null and cateBrief is null and midcateName is null and midcateID is null and productPic is null and productItemNo is null and productName is null and productSpecification is null and productID is null and subcateID=".$midcate_rows['ID']." order by Num asc";
																$subcate_query=$opendb->query($subcate_sql);
																$subcate_myrows=mysql_num_rows($subcate_query); 
																 
																
						
																if($subcate_myrows==0)
																{
																	$subcate2_sql="select * from ls_products where productID=".$midcate_rows['ID']." order by Num desc";
																	$subcate2_query=$opendb->query($subcate2_sql);
																	$subcate_myrows2=mysql_num_rows($subcate2_query);
																	if($subcate_myrows2==0)
																	{
																	echo "		<a href='savemidcate.php?id=".$midcate_rows['ID']."&ls=del' onclick='return ConfirmDelCate();' title='删除'><img src='images/b_drop.png' border='0'></a>";
																	}
																	else
																	{
																	echo "		<img src='images/b_undrop.png' border='0' alt='中类下面有产品。&#10请先删除产品。'>";
																	}
																}
																else
																{
																echo "		<img src='images/b_undrop.png' border='0' alt='中类下面还有小类。&#10请先删除小类。'>";
																}

																echo "  </td>";
								echo "	<td align='center'>";
						  
								  if($subcate_myrows==0)
									{
										echo "<a href=batchadd.php?id=".$midcate_rows['ID']."&name=".urlencode($midcate_rows['midcateName'])."&ls=batchadd title='导入'><=</a>";
									}
															
								echo "	</td>";
																echo "	<td align='center'>";

															  if($midcate_rows['enable']==1)
																{
																echo "<a href='?id=".$midcate_rows['ID']."&ls=unenable' title='显示'><img src='images/enable.gif' border='0'></a>";
																}
															  if($midcate_rows['enable']==0)
															  	{
																echo "<a href='?id=".$midcate_rows['ID']."&ls=enable' title='不显示'><img src='images/unenable.gif' border='0'></a>";
																}
															
																echo "	</td>";
																  echo "</tr>";
								
															  

																if($cate_rows['enable']==1)
																{
																	$k=1;
																	
																	//缓存一个小类的用于指示排序参数的数组
																	
																	if($subcate_myrows!=0){
																		$topSignSub=0;
																		$subCateNumArray=array();
																		while($subCateArray=mysql_fetch_assoc($subcate_query)){
																			if($topSignSub==0){
																			   $prevNumIdSub=$subCateArray['ID'];
																			   $topSignSub++;
																			   continue;
																			}
																			
																			$subCateNumArray[$prevNumIdSub]['down']=$subCateArray['ID'];
																			$subCateNumArray[$subCateArray['ID']]['up']=$prevNumIdSub;
																			$prevNumIdSub=$subCateArray['ID'];
																			$topSignSub++;
																		}
																		mysql_data_seek($subcate_query,0);		//缓存结束，重置指针
																	}
																	
														  			while($subcate_rows=mysql_fetch_assoc($subcate_query))
																	  {
																		  echo "<tr bgcolor='#f4f4f4'";
																		echo "onMouseOver=";
																		echo " 	this.style.backgroundColor='#CCFFCC' ";
																		echo "onMouseOut=";
																		echo " 	this.style.backgroundColor='#f4f4f4' ";
																		echo ">";
							  											echo "	<td  align=\"center\">";
							  											echo 			"S-".$subcate_rows['Num'];
							  											echo "	</td>";
																		  echo "	<td style='padding-left:35px;' rel={$subcate_rows[ID]}>";
																		  echo "		&nbsp; <img src='images/tree_cate.gif'>";
																		  echo 			$subcate_rows['subcateName'];
																		  echo "	</td>";
																		  echo "	<td align='center'> ";
																		   if($k!=1)
															 				 {
																			echo "		<a href='?ls=sort&opid=".$subcate_rows['ID']."&tgid=".$subCateNumArray[$subcate_rows['ID']]['up']."' title='向上移动'><img src='images/up.gif' border='0'></a>";
																			}
																			echo "  </td>";
																			echo "	<td align='center'> ";
																			if($k!=$subcate_myrows)
															 				 {
																			echo "	 <a href='?ls=sort&opid=".$subcate_rows['ID']."&tgid=".$subCateNumArray[$subcate_rows['ID']]['down']."' title='向下移动'><img src='images/down.gif' border='0'></a>";
																			}
																			
																			$k++;
																			echo "  </td>";
																			echo "	<td align='center'> ";
																			echo "";
																			echo "  </td>";
																			echo "	<td align='center'> ";
																			echo "		<a href='modisubcate.php?id=".$subcate_rows['ID']."' title='编辑'><img src='images/b_edit.png' border='0'></a>";
																			echo "  </td>";
																			echo "	<td align='center'> ";
																
																	
																			$products_sql="select * from ls_products where cateName is null and cateFolders is null and cateBrief is null and subcateName is null and subcateFolders is null and subcateBrief is null and subcateID is null and midcateName is null and midcateID is null and productID=".$subcate_rows['ID']." order by Num desc ";													
																			$products_query=$opendb->query($products_sql);
																			$products_num=mysql_num_rows($products_query);
																			if($products_num!=0)
																			{
																				echo "		<img src='images/b_undrop.png' border='0' alt='小类下面还有产品。&#10请先删除产品。'>";
																			}
																			else
																			{
																			  echo "		 <a href='savesubcate.php?id=".$subcate_rows['ID']."&ls=del' onclick='return ConfirmDelSubCate();' title='删除'><img src='images/b_drop.png' border='0'></a>";
																			}
																			echo "  </td>";
								echo "	<td align='center'>";
										echo "<a href=batchadd.php?id=".urlencode($subcate_rows['ID'])."&ls=batchadd title='导入'><=</a>";
								echo "	</td>";
																			echo "	<td align='center'>";
																			  if($subcate_rows['enable']==1)
																			{
																			echo "<a href='?id=".$subcate_rows['ID']."&ls=unenable' title='显示'><img src='images/enable.gif' border='0'></a>";
																			}
																		  if($subcate_rows['enable']==0)
																		  	{
																			echo "<a href=batchadd?id=".$subcate_rows['ID']."&name=".$subcate_rows['subcateName']."&ls=enable title='不显示'><img src='images/unenable.gif' border='0'></a>";
																			}
																			echo "	</td>";
																		  echo "</tr>";
															  
													}
						
						
						
									}
															  
															  
															  
							}
			}
							
							  
							  
} 
						
								?>
			</table>
			<br></td>
	</tr>
</table>
<br>
<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
	<tr>
		<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>操作说明</strong></font></div></td>
	</tr>
	<tr>
		<td valign="top" bgcolor="#dee7ff"><br>
			<table border="0" width="90%" align="center">
				<tr>
					<td><ul>
							<li> <img src="images/b_add.gif"> 添加产品小类 <img src="images/b_unadd.gif"> 不能添加产品小类 </li>
							<li> <img src="images/b_edit.png"> 修改/编辑 </li>
							<li> <img src="images/b_drop.png"> 删除 <img src="images/b_undrop.png"> 不能删除 </li>
							<li> &lt;=  &nbsp;批量导入产品图片 </li>
							<li> <img src="images/enable.gif"> 显示<br>
								<img src="images/unenable.gif"> 不显示 大类不显示后，大类下的小类及产品都不显示。 </li>
							<li> 扫描并纠正类别错误将自动将无法正常排序的类别顺序重置。您可以在纠正操作完成后重新调整类别顺序。 </li>
						</ul></td>
				</tr>
			</table>
			<br></td>
	</tr>
</table>
</body>
</html>