<?php
	include_once("check_login.php");
	include_once("include/config.php");
	$action = trim($_GET["action"]);
	if($action){
		check_site_url();
		$new_big_arr = $_POST["new_big"];
		$new_small_arr = $_POST["new_small"];
		$t_name_arr = $_POST["t_name"];
		$t_order_arr = $_POST["t_order"];
		//先查看有无新加的大分类
		if(is_array($new_big_arr)){
			foreach($new_big_arr as $key => $val){
				$val = trim($val);
				if(strlen($val) < 1)
					continue;
				else{
					$val = addslashes($val);
					$filter_arr[0] = array("1",$val,false);
					if($hm_arr = get_product_t($filter_arr))
						show_err("系统中已有[$val]分类");
					else{
						mysql_query("insert into hmcss_product_type (t_name) values ('$val')")
							or show_err("添加新大分类出错，请联系管理员");
						header("Location:set_product_type.php");
					}
				}
			}
		}
		unset($new_big_arr);
		//查有无小分类添加
		if(is_array($new_small_arr)){
			foreach($new_small_arr as $key => $val){
				if(is_array($val)){
					foreach($val as $key_s => $val_s){
						$val_s = trim($val_s);
						if(strlen($val_s) < 1)
							continue;
						else{
							$val_s = addslashes($val_s);
							$filter_arr[0] = array("1",$val_s,false);
							if($hm_arr = get_product_t($filter_arr))
								show_err("系统中已有[$val_s]分类");
							else{
								mysql_query("insert into hmcss_product_type (f_id,t_name) values ($key,'$val_s')")
									or show_err("添加新小分类出错，请联系管理员");
								header("Location:set_product_type.php");
							}
						}
					}
				}	
			}
		}
		unset($new_small_arr);
		if(is_array($t_name_arr)){
			//更新排序数字
			foreach($t_order_arr as $key => $val){
				if(check_is_numb($val))
					mysql_query("update hmcss_product_type set t_order = $val where t_id = $key")
						or show_err("更新分类排序出错，请联系管理员");
				else
					show_err("排序只能是数字");
			}
			//更新分类名
			foreach($t_name_arr as $key => $val){
				$val = trim($val);
				if(strlen($val) > 1){
					$val = addslashes($val);
					mysql_query("update hmcss_product_type set t_name = '$val' where t_id = $key")
						or show_err("更新分类名称出错，请联系管理员");
				}
			}
		}
		unset($t_name_arr,$t_order_arr);
	}
?>
<!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">
$(function(){
	$(".set_main_type li").find("input:text").parent().bind({
		mouseover:function(){
			$(this).find("a").css("display","");
		},
		
		mouseout:function(){
			$(this).find("a").css("display","none");
		}
	});
}); 
function add_big_type(obj)
{
	$(obj).parent().after($(obj).parent().clone());
	$(obj).parent().html("<input name=\"new_big[]\" type=\"text\" id=\"new_big[]\" maxlength=\"50\" style=\"margin-top:4px; width:150px;\" />");
}

function add_small_type(obj,f_id)
{
	$(obj).parent().after($(obj).parent().clone());
	$(obj).parent().html("<span class=\"type_span\"></span><input name=\"new_small[" + f_id + "][]\" type=\"text\" id=\"new_small[" + f_id + "][]\" maxlength=\"50\" style=\"margin-top:4px; width:150px;\" />");
}

function del_type(obj)
{
	if(confirm("确定要删除该分类吗"))
		window.location.href = "DEL.php?type=type&id=" + obj;
	else
		return;
}
</script>
<style type="text/css">
.type_span{display:inline-block; width:100px; background-image:url(pics/type_line.gif); background-repeat:no-repeat; height:30px; float:left; margin-left:20px;}
.set_main_type{width:800px; margin:20px auto;}
.set_main_type ul{list-style:none; padding:0px; margin:0px;}
.set_main_type li{list-style:none; width:800px; height:35px; line-height:35px; float:left;}
</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><span class="head_menu_on"><a href="set_product_type.php">产品分类</a></span><a href="set_product_img.php">产品图片</a><a href="set_img_upload.php">图片上传</a></div>
<div class="set_main_out">
  <form id="form1" name="form1" method="post" action="set_product_type.php?action=save">
    <div class="set_main_type">
      <ul>
      <?php
      	$filter_arr[0] = array("2",0,false);
		if($hm_arr = get_product_t($filter_arr)){
			foreach($hm_arr as $key => $val){
				echo "<li><input name=\"t_order[$val[0]]\" type=\"text\" id=\"t_order[$val[0]]\" maxlength=\"2\" style=\"margin-top:2px; width:30px;\" value=\"$val[3]\" />&nbsp;&nbsp;<input name=\"t_name[$val[0]]\" type=\"text\" id=\"t_name[$val[0]]\" maxlength=\"50\" style=\"margin-top:2px; width:150px;\" value=\"$val[2]\" />&nbsp;&nbsp;<a id=\"add_big_type\" class=\"del_style\" style=\"width:50px; display:none;\" onclick=\"del_type($val[0])\">删除</a></li>\n\r";
				//取子分类
				$filter_arr[0] = array("2",$val[0],false);
				if($hm_t_arr = get_product_t($filter_arr)){
					foreach($hm_t_arr as $key_t => $val_t)
						echo "<li><span class=\"type_span\"></span><input name=\"t_order[$val_t[0]]\" type=\"text\" id=\"t_order[$val_t[0]]\" maxlength=\"2\" style=\"margin-top:2px; width:30px;\" value=\"$val_t[3]\" />&nbsp;&nbsp;<input name=\"t_name[$val_t[0]]\" type=\"text\" id=\"t_name[$val_t[0]]\" maxlength=\"50\" style=\"margin-top:2px; width:150px;\" value=\"$val_t[2]\" />&nbsp;&nbsp;<a id=\"add_big_type\" class=\"del_style\" style=\"width:50px; display:none;\" onclick=\"del_type($val_t[0])\">删除</a></li>\n\r";
				}
				echo "<li><span class=\"type_span\"></span><a id=\"$val[0]\" class=\"do_style\" onclick=\"add_small_type(this,$val[0])\">增加小类</a></li>\n\r";
			}
		}
	  ?>
        <li><a id="add_big_type" class="do_style" onclick="add_big_type(this)">增加大类</a></li>
        <li><input type="submit" name="button" id="button" value="  提 交  " /></li>
      </ul>
    </div>
  </form>
</div>
</body>
</html>