<?php
global $pro_ads_statistics, $pro_ads_main, $pro_ads_multisite, $wppas_stats, $wppas_stats_tpl;
?>

<div class="wrap">

<?php 
// Reset stats
if( isset($_GET['reset_stats']) && $_GET['reset_stats'] == 1 )
{
	$wppas_stats->reset_stats();
	$wppas_stats->remove_user_cookie();
}

$wpproads_enable_stats = get_option('wpproads_enable_stats', 0);
if($wpproads_enable_stats)
{
	$save_stats_type = $pro_ads_multisite->wpproads_get_option('wpproads_save_stats_type', 'unique');
	$unique_stats = $save_stats_type == 'all' ? 0 : 1;
	
	echo '<div class="bubblingG"><span id="bubblingG_1"></span><span id="bubblingG_2"></span><span id="bubblingG_3"></span></div>';
	
	if( PAS()->version >= '4.7.5')
	{
		/*$max_time = ini_get("max_execution_time");
		$mem_limit = ini_get('memory_limit');
		$mem_limit_byte = wp_convert_hr_to_bytes($mem_limit);
		$mem_limit_byte_boolean = $mem_limit_byte<268435456;
		echo $mem_limit_byte_boolean==false ? 'ok': '0';
		//echo $max_time;
		*/

		$group = isset($_GET['group']) ? $_GET['group'] : '';
		$group_id = isset( $_GET['group_id']) ? $_GET['group_id'] : '';
		$select = !empty($group) ? $wppas_stats->select_array( array('group' => $group.'_id', 'group_id' => $group_id) ) : array();
		$select = wp_parse_args($wppas_stats->default_select(), $select);
		
		echo '<h2 class="messages-position"></h2>';
		
		if(!empty($group))
		{
			echo '<div style="background: #ABDB6D; color: #FFF; font-size: 24px; border-radius: 5px; padding: 20px;">';
				echo sprintf(__('Stats for %s: %s', 'wpproads'), $group, '<a href="post.php?post='.$group_id.'&action=edit" style="color: #FFF; padding: 0px 10px; border-radius: 5px;">'.get_the_title($group_id).' - ID.'.$group_id.'</a>');
			echo '</div>';	
		}
		
		// stat vars
		$stype = !empty($_GET['stype']) ? $_GET['stype'] : 'day';
		$sd = !empty($_GET['sd']) ? $_GET['sd'] : $wppas_stats->day;
		$sm = !empty($_GET['sm']) ? $_GET['sm'] : $wppas_stats->month;
		$sy = !empty($_GET['sy']) ? $_GET['sy'] : $wppas_stats->year;
			
		echo '<div class="pro_ad_stats_graph">';
			echo $wppas_stats_tpl->stats_tpl(array('select' => $wppas_stats->encode_array($select), 'day' => $sd, 'month' => $sm, 'year' => $sy, 's_type' => $stype, 'unique' => $unique_stats));
		echo '</div>';
	}
	else
	{
		// OLD STATS < 4.7.5
		echo '<div class="pro_ad_stats_graph">';
			$group = isset($_GET['group']) ? $_GET['group'] : '';
			$group_id = isset( $_GET['group_id']) ? $_GET['group_id'] : '';
			echo $pro_ads_statistics->pro_ad_show_statistics(array('rid'  => 4, 'range' => 'day', 'day' => $pro_ads_main->time_by_timezone('d'), 'group' => $group, 'group_id' => $group_id)); 
		echo '</div>';
	}
}
else
{
	?>
    <h2><?php _e('Statistics are disabled','wpproads'); ?></h2>
	<p><?php _e('You can enable statistics on the AD Dashboard under <em>General Settings</em>','wpproads'); ?></p>
    <?php
}
?>


</div>
<!-- end wrap -->