<?php
use function App\Core\{e, url};

$section = 'media';
$settings = $settings ?? [];
$mediaFiles = $mediaFiles ?? [];
$logoTargets = [
    'site_logo' => 'Ana logo',
    'music_logo' => 'Music logo',
    'studio_logo' => 'Studio logo',
    'zpanel_logo' => 'ZPanel logo',
    'favicon' => 'Favicon',
];
$formatSize = static function (mixed $bytes): string {
    $bytes = (int) $bytes;
    if ($bytes >= 1024 * 1024) {
        return number_format($bytes / 1024 / 1024, 2, ',', '.') . ' MB';
    }

    return number_format(max(0, $bytes) / 1024, 1, ',', '.') . ' KB';
};
?>
<div class="panel zpanel zpanel-app">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body zpanel-main" data-zpanel-page>
        <div class="zpanel-head">
            <div>
                <p class="eyebrow">Genel</p>
                <h1>Medya / Logo Yonetimi</h1>
                <p class="muted">Logo, favicon ve site medya dosyalarini tek yerden yonet.</p>
            </div>
        </div>

        <div class="zpanel-grid two media-manager">
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Yeni medya yukle</h2></div>
                <form class="zpanel-form" data-zpanel-form action="/api/zpanel/media/upload" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                    <label>Kullanim alani
                        <select name="context">
                            <option value="general">Genel medya</option>
                            <?php foreach ($logoTargets as $key => $label): ?>
                                <option value="<?= e($key) ?>"><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>Dosya
                        <input type="file" name="file" accept=".jpg,.jpeg,.png,.webp,.gif,image/jpeg,image/png,image/webp,image/gif" required>
                    </label>
                    <p class="muted">JPG, PNG, WEBP ve GIF kabul edilir. SVG ve calistirilabilir dosyalar guvenlik nedeniyle kapali.</p>
                    <button class="pill primary" type="submit">Yukle</button>
                </form>
            </section>

            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Aktif logo ayarlari</h2><a href="/zpanel/settings">Site ayarlari</a></div>
                <div class="media-logo-list">
                    <?php foreach ($logoTargets as $key => $label): ?>
                        <?php $path = (string) ($settings[$key] ?? ''); ?>
                        <article>
                            <span><strong><?= e($label) ?></strong><small><?= e($path ?: 'Varsayilan dosya kullaniliyor') ?></small></span>
                            <?php if ($path !== ''): ?><img src="<?= e(url($path)) ?>" alt="" loading="lazy"><?php endif; ?>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        </div>

        <section class="zpanel-card">
            <div class="zpanel-card-head"><h2>Medya dosyalari</h2><span><?= count($mediaFiles) ?> kayit</span></div>
            <div class="table-scroll">
                <table class="zpanel-table media-table">
                    <thead><tr><th>Onizleme</th><th>Dosya</th><th>Tur</th><th>Boyut</th><th>Yukleme</th><th>URL</th><th>Aksiyon</th></tr></thead>
                    <tbody>
                    <?php foreach ($mediaFiles as $file): ?>
                        <?php $path = (string) ($file['file_path'] ?? ''); ?>
                        <tr>
                            <td><?php if ($path !== ''): ?><img class="media-thumb" src="<?= e(url($path)) ?>" alt="" loading="lazy"><?php endif; ?></td>
                            <td><strong><?= e($file['file_name'] ?? '-') ?></strong><span class="muted block"><?= e($file['context'] ?? 'general') ?></span></td>
                            <td><?= e($file['mime_type'] ?? '-') ?></td>
                            <td><?= e($formatSize($file['file_size'] ?? 0)) ?></td>
                            <td><?= e($file['created_at'] ?? '') ?></td>
                            <td><input class="copy-url" value="<?= e($path) ?>" readonly data-copy-value="<?= e($path) ?>"></td>
                            <td class="action-row">
                                <?php foreach (['site_logo' => 'Ana logo', 'music_logo' => 'Music', 'studio_logo' => 'Studio', 'zpanel_logo' => 'ZPanel'] as $target => $label): ?>
                                    <button type="button" data-zpanel-action="/api/zpanel/media/set-logo" data-target="<?= e($target) ?>" data-file-path="<?= e($path) ?>"><?= e($label) ?></button>
                                <?php endforeach; ?>
                                <button type="button" data-zpanel-action="/api/zpanel/media/set-favicon" data-file-path="<?= e($path) ?>">Favicon</button>
                                <button type="button" class="danger" data-zpanel-action="/api/zpanel/media/delete" data-id="<?= e($file['id'] ?? '') ?>" data-confirm="Bu medya dosyasi pasife alinsin mi?">Pasif</button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($mediaFiles)): ?><tr><td colspan="7">Henuz medya dosyasi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </section>
</div>
