<?php
include "../../include/conn.php";
include "../../include/function.php";
include "../admin_loginstate.php";
include "../admin_version.php";

//'**************************************************************************************************
//' 软件名称: 搜一次 Content Management System
//' 版本编号: V4.0.0版
//' 官方网站: http://www.syccms.com
//' 官方论坛：http://www.syccms.cn
//' 版权所有: 搜一次运营团队
//'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
//'    2、搜一次网络有限公司保留此软件的法律追究权利
//'**************************************************************************************************
?>
<!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=gb2312" />
<title>站内信管理</title>
<script type="text/javascript" language="javascript">var tentpath='<%=S_SitePath%>';</script>
<script src="../js/jquery.js"></script>
<script src="../js/alerts.js"></script>
<link href="../images/alerts.css" rel="stylesheet" type="text/css" media="screen" />
<link  href="../images/style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
	$(document).ready(function(){
		$("#ChkAll").click(function(){
			if($(this).attr("checked")==true){
				$("input[name='S_ID']").each(function(){
					$(this).attr("checked",true);									 
				})							
			}else{
				$("input[name='S_ID']").each(function(){
					$(this).attr("checked",false);									 
				})							
			}
		});
		$("#SelAll").click(function(){
			if($(this).attr("checked")==true){
				$("input[name='S_Type[]']").each(function(){
					$(this).attr("checked",true);									 
				})							
			}else{
				$("input[name='S_Type[]']").each(function(){
					$(this).attr("checked",false);									 
				})							
			}
		});
		$("#submit1").click(function(){
			var S_Title=$("#S_Title").val();
			var S_Content=$("#S_Content").val();
			$("#S_Title").css("border-color","#666 #ccc #ccc #666");
			$("#S_Content").css("border-color","#666 #ccc #ccc #666");
			if(S_Title==""){
				$("#tishi").text("请您输入短信标题！");
				$("#S_Title").css("border","1px solid red");
				return false;}
			if(S_Content==""){
				$("#tishi").text("请您请输入短信内容！");
				$("#S_Content").css("border","1px solid red");
				return false;}
		});
		
	});
function del(id){
	jConfirm('您确定要删除该短信吗?', '操作提示', function(r) {
		if(r==true){
			window.location='?action=del&S_ID='+id;
			}
				});
	};
function delall(){
	jConfirm('您确定要执行操作吗?', '操作提示', function(r) {
		if(r==true){form2.submit();}
		});
};
function showwb(types){
	if(types==0){
	document.all("rb1")[0].checked=true;
	$("#geren").css("display","block");
	$("#huiyuanzu").css("display","none");
	}
	else{
	document.all("rb1")[1].checked=true;
	$("#geren").css("display","none");
	$("#huiyuanzu").css("display","block");
		}
};
function rebook(id){
	window.location='?action=rebook&S_ID='+id;
	};
function backpage(){
	window.history.go(-1);
	};
</script>
</head>
<body>
<?php
class ui_stationmes{
	public function editboard($ActionUrl,$emValues){
		global $db;
		if(is_array($emValues)){
			$S_ID = $emValues[0];
			$S_Title = $emValues[1];
			$S_Content = $emValues[2];
			$S_Name = $emValues[3];
			$S_Type = $emValues[4];
			$S_CreateTime = $emValues[5];
			$S_Class = $emValues[6];
		}
?>
<form method="post" action="<?php echo $ActionUrl?>">
	<table width="100%" border="0" cellpadding="0" cellspacing="0"  class="tb_style">
    	<tr>
    	  <td colspan="2" class="td_title">&nbsp;会员短信编辑</td>
    	</tr>
    	<tr>
        	<td width="14%" height="30" class="td_border">
            &nbsp;&nbsp;短信标题：
            </td>
            <td width="86%" height="30" class="td_border">
            <input name="S_Title" type="text" id="S_Title" value="<?php echo $S_Title?>" size="30" />
            <font color="#FF0000">＊</font>
            <label id="tishi" style="color:#F00"></label>
            </td>
        </tr>
    	<tr>
        	<td height="30" class="td_border">
            &nbsp;&nbsp;短信内容：
            </td>
            <td height="30" class="td_border">
            <textarea name="S_Content" id="S_Content" style="width:500px;height:100px"><?php echo $S_Content?></textarea>
            </td>
        </tr>
        <tr>
        	<td height="30" class="td_border">
            &nbsp;&nbsp;发送方式：
            </td>
            <td height="30" class="td_border">
            <input type="radio" value="0" name="rb1" id="rb1" class="radio" onfocus="showwb(0)" checked="checked"/>指定会员&nbsp;
            <input type="radio" value="1" name="rb1" id="rb1" class="radio" onfocus="showwb(1)" <?php if(strpos($S_Type,",")!=0){echo("checked='checked'");}?>/>
            群组发送
            </td>
        </tr>
        <tr id="geren">
        	<td height="30" class="td_border">
            &nbsp;&nbsp;会员名称：
            </td>
          <td height="30" class="td_border">
              <input name="S_Name" type="text" id="S_Name" style="width:500px;" value="<?php echo $S_Name?>" />
            <font color="#FF0000">＊</font><br />
          <font color="#FF0000">  注意：多个会员请使用,号间隔分开　如：会员１号,会员２号</font></td>
        </tr>
        <tr id="huiyuanzu" style="display:none">
        	<td height="30" class="td_border">
            &nbsp;&nbsp;会员分类：
            </td>
            <td height="30" class="td_border">
            <?php
			$sqlug="select * from s_usersgroup";
			$resultug=$db->query($sqlug);
			while($rowug=$db->fetch_array($resultug)){
			?>
            <input type="checkbox" name="S_Type[]" id="S_Type" <?php echo isselect($S_Type,$rowug["S_ID"])?> class="checkbox" value="<?php echo $rowug["S_ID"]?>" /><?php echo $rowug["S_Name"]?>
            <?php
			}
			?>
            <input type="checkbox" class="checkbox" id="SelAll" />全选
            </td>
        </tr>
        <tr>
        	<td height="40" class="td_border" align="right">&nbsp;</td>
            <td height="40" class="td_border" align="left"><input type="submit" name="submit1" id="submit1" value="发 送" class="rb1"/>
             &nbsp; <input type="reset" class="rb1" value="重 置" />
              &nbsp; <input type="button" class="rb1" value="返 回" onclick="backpage()" />
          </td>
        </tr>
    </table>
    </label>
    <?php
	if(strpos($S_Type,",")>0){
		echo("<script language='javascript'>showwb(1);</script>");
	}

	}
	
	function lists($sql,$size){
		global $db;
		$Arr=getpagerow($sql,$size);//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$action=SafeRequest("action","get");
		?>
<form id="form2" action="?action=delall" method="post" name="form2">
    <table  width="100%" border="0" cellpadding="0" cellspacing="0" class="tb_style">
    	<tr>
            <td  height="30" colspan="5" class="td_title">&nbsp;<%=P_Title%></td>
        </tr>
        <tr>
            <td width="10%" height="30" bgcolor="#FFFFFF" class="td_btop3">&nbsp;编号</td>
            <td width="23%" height="30" bgcolor="#FFFFFF" class="td_btop3">&nbsp;标题</td>
            <td width="38%" height="30" bgcolor="#FFFFFF" class="td_btop3">&nbsp;短信内容</td>
            <td width="18%" height="30" align="center" bgcolor="#FFFFFF" class="td_btop3">发送时间</td>
            <td width="11%" height="30" align="center" bgcolor="#FFFFFF" class="td_btop3">操作</td>
        </tr>
    <?php
$numdate="a";
if($result){
	while ($row = $db ->fetch_array($result)){
		$numdate="b";
    ?>
    
    <tr>
        <td height="30" class="td_border"><input type="checkbox" value="<?php echo $row["S_ID"]?>" name="S_ID" class="checkbox"/><?php echo $row["S_ID"]?>.</td>
        <td height="30" class="td_border">&nbsp;<a href="?action=edit&S_ID=<?php echo $row["S_ID"]?>"><?php echo $row["S_Title"]?></a></td>
        <td height="30" class="td_border">&nbsp;<?php echo $row["S_Content"]?></td>
        <td height="30" class="td_border" align="center"><?php echo $row["S_CreateTime"]?></td>
        <td height="30" class="td_border" align="center">
        <?php if($action=="list1"){?>
        <a href="?action=readmes&S_ID=<?php echo $row["S_ID"]?>">查看</a>&nbsp;&nbsp;&nbsp;
        <a href="?action=rebook&S_ID=<?php echo $row["S_ID"]?>">回复</a>&nbsp;&nbsp;&nbsp;
        <?php }else{?>
        <a href="?action=edit&S_ID=<?php echo $row["S_ID"]?>">编辑</a>&nbsp;&nbsp;&nbsp;
        <?php }?>
        <a style="cursor:pointer;" onclick="del(<?php echo $row["S_ID"]?>)">删除</a></td>
    </tr>
    <?php
	}
}
if($numdate=="a"){
	echo("<tr><td height='30' align='center' class='td_border' colspan='5'><b>暂无消息！</b></td></tr>");
}
	?>
    <tr>
        <td height="30" bgcolor="#FFFFFF"  colspan="10"><input type="checkbox" name="ChkAll" id="ChkAll" ids="S_ID" class="checkbox"/>
          全选 &nbsp;
        <input type="button" value="删除所选短信" class="rb1" onclick="delall()"/>        </td>
      </tr>
    <tr>
          <td height="40" colspan="10" align="right" valign="bottom" bgcolor="#FFFFFF" class="td_btop3">
          <?php echo $Arr[0];?>
          </td>
      </tr>
    </table>	
</form>
    <?php
	}
	
}
?>
    
    
<?php
$action=SafeRequest("action","get");
$ui=new ui_stationmes;

switch($action){
	case "del" :
		Del();
		break;
	case "delall" :
		DelAll();
		break;
	case "add" :
		Add();
		break;
	case "edit" :
		Edit();
		break;
	case "saveedit" :
		SaveEdit();
		break;
	case "saveadd" :
		SaveAdd();
		break;
	case "list2" :
		List2();
		break;
	case "readmes" :
		ReadMes();
		break;
	case "rebook" :
		ReBook();
		break;
	default:
		Main();
		break;
}

function Main(){
	global $ui;
	$ui->lists("select * from s_stationmes Order by S_ID desc",30);
}
	
function List2(){
	global $ui;
	$ui->lists("select * from s_stationmes where S_Class=0 Order by S_ID desc",30);
}

function Del(){
	global $db;
	$S_ID = SafeRequest("S_ID","get");
	$sql="delete from s_stationmes where S_ID=".$S_ID;
	if($db->query($sql)){
		echo "<script>window.location='admin_stationmes.php'</script>";
		}
	else{
		echo "<script>jAlert('删除失败!','操作提示',function(R){history.back();})</script>";
		}
}

function DelAll(){
	global $db;
	$S_ID=RequestBox("S_ID");
	$S_ID=explode(",",$S_ID);
	for($i=0;$i<count($S_ID);$i++){
		$sql="delete from s_stationmes where S_ID=".$S_ID[$i];
		$db->query($sql);
		}
	echo "<script>window.location='admin_stationmes.php'</script>";
}

function Add(){
	global $ui;
	$arr=array("","","","","","","");
	$ui->editboard("?action=saveadd",$arr);
}

function SaveAdd(){
	global $db;
	$S_Title=SafeRequest("S_Title","post");
	$S_Content=SafeRequest("S_Content","post");
	$S_Name=SafeRequest("S_Name","post");
	$S_Type=RequestBox("S_Type");
	$rb1=SafeRequest("rb1","post");
	$S_CreateTime=date('Y-m-d');
	$S_Class=0;
	$S_Vip="";
	if($rb1==0){
		$S_Vip=$S_Name;
		if($S_Vip==""){
			echo "<script>jAlert('请输入要发送的对象!','操作提示',function(R){history.back();})</script>";
		}
	}
	else{
		if($S_Type==""){
			$S_Vip="";
		}
		else{
			$S_TypeArr=explode(",",$S_Type);
			for($i=0;$i<count($S_TypeArr);$i++){
				$sqlu="select * from s_users where S_Type=".$S_TypeArr[$i];
				$resultu=$db->query($sqlu);
				while($rowu=$db->fetch_array($resultu)){
					$S_Vip=$S_Vip.$rowu["S_Name"].",";
				}
			}
			if($S_Vip==""){
				$S_Vip="";
			}
			else{
			$S_Vip=substr($S_Vip,0,strlen($S_Vip)-1);
			}
		}
	}
	$sql="insert into s_stationmes (S_Title,S_Content,S_Name,S_Type,S_CreateTime,S_Class) values ('".$S_Title."','".$S_Content."','".$S_Vip."','".$S_Type."','".$S_CreateTime."','".$S_Class."')";
	if($db->query($sql)){
		echo "<script>window.location='admin_stationmes.php'</script>";
	}
	else{
		echo "<script>jAlert('短信发送已成功!','操作提示',function(R){history.back();})</script>";
	}
}

function Edit(){
	global $db;
	global $ui;
	$S_ID=SafeRequest("S_ID","get");
	$sql="select * from s_stationmes where S_ID=".$S_ID;
	$result=$db->query($sql);
	$row=$db->fetch_array($result);
	$S_Title = $row["S_Title"];
	$S_Content = $row["S_Content"];
	$S_Name = $row["S_Name"];
	$S_Type = $row["S_Type"];
	$S_CreateTime = $row["S_CreateTime"];
	$S_Class = $row["S_Class"];
	$Arr=array($S_ID,$S_Title,$S_Content,$S_Name,$S_Type,$S_CreateTime,$S_Class);
	$ui->editboard("?action=saveedit&S_ID=".$S_ID,$Arr);
}

function Rebook(){
	global $db;
	global $ui;
	$S_ID=SafeRequest("S_ID","get");
	$sql="select * from s_stationmes where S_ID=".$S_ID;
	$result=$db->query($sql);
	$row=$db->fetch_array($result);
	$S_Title = $row["S_Title"];
	$S_Content = $row["S_Content"];
	$S_Name = $row["S_Name"];
	$S_Type = $row["S_Type"];
	$S_CreateTime = $row["S_CreateTime"];
	$S_Class = $row["S_Class"];
	$S_Title="回复 ".$S_Title;
	$Arr=array($S_ID,$S_Title,"",$S_Name,$S_Type,$S_CreateTime,$S_Class);
	$ui->editboard("?action=saveedit&S_ID=".$S_ID,$Arr);
}

function SaveEdit(){
	global $db;
	$S_ID=SafeRequest("S_ID","get");
	$S_Title=SafeRequest("S_Title","post");
	$S_Content=SafeRequest("S_Content","post");
	$S_Name=SafeRequest("S_Name","post");
	$S_Type=RequestBox("S_Type");
	$rb1=SafeRequest("rb1","post");
	$S_Vip="";
	if($rb1==0){
		$S_Vip=$S_Name;
		if($S_Vip==""){
			echo "<script>jAlert('请输入要发送的对象!','操作提示',function(R){history.back();})</script>";
		}
	}
	else{
		if($S_Type==""){
			$S_Vip="";
		}
		else{
			
			$S_TypeArr=explode(",",$S_Type);	
			for($i=0;$i<count($S_TypeArr);$i++){
				$sqlu="select * from s_users where S_Type=".$S_TypeArr[$i];
				$resultu=$db->query($sqlu);
				while($rowu=$db->fetch_array($resultu)){
					$S_Vip=$S_Vip.$rowu["S_Name"].",";
				}
			}
			if($S_Vip==""){
				$S_Vip="";
			}
			else{
			$S_Vip=substr($S_Vip,0,strlen($S_Vip)-1);
			}
		}
	}
	$sql="update s_stationmes set S_Title='".$S_Title."',S_Content='".$S_Content."',S_Name='".$S_Vip."',S_Type='".$S_Type."' where S_ID=".$S_ID;
	if($db->query($sql)){
		echo "<script>window.location='admin_stationmes.php'</script>";
	}
	else{
		echo "<script>jAlert('短信修改成功!','操作提示',function(R){history.back();})</script>";
	}
}

function ReadMes(){
	global $db;
	$S_ID=SafeRequest("S_ID","get");
    $sql="select * from s_stationmes where S_ID=".$S_ID;
    $result=$db->query($sql);
    $row=$db->fetch_array($result);
	?>
    	<table width="100%" border="0" cellpadding="0" cellspacing="0"  class="tb_style">
    	<tr>
    	  <td colspan="2" class="td_title">&nbsp;会员短信编辑</td>
    	</tr>
    	<tr>
        	<td width="14%" height="30" class="td_border">
            &nbsp;&nbsp;标题：
            </td>
            <td width="86%" height="30" class="td_border">
            <label><?php echo $row["S_Title"]?></label>
            <input type="text" name="S_ID" value="<%=S_ID%>" style="display:none" />
            </td>
        </tr>
    	<tr>
        	<td height="30" class="td_border">
            &nbsp;&nbsp;内容：
            </td>
            <td height="30" class="td_border">
            <textarea name="S_Content" id="S_Content" style="width:300px;height:100px" readonly="readonly"><?php echo $row["S_Content"]?></textarea>
            </td>
        </tr>
        <tr>
        	<td height="30" class="td_border">
            &nbsp;&nbsp;会员名称：
            </td>
            <td height="30" class="td_border">
            <label><?php echo $row["S_Name"]?></label>
            </td>
        </tr>
        <tr>
        	<td height="40" class="td_border" align="right">&nbsp;</td>
            <td height="40" class="td_border" align="left">
              <input type="button" name="button1" id="button1" value="回 复" class="rb1" onclick="rebook(<?php echo $S_ID?>)"/>
              <input type="button" name="button2" id="button2" value="删 除" class="rb1" onclick="del(<?php echo $S_ID?>)"/>
            <input type="button" value="返 回" class="rb1" onclick="backpage()" />
          </td>
        </tr>
    </table>
    <?php
}
	function isselect($S_PurView,$values){
		if(($S_PurView=="0")||($S_PurView=="")){
			
		}
		else{
			$S_PurViewarr=explode(",",$S_PurView);
			for($i=0;$i<count($S_PurViewarr);$i++){
				if($S_PurViewarr[$i]==$values){
					echo("checked");
				}
			}
		}
	}
include "../admin_bottom.php";
?>

</body>
</html>