<?php
declare(strict_types=1);

/**
ET-Chat Copyright by SEDesign | ET-Chat v3.x.x Lizenz: CCPL | Autor: Evgeni Tcherkasski (info@s-e-d.de)
ET-Chat_T-FISH-MOD Weiterentwicklungen, Optimierungen und Ergänzungen am Code sind das geistige Eigentum von Tommy (Thomas Kuhn) und Harlekin (Bernd Witte).

Die darauf folgende Optimierungen und Ergänzungen am Code sind das Geistige Eigentum von Powermen (Christian K.)
und wird unter der Namenserweiterung M.C.v.x.x.x. geführt. 
PHP 8/9 Ready, XML Parser, PDO Prepared Statements Get Set sicherheit usw.
 */

ob_start();

$style = htmlspecialchars(
    $_SESSION['etchat_' . $this->_prefix . 'style'] ?? '',
    ENT_QUOTES,
    'UTF-8'
);
?>

<style>
.grid { display:grid; grid-template-columns:repeat(auto-fill, minmax(260px,1fr)); gap:20px; }
.box { border:2px solid #333; padding:5px; border-radius:12px; background: rgba(0, 0, 0, 0.5); text-align:center; }
.btn { padding:8px 14px; border:none; cursor:pointer; border-radius:8px; }
.upload { background:#007bff; color:white; }
.reset { background:#ffc107; }
.bigreset { background:#c0392b; color:white; width:100%; }
img.preview { max-width:80px; height:auto; margin-bottom:10px; }
hr.sep { border:0; border-top:2px solid #444; margin:28px 0; }
h3 { margin-top:5px; margin-bottom: 5px;}
p { margin:5px; }
</style>

<script>
function checkFileType(input, defaultFilePath, msgId) {
    const msg = document.getElementById(msgId);
    msg.textContent = '';

    if (!input.files || !input.files[0]) return;

    const file = input.files[0];
    const uploadedExt = file.name.split('.').pop().toLowerCase();
    const defaultExt = defaultFilePath.split('.').pop().toLowerCase();

    if (uploadedExt !== defaultExt) {
        msg.textContent = `Ungültiges Dateiformat! Nur .${defaultExt} erlaubt.`;
        input.value = '';
    }
}
</script>

<form method="post">
	<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
    <button class="bigreset" name="reset_all">
        <h3>Alle Icons zurücksetzen</h3>
    </button>
</form>

<!-- SYSTEM ICONS -->
<hr>
<h2>System Icons</h2>
<div class="grid">
<?php foreach ($icons as $icon): ?>
    <?php if (strpos($icon['id'], 'status_') === 0) continue; ?>

    <?php
        $status = file_exists($icon['path']) ? "Vorhanden" : "Fehlt";
        $size = file_exists($icon['path']) ? $this->formatBytes(filesize($icon['path'])) : "-";
        $changed = file_exists($icon['path']) ? date("d.m.Y H:i", filemtime($icon['path'])) : "-";
    ?>

    <div class="box">
        <h3><?= e($icon['title']) ?></h3>

        <img class="preview" src="<?= e($icon['path']) ?>?v=<?= time() ?>" alt="Icon">

        <p>Status: <b><?= $status ?></b></p>
        <p>Größe: <?= $size ?></p>
        <p>Geändert: <?= $changed ?></p>
        <p>Standard: <?= file_exists($icon['default']) ? "Vorhanden" : "Fehlt" ?></p>

        <form method="post" enctype="multipart/form-data">
			<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <input type="hidden" name="id" value="<?= e($icon['id']) ?>">

            <input type="file" name="file" accept="image/*"
                   onchange="checkFileType(this, '<?= e($icon['default']) ?>', 'msg_<?= e($icon['id']) ?>')"><br>

            <span id="msg_<?= e($icon['id']) ?>" style="color:red;font-weight:bold;"></span><br>

            <button class="btn upload" name="upload">Hochladen</button>
            <button class="btn reset" name="reset">Zurücksetzen</button>
        </form>
    </div>
<?php endforeach; ?>
</div>

<!-- STATUS ICONS -->
<hr>
<h2>Status Icons</h2>
<div class="grid">
<?php foreach ($icons as $icon): ?>
    <?php if (strpos($icon['id'], 'status_') !== 0) continue; ?>

    <?php
        $status = file_exists($icon['path']) ? "Vorhanden" : "Fehlt";
        $size = file_exists($icon['path']) ? $this->formatBytes(filesize($icon['path'])) : "-";
        $changed = file_exists($icon['path']) ? date("d.m.Y H:i", filemtime($icon['path'])) : "-";
    ?>

    <div class="box">
        <h3><?= e($icon['title']) ?></h3>

        <img class="preview" src="<?= e($icon['path']) ?>?v=<?= time() ?>" alt="Icon">

        <p>Status: <b><?= $status ?></b></p>
        <p>Größe: <?= $size ?></p>
        <p>Geändert: <?= $changed ?></p>
        <p>Standard: <?= file_exists($icon['default']) ? "Vorhanden" : "Fehlt" ?></p>

        <form method="post" enctype="multipart/form-data">
			<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <input type="hidden" name="id" value="<?= e($icon['id']) ?>">

            <input type="file" name="file" accept="image/*"
                   onchange="checkFileType(this, '<?= e($icon['default']) ?>', 'msg_<?= e($icon['id']) ?>')"><br>

            <span id="msg_<?= e($icon['id']) ?>" style="color:red;font-weight:bold;"></span><br>

            <button class="btn upload">Hochladen</button>
            <button class="btn reset">Zurücksetzen</button>
        </form>
    </div>
<?php endforeach; ?>
</div>

<!-- MENÜ ICONS -->
<hr>
<h2>Menü Icons & Banner</h2>
<p>Ordner: <code>/img/menu/</code></p>

<div class="grid">
<?php
$menuDir = "img/menu/";
if (is_dir($menuDir)) {
    foreach (array_diff(scandir($menuDir), ['.','..']) as $file) {
        if (!preg_match('/\.(png|jpe?g|gif)$/i', $file)) continue;
        $full = $menuDir.$file;
?>
    <div class="box">
        <h3><?= e($file) ?></h3>
        <img class="preview" src="<?= e($full) ?>?v=<?= time() ?>">

        <p>Größe: <?= $this->formatBytes(filesize($full)) ?></p>
        <p>Geändert: <?= date("d.m.Y H:i", filemtime($full)) ?></p>

        <form method="post">
			<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <input type="hidden" name="delete_menu" value="<?= e($file) ?>">
            <button class="btn reset" onclick="return confirm('Wirklich löschen?')">Löschen</button>
        </form>
    </div>
<?php
    }
}
?>
</div>

<form method="post" enctype="multipart/form-data" style="margin-top:12px;">
	<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
    <input type="file" name="upload_menu" accept="image/*" required>
    <button class="btn upload">Hochladen</button>
</form>

<!-- HINTERGRUND -->
<hr>
<h2>Hintergründe</h2>

<?php
$styleFolder = "styles/" . $style . "/hintergrund/";

if (is_dir($styleFolder)) {
    echo "<p>Ordner: <code>$styleFolder</code></p>";
    echo '<div class="grid">';

    foreach (array_diff(scandir($styleFolder), ['.','..']) as $file) {
        if (!preg_match('/\.(png|jpe?g|gif)$/i', $file)) continue;
        $full = $styleFolder . $file;
?>
    <div class="box">
        <h3><?= e($file) ?></h3>
        <img class="preview" src="<?= e($full) ?>?v=<?= time() ?>">

        <p>Größe: <?= $this->formatBytes(filesize($full)) ?></p>
        <p>Geändert: <?= date("d.m.Y H:i", filemtime($full)) ?></p>

        <form method="post">
			<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <input type="hidden" name="delete_bg" value="<?= e($file) ?>">
            <button class="btn reset" onclick="return confirm('Wirklich löschen?')">Löschen</button>
        </form>
    </div>
<?php
    }

    echo "</div>";
} else {
    echo "<p>Ordner existiert nicht: <code>$styleFolder</code></p>";
}
?>

<?php if (is_dir($styleFolder) && is_writable($styleFolder)): ?>
<form method="post" enctype="multipart/form-data" style="margin-top:12px;">
    <input type="file" name="upload_bg" accept="image/*" required>
    <button class="btn upload">Hochladen</button>
</form>
<?php endif; ?>

<!-- IMAGES -->
<hr>
<h2>Images</h2>
<p>Ordner: <code>/img/images/</code></p>

<div class="grid">
<?php
$imgDir = "img/images/";
if (is_dir($imgDir)) {
    foreach (array_diff(scandir($imgDir), ['.','..']) as $file) {
        if (!preg_match('/\.(png|jpe?g|gif)$/i', $file)) continue;
        $full = $imgDir.$file;
?>
    <div class="box">
        <h3><?= e($file) ?></h3>
        <img class="preview" src="<?= e($full) ?>?v=<?= time() ?>">

        <p>Größe: <?= $this->formatBytes(filesize($full)) ?></p>
        <p>Geändert: <?= date("d.m.Y H:i", filemtime($full)) ?></p>

        <form method="post">
			<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <input type="hidden" name="delete_image" value="<?= e($file) ?>">
            <button class="btn reset" onclick="return confirm('Wirklich löschen?')">Löschen</button>
        </form>
    </div>
<?php
    }
}
?>
</div>

<form method="post" enctype="multipart/form-data" style="margin-top:12px;">
	<input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
    <input type="file" name="upload_image" accept="image/*" required>
    <button class="btn upload">Hochladen</button>
</form>

<?php
$content = ob_get_clean();

$pageTitle = "Icon Bild Verwaltung";
$backLink  = './?AdminIndex';
$footerNote = 'Beachte, dass Änderungen im Chat etwas verzögert ankommen bzw. ein Reload (Strg + F5) nötig ist.';


include "admin_master_template.php";