<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
//$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
// checkAuth('32');	//判断登录和权限

$title = "佣金立减抽奖管理";

if ($actiontype) doaction($actiontype,$actionid);
$css="<link href='http://esfadmin.house365.com/script/css/autocomplete.css' rel='stylesheet' type='text/css' />";
$js = "<script type='text/javascript' src='http://esfadmin.house365.com/script/js/showpic.js'></script>
<script type='text/javascript' src='http://esfadmin.house365.com/script/js/public.js'></script>";

$tbl='esf_20140501_jmyj_prize';
$tbl_log='esf_20140501_jmyj_prize_log';

//查询条件
$cond_arr=array();
$cond_arr["1"]	="*奖品类型*";

$cond_arr_2["0"]	    ="请选择";
$cond_arr_2["7"]	    ="幸运奖";
$cond_arr_2["6"]	    ="10元话费";
$cond_arr_2["5"]	    ="30元话费";
$cond_arr_2["4"]	    ="电水壶";
$cond_arr_2["3"]	    ="电磁炉";
$cond_arr_2["2"]	    ="迷你冰箱";

//查询条件
$cond_search = tonyForm("select","search_type",$cond_arr,$search_type);
$cond_search_2	= tonyForm("select","type",$cond_arr_2,$type);

//查询条件处理开始
$where = " where 1=1 ";
if($type && $type!=7){
  $where.= " and a.type = '{$type}'";
}

//分页参数
$pagesize	= $disp_rows ? intval($disp_rows) : 20;
$page_now	= $page_now ? max(1, intval($page_now)) : 1 ; // 获取当前页数
$offset		= ( $page_now - 1 ) * $pagesize;
$limit = " LIMIT ".$offset.",".$pagesize;

if($type && $type!=7){
	//获得数据总条数
	$sql1 = "select COUNT(*) as num from $tbl where type='{$type}' ";

	//获得数据当前页面数据
	$sql2 = "select * from $tbl as a left join $tbl_log as b on a.lid=b.id $where ";
}elseif($type==7){//查询幸运奖
	$sql = "select lid from $tbl where lid !=0";
	$result = $dbback->GetAll($sql);
	$lid_arr = array();
	if(is_array($result)){
		foreach($result as $val){
			$lid_arr[] = $val['lid'];
		}
	}
	$lids = implode(',',$lid_arr);
	if($lids){
		$where_conf = " where id not in ({$lids})";
	}else{
		$where_conf = '';
	}
    
	//获得数据总条数
	$sql1 = "select COUNT(*) as num from $tbl_log $where_conf";
	$numrows = $dbback->GetOne($sql1);

	//获得数据当前页面数据
	$sql2 = "select * from $tbl_log $where_conf ";
 
}else{ //查询所有奖品
	//获得数据总条数
	$sql1 = "select COUNT(*) as num from $tbl_log ";
 
	//获得数据当前页面数据
	$sql2 = "select * from $tbl_log as a left join $tbl as b on a.id = b.lid  ";
}

if($act == 'export_data'){
	//下载数据
    $data_info = $dbback->GetAll($sql2);

	$data = "奖品名称,状态,中奖人姓名,中奖人电话,中奖人ip,开始时间,中奖时间,结束时间
	";
	if(is_array($data_info)){
		foreach($data_info as $val){
			if($val['name']){
				$data .= $val['name'].',';
			}else{
				$data .= '租房大礼包,';
			}
			if($val['status']==1){
				$data .= '已中,';
			}else{
				$data .= '未中,';
			}
			$data .= "{$val['userName']},{$val['tel']},{$val['ip']},";
			if($val['startTime']){
				$data .= date('Y-m-d H:i:s',$val['startTime']).',';
			}else{
				$data .= ' ,';
			}
			$data .= $val['ctime'].',';
			if($val['endTime']){
				$data .= date('Y-m-d H:i:s',$val['endTime']).',
				';
			}else{
				$data .= ' ,
				';
			}
		}
	}

	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=中奖情况.csv");
    $act = '';
    echo $data;
    exit();
}else{
	//获取数据
	$numrows = $dbback->GetOne($sql1);
	$data_info = $dbback->GetAll($sql2.''.$limit);
}

$pagenum = $pagesize;//当前页面数量

$numrows = $numrows >0 ? $numrows : 0;
$time = time();
if($numrows <= 0 || $pagenum <=0){
	$pages = 0 ;  //计算总页数
}else{
	$pages = ceil($numrows/$pagesize) ;  //计算总页数
	$new_pagenavi = new_pagenavi2($page_now,$pages,$numrows);
}
 
//引用模版文件
include_once(Template("header"));
include_once(Template("choujiang/yjlj20140505"));
include_once(Template("footer"));
?>