<?php
	require_once('_approot.php');
	
	$admininfo = $admin->getInfo();
	
	if ($_GET['ls_msg']==""){
		$ls_msg="管理操作";
	}else{
		$ls_msg=$_GET['ls_msg'];	
	}
	$ls=$_GET['ls'];
	$id=$_GET['id'];
	$productName=$_GET['productName'];
	$perpage=$_POST['perpage']!=""?$_POST['perpage']:$_SESSION['perpage'];
	$_SESSION['perpage']=$perpage;
	
	$sortpro=$_GET['sortpro'];
	switch ($sortpro)
	{
	 	case "enable":
			$sortstr=" order by enable ASC"; $ls_msg.=" 禁用产品";
			break;
		
	 	case "date":
			$sortstr=" order by date ASC"; $ls_msg.=" 日期倒序";
			break;
		
	 	case "H":
			$sortstr=" order by AdditionalAttributes=1 DESC"; $ls_msg.=" 首页热点";
			break;

		  
	 	case "C":
			$sortstr=" order by AdditionalAttributesClassPro DESC"; $ls_msg.=" 类别推荐产品";
			break;
		
	 	case "P":
			$sortstr=" order by AdditionalAttributes=2 DESC"; $ls_msg.=" 推荐产品";
			break;
		
	 	case "N":
			$sortstr=" order by AdditionalAttributes=4 DESC"; $ls_msg.=" 新产品";
			break;

		default:
			$sortstr=" order by Num asc";
	
	}
	
	$nownum=$_GET['Num'];
	if($_GET['skipup1']>1){
		$skipup=$_GET['skipup1'];	
	}else{
		$skipup=$_GET['skipup'];	
	}//编号排序时用到的数字索引	升序
	
	if($_GET['skipdown1']>1){
		$skipdown=$_GET['skipdown1'];
	}else{
		$skipdown=$_GET['skipdown']	;
	}//编号排序时用到的数字索引	降序

	//print_r($_GET);
	$cateid=$_GET['cateid'];
	$catename=$_GET['catename'];
	$midcateid=$_GET['midcateid'];
	$midcatename=$_GET['midcatename'];
	$subcateid=$_GET['subcateid'];
	$subcatename=$_GET['subcatename'];
	$productID=$_GET['productID'];
	$searchproductName=trim(($_POST['searchproductName']==""?$_GET['searchproductName']:$_POST['searchproductName']));
	$searchrange=$_POST['searchrange']==""?$_GET['searchrange']:$_POST['searchrange']  ;
	//echo $searchproductName."---".$searchrange;
	//var_dump($HTTP_POST_VARS);
	//修正分类指示
	if(($cateid!="")&&($catename=="")){
		$getNameSql="select cateName from ls_products where ID=".$cateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$catename=$getNameRow['cateName'];
	}
	if(($midcateid!="")&&($midcatename=="")){
		$getNameSql="select midcateName from ls_products where ID=".$midcateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$midcatename=$getNameRow['midcateName'];
	}
	if(($subcateid!="")&&($subcatename=="")){
		$getNameSql="select subcateName from ls_products where ID=".$subcateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$subcatename=$getNameRow['subcateName'];
	}
	//获取并定义排序用参数
	$sortKind=$_POST['SortKind'];
	$sortMethod=$_POST['SortMethod'];
	$sortIgnoreCate=($_POST['IgnoreCate']=="Ignore")?true:false;
	$sortCateOnly=($_POST['onlyGrandCate']=="OnlyCate")?true:false;
		
	if(isset($_GET['page'])&&$_GET['page']!=0){
	   $page = intval( $_GET['page'] );
	}else{
		$page = 1;
	}
	if($_POST['gopage']!=""){$page =(ctype_digit($_POST['gopage'])?$_POST['gopage']:1);}
		//更新的产品类别指示
		if($subcateid=="" && $midcateid=="" && $cateid==""){
			$ls_msg2="产品管理 >> 所有产品 ";	
		}
		if(($_GET['catename']!="")||($catename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">产品管理</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> 所有产品 "	;
		}
		if(($_GET['midcatename']!="")||($midcatename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">产品管理</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&midcateid=".$midcateid."&midcatename=".$midcatename."\">".(($_GET['midcatename']!="")?$_GET['midcatename']:$midcatename)."</a> >> 所有产品 "	;
		}

		if(($_GET['subcatename']!="")||($subcatename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">产品管理</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&midcateid=".$midcateid."&midcatename=".$midcatename."\">".(($_GET['midcatename']!="")?$_GET['midcatename']:$midcatename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&subcateid=".$subcateid."&subcatename=".$subcatename."\">".(($_GET['subcatename']!="")?$_GET['subcatename']:$subcatename)."</a> >> 所有产品 "	;
		}
	
	switch($ls){
		case "up":
			$productID_sql="select * from ls_products where ID=".$id." ";
			$productID_query=$opendb->query($productID_sql);
			$productID_row=mysqli_fetch_assoc($productID_query);

			$allnum_sql="select * from ls_products where productID=".$productID_row['productID'];
			$allnum_query=$opendb->query($allnum_sql);
			$allnum=mysqli_num_rows($allnum_query);
			
			if($skipup>$nownum-1){
				//如果向上移动的值 大于 当前记录索引-1   则 向上移动的值 = 当前记录索引-1
				$skipup=$nownum-1;
			}
			if($skipup==0){
				//如果向上移动的值为0  则 向上移动的值为1
				$skipup=1;
			}
			
			$up=$nownum-$skipup;
			//echo $skipup."======";
			$up_sql="update ls_products set Num=Num+1 where Num>=".$up." and Num<".$nownum." and productID=".$productID_row['productID'];
			$up_sql2="update ls_products set Num=Num-".$skipup." where ID=".$id;
			$up_query=$opendb->query($up_sql);
			$up_query=$opendb->query($up_sql2);
			$ls_msg="下移操作成功！";		
			break;
		case "down":
			$productID_sql="select * from ls_products where ID=".$id." ";
			$productID_query=$opendb->query($productID_sql);
			$productID_row=mysqli_fetch_assoc($productID_query);

			$allnum_sql="select * from ls_products where productID=".$productID_row['productID'];
			$allnum_query=$opendb->query($allnum_sql);
			$allnum=mysqli_num_rows($allnum_query);
			
			if($skipdown>$allnum-$nownum){
				//如果向下移动的值 大于 总记录数-当前记录索引   则 向下移动的值 = 总记录数-当前记录索引
				$skipdown=$allnum-$nownum;
			}
			//echo $skipdown;exit;
			if($skipdown==0){
				//如果向下移动的值为0  则 向下移动的值为1
				$skipdown=1;
			}
			
			$down=$nownum+$skipdown;

			$down_sql="update ls_products set Num=Num-1 where Num<=".$down." and Num>".$nownum." and productID=".$productID_row['productID'];
			$down_sql2="update ls_products set Num=Num+".$skipdown." where ID=".$id;
			$down_query=$opendb->query($down_sql);
			$down_query=$opendb->query($down_sql2);

			$ls_msg="上移操作成功！";
			break;
		case "reserverthiscate":	
			$productID_sql="select * from ls_products where productID=".$productID." ";
			$productID_query=$opendb->query($productID_sql);
			$productID_num=mysqli_num_rows($productID_query);
			$update_sql="update ls_products set Num=".$productID_num."-Num+1 where productID=".$productID." ";
			$productID_query=$opendb->query($update_sql);

			break;
		case "restcate":
			$productID_sql="select * from ls_products where productID=".$productID." order by Num asc";
			$productID_query=$opendb->query($productID_sql);
			$i=1;
			while($row = mysqli_fetch_assoc($productID_query)){
				$sql = "update ls_products set Num=$i where ID=".$row['ID'];
				$opendb->query($sql);
				$i++;
			}
			$ls_msg="排序完成！";
			break;

		//全新群体排序模块
		case "sorting":
			SortPros($cateid,$midcateid,$subcateid,$sortKind,$sortMethod,$sortIgnoreCate,$sortCateOnly);
			$ls_msg="批量排序完成！";
			break;
	 	case "unenable":
			$unenable_sql="update ls_products set enable=0 where id=".$id." ";
			$unenable_query=$opendb->query($unenable_sql);
			$ls_msg="不显示操作成功！";
			break;
		case "enable":
			$enable_sql="update ls_products set enable=1 where id=".$id." ";
			$enable_query=$opendb->query($enable_sql);
			$ls_msg="显示操作成功！";
			break;
		case "del":
			//获取文件目录，采集序号Num变量
			$subcate_sql="select productID from ls_products where ID=".$id; 
			$subcate_sql="select * from ls_products where ID=(".$subcate_sql.")";
			$subcate_query=$opendb->query($subcate_sql);
			$subcate_myrows=mysqli_fetch_assoc($subcate_query);
			if($subcate_myrows['subcateFolders']!=''){
				//如果产品在三级类下
				$pathFoldersProducts=$subcate_myrows['subcateFolders'];
	
				$productPID=$subcate_myrows['ID'];// 为产生顺序号Num采集变量
				$midcate_sql="select * from ls_products where ID=(".$subcate_myrows['subcateID'].")";
				$midcate_query=$opendb->query($midcate_sql);
				$midcate_myrows=mysqli_fetch_assoc($midcate_query);
				$pathFoldersProducts=$midcate_myrows['midcateFolders']."/".$pathFoldersProducts;
	
				$cate_sql="select * from ls_products where ID=(".$midcate_myrows['midcateID'].")";
				$cate_query=$opendb->query($cate_sql);
				$cate_myrows=mysqli_fetch_assoc($cate_query);
				$pathFoldersProducts="../".$cate_myrows['cateFolders']."/".$pathFoldersProducts;
			}else{
				$midcate_sql="select productID from ls_products where ID=".$id; 
				$midcate_sql="select * from ls_products where ID=(".$midcate_sql.")";
				$midcate_query=$opendb->query($subcate_sql);
				$midcate_myrows=mysqli_fetch_assoc($midcate_query);
				
				if($midcate_myrows['midcateFolders']!=''){
					//如果产品在二级类下
					$pathFoldersProducts=$midcate_myrows['midcateFolders'];
	
					$productPID=$midcate_myrows['ID'];// 为产生顺序号Num采集变量
					$cate_sql="select * from ls_products where ID=(".$midcate_myrows['midcateID'].")";
					$cate_query=$opendb->query($cate_sql);
					$cate_myrows=mysqli_fetch_assoc($cate_query);
					$pathFoldersProducts="../".$cate_myrows['cateFolders']."/".$pathFoldersProducts;
				}else{
					//如果产品在大类下
					$cate_sql="select productID from ls_products where ID=".$id; 
					$cate_sql="select * from ls_products where ID=(".$cate_sql.")";
					$cate_query=$opendb->query($cate_sql);
					$cate_myrows=mysqli_fetch_assoc($cate_query);
					$pathFoldersProducts="../".$cate_myrows['cateFolders'];
					$productPID=$cate_myrows['ID'];// 为产生顺序号Num采集变量
				}
			}
	
			$products_sql="select * from ls_products where ID=".$id;
			$products_query=$opendb->query($products_sql);
			$products_rows=mysqli_fetch_assoc($products_query);
			//获取文件路径
			$Bigpicsrc=$pathFoldersProducts."/".$BigFoldersProducts."/".$products_rows['productPic'];
			$Smallpicsrc=$pathFoldersProducts."/".$SmallFoldersProducts."/".$products_rows['productPic'];
			$pagesrc=$pathFoldersProducts."/".trim(str_replace(substr($products_rows['productPic'],-4),"",$products_rows['productPic'])).".html";
			//获取大图，缩略图，网页文件所在目录
			$Bigpicsrc1=$pathFoldersProducts."/".$BigFoldersProducts;
			$Smallpicsrc1=$pathFoldersProducts."/".$SmallFoldersProducts;
			$pagesrc1=$pathFoldersProducts;
			//删除文件 
			$products_sql="select productID from ls_products where ID=".$id;			
			$products_sql="select * from ls_products where productID=(".$products_sql.")";	
			$products_query=$opendb->query($products_sql);
			$products_num=mysqli_num_rows($products_query);
		  	if($products_num!=1){
				unlink($Bigpicsrc);
				@unlink($Bigpicsrc.".bak");//同时删除大图的备份文件
				unlink($Smallpicsrc);
				@unlink($Smallpicsrc.".bak");//同时删除缩略图的备份文件
				@unlink($pagesrc);
			}else{
				unlink($Bigpicsrc);
				@unlink($Bigpicsrc.".bak");//同时删除大图的备份文件
				unlink($Smallpicsrc);
				@unlink($Smallpicsrc.".bak");//同时删除缩略图的备份文件
				@unlink($pagesrc);
				rmdir($Bigpicsrc1);		//如果是最后一个产品，同时删除大图目录
				rmdir($Smallpicsrc1);	//如果是最后一个产品，同时删除缩略图目录
				rmdir($pagesrc1);		//如果是最后一个产品，同时删除网页文件目录
			}
			//删除数据库中该条产品的记录
			$del_sql="DELETE FROM `ls_products` WHERE ID=".$id;
		   	$del_query=$opendb->query($del_sql);
			//更新剩下的产品序号
			$num_sql2="UPDATE `ls_products` SET Num=Num-1 where productID=".$productPID." and Num>".$products_rows['Num'];
			$down_query=$opendb->query($num_sql2);
			//将操作写入系统日志
			
			//删除属性
			$del_sql="DELETE FROM `ls_prospec` WHERE productID=".$id;
		   	$del_query=$opendb->query($del_sql);

			//删除评论
			$del_comments_sql="DELETE FROM `ls_comments` WHERE proID=".$id;
		   	$del_comments_query=$opendb->query($del_comments_sql);
			
			if($ifmutipic) include("widget/_products_del_muti.php"); // 多角度图片删除

			//生成的产品文件，是否删除。记录 ls_writefiles，上线前默认删除，上线后默认不删除 22-2-24
			//flagdelete("ls_products",$id,$products_rows['filename']);
			writelog($_SERVER['PHP_SELF'],"Delete products: ".$products_rows['productName'],"ID: ".$id);
		   	$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">

<title>无标题文档</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script   language="JavaScript" src="js/ls.js" ></script>
<script   language="JavaScript" src="js/ls2.js" ></script>
<link href="css/admin.css" rel="stylesheet" type="text/css">
 <link rel="stylesheet" href="css/style2.css" />
 <style>
 button {
    padding:5px 15px;
    cursor: pointer;
    background-color: #00c0ef;
    border-color: #00acd6;
    border-radius: 5px;
    border: 1px;
    color: #fff;
}
 </style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td height="18" colspan="2" style="border-bottom:2px solid #00c0ef; padding-bottom:5px;"><div align="center"><font color="#3c8dbc" style="font-size:16px;">管理‘<?=$productName?>’的相关产品</font></div>
				</td>
  </tr>
  <tr>
    <td height="213" colspan="2" valign="top"><table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" >
      
    </table>
      <form name="del" method="Post" action="delall.php" onSubmit="return ConfirmDel();">
        <br />
        <?php
		//大类//////////////////////////////////////////////////////////////////////////
		$cate_sql="select * from ls_products where midcateName is null and midcateID 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 enable=1 ".$sortstr;
		$cate_query=$opendb->query($cate_sql);
		$cate_num=mysqli_num_rows($cate_query); 
		if($cate_num!=0)
		{
			$i=1;
			?>
	 <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" >
        		<tr>
        			<td>
	<?php
			while($cate_rows=mysqli_fetch_assoc($cate_query))
			 {
if($i==1)
{  
    ?> 大类: <?php
	echo "&nbsp;<a href=\"?cateid=".$cate_rows['ID']."&catename=".$cate_rows['cateName']."\" >";
	if($cateid==$cate_rows['ID'])
	{
		echo "<font color=\"#FF0000\"><b>".$cate_rows['cateName']."</b></font>";
	}
	else
	{
	    
		echo $cate_rows['cateName'];
	}
	echo "</a> ";
			 	}
else
{
	echo "| <a href=\"?cateid=".$cate_rows['ID']."&catename=".$cate_rows['cateName']."\" >";
	if($cateid==$cate_rows['ID'])
	{
		echo "<font color=\"#FF0000\"><b>".$cate_rows['cateName']."</b></font>";
	}
	else
	{
		echo $cate_rows['cateName'];
	}
	echo "</a> ";
	}
	$i++;
	}
?>
	 
    | <a href="<?=$_SERVER['PHP_SELF']?>">全部</a>
     
     </td>
	</tr>
	 </table>
<?php
		
		//中类/////////////////////////////////////////////////////////////////////////
		if($cateid!="")
		{
			$midcate_sql="select * from ls_products where cateName is null and cateFolders is null and subcateName is null and subcateID is null and cateBrief is null and productPic is null and productItemNo is null and productName is null and productSpecification is null and productID is null and enable=1 and midcateID=".$cateid." order by Num asc";
			$midcate_query=$opendb->query($midcate_sql);
			$midcate_query2=$opendb->query($midcate_sql);
			$midcate_num=mysqli_num_rows($midcate_query);
			$midcate_id = mysqli_fetch_assoc($midcate_query2);
			if($midcateid == '')
				$midcateid = $midcate_id['ID'];
			if($midcate_num!=0)
			{
			$i=1;
			?>
			 <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
		    	<tr>
		    	<td>
		<?php
		while($midcate_rows=mysqli_fetch_assoc($midcate_query))
		 {
		if($i==1)
		{  
		?>   中类 : 
        <?php
		echo "&nbsp;<a href=\"?midcateid=".$midcate_rows['ID']."&cateid=".$cateid."&catename=".$_GET['catename']."&midcatename=".$midcate_rows['midcateName']."\">";
		$j=0;
		$firstmidcateid=$midcate_rows['ID'];
		if($midcateid==$midcate_rows['ID'])
		{
		echo "<font color=\"#880812\"><b>".$midcate_rows['midcateName']."</b></font>";
		$j++;
	}
	else
	{
		echo $midcate_rows['midcateName'];
	}
			
	echo "</a>";
}
else
{
	echo " | <a href=\"?midcateid=".$midcate_rows['ID']."&cateid=".$cateid."&catename=".$_GET['catename']."&midcatename=".$midcate_rows['midcateName']."\">";
	if($midcateid==$midcate_rows['ID'])
	{
		echo "<font color=\"#880812\"><b>".$midcate_rows['midcateName']."</b></font>";
		$j++;
	}
	else
	{
		echo $midcate_rows['midcateName'];
	}
	echo "</a>";
}
$i++;
			 }
			 if($j==0)
			{
			$midcateid=$firstmidcateid;
			}
			 ?>
			</td>
			</tr>
	    </table>
			<?php
		}
	}
		
			//小类///////////////////////////////////////////////////////////////////////////////
		//echo $cateid."--".$midcateid."--".$subcateid;	
		if($cateid!="" && $midcateid!="")
		{
				$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 enable=1 and subcateID=".$midcateid." order by Num asc";
				$subcate_query=$opendb->query($subcate_sql);
				$subcate_query2=$opendb->query($subcate_sql);
				$subcate_num=mysqli_num_rows($subcate_query); 
				$subcate_id=mysqli_fetch_assoc($subcate_query2);
				if($subcateid =='')
					$subcateid = $subcate_id['ID'];
				if($subcate_num!=0)
				{
					$i=1;
				?>
						<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
		        		<tr>
		        			<td>
				<?php
					while($subcate_rows=mysqli_fetch_assoc($subcate_query))
					 {
						if($i==1)
						{  
						?>   小类 : 
                        <?php
							echo "&nbsp;<a href=\"?subcateid=".$subcate_rows['ID']."&cateid=".$cateid."&catename=".$catename."&midcatename=".$midcatename."&midcateid=".$midcateid."&subcatename=".$subcate_rows['subcateName']."\">";
							$k=0;
							$firstsubcatetid=$subcate_rows['ID'];
							if($subcateid==$subcate_rows['ID'])
							{
								echo "<font color=\"#880812\"><b>".$subcate_rows['subcateName']."</b></font>";
								$k++;
							}
							else
							{
								echo $subcate_rows['subcateName'];
							}
							echo "</a>";
						}
						else
						{
							echo " | <a href=\"?subcateid=".$subcate_rows['ID']."&cateid=".$cateid."&catename=".$catename."&midcatename=".$midcatename."&midcateid=".$midcateid."&subcatename=".$subcate_rows['subcateName']."\">";
							if($subcateid==$subcate_rows['ID'])
							{
								echo "<font color=\"#880812\"><b>".$subcate_rows['subcateName']."</b></font>";
								$k++;
							}
							else
							{
								echo $subcate_rows['subcateName'];
							}
							echo "</a>";
						}
						$i++;
					 }
					 if($k==0)
					 {
					 	$subcatetid=$firstsubcatetid;
					 }
					?>				 			  <a href="addproducts.php" target="right"></a></td>
                          </tr>
	    </table>
				<?php
				}
		}
		
			
?>
        <?php 
	// 每页数量
	$PageSize =  $perpage==0?$PageNum:$perpage;
	
	// 获取总数据量
	/********************************************************************************/
		$sql="select * from ls_products where midcateName is null and 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 ";
	if($admininfo['branchID']==0){
			
		}else{
			$sql .= " and branchID=".$admininfo['branchID']." ";
		}
	//大类
	$sqlID="";
	$displaydownup="";//显示上移下移箭头
	if($cateid!=""  && $midcateid=="" && $subcateid=="" && $searchproductName=="")
	{
		//大类下有中类
		$sqlstr="select ID from ls_products where midcateID =".$cateid." ";
		$query1=$opendb->query($sqlstr);
		$mynum=mysqli_num_rows($query1);
		if($mynum!=0)
		{
			$i=1;
			while($myrows=mysqli_fetch_assoc($query1))			
			{
				$sqlstr2="select ID from ls_products where subcateID =".$myrows['midcateID']." ";
				$query2=$opendb->query($sqlstr2);
				$mynum2=mysqli_num_rows($query2);
				
if($mynum2!=0) //中类下有小类
{
	while($myrows2=mysqli_fetch_assoc($query2))			
	{
		if($i==1)
		{
			$sqlID.="productID=".$myrows2['ID'];
		}
		else
		{
			$sqlID.=" or productID=".$myrows2['ID'];
		}
		$i++;
	}
	
}
else//中类下没小类
{
	if($i==1)
	{
		$sqlID.="productID=".$myrows['midcateID'];
	}
	else
	{
		$sqlID.=" or productID=".$myrows['midcateID'];
	}
	$i++;
}
			
			}
			
		//	echo $productID."<br>";
		$sql.="and （".$sqlID."）";
	  }
	  else
	  {//大类下没有中类
	  	$displaydownup="ok";
	  	$sql.="and productID=".$cateid;
	  }
	}
	
	//中类
	
	if($midcateid!="" && $subcateid=="" && $searchproductName=="")
	{
		//中类下有小类
		$sqlstr="select ID from ls_products where subcateID =".$midcateid." ";
		$query1=$opendb->query($sqlstr);
		$mynum=mysqli_num_rows($query1);
		if($mynum!=0)
		{
			$i=1;
			while($myrows=mysqli_fetch_assoc($query1))
			{
				if($i==1)
				{
					$sqlID.="productID=".$myrows['ID'];
				}
				else
				{
					$sqlID.=" or productID=".$myrows['ID'];
				}
				$i++;
			}
		//	echo $productID."<br>";
		$sql.="and (".$sqlID.")";
	  } else {//中类下没有小类
		  	$sql.="and productID=".$midcateid;
  	  		$displaydownup="ok";
	  }
	}
	
	//小类
	if($subcateid!="" && $searchproductName==""){
		$sql.="and productID=".$subcateid;
		$displaydownup="ok";
	}
		
	$products_sql=$sql; // for products list 查询数据库
		
	//查询数据
	if($searchproductName!=""){
		switch($searchrange){
			case 'productname':
				$sql.="and productName like '%".$searchproductName."%'";
				$searchsql ="and productName like '%".$searchproductName."%'";
				break;
			case 'productno':
				$sql.="and productItemNo like '%".$searchproductName."%'";
				$searchsql ="and productItemNo like '%".$searchproductName."%'";
				break;
			case 'both':
				$sql.="and (productItemNo like '%".$searchproductName."%' or productName like '%".$searchproductName."%' or ID like '%".$searchproductName."%' or productSpecification like '%".$searchproductName."%' or productDescription  like '%".$searchproductName."%' or canshu  like '%".$searchproductName."%' or basicInfo like '%".$searchproductName."%' or additionalInfo  like '%".$searchproductName."%' or productKeywords  like '%".$searchproductName."%' or productTitle  like '%".$searchproductName."%' or parameters  like '%".$searchproductName."%' or advantages  like '%".$searchproductName."%' or characteristics  like '%".$searchproductName."%' or date  like '%".$searchproductName."%' or productVideo  like '%".$searchproductName."%' or wholepath  like '%".$searchproductName."%' or filename like '%".$searchproductName."%'  )";
				$searchsql ="and (productItemNo like '%".$searchproductName."%' or productName like '%".$searchproductName."%'  or ID like '%".$searchproductName."%' or productSpecification like '%".$searchproductName."%' or productDescription  like '%".$searchproductName."%' or canshu  like '%".$searchproductName."%' or basicInfo like '%".$searchproductName."%' or additionalInfo  like '%".$searchproductName."%' or productKeywords  like '%".$searchproductName."%' or productTitle  like '%".$searchproductName."%' or parameters  like '%".$searchproductName."%' or advantages  like '%".$searchproductName."%' or characteristics  like '%".$searchproductName."%' or date  like '%".$searchproductName."%' or productVideo  like '%".$searchproductName."%' or wholepath  like '%".$searchproductName."%' or filename like '%".$searchproductName."%')";
				break;
			default:
				$sql.="and productName like '%".$searchproductName."%'";
				$searchsql ="and productName like '%".$searchproductName."%'";
		}
	}
	
	
	$sql.=$sortstr;	
	 

		$result=$opendb->query($sql);
	
	/********************************************************************************/
	// 获取总数据量
	
	$row = mysqli_num_rows($result);
	$amount = $row;
	
	// 记算总共有多少页
	if( $amount ){
	   if( $amount < $PageSize ){ $page_count = 1; }               //如果总数据量小于$PageSize，那么只有一页
	   if( $amount % $PageSize ){                                  //取总数据量除以每页数的余数
	       $page_count = (int)($amount / $PageSize) + 1;           //如果有余数，则页数等于总数据量除以每页数的结果取整再加一
	   }else{
	       $page_count = $amount / $PageSize;                      //如果没有余数，则页数等于总数据量除以每页数的结果
	   }
	}
	else{
	   $page_count = 0;
	}
	
	// 翻页链接
	$page_string = '';
	$pagecatestr="subcateid=".$subcateid."&midcateid=".$midcateid."&cateid=".$cateid."&catename=".urlencode($catename)."&midcatename=".urlencode($midcatename)."&subcatename=".urlencode($subcatename)."&searchproductName=".urlencode($searchproductName)."&sortpro=".$sortpro; 
	if( $page == 1 ){
	   $page_string .= '首页 | 上一页 |';
	}
	else{
	   $page_string .= '<a href=?&page=1&'.$pagecatestr.'><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?&page='.($page-1)."&".$pagecatestr.'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
	}
	if( ($page == $page_count) || ($page_count == 0) ){
	   $page_string .= ' 下一页 | 尾页';
	}
	else{
	   $page_string .= ' <a href=?&page='.($page+1).'&'.$pagecatestr.'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?&page='.$page_count.'&'.$pagecatestr.'><u><font color=\"#0000FF\">尾页</font></u></a>';
	}
		 $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
		 $page_string .= " &nbsp;&nbsp;<b>".$PageSize."</b>个产品每页";
		 $page_string .= "&nbsp; &nbsp;共 <b style='color:red;'>".$row."</b> 个产品";
	
	
	?>
    <br>
<table border="0" width="98%" align="center">
	<tr>
		<td width="79%"><?=$ls_msg2?></td>
		<td width="21%" align="right">共 <b style='color:red;'><?=$row?></b> 个产品 &nbsp; &nbsp; <?php if(!(strpos($_SERVER['HTTP_REFERER'],"managearticle")===false)){ ?> <a href="managearticle.php">返回</a><?php } ?></td>
	</tr>
</table>
<?php
		 
		/*\***************************************************************************\*/
		/*\查询数据库	\*/
		/*\***************************************************************************\*/ 
//	$sort='Num';
	//查询数据
	if($searchproductName!=""){$products_sql.= $searchsql;}
	$products_sql.=$sortstr." limit ". ($page-1)*$PageSize .",".$PageSize." ";	
	
	//echo $products_sql."<br>";
	$products_query=$opendb->query($products_sql);
		/*\***************************************************************************\*/
		/*\查询数据库	\*/
		/*\***************************************************************************\*/ 			
?>
	<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" style="background-color: #f0f0ee;">
        <tr height="37">
			<td align="center" width="2%"></td>
			<td align="center" width="11%">编号/标志<br />
			  <a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"H":""?>' title='首页热点'>H</a>
               <a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"C":""?>' title='类别代表图'>C</a>
               <a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"P":""?>' title='推荐产品'>P</a>
                <a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"N":""?>' title='新产品'>N</a></td>
			<td align="center"  width="7%">相关产品</td>
			<td align="center"  width="9%">产品名称</td>
            <td align="center"  width="17%">Title</td>
            <td align="center"  width="9%"><a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"date":""?>' title='日期倒序'>日期</a></td>
            <td align="center"  width="20%">Description</td>
			<td align="center" width="7%">图片</td>
			<td align="center" width="3%">序号</td>
			<td align="center" width="3%">点击</td>
			<td align="center" colspan="2">排序</td>
			<td align="center" colspan="2">操作</td>
			<td align="center" width="2%"><a href='products.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"enable":""?>' title='列出禁止'>允许</a></td>
		</tr>
<?php
	
	$i=1;
	while($products_rows=mysqli_fetch_assoc($products_query)){
		
		 	//查询大类
		 	$search_cate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_cate_sql="select midcateID from ls_products where ID=(".$search_cate_sql.")";
			$search_cate_sql="select * from ls_products where ID=(".$search_cate_sql.")";
		 	$search_cate_query=$opendb->query($search_cate_sql);
		 	$search_cate_rows=mysqli_fetch_assoc($search_cate_query);
			
			//查询中类
		 	$search_midcate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_midcate_sql="select * from ls_products where ID=(".$search_midcate_sql.")";
		 	$search_midcate_query=$opendb->query($search_midcate_sql);
		 	$search_midcate_rows=mysqli_fetch_assoc($search_midcate_query);
		
		 	//查询小类
		 	$search_subcate_sql="select * from ls_products where ID = ".$products_rows['productID']."";
		 	$search_subcate_query=$opendb->query($search_subcate_sql);
		 	$search_subcate_rows=mysqli_fetch_assoc($search_subcate_query);
		 	
		 	//echo $search_subcate_sql."<br>";
		if(($search_cate_rows['enable']==1 || $search_cate_rows['enable']=="")&&($search_midcate_rows['enable']==1 || $search_midcate_rows['enable']=="")){
		if($search_subcate_rows['enable']==1){ ?>
	 	  	
			<tr class="preview" bgcolor='#f9f9f9'height='37' 
			onMouseOver="
			this.style.backgroundColor='#e5e5e5';"
			 onMouseOut=
			this.style.backgroundColor='#f9f9f9' 
			>
			<td align='center'>&nbsp;</td>
			<td style='padding-left:10px;'>
			<?php echo $products_rows['productItemNo']; ?>
			
					<br/><span style='color:red'>
                   <?=$products_rows['isbranch']!=0?"M ":"" ?>
                   <?=intval($products_rows['AdditionalAttributes'])&1? "<span title='首页热点'>H</span> ":"" ?>
                   <?=$ls_classproType == "selectPic" && intval($products_rows['AdditionalAttributesClassPro'])&1?"<span title='类别代表图'>C</span> ":"" ?> 
                   <?=intval($products_rows['AdditionalAttributes'])&2?"<span title='推荐产品'>P</span> ":"" ?>
                   <?=intval($products_rows['AdditionalAttributes'])&4?"<span title='新产品'>N</span> ":"" ?>

                    </span>
			 
			</td>
			<td style='padding-left:10px;'><?=$products_rows['ID']?>
              <input name='selectid<?=$i?>' type='checkbox' onclick="unselectall()" id="select_id" value='<?=$products_rows['ID']?>' /></td>
			<td style='padding-left:10px;'>
			<?=$products_rows['productName']?>
			</td>
			<td><a class='proTil' href='javascript:void(0);' title='<?=$products_rows['productTitle']?>' >
			<?=$products_rows['productTitle']?>
			</a></td>
			<td><a class='proKey' href='javascript:void(0);' title='<?=$products_rows['productKeywords']?>' >
			<?=$products_rows['date']?>
			</a></td>
			<td><a class='proDesc' href='javascript:void(0);' title='<?=strip_tags($products_rows['productDescription'])?>' >
			<?=strip_tags($products_rows['productDescription'])?>
			</a></td>
			<td style='padding-left:4px;' align='center'>
             <?php 				$picsrc= $products_rows['wholepathpic']; //  21-7-11 换后台，新传图片优先使用，新图为空，使用老图
				if($picsrc!=NULL){
			?>
				<img src='<?=$_SESSION['dda']?>../<?=$picsrc?>?i=<?=rand()?>' width=<?=($SmallPicW/3)?> height=<?=($SmallPicH/3)?> border='0'>
                
			<?php }else{ ?>
            
            <img src='<?=$_SESSION['dda']?>../<?=getproductfolder($products_rows['productID'])?>/<?=$SmallFoldersProducts?>/<?=$products_rows['productPic']?>?i=<?=rand()?>' width=<?=($SmallPicW/3)?> height=<?=($SmallPicH/3)?> border='0'>
           <?php } ?>

          
			</td>
			<td style='padding-left:4px;' align='center'><?=$products_rows['Num']?>
			</td>
			<td style='padding-left:4px;' align='center'><?=$products_rows['hits']?>
			</td>
			
			<td width="3%" align='center'> 
			<?php if(($i+($PageNum*($page-1)))!=$amount&&$displaydownup=="ok"){ ?>
				<a id='dpp<?=$products_rows['ID']?>' href='?skipdown=1&id=<?=$products_rows['ID']?>&Num=<?=$products_rows['Num']?>&ls=down&page=<?=$page?>&cateid=<?=$cateid?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&sortpro=<?=$sortpro?>' title='向下移动'><img src='images/down.gif' border='0'></a>
			  <input type="text" name="down<?=$products_rows['ID']?>" size="1" maxlength="5" value=1        onblur="document.getElementById('dpp<?=$products_rows['ID']?>').href=document.getElementById('dpp<?=$products_rows['ID']?>').href+'&skipdown1='+this.value">
			<?php } ?>
			</td>
			
			<td width="3%" align='center'> 
			<?php if(($i+($PageNum*($page-1)))!=1&&$displaydownup=="ok"){ ?>
				<a id='upp<?=$products_rows['ID']?>' href='?skipup=1&id=<?=$products_rows['ID']?>&Num=<?=$products_rows['Num']?>&ls=up&cateid=<?=$cateid?>&page=<?=$page?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&sortpro=<?=$sortpro?>' title='向上移动'><img src='images/up.gif' border='0'></a>
				<input type="text" name="up<?=$products_rows['ID']?>" size="1" maxlength="5" value=1          onblur="document.getElementById('upp<?=$products_rows['ID']?>').href=document.getElementById('upp<?=$products_rows['ID']?>').href+'&skipup1='+this.value">
			<?php } ?>
			</td>
			
			<td width="2%" align='center'>
			<?php echo ($i!="1"?"":"! " )?> <a href='modiproducts.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro?>' title='编辑'><img src='images/b_edit.png' border='0'></a>
			</td>
			<td width="2%" align='center'>
			<a href='?id=<?=$products_rows['ID']?>&ls=del&page=<?=$page?>&cateid=<?=$cateid?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro?>' onclick='return ConfirmDelProduct("<?=$products_rows['productItemNo']?>","<?=$products_rows['productName']?>");' title='删除' ><img src='images/b_drop.png' border='0'></a>
			</td>
			<td align='center'>
			  
			<?php if($products_rows['enable']==1){ ?>
				<a href='?id=<?=$products_rows['ID']?>&ls=unenable&page=<?=$page?>&cateid=<?=$cateid?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro?>' title='显示'><img src='images/enable.gif' border='0'></a>
			<?php }else{ ?>
				<a href='?id=<?=$products_rows['ID']?>&ls=enable&page=<?=$page?>&cateid=<?=$cateid?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro?>' title='不显示'><img src='images/unenable.gif' border='0'></a>
			
<?php }?>
		
			</td>
			</tr>
			<?php $productID2=$products_rows['productID'];
		 }
	 }
	$i++;
	
	} ?>
	<tr>
	<td colspan='15' align='center' height='37'>&nbsp;</td>
	</tr>
	 
	</table>
        <table width="98%" border="0" align="center" cellpadding="5" cellspacing="0">
          <tr bgcolor="#f0f0ee">
            <td width="23%" style="padding-left:6px;"><input name="chkAll" type="checkbox" id="chkAll" onClick="CheckAll(this.form);" value="checkbox">
              所有
                <button name="submitdel" type='submit' value='删除'>删除</button>
              <input name="Action" type="hidden" id="Action" value="proDel" ></td>
            <td width="37%" style="padding-left:6px;"><input type="text" name="searchproductName" size="16" maxlength="50"  value="<?=$searchproductName?>" />
              <select name="searchrange" id="searchrange">
                <option value="both">综合查询</option>
                <option value="productname">查找产品名称</option>
                <option value="productno">查找产品编号</option>
              </select>
              <button type="button"  value="查询"   onclick="document.del.action='<?=$_SERVER['PHP_SELF'];?>';document.del.submit();" />查询</button>
              <button name="重置" type="reset"   onclick="this.form.searchproductName.value='';document.del.action='<?=$_SERVER['PHP_SELF'];?>';document.del.submit();"  value="重置" />重置</button></td>
            <td width="24%" style="padding-left:6px;"><input name="gopage" id="gopage" type="text" size="3" />
页
  <button type="button" name="Submit" value="GO"   onclick="document.del.action='<?=$_SERVER['PHP_SELF'];?>';document.del.submit();" />GO</button> &nbsp;  &nbsp; 
每页
<select name="perpage" id="perpage" onchange="document.del.action='<?=$_SERVER['PHP_SELF'];?>';document.del.submit();">
  <option value="12" <?=$perpage==12||$perpage==""?"selected":""?>>12</option>
  <option value="20" <?=$perpage==20?"selected":""?>>20</option>
  <option value="50" <?=$perpage==50?"selected":""?>>50</option>
  <option value="100" <?=$perpage==100?"selected":""?>>100</option>
  <option value="200" <?=$perpage==200?"selected":""?>>200</option>
  <option value="500" <?=$perpage==500?"selected":""?>>500</option>
  <option value="1000" <?=$perpage==1000?"selected":""?> >1000</option>
</select></td>
            <td width="16%" style="padding-left:6px;">&nbsp;</td>
          </tr>
        </table>
      </form></td>
  </tr>
  <tr>
    <td width="1%" height="25" valign="top">&nbsp;</td>
    <td width="99%" align="right" valign="top">标志：H首页图片，C类别页图片，P推荐图片，N新产品</td>
  </tr>
</table>
</body>
</html>
