<?php
if(!defined('CORE'))exit("error!"); 
session_start();
//首页	
if($do==""){
	
	echo '<script>';
    echo "location='?action=index&do=user_vote'";
	echo '</script>';
	
}

if($do=="show"){
	$smt = new smarty();smarty_cfg($smt);
	$sql="SELECT * FROM article where id='{$id}' LIMIT 1";
	$db->query($sql);
	$row=$db->fetchRow();
	
	$smt->assign('row',$row);
	$smt->display('news_show.htm');
	exit;
}



    //普通用户页面
if($do=="user_vote"){

	//判断检索值
	if($_POST['title']){$search .= " and item like '%$_POST[title]%'";}		
	if($_POST['time_start']!="" && $_POST['time_over']!=""){
		$search .= " and `created_at` >  '".strtotime($_POST[time_start]." 00:00:00")."' AND  `created_at` <  '".strtotime($_POST[time_over] ." 23:59:59")."'";
	}
	
	//设置分页
	if($_POST[numPerPage]==""){$numPerPage="10";}else{$numPerPage=$_POST[numPerPage];}
	if($_GET[pageNum]==""||$_GET[pageNum]=="0" ){$pageNum="0";}else{$pageNum=($_GET[pageNum]-1)*$numPerPage;}
	$num=mysql_query("SELECT * FROM vote where 1=1 $search");//当前频道条数
	$total=mysql_num_rows($num);//总条数	
	$page=new page(array('total'=>$total,'perpage'=>$numPerPage)); 

	//查询
	$sql="SELECT * FROM vote  where 1=1 $search order by id desc LIMIT $pageNum,$numPerPage";
	
	//echo $sql;
	$db->query($sql);
	$list=$db->fetchAll();

	//模版
	$smt = new smarty();smarty_cfg($smt);
	$smt->assign('list',$list);
	$smt->assign('numPerPage',$_POST[numPerPage]); //显示条数
	$smt->assign('pageNum',$_GET[pageNum]); //当前页数
	$smt->assign('total',$total);  //总条数
	$smt->assign ('page',$page->show());
	$smt->assign ('select_catlist',select($cfg["webcat"],"catid","","分类","span2"));
	$smt->assign('title',"投票管理");
	$smt->display('user_vote.htm');
	exit;
} 
	
//普通用户页面	
if($do=="vote_add"){

	if($_SESSION["vote"]==session_id())
	{
		?>
		<script language="javascript">
			alert("您已经投票了");
			location.href="?action=index&do=user_vote";
		</script>
		<?php
		exit();
	}
	
	
	$id=$_POST["itm"];
	$sql="update vote set count=count+1 where id=$id";
	if(mysql_query($sql))
	{
		$_SESSION["vote"]=session_id();
	?>
	<script language="javascript">alert("投票成功,点确定查看结果");location.href="?action=index&do=vote_result";</script>
	<?php
	}
	else
	{
	?>
	<script language="javascript">alert("投票失败");location.href="?action=index&do=user_vote";</script>
	<?php
	}
}


if($do=="vote_result"){
    //结果页面
	$smt = new smarty();smarty_cfg($smt);
	$smt->display('vote_result.htm');
	exit;
}


//退出	
if($do=="logout"){
	$_SESSION = array();
	session_destroy();
	exit($lang_cn['rabc_login_user']);
}


?>


