<?php
require_once(dirname(__FILE__)."/../../inc/common.php");
AjaxHead();

$id=getForm("gid","both");$itype=getForm("type","both");$page=getForm("page","get");$pCount=0;
if (!is_numeric($id)){ $id=0;}else{ $id=intval($id);$id=ifthen($id<0,0,$id);}
if (!is_numeric($page)){ $page=1;}else{ $page=intval($page);$page=ifthen($page<1,1,$page);}
if (!is_numeric($itype)){ $itype=1;}else{ $itype=intval($itype);}

$h=ReadData($id,$page);echo($h);

function ReadData($gid,$page){
    return Readmlist($gid,$page,10);
}

function Readmlist($gid,$page,$size){
	global $itype,$pCount;
	$ml=array();$x = '';
	$ret=array("","",$page,0,10,$itype,$gid);
	if ($gid>0){
		$sqlStr = "select `m_id`,`m_author`,`m_addtime`,`m_reply`,`m_ip`,`m_content`,`m_agree`,`m_anti`,`m_pic`,`m_vote`,`m_check` from ".DB::table('review')." where `m_type`=".$itype." AND m_videoid=".$gid." order by `m_id` desc";
		$allRecordset = db::getCount($sqlStr);
	    $pCount = ceil($allRecordset / $size);
	    if ($page > $pCount) $page = $pCount;
	    $page = max(intval($page), 1);
        $offset = $size*($page-1);
		$query = DB::query("$sqlStr limit $offset, $size");
		while ($row = DB::fetch($query)) {
			if (is_numeric($row['m_reply'])){
				$row['m_reply'] .= ReadReplyID($gid,$row['m_reply']);
		    }else{
				$row['m_reply'] = "";
			}
			$x .= $row['m_reply'];
			$ml[] = "{\"cmid\":".$row['m_id'].",\"uid\":0,\"tmp\":\"\",\"nick\":\"\",\"face\":\"\",\"star\":\"\",\"anony\":".ifthen($row['m_author']!="",0,1).",\"from\":\"".ifthen($row['m_author']!="",$row['m_author'],GetAddress($row['m_ip']))."\",\"time\":\"".MyDate("Y-m-d H:i:s", $row['m_addtime'])."\",\"reply\":\"".$row['m_reply']."\",\"content\":\"".$row['m_content']."\",\"agree\":".$row['m_agree'].",\"aginst\":".$row['m_anti'].",\"pic\":\"".$row['m_pic']."\",\"vote\":\"".$row['m_vote']."\",\"allow\":\"".ifthen($row['m_anti']!="",1,0)."\",\"check\":\"".$row['m_check']."\"}";
		}
		$ret[0]=join($ml,",");$ret[3]=$pCount;
	    if ($x!=""){
		    $ret[1]=Readrlist($x,1,10000);
	    }
	}
	return FormatJson($ret);
}

function Readrlist($ids,$page,$size){
	global $itype,$page;
	$rl=array();
	$sqlStr = "select `m_id`,`m_author`,`m_addtime`,`m_reply`,`m_ip`,`m_content`,`m_agree`,`m_anti`,`m_pic`,`m_vote`,`m_check` from ".DB::table('review')." where `m_type`=".$itype." AND `m_id` in ($ids) order by `m_id` desc";
	$allRecordset = db::getCount($sqlStr);
	$tpagecount = ceil($allRecordset / $size);
	if ($page > $tpagecount) $page = $tpagecount;
	$page = max(intval($page), 1);
    $offset = $size*($page-1);
	$query = DB::query("$sqlStr limit $offset, $size");
	while ($row = DB::fetch($query)) {
		$rl[] = "\"".$row['m_id']."\":{\"uid\":0,\"tmp\":\"\",\"nick\":\"\",\"face\":\"\",\"star\":\"\",\"anony\":".ifthen($row['m_author']!="",0,1).",\"from\":\"".ifthen($row['m_author']!="",$row['m_author'],GetAddress($row['m_ip']))."\",\"time\":\"".MyDate("Y-m-d H:i:s", $row['m_addtime'])."\",\"reply\":\"".$row['m_reply']."\",\"content\":\"".$row['m_content']."\",\"agree\":".$row['m_agree'].",\"aginst\":".$row['m_anti'].",\"pic\":\"".$row['m_pic']."\",\"vote\":\"".$row['m_vote']."\",\"allow\":\"".ifthen($row['m_anti']!="",1,0)."\",\"check\":\"".$row['m_check']."\"}";
	}
	return join($rl,",");
}

function ReadReplyID($gid,$cmid){
	$cmid=intval($cmid);
	if ($cmid>0){
		$row = DB::fetch_first("SELECT m_reply FROM ".DB::table('review')." WHERE m_id=".$cmid);
		if (is_array($row)) {
			return ",".$row['m_reply'].ReadReplyID($gid,$row['m_reply']);
		}else{
			return "";
		}
	}else{
		return "";
	}
}


function FormatJson($ary){
	$x="{\"mlist\":[%0%],\"rlist\":{%1%},\"page\":{\"page\":%2%,\"count\":%3%,\"size\":%4%,\"type\":%5%,\"id\":%6%}}";
	for ($i = 0;$i < 7;$i++) {
		$x=replaceStr($x,"%".$i."%",$ary[$i]);
    }
	return jsonescape($x);
}

function jsonescape($txt){
	return replaceStr(replaceStr(unescape(replaceStr(escape("".$txt),"%u","\u")),chr(10),"\n"),chr(13),"\r");
}
?>