<?php
if(empty($op)) $op='shou';
?>
  <!--- content -->
  <div class="neir">
    <div class="top">消息留言</div>
      <div class="content">
		<div class="main_nav">
			<ul>
				<li class="<?php if($op=='shou'){ echo 'nava_4';}else{ echo 'navb_5';}?>"><a href="?ac=msg&op=shou">收件箱</a></li>
				<li class="<?php if($op=='fa'){ echo 'nava_4';}else{ echo 'navb_5';}?>"><a href="?ac=msg&op=fa">发件箱</a></li>
				<li class="<?php if($op=='book'){ echo 'nava_4';}else{ echo 'navb_5';}?>"><a href="?ac=msg&op=book">留言管理</a></li>
			</ul>
			<div class="action"><a href="?ac=msg&op=add">新消息</a></div>
		</div>
<?php if($op=='add'){ ?>
		   <div class="main_nav2">
<script language="javascript">
$(document).ready(function(){
	$("#addxiaoxi").click(function(){
		var CS_Usera=$("#CS_Usera").val();
		var CS_Name=$("#CS_Name").val();
		var CS_Neir=$("#CS_Neir").val();
	$("#CS_Usera").css("border-color","#666 #ccc #ccc #666");
	$("#CS_Name").css("border-color","#666 #ccc #ccc #666");
	$("#CS_Neir").css("border-color","#666 #ccc #ccc #666");
	if(CS_Usera==""){
		$("#CS_Usera").css("border","1px solid red");
		$("#Msg").text("出错了，请填写接收的会员名！").css("color","red");
		return false;
		}
	else if(CS_Name==""){
		$("#CS_Name").css("border","1px solid red");
		$("#Msg").text("出错了，请填写消息标题！").css("color","red");
		return false;
		}
	else if(CS_Neir==""){
		$("#CS_Neir").css("border","1px solid red");
		$("#Msg").text("出错了，请填写消息内容！").css("color","red");
		return false;
		}
	});
});
</script>
<script type="text/javascript" charset="utf-8" src="../public/editor/kindeditor.js"></script>
<script type="text/javascript"> KE.show({
	id : 'CS_Neir',
	resizeMode : 1,
	allowPreviewEmoticons : false,
	allowUpload : false,
	items : [
	'source', '|', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',
	'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
	'insertunorderedlist', '|', 'image', 'link', 'unlink']
	});
</script>
	             <form action="do.php?ac=msg&op=add" method="post">
	             <table class="user_mod_list" cellpadding="0" cellspacing="0">
		             <tr><td height='50'><span class="t"><i>*</i>发送给：</span><input type="text" class="input_only" id="CS_Usera" name="CS_Usera" value="<?php echo $id?>"><span class="reg_msg">接收会员名,发给管理员填写 “admin”</span></td></tr>
		             <tr><td height='50'><span class="t"><i>*</i>标题：</span><input type="text" class="input_only" id="CS_Name" name="CS_Name" value=""><span class="reg_msg">消息标题</span></td></tr>
		             <tr><td style="height:140px;"><span class="t"><i>*</i>内容：</span><textarea style="width:450px; height:140px;" id="CS_Neir" name="CS_Neir"></textarea></td></tr>
	             </table>
	             <div style="padding-top:10px;margin-left:150px;"><div id="Msg"></div><span class="button-main"><span><button type="submit" id="addxiaoxi" >确定发送</button></span></span></div>
	             </form>
		   </div>
<?php }elseif($op=='book'){ ?>
		   <div class="main_nav2">
				<table class="msg_hang msg_book" cellpadding="0" cellspacing="0">
<?php
        $j=1;
	$sql="select * from ".Getdbname('gbook')." where cs_usera='".$cscms_name."' and cs_fid=0 order by cs_addtime desc";
		$Arr=getuserpage($sql,"5");//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$numdate="a";
		if($result){
			while ($row = $db ->fetch_array($result)){
                        $us=$db->getrow("select * from ".Getdbname('user')." where CS_Name='".$row['CS_Userb']."'");
		        $numdate="b";
?>
				  <tr>
				     <td class="msg1">&nbsp;</td>
				     <td class="msg2"><a href="<?php echo userlink('index',$us['CS_ID'])?>" target="_blank" ><img class="photo_48" src="<?php echo piclink('logo',$us['CS_Logo'])?>" width="48" height="48" title="<?php echo $row['CS_Userb']?>" /></a></td>
				     <td class="msg3"><a href="<?php echo userlink('index',$us['CS_ID'])?>" target="_blank"><?php echo $row['CS_Userb']?></a><br /><?php echo datetime($row['CS_AddTime'])?></td>
				     <td class="msg4">
				     <span class="book_hs">在你主页留言道：</span>
				     <div class="msg_book_info"><?php echo $row['CS_Neir']?></div>
				     <div class="msg_book_box">
                             <?php 
		                 //处理此ID下的回复
		                 $sql2="select * from ".Getdbname('gbook')." where CS_FID=".$row['CS_ID']."";
		                 $class2=$db->query($sql2);
		                 if($class2){
			                 while($rowclass2=$db->fetch_array($class2)){
                                         $us2=$db->getrow("select * from ".Getdbname('user')." where CS_Name='".$rowclass2['CS_Userb']."'");

				         echo "<dl class=\"book_my\">
				         <dt><a href=\"javascript:delgbook('".$rowclass2['CS_ID']."','delhf')\" class=\"book_rdel\" title=\"删除回复\">删除</a> <a href=\"".userlink('index',$us2['CS_ID'])."\" target=\"_blank\">".$rowclass2['CS_Userb']."</a> ".datetime($rowclass2['CS_AddTime'])."</dt><dd class=\"book_d1\"><a href=\"".userlink('index',$us2['CS_ID'])."\" target=\"_blank\"><img src=\"".piclink('logo',$us2['CS_Logo'])."\" width=\"32\" height=\"32\" title=\"".$rowclass2['CS_Userb']."\"/></a></dd>
				         <dd class=\"book_d2\">".$rowclass2['CS_Hfneir']."</dd>
				         </dl>";

			                 }
		                 }
                             ?>
				     </div>
				     <div class="book_reply">
				     <textarea name="CS_Hfneir" id="CS_Hfneir<?php echo $row['CS_ID']?>" cols="35" rows="1"></textarea>
				     <span class="button-main"><span><button type="button" onclick="gbookhf(<?php echo $row['CS_ID']?>)">回复</button></span></span>
				     </div>
				     <div class="book_del">
				     <a href="?ac=msg&op=add&id=<?php echo $us['CS_Name']?>">发站内信</a>&nbsp;&nbsp;|&nbsp;&nbsp;
				     <a href="javascript:delgbook('<?php echo $row['CS_ID']?>','del')">删除留言</a>
				     </div>
				     </td>
				  </tr>
<?php
	                }
                }
        if($numdate=="a") echo '<tr><td>没有相关记录!</td></tr>';
?>
				</table>
		   </div>
		<div class="page">
		<div class="pages"><?php echo $Arr[0];?></div>
	        </div>
<?php }else{ ?>
		   <div class="main_nav2">
             <?php 
                  if(!empty($id)){ 
                   $row=$db->getrow("select * from ".Getdbname('xiaoxi')." where CS_ID=".$id."");
                   if($row){
                        $db->query("update ".Getdbname('xiaoxi')." set CS_DID=1 where CS_ID=".$id." and cs_usera='".$cscms_name."'");
                        if($row['CS_Userb']=='系统消息'){
                           $logo=piclink('logo','');
                           $link='#';
                        }else{
                           $us=$db->getrow("select * from ".Getdbname('user')." where CS_Name='".$row['CS_Userb']."'");
                           $logo=piclink('logo',$us['CS_Logo']);
                           $link=userlink('index',$us['CS_ID']);
                        }
			   echo '<table class="msg_hang msg_view" cellpadding="0" cellspacing="0" id="ListNote">					
					 <caption>&nbsp;&nbsp;我和<a href="'.$link.'" target="_blank">'.$row['CS_Userb'].'</a>之间的对话</caption>
				    <tr>
				     <td class="msg1">&nbsp;</td>
				     <td class="msg2"><a href="'.$link.'"><img class="photo_48" src="'.$logo.'" width="48" height="48" title="'.$row['CS_Userb'].'" /></a></td>
				     <td class="msg3"><a href="'.$link.'">'.$row['CS_Userb'].'</a><br />'.datetime($row['CS_AddTime']).'<br /><a href="?ac=msg&op=add&id='.$row['CS_Userb'].'">回复</a></td>
				     <td class="msg4">'.$row['CS_Neir'].'</td>
				    </tr>				
				</table>';
                    }else{
                        echo '<div>该消息不存在!</div>';
                    }
             ?>
	        </div>			
             <?php }else{ ?>
				<div class="msg_do">
				<a onclick="return confirm('确定要清空收件箱吗？');" class="msg_icon1" href="javascript:delmsg()">清空收件箱</a> <a class="msg_icon2" href="javascript:dumsg()">全部设为已读</a>
				</div>
				<table class="msg_hang" cellpadding="0" cellspacing="0">
<?php
        $j=1;
	$sql="select * from ".Getdbname('xiaoxi')." where 1=1";
        if($op=="shou"){
             $sql.=" and cs_usera='".$cscms_name."'";
        }else{
             $sql.=" and cs_userb='".$cscms_name."'";
        }
        $sql.=' order by cs_addtime desc';
		$Arr=getuserpage($sql,"5");//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$numdate="a";
		if($result){
			while ($row = $db ->fetch_array($result)){
		        $numdate="b";
                        if($row['CS_Userb']=='系统消息'){
                           $logo=piclink('logo','');
                           $link='#';
                        }else{
                           if($op=='shou'){
                                $us=$db->getrow("select * from ".Getdbname('user')." where CS_Name='".$row['CS_Userb']."'");
                           }else{
                                $us=$db->getrow("select * from ".Getdbname('user')." where CS_Name='".$row['CS_Usera']."'");
                           }
                           $logo=piclink('logo',$us['CS_Logo']);
                           $link=userlink('index',$us['CS_ID']);
                        }
?>
				 <tr class="yidu">
				     <td class="msg1">&nbsp;</td>
				     <td class="msg2"><a href="<?php echo $link?>"><img class="photo_48" src="<?php echo $logo?>" width="48" height="48" title="<?php echo $row['CS_Userb']?>" /></a></td>
				     <td class="msg3"><a href="<?php echo $link?>"><?php if($op=='shou'){echo $row['CS_Userb'];}else{echo $row['CS_Usera'];}?></a><?php if($row['CS_DID']==0) echo '<font color=#0000ff>(未读)</font>';?><br /><?php echo datetime($row['CS_AddTime'])?></td>
				     <td class="msg4"><?php echo $row['CS_Name']?></td>
				     <td class="msg5"><a href="?ac=msg&op=shou&id=<?php echo $row['CS_ID']?>">查看该消息</a><br /></td>
				 </tr>
<?php
	                }
                }
        if($numdate=="a") echo '<tr class="yidu"><td>没有相关记录!</td></tr>';
?>
				</table>

		</div>
		<div class="page">
		<div class="pages"><?php echo $Arr[0];?></div>
	        </div>
<?php } } ?>
      </div>
    <div class="end"></div>
  </div>
