<?
#Старт сессии
session_start();

#Подключаем базу
include '../engine/config.php';
  
#Проверяем способ вызова файла
if ( !defined( 'MISOFT_ADMIN_DIRR' ) ) exit();

if ( !isset( $_SESSION['iduser'] ) or $config_user['account'] != 5 ) {
    echo '<div class="stabilisacion-engine"><div class="alert alert-danger" role="alert">
	  <strong>Внимание!</strong> Нарушен механизм безопасности! В силу безопасности и целостности сайта, выполнение скриптов остановлено. Ошибка №A1
	</div></div>';
	exit();
} else {
  
  #Считаем кол-во и сумму выплат за 24ч
  $payments_today = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count, SUM(money) as sum FROM pay_history WHERE date >= '" . ( strtotime( date('Y').'-'.date('m').'-'.date('d').' 00:00' ) - 86400 ) . "' and date <= '" . ( strtotime( date('Y').'-'.date('m').'-'.date('d').' 00:00' ) + 86400 ) . "'" ) );
  
  if ( $payments_today['sum'] == '' ) {
    $payments_today['sum'] = 0;
    $payments_today['count'] = 0;
  }
  
  #Считаем кол-во и сумму попоплнений за 24ч
  $pay_today = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM history_reklama_pay WHERE date + 86400 > '" . time() . "'" ) );
  $pay_summ_today = mysql_fetch_array( mysql_query( "SELECT SUM(price) as sum FROM history_reklama_pay WHERE date + 86400 > '" . time() . "'" ) );
  
  #Считаем кол-во пользователей
  $all_user = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count, SUM(paid) as sum FROM user" ) );
  $all_usser_today = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM user WHERE regdate + 86400 > '" . time() . "'" ) );
  
  #Проверка ответа ТП
  $new_tiket_count = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM tickets WHERE status = '0'" ) );
  
  #Общая сумма прибыли
  $viruchka_sum = mysql_fetch_array( mysql_query( "SELECT id, ident, money, SUM(money) as sum FROM viruchka" ) );

  include 'engine/classes/home_graphics.class.php';
  ?>
    <div class="content-wrapper">
		<div class="container-fluid">

			<div class="row">
				<div class="col-md-12">

					<h2 class="page-title">Статистика</h2>
					
					<div class="row">
						<div class="col-md-12">
							<div class="row">
								<div class="col-md-3">
									<div class="panel panel-default">
										<div class="panel-body bk-primary text-light">
											<div class="stat-panel text-center" style="margin-top: -10px;">
												<div class="stat-panel-number h1 "><?=$all_usser_today['count']?></div>
												<div class="stat-panel-title text-uppercase">Новых пользователей</div>
											    <div class="stat-panel-title text-uppercase text-size-11">Всего пользователей (<?=$all_user['count']?>)</div>
											</div>
											
										</div>
										<a href="?page=user" class="block-anchor panel-footer text-center">Перейти к пользователям &nbsp; <i class="fa fa-arrow-right"></i></a>
									</div>
								</div>
								<div class="col-md-3">
									<div class="panel panel-default">
										<div class="panel-body bk-success text-light">
											<div class="stat-panel text-center">
												<div class="stat-panel-number h1 "><?=$new_tiket_count['count']?></div>
												<div class="stat-panel-title text-uppercase">Новых тикетов ТП</div>
											</div>
										</div>
										<a href="?page=support" class="block-anchor panel-footer text-center">Подробней &nbsp; <i class="fa fa-arrow-right"></i></a>
									</div>
								</div>
								<div class="col-md-3">
									<div class="panel panel-default">
										<div class="panel-body bk-info text-light">
											<div class="stat-panel text-center" style="margin-top: -10px;">
												<div class="stat-panel-number h1 "><?=$pay_today['count']?></div>
												<div class="stat-panel-title text-uppercase">Новых пополнений (24h)</div>
												<div class="stat-panel-title text-uppercase text-size-11">На сумму <?=$pay_summ_today['sum'] . ' ' . $config['valutaname']?></div>
											</div>
										</div>
										<a href="#" class="block-anchor panel-footer text-center">&nbsp;<!--Подробней &nbsp; <i class="fa fa-arrow-right"></i>--></a>
									</div>
								</div>
								<div class="col-md-3">
									<div class="panel panel-default">
										<div class="panel-body bk-warning text-light">
											<div class="stat-panel text-center">
											    <div class="stat-panel-number h1 "><?=$payments_today['count']?></div>
												<div class="stat-panel-title text-uppercase">Новых выплат (24h)</div>
												<div class="stat-panel-title text-uppercase text-size-11">На сумму <?=$payments_today['sum'] . ' ' . $config['valutaname']?></div>
											</div>
										</div>
										<a href="?page=payments" class="block-anchor panel-footer text-center">Подробней &nbsp; <i class="fa fa-arrow-right"></i></a>
									</div>
								</div>
							</div>
						</div>
					</div>

					<div class="row">
						<div class="col-md-6">
							<div class="panel panel-default">
								<div class="panel-heading">Статистика пополнений, выплат</div>
								<div class="panel-body">
									<div class="chart">
										<canvas id="dashReport" height="310" width="600"></canvas>
									</div>
									<div id="legendDiv"></div>
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="panel panel-default">
								<div class="panel-heading">Статистика прибыли</div>
								<div class="panel-body">
									<div class="alert alert-dismissible alert-success">
										<button type="button" class="close" data-dismiss="alert"><i class="fa fa-close"></i></button>
										Общая сумма прибыли: <?=$viruchka_sum['sum'] . ' ' . $config['valutaname']?>
									</div>
									<table class="table table-hover">
										<thead>
											<tr>
												<th>#</th>
												<th>Название источника</th>
												<th>Сумма</th>
											</tr>
										</thead>
										  <tbody>
										    <?
											  $get_viruchka = mysql_query( "SELECT * FROM viruchka ORDER BY money DESC" );
											  $ids = 0;
											  while ( $get_viruchka_row = mysql_fetch_array( $get_viruchka ) ) {
												$ids++;
												switch ( $get_viruchka_row['ident'] ) {
													case 1:
														$viruchka_text = 'Реклама в серфинге';
														break;
													case 2:
														$viruchka_text = 'Контекстная реклама';
														break;
													case 3:
														$viruchka_text = 'Баннеры';
														break;
													case 4:
														$viruchka_text = 'Тесты';
														break;
													case 5:
													$viruchka_text = 'Задания';
														break;
													case 6:
													$viruchka_text = 'Разбудить реферала';
														break;
													case 30:
														$viruchka_text = 'Вконтакте';
														break;
													case 31:
														$viruchka_text = 'Ютуб';
														break;
													case 14:
														$viruchka_text = 'Доска почета';
														break;
													case 15:
														$viruchka_text = 'Ярмарка';
														break;
													case 16:
														$viruchka_text = 'Конкурсы рефералам от реферера';
														break;
													case 18:
														$viruchka_text = 'Рекламная цепочка';
														break;
													case 20:
														$viruchka_text = 'Статическая реклама';
														break;
													case 25:
														$viruchka_text = 'Рассылка рефералам';
														break;
													case 32:
														$viruchka_text = 'Каталог сайтов';
														break;
												}
											  ?>
											  <tr>
												<th scope="row"><?=$ids?></th>
												<td><?=$viruchka_text?></td>
												<td><? if ( $get_viruchka_row['money'] >= 0 ) { echo $get_viruchka_row['money'] . ' ' . $config['valutaname']; } else { echo '<font style="color: red;">'.$get_viruchka_row['money'] . ' ' . $config['valutaname'].'</font>'; }?></td>
											  </tr>
											<? } ?>
										  </tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
					
					<div class="row">
						<div class="col-md-6">
							<div class="panel panel-default">
								<div class="panel-heading">Количество активных рекламных площадок по категориях</div>
								<div class="panel-body">
									<div class="row">
										<div class="col-md-4">
											<ul class="chart-dot-list">
												<li class="a1d">Серфинг</li>
												<li class="a2d">Авто-серфинг</li>
												<li class="a3d">Тест-драйв серфинга</li>
												<li class="a4d">Тесты</li>
												<li class="a5d">Оплачиваемые задания</li>
												<li class="a6d">Контекстная реклама</li>
												<li class="a7d">Статическая реклама</li>
												<li class="a8d">Рекламная цепочка</li>
												<li class="a9d">Баннерная реклама</li>
												<li class="a10d">Вконтакте</li>
												<li class="a11d">Ютуб</li>
											</ul>
										</div>
										<div class="col-md-8">
											<div class="chart chart-doughnut">
												<canvas id="chart-area3" width="1200" height="900" />
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="panel panel-default">
								<div class="panel-heading">Денег на проекте</div>
								<div class="panel-body">
									<div class="row">
										<div class="col-md-4">
											<ul class="chart-dot-list">
												<li class="a1">На счетах рекламных площадок</li>
												<li class="a2">На рабочих счетах пользователей</li>
												<li class="a3">На рекламных счетах рекламодателей</li>
												<li class="a4s">Всего: <?=$area_sum + $user_sum_adv['sum'] + $user_sum['sum'] . ' ' . $config['valutaname']?></li>
												
											</ul>
										</div>
										<div class="col-md-8">
											<div class="chart chart-doughnut">
												<canvas id="chart-area4" width="1200" height="900" />
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<script>
					  window.onload = function(){
					
						// Line chart from swirlData for dashReport
						var ctx = document.getElementById("dashReport").getContext("2d");
						window.myLine = new Chart(ctx).Line(swirlData, {
						  responsive: true,
						  scaleShowVerticalLines: false,
						  scaleBeginAtZero : true,
						}); 
						
						// Pie Chart from doughutData
						var doctx = document.getElementById("chart-area3").getContext("2d");
						window.myDoughnut = new Chart(doctx).Pie(doughnutData, {responsive : true});

						// Dougnut Chart from doughnutData
						var doctx = document.getElementById("chart-area4").getContext("2d");
						window.myDoughnut = new Chart(doctx).Doughnut(doughnutDatatatus, {responsive : true});
 
					  }
					
					  var swirlData = {
						labels: [
						  "<?=( int ) date( 'd', time() - 86400 * 6 ) . $thismonth7?>",
						  "<?=( int ) date( 'd', time() - 86400 * 5 ) . $thismonth6?>",
						  "<?=( int ) date( 'd', time() - 86400 * 4 ) . $thismonth5?>",
						  "<?=( int ) date( 'd', time() - 86400 * 3 ) . $thismonth4?>",
						  "<?=( int ) date( 'd', time() - 86400 * 2 ) . $thismonth3?>",
						  "<?=( int ) date( 'd', time() - 86400 ) . $thismonth2?>",
						  "<?=( int ) date( 'd', time() ) . $thismonth1?>"
						],
						datasets: [
						  {
							label: "Выплат",
							fillColor: "rgba(220,220,220,0.2)",
							strokeColor: "rgba(220,220,220,1)",
							pointColor: "rgba(220,220,220,1)",
							pointStrokeColor: "#fff",
							pointHighlightFill: "#fff",
							pointHighlightStroke: "rgba(220,220,220,1)",
							data: [<?=$view7['sum']?>, <?=$view6['sum']?>, <?=$view5['sum']?>, <?=$view4['sum']?>, <?=$view3['sum']?>, <?=$view2['sum']?>, <?=$view1['sum']?>]
						  },
						  {
							label: "Пополнений (ЭПС)",
							fillColor: "rgba(151,187,205,0.2)",
							strokeColor: "rgba(151,187,205,1)",
							pointColor: "rgba(151,187,205,1)",
							pointStrokeColor: "#fff",
							pointHighlightFill: "#fff",
							pointHighlightStroke: "rgba(151,187,205,1)",
							data: [<?=$view_adv7['sum']?>, <?=$view_adv6['sum']?>, <?=$view_adv5['sum']?>, <?=$view_adv4['sum']?>, <?=$view_adv3['sum']?>, <?=$view_adv2['sum']?>, <?=$view_adv1['sum']?>]
						  }
						]
					  };
					  
					  var doughnutData = [
						{
							value: <?=mysql_num_rows( $rows_sum_dynlink )?>,
							color:"#1D4867",
							highlight: "#24597F",
							label: "Серфинг"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_autodynlink )?>,
							color: "#3DC49B",
							highlight: "#46DBAC",
							label: "Авто-серфинг"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_tddynlink )?>,
							color: "#46BFBD",
							highlight: "#4FD6D3",
							label: "Тест-драйв серфинга"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_tests )?>,
							color: "#333333",
							highlight: "#4C4C4C",
							label: "Тесты"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_tasks )?>,
							color: "#1D8BAE",
							highlight: "#21A0C6",
							label: "Оплачиваемые задания"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_klink )?>,
							color: "#9370DB",
							highlight: "#9F7CEF",
							label: "Контекстная реклама"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_stat )?>,
							color: "#FDB45C",
							highlight: "#FFC870",
							label: "Статическая реклама"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_chain )?>,
							color: "#8FBC8F",
							highlight: "#A2D3A2",
							label: "Рекламная цепочка"
						},
						{
							value: <?=mysql_num_rows( $rows_sum_banners )?>,
							color: "#F7464A",
							highlight: "#FF595E",
							label: "Баннерная реклама"
						},
						{
							value: 0,
							color: "#5581B5",
							highlight: "#5F92CC",
							label: "Вконтакте"
						},
						{
							value: 0,
							color: "#CA5B5D",
							highlight: "#FF5A5E",
							label: "Ютуб"
						}
					  ]
					  
					  var doughnutDatatatus = [
						{
							value: <?=$area_sum ?>,
							color:"#F7464A",
							highlight: "#FF5A5E",
							label: "На счетах рекламных площадок"
						},
						{
							value: <?=$user_sum['sum'] ?>,
							color: "#46BFBD",
							highlight: "#5AD3D1",
							label: "На рабочих счетах пользователей"
						},
						{
							value: <?=$user_sum_adv['sum'] ?>,
							color: "#FDB45C",
							highlight: "#FFC870",
							label: "На рекламных счетах рекламодателей"
						}
					  ]
					</script>
				</div>
			</div>

		</div>
	</div>
  <?
} 
?>