<?php
	include_once("check_login.php");
	include_once("include/config.php");
	$p_id = trim($_GET["p_id"]);
	if(check_is_numb($p_id) && $p_id > 0){
		$filter_arr[0] = array("0",$p_id,false);
		if(!$hm_arr = get_product($filter_arr))
			show_err("系统中没有对应的产品");
	}
	else
		$hm_arr = "";
	$action = trim($_GET["action"]);
	if($action == "save"){
		check_site_url();
		array_pop($_POST);//表单提交上来的数据都存入$_POST数组中，现在弹出最后一个数组值，因为最后一个是按纽
		extract($_POST);//把数组名作为变量名，数组值作为变量值
		//先检查所有排序号是否为数字
		foreach($pic_order as $key => $val){
			if(!check_is_numb($val))
				show_err("排序号只能是数字");
		}
		asort($pic_order);//按值进行排序,并保持索引
		if($hm_arr[0][8])
			strpos($hm_arr[0][8],"|") ? $pic_arr = explode("|",$hm_arr[0][8]) : $pic_arr[0] = $hm_arr[0][8];
		//先设置主图
		$main_pic = check_is_numb($p_id) ? $pic_arr[$main_pic] : "";
		$pic_str = "";
		foreach($pic_order as $key => $val)
			$pic_str .= $pic_arr[$key] . "|";
		$pic_str = trim($pic_str,"|");
		mysql_query("update hmcss_product set p_main_pic = '$main_pic',p_pics = '$pic_str' where p_id = $p_id")
			or show_err("更改产品图片出错，请联系管理员");
		unset($hm_arr,$pic_arr,$pic_str);
		show_err("产品图片已成功更改","set_product_img.php?p_id=$p_id");
	}
	else{
		//构建所有产品下拉框
		$p_str = "";
		$p_str = "<select name=\"p_id\" id=\"p_id\">\n\r";
		$p_str .=" <option value=\"0\" selected=\"selected\">请选择……</option>\n\r";
		if($p_arr = get_product()){
			foreach($p_arr as $key => $val)
				$p_str .=" <option value=\"$val[0]\">$val[3]</option>\n\r";
		}
		$p_str .=" </select>";
	}
?>
<!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 language = "javascript" src="include/jquery.min.js" type="text/javascript"></script>
<script language = "javaScript" src = "include/hmcss_ajax.js" type="text/javascript"></script>
<script language = "javaScript" src = "inc/admin.js" type="text/javascript"></script>
<script language="javascript">
$(function(){
	$("#floading").css("display","none");
	var args = getArgs( );
	if(args["p_id"])
		$("#p_id").val(args["p_id"]);
	$("#p_id").bind({
		change:function(){
			location.href = "set_product_img.php?p_id=" + $(this).val();
			$("#floading").css("display","");
		}
	});
}); 
</script>
<style type="text/css">
.set_main_pics{width:840px; height:auto; margin:0px auto;}
.img_div{width:200px; height:180px; margin:5px; float:left;}
.img_span{display:block; margin:0px; padding:0px; float:left; height:150px; width:200px;}
.order_span{width:200px; display:block; margin:0px; padding:0px; height:30px; line-height:30px; text-align:center; background-color:#C8E2FB;}
</style>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<div class="head_menu"><a href="set_product_list.php">产品列表</a><a href="set_product_new.php">添加产品</a><a href="set_product_type.php">产品分类</a><span class="head_menu_on"><a href="set_product_img.php">产品图片</a></span><a href="set_img_upload.php">图片上传</a></div>
<div style="width:800px; height:30px; margin:20px auto; padding:0px; line-height:30px; text-align:center;">请选择产品：&nbsp;&nbsp;<?php echo $p_str; ?>&nbsp;&nbsp;<span id="floading" style="height:30px; line-height:30px; width:200px; color:#F06;">正在查找，请稍等...</span></div>
<div class="set_main_out">
  <form id="form1" name="form1" method="post" action="set_product_img.php?action=save&p_id=<?php echo $p_id; ?>">
    <div class="set_main_pics">
      <?php
			if($hm_arr[0][8]){
				strpos($hm_arr[0][8],"|") ? $pic_arr = explode("|",$hm_arr[0][8]) : $pic_arr[0] = $hm_arr[0][8];
        		foreach($pic_arr as $key_pic => $val_pic){
					if($val_pic){
						echo "<div class=\"img_div\">";
						$bg_color = ($hm_arr[0][7] == $val_pic)? "#FBE1F3" : "#E1F0FD";
						$chose_str = ($hm_arr[0][7] == $val_pic)? "checked=\"checked\"" : "";
						echo "<span class=\"img_span\" style=\"background:url(../product/small/$val_pic) center no-repeat $bg_color;\"></span>\n";
						echo "<span class=\"order_span\"><input type=\"radio\" name=\"main_pic\" id=\"main_pic\" value=\"$key_pic\" title=\"设为主图\" $chose_str />&nbsp;&nbsp;&nbsp;&nbsp;<input name=\"pic_order[$key_pic]\" type=\"text\" id=\"pic_order[$key_pic]\" style=\"width:40px;\" maxlength=\"2\" value=\"" . ($key_pic+1) . "\" title=\"图片显示顺序\" />&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=\"if(confirm('确定要删除该图片吗?')) location.href='DEL.php?type=p_pic&p_str=$val_pic&id=$p_id'; else return false;\" class=\"do_style\" style=\"width:50px; height:20px; margin-bottom:4px; padding-bottom:2px;\">删除</a></span>\n";
						echo "</div>\n";
					}
				}
				echo "<div style=\"width:840px; margin:20px auto; text-align:center; clear:both;\">\n";
				echo "<input type=\"submit\" name=\"button\" id=\"button\" value=\"  提 交  \" />\n";
				echo "</div>\n";
			}
			else
				echo " <div style=\"width:840px; margin:20px auto; text-align:center;\">目前该产品没有图片,<a onclick=\"location.href='set_img_upload.php'\" class=\"do_style\">点击这里上传</a></div>";
		?>
    </div>
  </form>
</div>
</body>
</html>