<?php include ("app_needed/config.php");
require_once("app_classes/module_statistics.inc"); 
require_once("app_classes/module_campaigns.inc"); 
require_once("app_classes/module_tags.inc"); 
if($_GET[unique])
{
	$tag = new class_tags($_GET[unique]);
	$campaign = new class_campaigns($_GET[unique]);
}
else
{
	$tag = new class_tags($_POST['this']);
	$campaign = new class_campaigns($_POST['this']);
}
$unique_user = $campaign->get_unique_user().$tag->get_unique_user();
$new_user = new class_users($unique_user);
if(!$unique_user)
$new_user = new class_users($logged->get_unique());
$stats = new class_statistics();
if(!$_SESSION[unique])
header("Location: ".APP_WEBSITE."/login.html");

if(!$_POST[start_date])
$start_date = date("Y-m-d",strtotime("-7 days"));
else
$start_date = $_POST[start_date];

if(!$_POST[end_date])
$end_date = date("Y-m-d");
else
$end_date = $_POST[end_date];

$parameters[0] = $start_date;
$parameters[1] = $end_date;

if($_GET[type] == "advertiser")
{
	$parameters[2] = $logged->get_unique();
	$parameters[3] = 1; 
	$daily_url = 'reports';
	$country_url = 'report';
}
if($_GET[type] == "publisher")
{
	$parameters[2] = $logged->get_unique();
	$parameters[3] = 2; 

	$daily_url = 'statistics';
	$country_url = 'stats';
}

if($_POST['this'] && $_GET[type])
{
	$parameters[4] = $_POST['this'];
}
 //echo $parameters[0] . " - ". $parameters[1] . " - ".$parameters[2] . " - ".$parameters[3] . " - ".$parameters[4]  ;

if($_GET[unique])
{
	if($tag->get_unique())
	$parameters[3] = 2;
	else
	$parameters[3] = 1;
	$parameters[2] = $new_user->get_unique();
	$parameters[4] = $_GET[unique];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= language("CHARSET") ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title><?= language('PAGE_stats_countries_title') ?> - <?= $config->get_value('platform_title') ?></title>
<link rel="shortcut icon" type="image/png" href="<?= $config->get_value('platform_favicon') ?>"/>
<link href="app_needed/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="app_needed/ammap/ammap.css" type="text/css">
<link rel="stylesheet" href="app_needed/style.tipsy.css" type="text/css" />

<script src="app_needed/ammap/ammap.js" type="text/javascript"></script>
<script src="app_needed/ammap/maps/js/worldLow.js" type="text/javascript"></script>
</head>

<body>
<div id="wrapper">
	<?php include("module.header.php"); ?>
	<?php include ("module.menu.php"); ?>
	<div class="content">
		<div class="info">
        <div class="left">
		<h1><?= $campaign->get_name().$tag->get_name()?> <?= language('PAGE_stats_countries_title') ?></h1>
        </div>
        <div class="clear"></div>
		</div>
		<div class="filter">
		  <ul>
            <?php if($_GET[unique]) { ?>
            <a href="personal-daily-<?= $_GET[unique] ?>.html"><li><?= language('PAGE_stats_menu_general_stats') ?></li></a>
            <a href="personal-countries-<?= $_GET[unique] ?>.html"><li class="active"><?= language('PAGE_stats_menu_stats_by_country') ?></li></a>
            <a href="personal-agents-<?= $_GET[unique]?>.html"><li><?= language('PAGE_stats_menu_stats_by_user_agents') ?></li></a>
            <a href="personal-pages-<?= $_GET[unique]?>.html"><li><?= language('PAGE_stats_menu_stats_by_website') ?></li></a>
            <?php }else { ?>
            <a href="<?= $daily_url ?>.html"><li><?= language('PAGE_stats_menu_general_stats') ?></li></a>
            <a href="<?= $country_url ?>-countries.html"><li class="active"><?= language('PAGE_stats_menu_stats_by_country') ?></li></a>
            <a href="<?= $country_url ?>-agents.html"><li><?= language('PAGE_stats_menu_stats_by_user_agents') ?></li></a>
            <a href="<?= $country_url ?>-pages.html"><li><?= language('PAGE_stats_menu_stats_by_website') ?></li></a>
            <?php } ?>
		    <div class="clear"></div>
	      </ul>
		  <div class="inside">
		    <h2><?= language('PAGE_stats_filter') ?></h2>
		    <hr />
		    <form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data">
		      <table width="100%" border="0">
		        <tr>
		          <td width="10%"><?= language('PAGE_stats_start_date') ?></td>
		          <td width="19%"><input type="text" placeholder="" name="start_date" id="start_date" value="<?= $start_date ?>"/></td>
		          <td width="10%"><?= language('PAGE_stats_end_date') ?></td>
		          <td width="19%"><input type="text" placeholder="" name="end_date" id="end_date" value="<?= $end_date ?>"/></td>
		          <?php if(!$_GET[unique]) { ?>
		          <td width="11%"><?php 
					if($_GET[type] == "advertiser")
						echo "Campaign";
					if($_GET[type] == "publisher")
						echo "Tag";
					?></td>
		          <td width="22%"><select name="this">
		            <option value="">All</option>
		            <?php 
					  	if($_GET[type] == "advertiser")
						{
					 	$sql = "SELECT * FROM `app_campaigns` WHERE `unique_user` = '".$_SESSION[unique]."' ORDER BY `date` DESC ";
						$result = mysql_query ($sql);
						}
						if($_GET[type] == "publisher")
						{
						$sql = "SELECT * FROM `app_tags` WHERE `unique_user` = '".$_SESSION[unique]."' ORDER BY `date` DESC ";
						$result = mysql_query ($sql);
						}
						while ($query = mysql_fetch_array($result))
						{
							$check = '';
							if($_POST['this'] == $query[unique])
							$check = 'selected';
							echo '<option value="'.$query[unique].'" '.$check.'>'.$query[name].'</option>';
						}
			?>
		            </select></td>
		          <?php } ?>
		          <td width="9%"><input type="submit" value="Filter" /></td>
	            </tr>
	          </table>
	        </form>
	      </div>
	  </div>
		<div class="box none">
        <?php $statistics =  $stats->__call('get_countries',$parameters); ?>
        <?php if(!$statistics) echo "<div class='inside'><div style='text-align:center; padding:100px 0 100px 0;'><h1>".language('MESSAGE_no_stats')."</h1></div></div>";else {  ?>
        <div class="inside"  id="map" style="height:400px;"></div>
        <?php } ?>
        
        </div>
        <div class="box  list_head">
        <div class="inside">
          <table width="100%" border="0">
           	  <tr>
            	  <td width="29%"><?= language('TABLE_country')?></td>
            	  <td width="21%"><?= language('TABLE_impressions')?></td>
            	  <td width="16%"><?= language('TABLE_clicks')?></td>
           	    </tr>
          	</table>
        </div>
        </div>
        <?php $statistics =  $stats->__call('get_countries',$parameters);
		$impressions = 0;
		$clicks = 0;
		if($statistics)
		foreach ($statistics as $country => $value)
		{
			$split = explode("|",$value);
			$impressions += $split[0];
			$clicks += $split[2];
		?>
        <div class="box list">
          <table width="100%" border="0">
            <tr>
              <td width="29%"><?= $country ?></td>
              <td width="21%"><?= $split[0] ?></td>
              <td width="16%"><?= $split[2] ?></td>
            </tr>
          </table>
        </div>
        <?php } ?>
        <p>
        <hr />
        </p>
        <div class="box list">
          <table width="100%" border="0">
           	  <tr>
            	  <td width="29%">Total</td>
            	  <td width="21%"><?= $impressions ?></td>
            	  <td width="16%"><?= $clicks ?></td>
       	    </tr>
       	  </table>
        </div>
    </div>
</div>
<div id='confirm'>
<div class="left">
	<div class='title'><span><?= language('CONFIRM') ?></span></div>
	<div class='message'></div>
</div>
<div class="right">
	<div class='buttons'>
				<span class='no simplemodal-close'><?= language('NO') ?></span><span class='yes'><?= language('YES') ?></span>
               
	</div>
</div>
<div class="clear"></div>
	</div>
		<!-- preload the images -->
	<div style='display:none'>
		<img src='img/confirm/header.gif' alt='' />
		<img src='img/confirm/button.gif' alt='' />
	</div>
    
<script>
<?php 		
		if($statistics)
		foreach ($statistics as $country => $value)
		{
			$split = explode("|",$value);
			$to_echo .= '{"code":"'.$country.'" , "name":"'.$country.'", "value":'.$split[0].', "color":"#eea638"},';
		}
		
?>
var mapData = [<?= substr($to_echo,0,-1)?>];
			 
</script>
<script type='text/javascript' src='app_needed/function.map.js'></script>
<script type="text/javascript" src="app_needed/jquery-1.9.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script type='text/javascript' src='app_needed/jquery.accordionmenu.js'></script>
<script type='text/javascript' src='app_needed/jquery.checkbox.js'></script>
<script type='text/javascript' src='app_needed/jquery.tipsy.js'></script>
<script type='text/javascript' src='app_needed/function.ajax.js'></script>
<script type='text/javascript' src='app_needed/simply-toast.min.js'></script>
<script type='text/javascript' src='app_needed/jquery.simplemodal.js'></script>
<script type='text/javascript' src='app_needed/functions.js'></script>
</body>
</html>
<?php mysql_close($conn); ?>