<?php
require_once('../wu_init.php');
if (!wu_token()) { exit('wu-error'); }

//Исправление бага с шириной графика
sleep(1);

//Рассчёт дней
$qin = mysqli_query($connect_db, "SELECT id,YEAR(FROM_UNIXTIME(dt)) AS `iyear`, MONTH(FROM_UNIXTIME(dt)) AS `imonth`, DAY(FROM_UNIXTIME(dt)) AS `iday` FROM `".DB_PREFIX."_operations` WHERE usr = '$u_id' GROUP BY iyear,imonth,iday");
$timeline = array();
while($rowi = mysqli_fetch_assoc($qin)) {
if ($rowi['iday'] < 10) { $rowi['iday'] = '0'.$rowi['iday']; }
if ($rowi['imonth'] < 10) { $rowi['imonth'] = '0'.$rowi['imonth']; }
$timeline[$rowi['iday'].'.'.$rowi['imonth'].'.'.$rowi['iyear']] = array('spent' => '0', 'profit' => '0', 'in' => '0', 'out' => '0');
}

//Сколько потрачено
$qin = mysqli_query($connect_db, "SELECT id,sum(s) AS `s`, YEAR(FROM_UNIXTIME(dt)) AS `iyear`, MONTH(FROM_UNIXTIME(dt)) AS `imonth`, DAY(FROM_UNIXTIME(dt)) AS `iday` FROM `".DB_PREFIX."_operations` WHERE usr = '$u_id' AND ty = '0' GROUP BY iyear,imonth,iday");
while($rowi = mysqli_fetch_assoc($qin)) {
if ($rowi['iday'] < 10) { $rowi['iday'] = '0'.$rowi['iday']; }
if ($rowi['imonth'] < 10) { $rowi['imonth'] = '0'.$rowi['imonth']; }
$timeline[$rowi['iday'].'.'.$rowi['imonth'].'.'.$rowi['iyear']]['spent'] = $rowi['s'];
}

//Сколько заработано
$qin = mysqli_query($connect_db, "SELECT id,sum(s) AS `s`, YEAR(FROM_UNIXTIME(dt)) AS `iyear`, MONTH(FROM_UNIXTIME(dt)) AS `imonth`, DAY(FROM_UNIXTIME(dt)) AS `iday` FROM `".DB_PREFIX."_operations` WHERE usr = '$u_id' AND ty = '1' GROUP BY iyear,imonth,iday");
while($rowi = mysqli_fetch_assoc($qin)) {
if ($rowi['iday'] < 10) { $rowi['iday'] = '0'.$rowi['iday']; }
if ($rowi['imonth'] < 10) { $rowi['imonth'] = '0'.$rowi['imonth']; }
$timeline[$rowi['iday'].'.'.$rowi['imonth'].'.'.$rowi['iyear']]['profit'] = $rowi['s'];
}

//Сколько введео
$qin = mysqli_query($connect_db, "SELECT id,sum(s) AS `s`, YEAR(FROM_UNIXTIME(dt)) AS `iyear`, MONTH(FROM_UNIXTIME(dt)) AS `imonth`, DAY(FROM_UNIXTIME(dt)) AS `iday` FROM `".DB_PREFIX."_operations` WHERE usr = '$u_id' AND ty = '2' GROUP BY iyear,imonth,iday");
while($rowi = mysqli_fetch_assoc($qin)) {
if ($rowi['iday'] < 10) { $rowi['iday'] = '0'.$rowi['iday']; }
if ($rowi['imonth'] < 10) { $rowi['imonth'] = '0'.$rowi['imonth']; }
$timeline[$rowi['iday'].'.'.$rowi['imonth'].'.'.$rowi['iyear']]['in'] = $rowi['s'];
}

//Сколько выведено
$qin = mysqli_query($connect_db, "SELECT id,sum(s) AS `s`, YEAR(FROM_UNIXTIME(dt)) AS `iyear`, MONTH(FROM_UNIXTIME(dt)) AS `imonth`, DAY(FROM_UNIXTIME(dt)) AS `iday` FROM `".DB_PREFIX."_operations` WHERE usr = '$u_id' AND ty = '3' GROUP BY iyear,imonth,iday");
while($rowi = mysqli_fetch_assoc($qin)) {
if ($rowi['iday'] < 10) { $rowi['iday'] = '0'.$rowi['iday']; }
if ($rowi['imonth'] < 10) { $rowi['imonth'] = '0'.$rowi['imonth']; }
$timeline[$rowi['iday'].'.'.$rowi['imonth'].'.'.$rowi['iyear']]['out'] = $rowi['s'];
}

//Создание строк
$timeline_e = '';
$ispent = '';
$iprofit = '';
$iin = '';
$iout = '';
$ispent_all = '';
$iprofit_all = '';
$iin_all = '';
$iout_all = '';
foreach ($timeline as $key => $val)
{
$timeline_e .= "'".$key."', ";
$ispent .= $timeline[$key]['spent'].", ";
$iprofit .= $timeline[$key]['profit'].", ";
$iin .= $timeline[$key]['in'].", ";
$iout .= $timeline[$key]['out'].", ";
$ispent_all = $ispent_all + $timeline[$key]['spent'];
$iprofit_all = $iprofit_all + $timeline[$key]['profit'];
$iin_all = $iin_all + $timeline[$key]['in'];
$iout_all = $iout_all + $timeline[$key]['out'];
}
$timeline_e = substr($timeline_e,0,-2);
$ispent = substr($ispent,0,-2);
$iprofit = substr($iprofit,0,-2);
$iin = substr($iin,0,-2);
$iout = substr($iout,0,-2);
if (empty($ispent_all)) { $ispent_all = 0; }
if (empty($iprofit_all)) { $iprofit_all = 0; }
if (empty($iin_all)) { $iin_all = 0; }
if (empty($iout_all)) { $iout_all = 0; }
?>

<div class="info-buttons">
<div class="row block">
<div class="col-md-3">
<a href="javascript://" id="iprofit_all" onclick="show_series('2');"><span class="full_label_content"><?php echo number_format($iprofit_all,2,'.',','); ?> руб.</span> <strong class="label full_label_top label-success">Всего заработано</strong></a>
</div>
<div class="col-md-3">
<a href="javascript://" id="ispent_all" onclick="show_series('3');"><span class="full_label_content"><?php echo number_format($ispent_all,2,'.',','); ?> руб.</span> <strong class="label full_label_top label-warning">Всего потрачено</strong></a>
</div>
<div class="col-md-3">
<a href="javascript://" id="iin_all" onclick="show_series('0');"><span class="full_label_content"><?php echo number_format($iin_all,2,'.',','); ?> руб.</span> <strong class="label full_label_top label-info">Всего введено</strong></a>
</div>
<div class="col-md-3">
<a href="javascript://" id="iout_all" onclick="show_series('1');"><span class="full_label_content"><?php echo number_format($iout_all,2,'.',','); ?> руб.</span> <strong class="label full_label_top label-primary">Всего выведено</strong></a>
</div>
</div>
</div>

<div id="wu-stat"></div>

<script type='text/javascript'>
$('#wu-stat').highcharts({
title: {
text: 'Финансовая статистика',
x: -20
},
xAxis: {
title: {
text: 'Дата'
},
categories: [<?php echo $timeline_e; ?>]
},
yAxis: {
title: {
text: 'Руб.'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ' руб.',
shared: true,
crosshairs: true
},
legend: {
borderWidth: 0
},
series: [{
name: 'Введено',
data: [<?php echo $iin; ?>]
},
{
name: 'Выведено',
data: [<?php echo $iout; ?>]
},
{
name: 'Заработано',
data: [<?php echo $iprofit; ?>]
},
{
name: 'Потрачено',
data: [<?php echo $ispent; ?>]
}
]
});

setTimeout(function() {
$('#wu-stat').highcharts().reflow();
}, 1000);

var chart = $('#wu-stat').highcharts();

//Раскрытие графиков
function show_series(id){
if (id == 0) { var tohide = 1; var tohide2 = 2;  var tohide3 = 3; }
if (id == 1) { var tohide = 0; var tohide2 = 2;  var tohide3 = 3; }
if (id == 2) { var tohide = 0; var tohide2 = 1;  var tohide3 = 3; }
if (id == 3) { var tohide = 0; var tohide2 = 1;  var tohide3 = 2; }
chart.series[id].show();
chart.series[tohide].hide();
chart.series[tohide2].hide();
chart.series[tohide3].hide();
};
</script>