<?php 
require "../Run.php";

require "../include/page.inc.php";


/*Fun Start*/
function start($p,$limit){
	if($p < 1){
		$p = 1;
	}
	return ($p-1)*$limit;
}
function get_chat_list($start,$limit){

	$sql = 'SELECT * FROM `chat` WHERE siteid=\''.WAPID.'\' AND gro=\''.$_GET['cid'].'\' ORDER BY chatid DESC LIMIT '.$start.', '.$limit.';';
	
	$res = mysql_query($sql);
	
	if($res && mysql_num_rows($res) > 0){
			$nums = 0;
			while(list($chatid,$userid,$siteid,$gro,$toid,$content,$chattime) = mysql_fetch_row($res)){
			$div = $num++%2 == 0?'line1':'line2';
			if($toid != NULL && $toid != '0'){
				$qz = '<a href="http://'.HTTP_HOST.'/chat/send.php?cid='.$_GET['cid'].'&toid='.user('xid','id',$userid).'">'.user('nc','id',$userid).'</a>@<a href="http://'.HTTP_HOST.'/chat/send.php?cid='.$_GET['cid'].'&toid='.user('xid','id',$toid).'">'.user('nc','id',$toid).'</a>:';
			}else{
				$qz = '<a href="http://'.HTTP_HOST.'/chat/send.php?cid='.$_GET['cid'].'&toid='.user('xid','id',$userid).'">'.user('nc','id',$userid).'</a>:';
			}
				$html .='<div class="'.$div.'">'.$qz.UBB($content).'<br>['.date('Y/m/d H:i:s',$chattime).']</div>'; 
			}
		}else{
			$html = '<div class="tip">暂时还没有人发言</div>';
		}
		return $html;
	}

	function post_content(){
	
		if($_POST['act'] == 'send'){
		if(LOGIN != 1){
			ehtml('请登录后发言');
			exit();
		}
		/*过滤掉非法的参数*/
			if($_POST['content'] == NULL || $_GET['cid'] == NULL || $_GET['cid'] == 0){
				return NULL;
			}else{
				if($_POST['toid'] != 0 ){
					$tid = $_POST['toid'];
				}else{
					$tid = $_POST['toid2'];
				}
				$sql = 'INSERT INTO `chat` (`chatid`, `userid`, `siteid`, `gro`, `toid`, `content`, `chattime`) VALUES (NULL, \''.MYID.'\', \''.WAPID.'\', \''.$_GET['cid'].'\', \''.user2('id',$tid,WAPID).'\', \''.$_POST['content'].'\', \''.time().'\');';
				$post = mysql_query($sql);
				if($post && mysql_affected_rows()>0){
					return '<div class="tip">发言成功</div>';
				}else{
					return '<div class="tip">发言失败</div>';
				}
			}
			
		}else{
			return NULL;
		}
	}
/*Fun End*/

$limit = 15;/*显示15条数据*/

$pageurl = 'http://'.HTTP_HOST.'/chat/list.php?cid='.$_GET['cid'].'&';

echo head(class_name($_GET['cid'],'cn',$wapid));
echo '<div class="title"><b>'.class_name($_GET['cid'],'cn',$wapid).'</b>聊天室</div>';
echo '<div class="subtitle">';
echo '<a href="http://'.HTTP_HOST.'/index.php?cid='.class_name($_GET['cid'],'gro',$wapid).'">退出</a>|';
echo '<a href="http://'.HTTP_HOST.'/chat/send.php?cid='.$_GET['cid'].'">发言</a>|';
echo '<a href="http://'.HTTP_HOST.'/index.php?cid='.class_name($_GET['cid'],'gro',$wapid).'">设置</a>|';
echo '<a href="http://'.HTTP_HOST.'/chat/list.php?cid='.$_GET['cid'].'">刷新</a>';
echo '</div>';
echo post_content();
echo get_chat_list(start($_GET['page'],$limit),$limit);

echo '<div class="showpage">'.page(mysql_num_rows('SELECT * FROM `chat` WHERE siteid=\''.WAPID.'\';'),$limit,$pageurl,NULL,'view').'</div>';
echo '<form  method="post" action="?cid='.$_GET['cid'].'"><div class="title">快速回复</div>';
    echo '<div class="content">';
	echo '<input name="toid" type="hidden" value="0">';
	echo '<input name="act" type="hidden" value="send">';
	echo '<textarea name="content" type="hidden" ></textarea><br>';
	echo '<input type="submit" value="我要发言">';
	echo '</div></form>';
echo '<div class="mylink"><a href="http://'.HTTP_HOST.'/index.php?cid='.class_name($_GET['cid'],'gro',$wapid).'">返回上级</a><a href="http://'.HTTP_HOST.'/index.php?cid=0">网站首页</a></div>';
echo footer(1);

?>