<?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(){

	$sql = 'SELECT * FROM `chat` WHERE siteid=\''.WAPID.'\' AND gro=\''.$_GET['cid'].'\' LIMIT 0, 20;';
	
	$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';
				$html .='<option value="'.user('xid','id',$userid).'">'.user('nc','id',$userid).user('xid','id',$userid).'</option>'; 
			}
		}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 post_content();
echo '<form  method="post" action="?cid='.$_GET['cid'].'">';
    echo '<div class="title">';
	if($_GET['toid'] == NULL){
		$itip = '填写此项请留空前项';
	}else{
		$itip = $_GET['toid'];
	}
	echo '对话人ID<select name="toid"><option value="0">所有人</option>'.get_chat_list().'</select> <input name="toid2" type="text" value="'.$itip.'">';
	echo '</duv><div class="content">';
	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.'/chat/list.php?cid='.$_GET['cid'].'">返回上级</a><a href="http://'.HTTP_HOST.'/index.php?cid=0">网站首页</a></div>';
echo footer(1);

?>