<?
	include_once("config.php");
	class Stats extends Fly5 {
	
		
		function execute(&$view, $path, $args)
		{
			$a = $_REQUEST;
			switch($path) {
			case "Stats/api/searchUseStats":
				$result = $this->searchUseStats($a);
				break;
			case "Stats/api/searchUseList":
				$result = $this->searchUseList($a);
				break;
			case "Schedule/api/uselist":
				$result = $this->uselist($a);
				break;
			default:
				$view = "$path.pug";
				return;
			}
			reply_json($result);
		}
		
		function searchUseStats(&$view){
			$a = $_REQUEST;
			
			if($a[selectWard] && $a[selectWard]!=='전체')
				$selectWard = "and ward=:selectWard";
			if($a[selectMenu] && $a[selectMenu]!=='전체')
				$selectMenu = "and content=:selectMenu";
			if($a[startDate])
				$sdate = " and date_format(visittime,'%Y-%m-%d') >= :startDate";
			if($a[endDate])
				$edate = " and date_format(visittime,'%Y-%m-%d') <= :endDate";
			
			$sumCnt = query ("select count(*) cnt from s4log where 1=1 $selectWard $selectMenu $sdate $edate and room != '임시' and bedno != '테스트'", $a);
			
			if($a[selectWard] && $a[selectWard]!=='전체'){
				$data = query("select max(visittime) as visittime, max(ward) as ward, max(room) as room, max(bedno) as bedno, max(content) as content  , COUNT(*) as cnt, round( (COUNT(*)/(select count(*) from s4log where 1=1 $selectWard $selectMenu $sdate $edate and room != '임시' and bedno != '테스트')*100),2) as percent 
				from s4log where 1=1 $selectWard $selectMenu $sdate $edate and room != '임시' and bedno != '테스트' group by content, ward ", $a);
			}else{
				$data = query("select max(visittime) as visittime, max(ward) as ward, max(room) as room, max(bedno) as bedno, max(content) as content  , COUNT(*) as cnt, round( (COUNT(*)/(select count(*) from s4log where 1=1 $selectWard $selectMenu $sdate $edate and room != '임시' and bedno != '테스트' )*100),2) as percent 
				from s4log where 1=1 $selectWard $selectMenu $sdate $edate and room != '임시' and bedno != '테스트' group by content ", $a);
			
			}
			return array(
					data=>$data,
					sumCnt=>$sumCnt,
			);
			
			
		}
		
		function searchUseList(&$view){
			$a = $_REQUEST;
			if($a[selectWard] && $a[selectWard]!=='전체')
				$selectWard = "and ward=:selectWard";
			if($a[selectMenu] && $a[selectMenu]!=='전체')
				$selectMenu = "and content=:selectMenu";
			if($a[selectList] && $a[selectList]!=='전체보기')
				$list = " limit $a[selectList]";
			if($a[startDate])
				$sdate = " and date_format(visittime,'%Y-%m-%d') >= :startDate";
			if($a[endDate])
				$edate = " and date_format(visittime,'%Y-%m-%d') <= :endDate";
			
			$data = query("select visittime , ward, room,  bedno, content 
			from s4log where 1=1 $selectWard $selectMenu $sdate $edatewhere order by visittime desc $list", $a);

			return array(
					data=>$data,
			);
			
			
		}
		
		function list(&$view)
		{
			$view = 'Stats/list.pug';
			$a = $_REQUEST;
				if(!$a[hid])
						$a[hid] = $_COOKIE[hid];
				//print_r($a);die();

				if($a[objective] && $a[objective] != 'all')
						$objective = " and a.name=:objective ";
				$menu = query("select content from s4log group by content", $a);
				//$wardList = query("select ward from wardmap group by ward", $a);
				$wardList = query("select name from s4ward", $a);
				$data = query("SELECT  visittime, ward, room, bedno, content FROM s4log order by visittime desc limit 100", $a);

				return array(
						data=>$data,
						wardList=>$wardList,
						menu=>$menu,

				);
		}
		
		function summary(&$view)
		{
			  $view = 'Stats/summary.pug';
				$a = $_REQUEST;
				if(!$a[hid])
						$a[hid] = $_COOKIE[hid];
				//print_r($a);die();

				if($a[objective] && $a[objective] != 'all')
						$objective = " and a.name=:objective ";

				$menu = query("select content from s4log group by content", $a);
				//$wardList = query("select ward from wardmap group by ward", $a);
				$wardList = query("select name from s4ward", $a);
				$sumCnt = query ("select count(*) cnt from s4log where 1=1 and room != '임시' and bedno != '테스트'", $a);
				$data = query("SELECT max(ward) ward, max(room) room, max(bedno) bedno, max(content) content  , COUNT(*) cnt, round( (COUNT(*)/(select COUNT(*) from s4log where 1=1 and room != '임시' and bedno != '테스트' )*100),2) percent FROM `s4log` where 1=1 and room != '임시' and bedno != '테스트' GROUP by content", $a);

				return array(
						objective=>$a[objective],
						data=>$data,
						menu=>$menu,
						sumCnt=>$sumCnt,
						wardList=>$wardList,
				);
		}
	}
?>