<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
if ($do == 'list') {
		//列表信息 
		$wheresql = '';
		$wheresqls = '';
		$page = max(1, getGP('page','G','int'));
		$pagesize = $_CONFIG->config_data('pagenum');
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'';
		if ($name = getGP('name','G')) {
			$wheresqls .= " AND b.name='".$name."' ";
			$url .= '&name='.rawurlencode($name);
		}
		//时间
		$venddate = getGP('venddate','G');
		$vstartdate = getGP('vstartdate','G');
		if ($vstartdate!='' && $venddate!='') {
			$wheresql .= " AND (date>='".$vstartdate."' and date<='".$venddate."')";
			$url .= '&vstartdate='.$vstartdate.'&venddate='.$venddate;
		}else{
			$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(date)";
		}
		$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."user a,".DB_TABLEPRE.DB_JT."user_view b WHERE a.id=b.uid ".$wheresqls." ORDER BY a.numbers asc");
		$sql = "SELECT b.name,a.id FROM ".DB_TABLEPRE.DB_JT."user a,".DB_TABLEPRE.DB_JT."user_view b WHERE a.id=b.uid ".$wheresqls." ORDER BY a.numbers asc LIMIT $offset, $pagesize";
		$result = $db->fetch_all($sql);
	include_once('charts/list.php');

}elseif ($do == 'excel') {
	$datename="attendancecharts_".get_date('YmdHis',PHP_TIME);
	$outputFileName = 'data/excel/'.$datename.'.xls';
		$content = array();
		$archive=array("姓名","出勤天数","早退时长","迟到时长","早退次数","迟到次数");
		$content[] = $archive;
		$wheresql = '';
		$wheresqls = '';
		if ($name = getGP('name','P')) {
			$wheresqls .= " AND b.name='$name' ";
		}
		//时间
		$venddate = getGP('venddate','P');
		$vstartdate = getGP('vstartdate','P');
		if ($vstartdate!='' && $venddate!='') {
			$wheresql .= " AND (date>='".$vstartdate."' and date<='".$venddate."')";
		}else{
			$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(date)";
		}
		$sql = "SELECT b.name,a.id FROM ".DB_TABLEPRE.DB_JT."user a,".DB_TABLEPRE.DB_JT."user_view b WHERE a.id=b.uid ".$wheresqls." ORDER BY a.numbers asc";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."attendance WHERE uid='".$row['id']."' ".$wheresql."");
			$ledates = $db->result("SELECT sum(ledate) AS ledates FROM ".DB_TABLEPRE.DB_JT."attendance WHERE uid='".$row['id']."' ".$wheresql."");
			$latedates = $db->result("SELECT sum(latedate) AS latedates FROM ".DB_TABLEPRE.DB_JT."attendance WHERE uid='".$row['id']."' ".$wheresql."");
			$edate = $db->result("SELECT COUNT(*) AS edate FROM ".DB_TABLEPRE.DB_JT."attendance WHERE uid='".$row['id']."' and ledate!='0' ".$wheresql."");
			$latedate = $db->result("SELECT COUNT(*) AS latedate FROM ".DB_TABLEPRE.DB_JT."attendance WHERE uid='".$row['id']."' and latedate!='0' ".$wheresql."");
			$archive = array(
				"".$row['name']."",
				"".$num."",
				"".attendance_date($ledates)."",
				"".attendance_date($latedates)."",
				"".$edate."",
				"".$latedate.""
			);
			$content[] = $archive;
		}
	$excel = new ExcelWriter($outputFileName);
	if($excel==false) 
		echo $excel->error; 
	foreach($content as $v){
		$excel->writeLine($v);
	}
	$excel->sendfile($outputFileName);
}

?>