<?php
/*================
  Beállítások*/

//$VersionBuild = 'ld' . time();     /* (Éles => l, Teszt => t) + (Aktív => a, Demó => d, BETA => b) + Timestamp */

$jsonFile = __DIR__ . '/managers/version.json';

$lastBuild = '';
if (file_exists($jsonFile)) {
  $jsonContent = file_get_contents($jsonFile);
  $versionData = json_decode($jsonContent, true);
  if (!empty($versionData['updates'])) {
    $lastBuild = $versionData['updates'][0]['build'];
  }
}

$VersionBuild = $lastBuild;

/*Beállítások vége
  ================*/

$VersionNumber = 'v'.date('y.n.j.', substr($VersionBuild, 2));

$VersionStatusBadgeColor = '#28a745';
$VersionStatus = 'Aktív';
if (substr($VersionBuild, 1, 1) == "d") {
  $VersionNumber = 'DEMO '.$VersionNumber;
  $VersionStatusBadgeColor = '#00b4d8';
  $VersionStatus = 'Demó';
} else if (substr($VersionBuild, 1, 1) == "b") {
  $VersionNumber = 'BETA '.$VersionNumber;
  $VersionStatusBadgeColor = '#28a78e';
  $VersionStatus = 'Béta';
}

$VersionDate = date('Y.m.d.', substr($VersionBuild, 2));

$VerisonEnvironment = 'Teszt';
if (substr($VersionBuild, 0, 1) == "l") {
  $VerisonEnvironment = 'Éles';
}

if (isset($_GET["v"]) && $_GET["v"] == 1) {
  echo $VersionNumber;
  exit();
} else {
  include 'managers/cookie.php';
}

$localIP = trim(shell_exec('hostname -I | cut -d" " -f1'));

?>
<!DOCTYPE html>
<html lang="hu">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Rendszer Verzió</title>
  <link rel="stylesheet" href="css/login.css">
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}

		body {
      background: linear-gradient(135deg, var(--bgcolorlight) 0%, var(--bgcolordark) 100%);
      min-height: 100vh;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.version-card {
			background: white;
			border-radius: 16px;
			box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
			padding: 2rem;
			max-width: 400px;
			width: 90%;
			text-align: center;
			position: relative;
			overflow: hidden;
		}

		.version-card::before {
			content: '';
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			height: 4px;
			background: linear-gradient(90deg, var(--bgcolorlight), var(--bgcolordark));
		}

		.system-icon {
			width: 80px;
			height: 80px;
			background: linear-gradient(135deg, var(--bgcolorlight), var(--bgcolordark));
			border-radius: 50%;
			margin: 0 auto 1.5rem;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 2rem;
			color: white;
		}

		.system-name {
			font-size: 1.5rem;
			font-weight: 700;
			color: #2c3e50;
			margin-bottom: 0.5rem;
		}

		.version-number {
			font-size: 2rem;
			font-weight: 300;
			color: var(--bgcolordark);
			margin-bottom: 1rem;
		}

		.version-info {
			background: #f8f9fa;
			border-radius: 8px;
			padding: 1rem;
			margin-bottom: 1.5rem;
		}

		.info-row {
			display: flex;
			justify-content: space-between;
			margin-bottom: 0.5rem;
			font-size: 0.9rem;
		}

		.info-row:last-child {
			margin-bottom: 0;
		}

		.info-label {
			color: #6c757d;
			font-weight: 500;
		}

		.info-value {
			color: #2c3e50;
			font-weight: 600;
		}

		.status-badge {
			background: <?php echo $VersionStatusBadgeColor;?>;
			color: white;
			padding: 0.25rem 0.75rem;
			border-radius: 12px;
			font-size: 0.8rem;
			font-weight: 600;
			display: inline-block;
		}

		.build-info {
			margin-top: 1.5rem;
			padding-top: 1rem;
			border-top: 1px solid #e9ecef;
			color: #6c757d;
			font-size: 0.8rem;
		}
    .settings {
      position: fixed;
      top: 15px;
      right: 15px;
    }
    .settings a {
      text-decoration: none;
      opacity: 0.6;
      cursor: pointer;
      color: #f5f5f5;
      font-size: 24px;
      transition: 0.3s;
    }
    .settings a:hover {
      opacity: 1;
    }

    .devplan {
      position: absolute;
      cursor: pointer;
      width: 48px;
      height: 48px;
      right: 5px;
      bottom: 5px;
    }
    .devplan img {
      filter: drop-shadow(-1px -1px 2px rgb(0 0 0 / 0.1));
      transition: 0.2s;
    }
    .devplan img:hover {
      filter: drop-shadow(2px 2px 2px rgb(0 0 0 / 0.5));
    }
    .devplan img:active {
      filter: drop-shadow(0px 0px 2px rgb(0 0 0 / 0.2));
    }
    .loading {
      position: fixed;
      width: 100%;
      height: 100%;
      z-index: 999;
      opacity: 0.8;
      background: #333;
      display: none;
    }
	</style>
</head>
<body>
  <div class="loading" id="loading"></div>
  <div class="settings" style="display: <?php if (UserHasPerm("god_profile") && $userID == 1 && $localIP == '192.168.15.10') { echo "unset"; } else { echo "none"; } ?>">
    <a href="#" onclick="OpenEditor()">&#9998;</a>
  </div>
  <div class="settings" style="display: <?php if (UserHasPerm("god_profile") && $userID == 1 && $localIP == '192.168.15.11') { echo "unset"; } else { echo "none"; } ?>">
    <a href="#" onclick="SynchDatabase()">&#x267A;</a>
  </div>
	<div class="version-card">
		<div class="system-icon">
      <img src="data:image/png;base64, " alt="saturn-planet">
		</div>

		<h1 class="system-name">Szaturnusz</h1>
		<div class="version-number"><?php echo $VersionNumber;?></div>

		<div class="version-info">
			<div class="info-row">
				<span class="info-label">Kiadás dátuma:</span>
				<span class="info-value"><?php echo $VersionDate;?></span>
			</div>
			<div class="info-row">
				<span class="info-label">Build szám:</span>
				<span class="info-value">#<?php echo $VersionBuild;?></span>
			</div>
			<div class="info-row">
				<span class="info-label">Környezet:</span>
				<span class="info-value"><?php echo $VerisonEnvironment;?></span>
			</div>
			<div class="info-row">
				<span class="info-label">Státusz:</span>
				<span class="status-badge"><?php echo $VersionStatus;?></span>
			</div>
		</div>

		<div class="build-info">
			<span style="font-weight: bold; ">Legutóbbi frissítések:</span>

      <?php
        $jsonPath = __DIR__ . '/managers/version.json';
        $jsonData = file_get_contents($jsonPath);
        $versionData = json_decode($jsonData, true);

        if (!empty($versionData['updates'])) {
            $updates = array_slice($versionData['updates'], 0, 3);

            echo '<div style="max-height: 150px; overflow-y: auto; padding: 0.75rem; border-radius: 8px; background: #f8f9fa; text-align: left;">';
            foreach ($updates as $update) {
                echo '<div style="margin-bottom: 1rem;">';
                echo '<strong>Build szám:  #' . htmlspecialchars($update['build']) . '</strong><small style="opacity: 0.8;"> ('.date('Y.m.d.', substr(htmlspecialchars($update['build']), 2)).')</small><br>';
                echo '<ul style="margin: 0 0 0 1rem; padding: 0;">';
                foreach ($update['changes'] as $change) {
                    echo '<li>' . htmlspecialchars($change) . '</li>';
                }
                echo '</ul></div>';
            }
            echo '</div>';
        }
      ?>

		</div>

    <div class="devplan">
      <a href="dashboard/devplan">
        <img title="Fejlesztési tervek" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEBklEQVR4nO2Zy1NTZxjGWbT/DHIXqhJR2kWnTqfFThfddNEarkpVRHS6cbpotdVOa2dclE5RO4AQLpJ7Tk5ITi6EQCzQKAQGkRKjMDRckz/g6eTzO1JPAu30+85hcPhmfpPdeZ/fubzPIjk5++c1PK2trW/e7TJ+f9dgSfzWY02099luSJL0Rs5eOR29tuud/Xbcuy+ga8CJbqMIg1H8blfCjFyufD906fiz0OVKDF86npWOPhs6+x2vBja50Gt1l/eYpaN9Ngn9di/uO3wYEPwwOodgEoMINB99Bb/MBR38Tbp4oFl3glkg2HIsrgwcTNNy7CXdJhEGsws9Fjd6rR7Ige332j6ydd75WA5sHhyGxR2CzTMKuxTODP0PfBd08DaVP2UWkEMvT4eRSqWysnWH/RgQAjCJQzC7grAMDsPqHnkZ2OF9AMH3O5z+MYiBiW2vtzQ1Al9TOYHHEyB3ebthabYCh7YJPA5xaAKu4B8YHI7AHXoIz8jkjtf0NpUTNBEIhB9tBfaPwRlQBn5EAkujU/CFo/A/mMZ49M+dBc4fITALDF2sQJqdhqmBdP4IYe8KnDtMYBYIXKwg20JrAc+5wwR2AbrmNBc4e4jALCDvaOWAiblV6H95gk9/fqyKgPvsIQK7AC0V5YD48iYMwSVU3ZxVTWDwi7fYBdLh04WSbYh3MpEh8GF3Eu+0b1I2KOsv6FijrFJWCFU9iYxrp8PzEaCF8l8Fqgz8BFyNZewCciMqBwSjK3j76gyKr0zh8zb+34GrsYyTAC0UrbeQq7EM4plSdgFplwTEM6V8BORC2Q0Bp5oCkfk1VP0wC93XUXQEF7kLOE8fJDALyIWiHPD8r02srCfRHliC/vYcdwHh9EGCagJpEmub+OTWLCxjy9x7QGgoITALyIWiHLC8msRnrU/IE1CjBxwNJQTVBG4Jz1F2JYp3b8yguWue+yvkqC8mMAvIhaL1FrLXFxP2rkBdEYFZQC6UbEMmF9YQja2rImCrKyKoJuCbXEH1r/Oo+GYaN4U4f4HaQgKzgFwo2Yake+BLwwJauvl/xNbaQoKqAid/nEXpV1G4HvLvAWtNAYFZgDRiQ0nGgGTyxe8d7yI++GmGew9YagoI7AK0EZUDbnsWUdc2j8pr0/jWGuP+Clmq8wnMAqQR64szBqxvJDE+t4rHzzZU2ULm6nwCuwAtFDVCpv5NQJ/HLkAasa5IcwGTPo/ALkALZTcEjKcOsAuQRqwt1FzAeOoAJwFaKLEY/02T2oZYbIGfgNyIoVBIU4zcBGihhIVeRCIRTRi1G/gJmGsK4qQVabGYs0E3RgY0xP8nl/1PPnNN/gmTPi+uDJ01MN0cfMh9atLnvscssH/2z2t+/gYsHCEPOwRHBgAAAABJRU5ErkJggg==" alt="shortlist">
      </a>
    </div>

	</div>
  <script type="text/javascript">
    function OpenEditor() {
      const popup = window.open('./dashboard/version_controll', 'Verzió szerkesztő', 'width=610,height=860');

      const timer = setInterval(() => {
        if (popup == null) {
          clearInterval(timer);
          console.log("Nem lehet elérni a megnyílt oldalt");
        } else if (popup.closed) {
          clearInterval(timer);
          location.reload();
        }
      }, 500);

    }

    async function SynchDatabase() {
      document.getElementById("loading").style.display = 'block';
      try {
        await fetch('./managers/synch.php?mode=importer');
        location.reload();
      } catch (error) {
        console.error('Hiba a szinkronizálás során:', error);
      }
    }
  </script>
</body>
</html>
