<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="admin.css" rel="stylesheet" type="text/css">
<title>无标题文档</title>
</head>
<body>
<?php
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');

	$opendb=new mydb();
	require_once('inc/char_set.php');

	$ls=$_POST['ls'];
	$id=$_POST['id'];
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$BigClassName=$_POST['BigClassName'];
	$MiddleClassName=$_POST['MiddleClassName'];
	$SmallClassName=$_POST['SmallClassName'];
	$showonhome=$_POST['showonhome'];
//	$memberPrice=$_POST['memberPrice'];
//	$normalPrice=$_POST['normalPrice'];
//	$agentPrice1=$_POST['agentPrice1'];
//	$agentPrice2=$_POST['agentPrice2'];

	$pic=$_POST['Pic'];

	$cateid=$_GET['cateid'];
	$midcateid=$_GET['midcateid'];
	$subcateid=$_GET['subcateid'];

	
	if($SmallClassName=="")
	{
		if($MiddleClassName=="")
		{		
			$productID=$BigClassName;
			$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
			$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
			$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders'];
			$productPID=$select_BigClassName_rows['ID'];// 为产生顺序号Num采集变量
		}
		else
		{
			$productID=$BigClassName;
			$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
			$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
						
			$productID=$MiddleClassName;
			$select_MiddleClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_MiddleClassName_query=$opendb->query($select_MiddleClassName_sql);
			$select_MiddleClassName_rows=mysql_fetch_assoc($select_MiddleClassName_query);
			$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders']."/".$select_MiddleClassName_rows['midcateFolders'];
			$productPID=$select_MiddleClassName_rows['ID'];

		}
    }
	else
	{

		
		$productID=$BigClassName;
		$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
		$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
						
		$productID=$MiddleClassName;
		$select_MiddleClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_MiddleClassName_query=$opendb->query($select_MiddleClassName_sql);
		$select_MiddleClassName_rows=mysql_fetch_assoc($select_MiddleClassName_query);
		
		$productID=$SmallClassName;	
		$select_SmallClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_SmallClassName_query=$opendb->query($select_SmallClassName_sql);
		$select_SmallClassName_rows=mysql_fetch_assoc($select_SmallClassName_query);
		
		$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders']."/".$select_MiddleClassName_rows['midcateFolders']."/".$select_SmallClassName_rows['subcateFolders'];
		$productPID=$select_SmallClassName_rows['ID'];

	}
	 
	$productItemNo=$_POST['productItemNo'];
	$productName=$_POST['productName'];
	$productSpecification=$_POST['productSpecification'];
	
	$image=$_FILES['productPic']['name'];   
  	$size=$_FILES['productPic']['size'];   
  	$type=$_FILES['productPic']['type'];
	switch ($ls)
	{
		case "insert":
			
			if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" )
				{
				 if($size < $PicSize) //开始判断图像大小
					{
						$pathBigPic=$pathFoldersProducts."/".$BigFoldersProducts;
						//echo $pathBigPic."<br>";
						//创建大图文件夹
						createFolder($pathBigPic);
						$pathSmallPic=$pathFoldersProducts."/".$SmallFoldersProducts;
						//echo $pathSmallPic."<br>";
						//创建小图文件夹
						createFolder($pathSmallPic);
						$pathPagePic=$pathFoldersProducts."/".$showproductsFolders;
						//echo $pathSmallPic."<br>";
						//创建大图浏览（.htm）文件夹
						createFolder($pathPagePic);
															
						$arr=array(" ",".");   
						//产生文件名   
						$productPic=str_replace($arr ,"",time()).".".substr($image,-3);
													  	
						$pathBigPic=$pathBigPic."/".$productPic;
						move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						$toW=$BigPicW;			//宽度
						$toH=$BigPicH;			//高度
						$toFile=$pathBigPic;		//目标地址
						$srcFile=$pathBigPic;	//目标地址

						ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size

						$srcFile=$pathBigPic; //源地址
						$toW=$SmallPicW;			//宽度
						$toH=$SmallPicH;			//高度
						$toFile=$pathSmallPic."/".$productPic; //目标地址
						ImageResize($srcFile,$toW,$toH,$toFile);
						$select_sql2="select * from ls_products where productID=".$productPID." ";
						$select_query2=$opendb->query($select_sql2);
						$select_rows2=mysql_num_rows($select_query2); //总产品数
//						$insert_sql=sprintf("insert into ls_products 															(productPic,productItemNo,productName,productSpecification,productID,Num,showOnHome,enable,normalPrice,memberPrice,agentPrice1,agentPrice2) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
						$insert_sql=sprintf("insert into ls_products 															(productPic,productItemNo,productName,productSpecification,productID,Num,showOnHome,enable) values (%s,%s,%s,%s,%s,%s,%s,%s) ",
						GetSQLValueString($productPic, "text"),
						GetSQLValueString($productItemNo, "text"),
						GetSQLValueString($productName, "text"),
						GetSQLValueString($productSpecification, "text"),
						GetSQLValueString($productID, "int"),
						GetSQLValueString($select_rows2+1, "int"),
						GetSQLValueString($showonhome, "int"),
						GetSQLValueString("1", "int")

						);
						//echo $insert_sql;
						//添加数据
						$insert_query=$opendb->query($insert_sql);
						writelog($_SERVER['PHP_SELF'],"add products: ".$productName,"ID: ".$productID);
														
						echo "
						<form name=\"fm\" method=\"post\" action=\"addproducts.php\">
						<input type=\"hidden\" value=\"".$BigClassName."\" name=\"BigClassName\">
						<input type=\"hidden\" value=\"".$SmallClassName."\" name=\"SmallClassName\">
						<input type=\"hidden\" value=\"".$MiddleClassName."\" name=\"MiddleClassName\">
						<input type=\"hidden\" value=\"".$productItemNo."\" name=\"productItemNo\">
						<input type=\"hidden\" value=\"".$productName."\" name=\"productName\">
						<input type=\"hidden\" value=\"". htmlspecialchars($productSpecification)."\" name=\"productSpecification\">
						<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 width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">
					 	<tr bgcolor=\"#B9DAFF\"> 
						<td height=\"28\" colspan=\"2\"></td>
						 </tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">所属类别：</td>
						<td>
						";
						if($select_BigClassName_rows['cateName']!="")
						{
						echo " 大类：".$select_BigClassName_rows['cateName'];
						}
						if($select_MiddleClassName_rows['midcateName']!="")
						{
						echo "  中类：".$select_MiddleClassName_rows['midcateName'];
						}
						if($select_SmallClassName_rows['subcateName']!="")
						{
						echo "  小类：".$select_SmallClassName_rows['subcateName'];
						}
						echo "</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品编号：</td>
						<td>
						".$productItemNo."
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品名称：</td>
						<td>
						".$productName."
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" >*缩略图：</td>
						<td style=\"padding-left:5px;\" >
						<img src=\" ".$toFile." \" width=\"".$toW."\" height=\"".$toH."\" border=\"1\" >
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品规格：</td>
						<td>
						".$productSpecification."
						</td>
						 </tr>
						<tr bgcolor=\"#B9DAFF\"> 
						<td height=\"28\" colspan=\"2\" align=\"center\">
						<input type=\"submit\" value=\"继续添加\">
						<input type=\"button\" value=\"返回列表\" onclick=\"javascript:location='products.php';\">
						</td>
						 </tr>
						</table>
						</td>
						</tr>
						</table>
						</form>
						";
										
					}						
						else
						{
							echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
						}
				
			 }
			else
				{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
				}
		break;
		
	case "update":
		if($ChangecatePagePic=="yes")
	  	{
	  	//判断图像格式
	  		if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" )
				{
					if($size < $PicSize) //开始判断图像大小
						{
							$pathBigPic=$pathFoldersProducts."/".$BigFoldersProducts;
							//echo $pathBigPic."<br>";
							$pathSmallPic=$pathFoldersProducts."/".$SmallFoldersProducts;
							
							$oldbigpic=$pathBigPic."/".$pic;
							unlink($oldbigpic);		//delete old big pic
							
							$oldsmallpic=$pathSmallPic."/".$pic;
							unlink($oldsmallpic);	//delete old small pic

															
							$arr=array(" ",".");   
							//产生文件名   
							$pic=substr($pic,0,(strlen($pic)-4));
							//echo $pic;
							$productPic=$pic.".".substr($image,-3);
												  														  	
							$pathBigPic=$pathBigPic."/".$productPic;
							//echo $pathBigPic;
							//上传文件
							move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
							$toW=$BigPicW;			//宽度
							$toH=$BigPicH;			//高度
							$toFile=$pathBigPic;		//目标地址
							$srcFile=$pathBigPic;	//目标地址

							ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size

													  	
							$srcFile=$pathBigPic; //源地址
							$toW=$SmallPicW;			//宽度
							$toH=$SmallPicH;			//高度
							$toFile=$pathSmallPic."/".$productPic; //目标地址
													  	//生成缩略图
							ImageResize($srcFile,$toW,$toH,$toFile);			
				        }
						else
						{
							echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
							break;
						}
					}
					else
					{
						echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
						break;
					}
					
					$update_pic_sql=sprintf("update ls_products set productPic=%s where ID=%s",
					GetSQLValueString($productPic, "text"),
					GetSQLValueString($id, "int")
					);
				//更新产品图片
					$update_query=$opendb->query($update_pic_sql);
				} 
	  			     	
				$update_sql=sprintf("update ls_products set productItemNo=%s, productName=%s, productSpecification=%s,  showOnhome=%s     where ID=%s",
				GetSQLValueString($productItemNo, "text"),
				GetSQLValueString($productName, "text"),
				GetSQLValueString($productSpecification, "text"),
				GetSQLValueString($showonhome, "int"),
				
				GetSQLValueString($id, "int")
				);
				//echo $update_sql;
				//更新数据
				$update_query=$opendb->query($update_sql);
				
				//更新类别
				$update_sql="";
				$products_sql="select * from ls_products where ID=".$id." ";
				$products_query=$opendb->query($products_sql);
				$products_rows=mysql_fetch_assoc($products_query);
				$oldfolder=getproductfolder($products_rows['productID']);
				if($BigClassName!=""&&$MiddleClassName==""&&$SmallClassName=="")
				{
					if($BigClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$BigClassName." where ID=".$id." ";
					$newfolder=getproductfolder($BigClassName);
					}
				}
				if($BigClassName!=""&&$MiddleClassName!=""&&$SmallClassName=="")
				{
					if($MiddleClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$MiddleClassName." where ID=".$id." ";
					$newfolder=getproductfolder($MiddleClassName);
					}
				}
				if($BigClassName!=""&&$MiddleClassName!=""&&$SmallClassName!="")
				{
					if($SmallClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$SmallClassName." where ID=".$id." ";
					$newfolder=getproductfolder($SmallClassName);
					}
				}
				echo $update_sql;
				if($update_sql!=""){$products_query=$opendb->query($update_sql);}
				if($products_query)//移动产品目录
				{
					if(!file_exists ("../".$newfolder."/".$SmallFoldersProducts)){mkdir ("../".$newfolder."/".$SmallFoldersProducts);}
					if(!file_exists ("../".$newfolder."/".$BigFoldersProducts)){mkdir ("../".$newfolder."/".$BigFoldersProducts);}
					if(!file_exists ("../".$newfolder."/".$showproductsFolders)){mkdir ("../".$newfolder."/".$showproductsFolders);}

					copy("../".$oldfolder."/".$SmallFoldersProducts."/".$products_rows['productPic'], "../".$newfolder."/".$SmallFoldersProducts."/".$products_rows['productPic']);
					copy("../".$oldfolder."/".$BigFoldersProducts."/".$products_rows['productPic'], "../".$newfolder."/".$BigFoldersProducts."/".$products_rows['productPic']);
					unlink ("../".$oldfolder."/".$SmallFoldersProducts."/".$products_rows['productPic']);
					unlink ("../".$oldfolder."/".$BigFoldersProducts."/".$products_rows['productPic']);


				}
				writelog($_SERVER['PHP_SELF'],"change products: ".$productName,"ID: ".$id);

				echo "<script>alert('数据更新成功!\\n请刷新本页！');location='modiproducts.php?id=".$id."&cateid=".$cateid."&midcateid=".$midcateid."&subcateid=".$subcateid."';</script>";
		break;
	}
?>
</body>
</html>
