<?php
date_default_timezone_set('Europe/Berlin');
ini_set('session.gc_maxlifetime', 32400);
ini_set('session.cookie_lifetime', 32400);
ini_set("display_errors",true);
error_reporting(E_ALL);
session_start();
if(isset($_GET['logoff']) and $_GET['logoff'] == 1 ) { session_destroy(); session_start();}
$_SESSION['debug'] = false;
  include("init.php");
  $dateiname = 'History'; // Pfad zur Datei

// Dateiinhalt zeilenweise in ein Array lesen
$zeilen = file($dateiname);

if ($zeilen !== false && !empty($zeilen)) {
    // Erste Zeile aus dem Array holen
    $ersteZeile = trim($zeilen[0]); // trim entfernt Leerzeichen am Anfang und Ende
    // Werte aus der ersten Zeile extrahieren
    $werte = explode(' ', $ersteZeile);
    if (!empty($werte)) {
        // Ersten Wert (0.22) in einer Variablen speichern
        $version = $werte[0];
    } else {
        $version=0;
    }
}

  // require_once("eaclass.php");
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php
    if(isset($_SESSION['lbname'])) {
      echo $_SESSION['lbname'];
    } else {
      echo "LOGBUCH";
    }
    ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style media="screen">
    <?php
    echo "
    body {
      background-color: #aaaaaa;
      /* color: #000; */
    }

    a {
      color: #000000;
    }
    a:active, a:visited {
      color: #000000;
      font-weight: bold;
      text-decoration: none;
    }
    a:hover {
      color: #555555;
      text-decoration: underline;
    }
    #del_entry {
      background-color: #ff0000;
      margin-left: 220px;
    }
    .container {
      width: 100%;
      /* border: 1px solid #000000; */
     display: grid;
     ";
      if(isset($_SESSION['dauereinaus']) && $_SESSION['dauereinaus'] == "1" ){

        echo "grid-template-columns: 90px 1fr 50px 1fr;";
      } else {
        echo "grid-template-columns: 90px 1fr 1fr;";
      }
    echo "
    }
    .liste {
      border-bottom: 1px dotted #000000;
    }
    .heller {
      background-color: #bbbbbb;
      border-bottom: 1px dotted #000000;
    }
    .dunkler {
      background-color: #aaaaaa;
      border-bottom: 1px dotted #000000;
    }
    ";
    ?>
    div.rechts-oben {
      font-size: 12px;
      position: fixed;
      top: 0;
      right: 0;
      padding: 0px;
    }
    div.rechts-oben a {
      color: #009900;
      text-decoration: none;
    }
    #menu {
      display: flex;
      align-items: center;
      gap: 12px;
      flex-wrap: wrap;
    }
    #menu a {
      display: inline-flex;
      align-items: center;
      gap: 4px;
      padding: 6px 10px;
      border-radius: 6px;
      text-decoration: none;
      color: #000;
      transition: background-color 0.2s;
    }
    #menu a:hover {
      background-color: rgba(0,0,0,0.1);
      text-decoration: none;
    }
    #menu a svg {
      width: 20px;
      height: 20px;
      fill: none;
      stroke: currentColor;
      stroke-width: 2;
      stroke-linecap: round;
      stroke-linejoin: round;
    }
    #menu .suche-wrapper {
      position: relative;
      margin-left: auto;
      display: inline-flex;
      align-items: center;
    }
    #menu .suche-wrapper svg.suche-icon {
      position: absolute;
      left: 8px;
      width: 16px;
      height: 16px;
      fill: none;
      stroke: #888;
      stroke-width: 2;
      stroke-linecap: round;
      stroke-linejoin: round;
      pointer-events: none;
    }
    #suche-input {
      padding: 5px 8px 5px 28px;
      border: 1px solid #888;
      border-radius: 4px;
      font-size: 14px;
      width: 180px;
      outline: none;
    }
    #suche-input:focus {
      border-color: #555;
    }
    #suche-ergebnis {
      display: none;
      position: absolute;
      top: 100%;
      left: 0;
      right: 0;
      background: #fff;
      border: 1px solid #888;
      border-top: none;
      border-radius: 0 0 4px 4px;
      max-height: 400px;
      overflow-y: auto;
      z-index: 100;
      box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    }
    #suche-ergebnis .such-kategorie {
      padding: 4px 10px;
      font-size: 11px;
      font-weight: bold;
      color: #666;
      background: #eee;
      text-transform: uppercase;
    }
    #suche-ergebnis .such-treffer {
      padding: 6px 10px;
      cursor: pointer;
      border-bottom: 1px solid #eee;
      font-size: 13px;
    }
    #suche-ergebnis .such-treffer:hover {
      background: #ddd;
    }
    #suche-ergebnis .such-treffer .such-ort {
      font-weight: bold;
      margin-right: 8px;
    }
    #suche-ergebnis .such-treffer .such-datum {
      color: #777;
      font-size: 11px;
      float: right;
    }
    #suche-ergebnis .such-leer {
      padding: 10px;
      color: #999;
      text-align: center;
      font-size: 13px;
    }
    </style>
  </head>
  <body>
    <div class="rechts-oben"><a href="index.php?cont=history">
      | eblok 2021 - <?php echo date("Y");?>
      | <?php echo "V ".$version." |"; ?></a>
    </div>

    <?php
    if(isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] == "menu"){
    echo "<h2>".$_SESSION['lbname']."</h2>";
    if(isset($_SESSION['readonly']) && $_SESSION['readonly'] == "1") { echo "Read Only Modus<br /><br />";}

   ?>
    <div id="menu">
      <?php
            if(isset($_SESSION['readonly']) && $_SESSION['readonly'] == "") {
              echo '<a href="index.php?cont=eingabe" title="Neuer Eintrag"><svg viewBox="0 0 24 24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg></a>';
              echo '<a href="index.php?cont=liste" title="Liste"><svg viewBox="0 0 24 24"><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><line x1="3" y1="6" x2="3.01" y2="6"/><line x1="3" y1="12" x2="3.01" y2="12"/><line x1="3" y1="18" x2="3.01" y2="18"/></svg></a>';
              echo '<a href="index.php?cont=gruppen" title="Gruppen"><svg viewBox="0 0 24 24"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/></svg></a>';
              echo '<a href="index.php?cont=konfig" title="Konfiguration"><svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg></a>';
            }
            echo '<a href="index.php?logoff=1" title="Abmelden"><svg viewBox="0 0 24 24"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg></a>';
      ?>
      <div class="suche-wrapper">
        <svg class="suche-icon" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
        <input type="text" id="suche-input" placeholder="Suchen..." autocomplete="off">
        <div id="suche-ergebnis"></div>
      </div>
    </div>
    <br />
    <br />
    <div id="inhalt">
    <?php
    if(isset($_SESSION['prefix'][3])) $cont = $_SESSION['prefix'][3];
    if(isset($_GET['cont'])) $cont = $_GET['cont'];
    if(isset($cont)) {
      switch ($cont) {
        case 'eingabe':
          $LB->eingabe();
          break;
        case "konfig":
          $LB->konfig();
          break;
        case "userkonfig":
          $USER->user_config();
          break;
        case 'liste':
          $LB->liste($_GET,$_POST);
          break;
        case 'gruppen':
          $LB->gruppen($_GET,$_POST);
          break;
        case 'show_group':
          $LB->show_group($_POST);
          break;
        case 'aendern':
          $LB->aendern($_GET);
          break;
        case 'schreiben':
          $LB->schreiben($_POST);
          break;
        case 'updaten':
          $LB->updaten($_POST);
          break;
        case 'adduser':
          $LB->addusergui($_POST);
          break;
        case 'history':
          $LB->history();
          break;

        // case "admin_update":
    		// 	if (isset($_GET['version'])) {$v = $_GET['version']; } else { $v = "";}
    		// 	$ADMIN->update($v);
  			//   break;
        // case "bereich":
    		// 	$ADMIN->admin_bereich_form($konfig['tbl_config'],$spalten['config']);
    		// 	break;
        //

        default:
          # code...
          break;
      }

    }
    // echo "<hr />\n";
    ?>
  </div>
  <?php
  }
  if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] == false) {
    $USER->user_login();
  }

  ?>
  <script>
  (function(){
    var input = document.getElementById('suche-input');
    var box = document.getElementById('suche-ergebnis');
    if(!input || !box) return;
    var timer = null;

    input.addEventListener('input', function(){
      clearTimeout(timer);
      var q = input.value.trim();
      if(q.length < 2) { box.style.display = 'none'; box.innerHTML = ''; return; }
      timer = setTimeout(function(){ doSuche(q); }, 300);
    });

    input.addEventListener('keydown', function(e){
      if(e.key === 'Escape') { box.style.display = 'none'; input.blur(); }
    });

    document.addEventListener('click', function(e){
      if(!e.target.closest('.suche-wrapper')) box.style.display = 'none';
    });

    function doSuche(q) {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', 'ajax_suche.php?q=' + encodeURIComponent(q));
      xhr.onload = function(){
        if(xhr.status !== 200) return;
        var data = JSON.parse(xhr.responseText);
        var html = '';

        if(data.gruppen.length > 0) {
          html += '<div class="such-kategorie">Gruppen</div>';
          for(var i = 0; i < data.gruppen.length; i++) {
            var g = data.gruppen[i];
            html += '<div class="such-treffer" onclick="sucheGruppe('+g.id+')">';
            html += '<span class="such-ort">' + esc(g.name) + '</span>';
            html += '</div>';
          }
        }

        if(data.eintraege.length > 0) {
          html += '<div class="such-kategorie">Eintr&auml;ge (' + data.eintraege.length + ')</div>';
          var max = Math.min(data.eintraege.length, 50);
          for(var i = 0; i < max; i++) {
            var e = data.eintraege[i];
            var ro = data.readonly;
            if(ro === '0' || ro === 0) {
              html += '<div class="such-treffer" onclick="location.href=\'index.php?cont=aendern&id='+e.id+'\'">';
            } else {
              html += '<div class="such-treffer">';
            }
            html += '<span class="such-datum">' + esc(e.datum_fmt) + '</span>';
            html += '<span class="such-ort">' + esc(e.ortname) + '</span> ';
            html += esc(e.text);
            html += '</div>';
          }
          if(data.eintraege.length > 50) {
            html += '<div class="such-leer">... und ' + (data.eintraege.length - 50) + ' weitere</div>';
          }
        }

        if(data.gruppen.length === 0 && data.eintraege.length === 0) {
          html = '<div class="such-leer">Keine Treffer</div>';
        }

        box.innerHTML = html;
        box.style.display = 'block';
      };
      xhr.send();
    }

    function esc(s) {
      var d = document.createElement('div');
      d.appendChild(document.createTextNode(s));
      return d.innerHTML;
    }

    window.sucheGruppe = function(id) {
      var f = document.createElement('form');
      f.method = 'post';
      f.action = 'index.php?cont=show_group';
      var inp = document.createElement('input');
      inp.type = 'hidden'; inp.name = 'gruppe'; inp.value = id;
      f.appendChild(inp);
      document.body.appendChild(f);
      f.submit();
    };
  })();
  </script>
  </body>
</html>
<?php
// $EA->liste();
 ?>
