<?php
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
	header("Cache-Control: no-cache, must-revalidate"); 
	header("Pragma: no-cache");
	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');
		
	//如果点击保存按钮
	if(isset($_POST['save'])){
		//获取一共有几条记录，因为下标从1开始，所以要加1。
		$nums = $_POST['nums']+1;
		for($i=1;$i<$nums;$i++){
			$IDs = $_POST["IDs{$i}"];
			$No = $_POST["No{$i}"];
			($_POST["AdditionalAttributes{$i}"]=='')?$AdditionalAttributes=2:$AdditionalAttributes=0;
			$enable = $_POST["enable{$i}"];
			$update_sql = "UPDATE ls_products SET No={$No}, AdditionalAttributes={$AdditionalAttributes}, enable={$enable} WHERE ID={$IDs}";
			$opendb->query($update_sql);
		}
		unset($IDs,$AdditionalAttributes,$No,$enable);
	}
	//查询在首页显示的所有产品
	$products_sql="SELECT * FROM ls_products WHERE AdditionalAttributes&2 ORDER BY No";
	$query = $opendb->query($products_sql);
	$query_row = mysql_num_rows($query);
?>

<!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" />
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<title>首页推荐产品管理</title>
<link type="text/css" rel="stylesheet" href="css/admin.css" />
<script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{padding:10px 30px;}
a{color:#000;text-decoration:none;}
a:hover{color:#076ac5;}
table{border-collapse:collapse;border-spacing:0;width:100%;border:1px solid #fff;}
tr{background:#f4f4f4;}
.tr1{background:#b9daff;}
th,td{padding:3px 5px;border:1px solid #fff;}
</style>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
	<input name="nums" type="hidden" value="<?php echo $query_row //用来记录一共有几条数据，数据库更新语句会用到。11行 ?>" />
	<table class="home_pro_list">
		<tr style="background:url(images/admin_bg.gif)">
			<th colspan="8">首页推荐产品排序设置</th>
		</tr>
		<tr>
			<td colspan="8">
				提示：<strong style="color:#f00" class="message"></strong>
			</td>
		</tr>
		<tr class="tr1">
			<th>产品缩略图</th>
			<th>产品编号</th>
			<th>产品名称</th>
			<th>首页显示位置</th>
			<th>所属类别</th>
			<th>操作选项</th>
			<th>是否显示</th>
		</tr>
	<?php
		$x=1;//用来给隐藏字段的名称后面加一个数字，做到区分不同的隐藏字段，因为提交的时候是将所有的隐藏字段一起提交，所以必须做下区分。
		while($query_array = mysql_fetch_assoc($query)){		
	?>
			<tr>
				<input type="hidden" name="IDs<?php echo $x ?>" value="<?php echo $query_array['ID'] ?>" />
				<td align="center"><img class="thumbnail" rel="<?php echo $query_array['productPic'] ?>" src="" height="120" border="1" /></td>
				<td><?php echo $query_array['productItemNo'] ?></td>
				<td><?php echo $query_array['productName'] ?></td>
				<td><input type="text" size="6" name="No<?php echo $x?>" value="<?php echo $query_array['No'] ?>" /></td>
				<td>
					<?php
						$search_catename = "SELECT * FROM ls_products WHERE ID={$query_array[productID]}";
						$catename_query = mysql_fetch_assoc($opendb->query($search_catename));
						if(!$catename_query['subcateName']){
							if(!$catename_query['midcateName']){
								//只有大类
								echo "<a href=\"products.php?cateid={$catename_query[ID]}&catename={$catename_query[cateName]}\" title=\"点击查看大类下所有产品\" rel=\"{$catename_query[cateFolders]}\">{$catename_query[cateName]}</a>";
							}else{
								//有大类、有中类
								//获得关于中类的记录
								$search_catename2 = "SELECT * FROM ls_products WHERE ID={$catename_query[midcateID]}";
								$catename_query2 = mysql_fetch_assoc($opendb->query($search_catename2));
								
								echo "<a href=\"products.php?cateid={$catename_query2[ID]}&catename={$catename_query2[cateName]}\" title=\"点击查看大类下所有产品\">{$catename_query2[cateName]}</a> &raquo; ";
								echo "<a href=\"products.php?cateid={$catename_query2[ID]}&catename={$catename_query2[cateName]}&midcateid={$catename_query[ID]}&midcatename={$catename_query[midcateName]}\" title=\"点击查看中类下所有产品\" rel=\"{$catename_query2[cateFolders]}/{$catename_query[midcateFolders]}\">{$catename_query[midcateName]}</a>";
							}
						}else{
							//有大类、有中类、有小类
							//获得关于中类的记录
							$search_catename2 = "SELECT * FROM ls_products WHERE ID={$catename_query[subcateID]}";
							$catename_query2 = mysql_fetch_assoc($opendb->query($search_catename2));
							
							//获得关于大类的记录
							$search_catename3 = "SELECT * FROM ls_products WHERE ID={$catename_query2[midcateID]}";
							$catename_query3 = mysql_fetch_assoc($opendb->query($search_catename3));
							
							echo "<a href=\"products.php?cateid={$catename_query3[ID]}&catename={$catename_query3[cateName]}\" title=\"点击查看大类下所有产品\">{$catename_query3[cateName]}</a> &raquo; ";
							echo "<a href=\"products.php?cateid={$catename_query3[ID]}&catename={$catename_query3[cateName]}&midcateid={$catename_query2[ID]}&midcatename={$catename_query2[midcateName]}\" title=\"点击查看中类下所有产品\">{$catename_query2[midcateName]}</a> &raquo; ";
							echo "<a href=\"products.php?cateid={$catename_query3[ID]}&catename={$catename_query3[cateName]}&midcateid={$catename_query[ID]}&midcatename={$catename_query[midcateName]}&subcateid={$catename_query[ID]}&subcatename={$catename_query[subcateName]}\" title=\"点击查看中类下所有产品\" rel=\"{$catename_query3[cateFolders]}/{$catename_query2[midcateFolders]}/{$catename_query[subcateFolders]}\">{$catename_query[subcateName]}</a>";
						}
					?>
				</td>
				<td><input type="checkbox" name="AdditionalAttributes<?php echo $x ?>" value="0"  id="show<?php echo $x ?>" /> <label for="show<?php echo $x ?>">取消首页显示</label></td>
				<td align="center">
					<input type="hidden" rel="enable" name="enable<?php echo $x++ ?>" value="<?php echo $query_array['enable'] ?>" />
					<?php
						if($query_array['enable']){
					?>
							<a href='#' rel="enable" title='显示'><img src='images/enable.gif' border='0'></a>
					<?php
						}else{
					?>
							<a href='#' rel="enable" title='不显示'><img src='images/unenable.gif' border='0'></a>
					<?php
						}
					?>
				</td>
			</tr>
		<?php } ?>
		<script type="text/javascript">
			$(function(){
				var enable = "images/enable.gif",unenable = "images/unenable.gif";
				$("a[rel='enable']").click(function(){
					if($(this).find("img:eq(0)").attr("src").match(/\/enable/)){
						$(this).find("img:eq(0)").attr("src",unenable);
						$(":hidden[rel='enable']:eq("+$(this).index("a[rel='enable']")+ ")").val(0);
						$(".message").text($(this).parent().siblings().filter(":eq(3)").text()+"不显示操作成功！");//消息提示框
					}else{
						$(this).find("img:eq(0)").attr("src",enable);
						$(":hidden[rel='enable']:eq("+$(this).index("a[rel='enable']")+ ")").val(1);
						$(".message").text($(this).parent().siblings().filter(":eq(3)").text()+"显示操作成功！");//消息提示框
					}
				})
				//表格行，鼠标经过变色
				$(".home_pro_list tr:gt(2)").not(".home_pro_list tr:last").hover(function(){
					$(this).css({background:"#ccffcc"});
				},function(){
					if(!$(this).data("bg")){
						$(this).css({background:"#f4f4f4"});
					}
				}).click(function(event){
					$(".home_pro_list tr:gt(2)").not(".home_pro_list tr:last").css({background:""}).data("bg",0);
					$(this).css({background:"#ccffcc"}).data("bg",1);
					event.stopPropagation();//只停止冒泡而不停止默认动作，否则单选框会出错
				})
				//点击表格外部，取消选中状态
				$("body").click(function(){
					$(".home_pro_list tr:gt(2)").not(".home_pro_list tr:last").css({background:"#f4f4f4"}).data("bg",0);
				})
				//获取图片显示路径
				$(".home_pro_list tr:gt(2)").not(".home_pro_list tr:last").each(function(){
					$(this).find(".thumbnail").attr("src","../"+$("td:eq(4) a:last",this).attr("rel")+"/thumbnails/"+$(this).find(".thumbnail").attr("rel"));
				})
			})
		</script>
		<tr class="tr1">
			<td colspan="6">
				最多可在首页设置显示 <strong style="color:red"><?php echo $proNum ?></strong> 张图片 | 目前已设置 <strong style="color:#076ac5"><?php echo $query_row ?></strong> 张 [<?php echo $query_row ?>/<?php echo $proNum ?>] | 还可设置 <strong style="color:#f60"><?php echo $proNum-$query_row ?></strong> 张
			</td>
			<td align="right" colspan="2"><input type="submit" name="save" value="保存"></td>
		</tr>
	</table>
</form>
</body>
</html>