<?php

include_once('../global.php');
include_once('../style.php');  
ShowHeader('Výpis z historie SunriseChatu','Historie Sunrisechatu');

if(array_key_exists('date',$_GET)) $Date = $_GET['date']; else $Date = date('Y-m-d');
$DateParts = explode('-',$Date);
  
$DbResult = $Database->select('sunrisechat_history','MAX(time), MIN(time)');
$RowTotal = $DbResult->fetch_array();
  
$StartDateTimeParts = explode(' ',$RowTotal['MIN(time)']);
$StartDateParts = explode('-',$StartDateTimeParts[0]);
$EndDateTimeParts = explode(' ',$RowTotal['MAX(time)']);
$EndDateParts = explode('-',$EndDateTimeParts[0]);
  
function dechexr($Num)
{
  $Num = dechex($Num);
  return(substr($Num,4,2).substr($Num,2,2).substr($Num,0,2));
}
  
if(!array_key_exists('year', $_SESSION)) $_SESSION['year'] = date('Y', time());
if(array_key_exists('year', $_GET)) $_SESSION['year'] = addslashes($_GET['year']);
  
if(!array_key_exists('month', $_SESSION)) $_SESSION['month'] = date('n', time());
if(array_key_exists('month', $_GET)) $_SESSION['month'] = addslashes($_GET['month']);
  
echo('<div style="font-size: small;">');
for($Year = $EndDateParts[0]; $Year >= $StartDateParts[0]; $Year--)
{
  if($_SESSION['year'] == $Year)
  {
    echo('<div style="border-style: solid; border-bottom-color: gray; border-width: 0 0 1 0; margin-bottom: 3px; padding-bottom: 2px;">'.$Year.'</div>
    <div style="margin-left: 15px; margin-right: 15px;">');
    if($Year == $StartDateParts[0]) $StartMonth = ($StartDateParts[1]+0); else $StartMonth = 1;
    if($Year == $EndDateParts[0]) $EndMonth = ($EndDateParts[1]+0); else $EndMonth = 12;
    for($Month = $EndMonth; $Month >= $StartMonth; $Month--)
    {
      if($_SESSION['month'] == $Month)
      {
      //      echo('<div><span>');
      echo('<div style="border-style: solid; border-bottom-color: gray; border-width: 0 0 1 0; margin-bottom: 3px; padding-bottom: 2px;">'.$MonthNames[$Month].'
      <span style="position: absolute; left: 100px;">');
      if(($Year == $StartDateParts[0]) and ($Month == $StartDateParts[1])) $StartDay = ($StartDateParts[2]+0); else $StartDay = 1;
      if(($Year == $EndDateParts[0]) and ($Month == $EndDateParts[1])) $EndDay = ($EndDateParts[2]+0); else $EndDay = date('t',mktime(0,0,0,$Month,0,$Year));
      for($Day = $StartDay; $Day <= $EndDay; $Day++) 
      { 
//        $Date = $Year.'-'.$Month.'-'.$Day;
        //DB_Select('sunrisechat_history','COUNT(*)',"roomtype=0 AND time>'".$Date." 00:00:00' AND time<'".$Date." //23:59:59'");
  //      $CR = DB_Row();
//        $Text = '<a href="history.php?date='.$Year.'-'.$Month.'-'.$Day.'">'.$Day.'('.$CR['COUNT(*)'].')</a> ';
        $Text = '<a href="history.php?date='.$Year.'-'.$Month.'-'.$Day.'">'.$Day.'</a> ';
        flush();
        if(($DateParts[0] == $Year) and ($DateParts[1] == $Month) and ($DateParts[2] == $Day)) $Text = '<strong style="border-style: solid; border-width: 1; border-color: black; padding-left: 3px;">'.$Text.'</strong>';
        echo($Text);
      }
      echo('</span></div>');
      } else echo('<span style="border-style: solid; border-bottom-color: gray; border-width: 0 0 0 0; margin-bottom: 3px; padding-bottom: 2px;"><a href="?month='.$Month.'">'.$MonthNames[$Month].'</a></span> '); 
    }
    echo('</div>');
    } else echo('<div style="border-style: solid; border-bottom-color: gray; border-width: 0 0 1 0; margin-bottom: 3px; padding-bottom: 2px;"><a href="?year='.$Year.'">'.$Year.'</a></div>');
  }
  echo('</div><div style="border-style: solid; border-bottom-color: gray; border-width: 0 0 1 0; margin-bottom: 3px; padding-bottom: 2px;"></div>');
  
    $DbResult = $Database->select('sunrisechat_history','nick,color,text,UNIX_TIMESTAMP(time)',"roomtype=0 AND time>'".$Date." 00:00:00' AND time<'".$Date." 23:59:59' ORDER BY time DESC");
    //echo('Vpis posledn�h 100 zpr�:<br>');
    echo('<div style="font-size: small;">');
    while($Row = $DbResult->fetch_array())
    {
      $Text = $Row['text'];;
      // StrTr($Row['text'], "\x8A\x8D\x8E\x9A\x9D\x9E", "\xA9\xAB\xAE\xB9\xBB\xBE"); 
      echo('['.date('d.m.Y H:i:s',$Row['UNIX_TIMESTAMP(time)']).'] <span style="color: #'.dechexr($Row['color']).'"><strong>&lt;'.$Row['nick'].'&gt;</strong> '.(htmlspecialchars($Text)).'</span><br>');
      flush();
    }
    echo('</div>');
  
  ShowFooter();
?>
