<?php
defined('PHP168_PATH') or die();

$this_controller->check_admin_action($ACTION) or message('no_privilege');

if(REQUEST_METHOD == 'GET'){

	$models = $this_system->get_models();
	$category = $this_system->load_module('category');
	$category->get_cache(false);
	$path = array();
	
	$models = $this_system->get_models();
	
	foreach($category->categories as $v){
		$parents = $category->get_parents($v['id']);
		foreach($parents as $vv){
			$path[$v['id']][] = $vv['id'];
		}
		$path[$v['id']][] = $v['id'];
	}
	$years= range(date("Y"),date("Y")-10);
	$months= range(01,12);
	
	$json = array(
		'json' => p8_json($category->top_categories),
		'path' => p8_json($path),
		'models' => p8_json($models)
	);
	include template($this_module, 'statistic_data', 'admin');
}elseif(REQUEST_METHOD == 'POST'){
	
	$act = isset($_POST['act'])? $_POST['act'] : '';
	$year = intval($_POST['year']);
	$month = intval($_POST['month']);
	$model = $_POST['model'];
	$cid = intval($_POST['cid']);
	$start_date = isset($_POST['start_date'])? $_POST['start_date'] : '';
	$end_date = isset($_POST['end_date'])? $_POST['end_date'] : '';
	$group = isset($_POST['group'])? $_POST['group'] : '';
	
	if($act=='query'){
		if($start_date || $end_date)
			$data = $this_module->getStatic_ranger($start_date,$end_date,$group,$cid,$model);
			//$data = $this_module->getStatic($year,$month,$cid,$model,0,false,$start_date,$end_date,$group);
		else
			$data = $this_module->getStatic($year,$month,$cid,$model);
		echo json_encode($data);
		exit;
	}elseif($act=='stat'){
		if(!$year)
		exit('false');
		
		$static = $this_module->statistic($year,$month,$cid,$model);
		echo json_encode($static);
		exit;
	}
	elseif($act=='download'){
		$static = $this_module->getStatic($year,$month,$cid,$model,0,true);
		exit;
	}
	

}
