<?php
if(!defined('CORE'))exit("error!"); 
if(!defined('CORE'))exit("error!"); 
if(!isLogin()){exit($lang_cn['rabc_is_login']);}  

//角色页面权限
function checkbox_area_action($areaid,$row_action){
	$action=explode(',',$row_action);
	$i=1;
	foreach($areaid as $key=>$val){
		if($key!="0"){
		if(in_array($key,$action)){
		$cbox .="<span style=\"float:left;width:150px\"><input name=\"areaid[]\" type=\"checkbox\" value=\"$key\" checked=\"checked\" />$val</span>\n";
		}else{
		$cbox .= "<span style=\"float:left;width:150px\"><input name=\"areaid[]\" type=\"checkbox\" value=\"$key\" />$val</span>\n";
		}
		}
		if($i==8){$cbox .="";}
		$i++;
	}
	return $cbox;
}


//列表	
if($do==""){
If_rabc(); //检测权限
//判断检索值
	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('admin_vote_list.htm');
	exit;	
	
	
}


//新建	
if($do=="new"){	
    If_rabc(); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//模版	
	$smt->assign ('select_catlist',select($cfg["webcat"],"catid","","分类","span2"));
	$smt->assign('title',"新建选项");
	$smt->display('vote_new.htm');
	exit;

}


//写入
if($do=="add"){
    
	If_rabc(); //检测权限
	$newitem=$_POST["newitem"];
	$created_at = time();
	$sql="insert into vote (titleid,item,count,`created_at`) values (1,'$newitem',1,'$created_at')";
	if($db->query($sql)){echo success($msg,"?action=vote");}else{echo error($msg);}
	exit;
}

//编辑投票选项	
if($do=="edit"){	
	If_rabc(); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//查询
	$sql=" SELECT * FROM vote WHERE id = '{$id}'LIMIT 1 ";
	$db->query($sql);
	$row=$db->fetchRow();
	
	//模版
	$smt->assign('row',$row);
	$smt->assign('title',"更新选项");
	$smt->display('vote_edit.htm');
	exit;
}




//更新
if($do=="updata"){
	If_rabc(); //检测权限
    $updated_at= time();
	$id=$_POST['id'];
	//$areaid=implode(',',$_POST[areaid]);
	$password=md5($_POST[password]);
	$sql="UPDATE vote SET 
	`item` = '$_POST[voteitem]',
	`count` = '$_POST[votecount]' WHERE `id` ='$_POST[id]' LIMIT 1 ;";
	
	if($db->query($sql)){echo success($msg,"?action=vote");}else{echo error($msg);}	
	exit;
}


//删除
if($do=="del"){
	If_rabc(); //检测权限	
	$sql="delete from `vote` where `id`=$id limit 1";
	if($db->query($sql)){echo success($msg,"?action=vote");}else{echo error($msg);}	
	exit;
}



//编辑	
if($do=="edit_votetitle"){	
	If_rabc(); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//查询
	$sql="SELECT * FROM votetitle WHERE titleid = '1'LIMIT 1 ";
	$db->query($sql);
	$row=$db->fetchRow();
	
	//模版
	$smt->assign('row',$row);
	$smt->assign('title',"更新选项");
	$smt->display('votetitle_edit.htm');
	exit;
}


//投票主题修改
if($do=="updata_votetitle"){
	If_rabc(); //检测权限
    $updated_at= time();
	$votetitle=$_POST['votetitle'];
	//$areaid=implode(',',$_POST[areaid]);
	$password=md5($_POST[password]);
	$sql="update votetitle set votetitle='$votetitle' where titleid='1'";
	
	if($db->query($sql)){echo success($msg,"?action=vote");}else{echo error($msg);}	
	exit;
}


?>