<?
@header("content-Type:text/html; charset=utf-8");

//error_reporting(0);

include("../../include/common.php");

session_start();

islogin();

$action=$_GET["action"];

//添加产品
if($action=="add")
{
	$title=addslashes($_POST["name"]);
	$category=intval(($_POST["category"]));
	$editor=addslashes($_POST["editor"]);
	$content=$_POST["content"];
	$comment=intval($_POST["comment"]);
	$datetime=$_POST["datetime"];
	
	$rownums=$mysql->rownums("select * from product where title='$name'");
	if($rownums)
	{
		die("sorry,有重复的标题");
	}
	
	//echo "ok";
	require_once("../uploadfiles.php");
	
	
	//插入数据库
	$sql="insert into product (title,time,bigclassid,content,iscomment,editor,attach) value ('$title','$datetime',$category,'$content',$comment,'$editor','$fileid')";
	
	//echo $sql;
	
	$mysql->query($sql);
	
	$aid=intval($mysql->insert_id());
	
	$mysql->query("update attachment set aid=$aid where id in ($fileid)");
	
	@header("location:../pmanage.php");
}

if($action=="del")
{
	$id=intval($_GET["id"]);
	if($id)
	{
		$mysql->query("delete from product where id=$id");
		$mysql->query("select * from attachment where aid=$id");
		while($arr=$mysql->fetcharray())
		{
			if(file_exists(ROOT.$arr["attach"]))
			{
				unlink(ROOT.$arr["attach"]);
			}
		}
		$mysql->query("delete from attachment where aid=$id");
		redirect();
	}
	else
	{
		die("错误参数");
	}
}

if($action=="dodel")
{
	$id=$_POST["dodel"];
	foreach($id as $key=>$value)
	{
		$value=intval($value);
		if($value)
		{
			$mysql->query("delete from product where id=$value");
			$mysql->query("select * from attachment where aid=$value");
			while($arr=$mysql->fetcharray())
			{
				if(file_exists(ROOT.$arr["attach"]))
				{
					unlink(ROOT.$arr["attach"]);
				}
			}
			$mysql->query("delete from attachment where aid=$value");
			redirect();
		}
		else
		{
			die("错误参数");
		}
	}
}

if($action=="edit")
{
	$id=intval($_POST["aid"]);
	$title=addslashes($_POST["name"]);
	$category=intval(($_POST["category"]));
	$editor=addslashes($_POST["editor"]);
	$content=$_POST["content"];
	$comment=intval($_POST["comment"]);
	$datetime=$_POST["datetime"];
	
	//echo "ok";
	require_once("../uploadfiles.php");
	
	
	//判断是否重新上传附件
	if($attach!=null)
	{
	//删除以前附件
	$mysql->query("select * from attachment where aid=$aid");
	while($arr=$mysql->fetcharray())
	{
		if(file_exists(ROOT.$arr["attach"]))
		{
			unlink(ROOT.$arr["attach"]);
		}
	}
	//删除以前附件数据库信息
	$mysql->query("delete from attachment where aid=$aid");
	
	//更新数据库
	$sql="update product set title='$title',time='$datetime',bigclassid=$category,content='$content',iscomment=$comment,editor='$editor',attach='$fileid' where id=$aid";
	
	//echo $sql;
	
	$mysql->query($sql);
	
	$mysql->query("update attachment set aid=$aid where id in ($fileid)");
	
	}
	else
	{
		$sql="update product set title='$title',time='$datetime',bigclassid=$category,content='$content',iscomment=$comment,editor='$editor' where id=$aid";
		$mysql->query($sql);
		
	}
	
	//echo $sql;
	@header("location:../pmanage.php");
	
}

//批量移动
if($action=="move")
{
	//echo "ok";
	
	$bigclassid=intval($_POST["category"]);
	$aid=$_POST["dodel"];
	if($bigclassid && is_int($bigclassid))
	{
		foreach($aid as $key=>$value)
		{
			$value=intval($value);
			if(is_int($value))
			{
				$mysql->query("update product set bigclassid=$bigclassid where id=$value");
			}
			else
			{
				die("参数错误");
			}
		}
	}
	redirect();
	
	
}
$mysql->close();
?>