<?php
if(!defined('IN_MYMPS')) exit('Forbidden');
if(submit_check('comment_submit')){
	
	switch($do_act){
		case 'reply':
			empty($id) && write_msg('','?m=comment&type=corp&error=1&id='.$id);
			$reply = isset($_POST['reply']) ? textarea_post_change($_POST['reply']) : '';
			
			$db -> query("UPDATE `{$db_mymps}member_comments` SET reply = '$reply',retime = '$timestamp' $where AND id = '$id'");
			write_msg('','?m=comment&type=corp&success=8&c='.$c.'&id='.$id);
		break;
		default:
			empty($selectedids) && write_msg('','?m=comment&type=corp&error=1&page='.$page.'&t='.$t.'&c='.$c);
			$db -> query("DELETE FROM `{$db_mymps}member_comments` {$where} AND id ".create_in($selectedids));
			write_msg('','?m=comment&type=corp&success=3&page='.$page.'&t='.$t.'&c='.$c);
		break;
	}
	
} else {
	
	if(empty($id)){

		!in_array($c,array('good','middle','bad','all')) && $c = 'all';
		!in_array($t,array('lastweek','lastmonth','last3month','all')) && $t = 'all';
		
		$lastweek  	= $timestamp - 86400*7;
		$lastmonth	= $timestamp - 86400*30;
		$last3month = $timestamp - 86400*90;
		
		if($t == 'lastweek'){
			$where .= " AND pubtime >= '$lastweek'";
		} elseif($t == 'lastmonth') {
			$where .= " AND pubtime >= '$lastmonth'";
		} elseif($t == 'last3month') {
			$where .= " AND pubtime >= '$last3month'";
		}
		
		unset($lastweek,$lastmonth,$last3month);
		
		if($c == 'good'){
			$where .= " AND enjoy IN('2','3')";
		} elseif($c == 'middle'){
			$where .= " AND enjoy = '1'";
		} elseif($c == 'bad'){
			$where .= " AND enjoy = '0'";
		}
		
		$rows_num = mymps_count("member_comments",$where);
		$param	  = setParam(array('m','t','c','type'));
		$comment  = page1("SELECT * FROM `{$db_mymps}member_comments` {$where} ORDER BY id DESC");
		if($c=='all') $count	  = get_comment_count();
		$here = '我的点评';
		$location = location('corp');
		include mayi_tpl('comment');

	} else {
		
		$comment = $db -> getRow("SELECT * FROM `{$db_mymps}member_comments` {$where} AND id = '$id'");
		if(!$comment['id']){
			write_msg('操作失败！该评论并不存在！');
		}
		$c = $comment['enjoy'] == 1 ? 'middle' : ($comment['enjoy'] == 0 ? 'bad' : 'good');
		$here = '点评管理';
		$location = location('corp');
		include mayi_tpl('comment_reply');
		
	}
	
}

function get_comment_count(){
	global $s_uid,$db,$db_mymps,$timestamp;
	static $count  = array();
	$where 		= "WHERE userid = '$s_uid'";
	$lastweek 	= " AND pubtime >= '$timestamp - 86400*7'";
	$lastmonth  = " AND pubtime >= '$timestamp - 86400*30'";
	$last3month = " AND pubtime >= '$timestamp - 86400*90'";
	$wms		= "";
	
	$good	= " AND enjoy IN('2','3')";
	$middle = " AND enjoy = '1'";
	$bad	= " AND enjoy = '0'";
	$gmb	= "";
	
	$sql 	= "SELECT COUNT(id) FROM `{$db_mymps}member_comments` {$where}";
	foreach(array('good','bad','middle') as $k => $v){
		foreach(array('lastweek','last3month','lastmonth','wms') as $key => $val){
			$count[$v][$val] = $db -> getOne($sql.${$v}.${$val});
		}
	}
	unset($lastweek,$lastmonth,$last3month);
	return $count;
}
?>