<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
include_once($conf_basedir."base.inc.php");
checkAuth('56');	//判断登录和权限

//获取上个月日期
$ym = date("Y-m");
$last_date = getMonthRange($ym);
$begin_date = $last_date[0];
$end_date = $last_date[1];
$lastym = date('Y-m',strtotime($begin_date));
 
//前台查询参数
$search_arr = array('head'=>'头像','cert'=>'经纪人证书','3'=>'real房证书');
$search_display = getSelect($search_arr,'search_type',$search_type);

//获取上个月总天数，生成日期
$day_num = substr($end_date,8,2);
$day_arr = array();
for($i=1;$i<=$day_num;$i++){
	$day = $i < 10 ? '0'.$i : $i ;
	$day_arr[] = $lastym.'-'.$day;
}

switch($search_type){
	case 'head' :
		$type_mane = '头像';
		break;
	case 'cert' :
		$type_mane = '经纪人证书';
		break;
	case 'real' :
		$type_mane = 'real房证书';
		break;
	default :
		$search_type = 'head';
	    $type_mane = '头像';
		break;
}

$fields = $search_type.'_pass,'.$search_type.'_no ';

$title = "月度质检统计报表(按类型)";
//获取个人经纪人审核质检结果 
$sql = "select id,dateline,admin, ".$fields." from admin_broker_qa_stat where dateline >='".$begin_date."' and dateline <='".$end_date."' ";
$data_info = $dbback->GetAll($sql);
 //echo $sql;die;
 //对数据进行重新整理
if(is_array($data_info) && !empty($data_info)){
	foreach($data_info as $key=>$val){
		$total = $val[$search_type.'_pass'] + $val[$search_type.'_no'];
		$pass_perc = $total ? ceil(($val[$search_type.'_pass']/$total)*100) : 0;
		$data_info[$val['admin']][$val['dateline']] = $pass_perc;
		$data_info[$val['admin']]['admin'] = $val['admin'];
		unset($data_info[$key]);
	}
}

 //下载报表
if($actiontype == 'exportcsv'){
	$data = $lastym."月,";
	foreach($day_arr as $key=>$val){ 
		$key = $key + 1;
		$data .= $key.'日,' ;
	} 
    $data = rtrim($data,',');
    $data .= '
	';
	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$value){
			$data .= $value['admin'].',';
			foreach($day_arr as $key=>$val){ 
				$data .= $value[$val] ? $value[$val].'%,' : '--,';
			}
			$data = rtrim($data,',');
			$data .= '
			';
		}
	}
	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=经纪人审核单日报表".$lastym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}
 
 //引用模版文件
include_once(Template("header"));
include_once(Template("broker_statistic/monthly_qa_type"));
include_once(Template("footer"));