<?
	require_once("admin__check.php");

	// 获取或设置参数
	$job=trim($job);
	$id=floor(trim($id));

	// 已提交表单 (添加|修改|删除|改变状态)
	if($check=="ok"){	

		if($job==""){
			$flag="error";
			show_msg("错误：未知操作！","-1");
		}
		else{
			if($job=="add" or $job=="modify"){
				$top=floor($top);
				$commend=floor($commend);
				$status=floor($status);
				if(strlen($title)<=0 or strlen($title)>200){
					$flag="error";
					show_msg("错误：没有填写标题，或者标题超过 100 个汉字！","-1");
				}
				if(strlen($content)<10){
					$flag="error";
					show_msg("错误：没有填写内容！","-1");
				}
			}
			else{
				if($id<>""){
					$ids=array($id);
				}
				else{
					$ids=split(",",$ids);
				}
			}
		}

		if($flag<>"error"){

			switch($job){
				case "add":	
					// 添加内容
					$sql="insert into ".TBL_BULLETIN."(title,content,dt,top,commend,status) values('$title','$content','$dt',$top,$commend,$status) ";
					mysql_query($sql);
					$id=mysql_insert_id();
					break;
				case "modify":
					// 修改内容
					$sql="update ".TBL_BULLETIN." set title='$title',content='$content',dt='$dt',top=$top,commend=$commend,status=$status where id=".$id;	
					mysql_query($sql);
					break;
				case "delete":
					// 删除内容
					foreach($ids as $id){
						$sql="update ".TBL_BULLETIN." set status=-1 where id=".$id;
						mysql_query($sql);		
					}
					break;
				default:
					// 改变状态
					foreach($ids as $id){
						$sql="update ".TBL_BULLETIN." set $job=1-$job where id=".$id;
						mysql_query($sql);					
					}		
					break;
			}

			if(mysql_error()<>""){
				show_msg("数据库操作出错，请联系客服！");			
			}
			else{
				if($job=="add" or $job=="modify"){
					show_msg("数据更新成功！","-2");
				}
				else{
					show_msg("数据更新成功！","-1");
				}
			}

		}

	}

	// 未提交表单
	if($check<>"ok"){
		
		$contents=array();
		if($job=="modify"){
			$contents=mysql_fetch_array(mysql_query("select * from ".TBL_BULLETIN." where id=".$id));
			foreach($contents as $key=>$value){
				$$key=$value;
			}
		}
		else{
			$status=1;
		}

?>

<fieldset>公告管理 - 编辑</fieldset>

<div id="toolbar">
	<button class="b1" onclick="go('')">返回公告列表</button>
</div>

<form method="post" onsubmit="return validate(this);">
<table cellspacing='0' cellpadding='3' border='0'>
	<tr>
		<td width="120" align="right">标题：</td>
		<td><input type="text" name="title"	value="<?=$title?>"></td>
	</tr>		
	<tr>
		<td align="right">发布时间：</td>
		<td><input type="text" name="dt"		value="<?=$dt==""?date("Y-m-d H:i:s"):$dt;?>"></td>
	</tr>
	<tr>
		<td align="right">发布状态：</td>
		<td>
			<input type="checkbox" id="top"			name="top"			value="1" <?=floor($top)==0?"":"checked";?>			> <label for="top">置顶显示</label>
			&nbsp;
			<input type="checkbox" id="commend" name="commend"	value="1" <?=floor($commend)==0?"":"checked";?>	> <label for="commend">重点推荐</label>
			&nbsp;&nbsp;
			<input type="checkbox" id="status"	name="status"		value="1" <?=floor($status)==0?"":"checked";?>	> <label for="status">正式发布</label>
		</td>
	</tr>
	<tr>
		<td align="right">正文：</td>
		<td>
			<xmp id="temp_content" style="display:none;"><?=$content?></xmp>
			<textarea id="content" name="content" style="display:none;"></textarea>
			<iframe id="plum" height="300" style='width:590;visibility: inherit;  z-index: 2' scrolling="no" frameborder="no" src="plugin/editor/editor.php"></iframe>
			<script language='javascript'>function doit(){if(plum.Editor){plum.Editor.document.body.innerHTML=temp_content.innerHTML;plum.Editor.document.body.click();}else{setTimeout('doit()',1000);}};if(document.readyStatus='complete'){doit()};</script>		
		</td>
	</tr>
	<tr align="left">
		<td>
			<input type="hidden" name="check" value="ok">
		</td>
		<td>
			<button type="submit">保存</button>
			&nbsp;
			<button type="button" onclick="location.reload()">还原</button>
		</td>
	</tr>
</table>
</form>

<script language="javascript">
function validate(obj)
{
	flag=true;
	if(obj.title.value.length<1)
	{
		flag=false;
		show_tip("请输入标题");
	}
	obj.content.value=plum.Editor.document.body.innerHTML;
	if(obj.content.value.length<1)
	{
		flag=false;
		show_tip("请输入内容");
	}
	return flag;
}
</script>

<?
	}
?>