<?php 
$pageTitle = Lang::get('literature') . ' - ' . Lang::get('app_name');
$pageDescription = Lang::get('literature_seo_description');
$pageKeywords = Lang::get('literature_seo_keywords');
$robotsMeta = 'index, follow';

// Structured data for literature page
$structuredData = [
    '@type' => 'CollectionPage',
    '@id' => APP_URL . '/literature#webpage',
    'name' => Lang::get('literature'),
    'description' => Lang::get('literature_seo_description'),
    'url' => APP_URL . '/literature',
    'isPartOf' => ['@id' => Schema::webSiteId()],
    'inLanguage' => Lang::getLocale(),
    'mainEntity' => [
        '@type' => 'ItemList',
        'numberOfItems' => count($literature ?? []),
        'itemListElement' => array_map(function($item) {
            return [
                '@type' => 'Book',
                'name' => htmlspecialchars($item['title'] ?? ''),
                'author' => htmlspecialchars($item['author'] ?? ''),
                'publisher' => htmlspecialchars($item['publisher'] ?? ''),
                'datePublished' => $item['year'] ?? '',
                'inLanguage' => $item['language'] ?? ''
            ];
        }, $literature ?? [])
    ]
];

$extraJsonLd = Schema::render([
    $structuredData,
    Schema::breadcrumbs([
        ['name' => Lang::get('home'), 'item' => APP_URL . '/'],
        ['name' => Lang::get('literature'), 'item' => APP_URL . '/literature'],
    ]),
]);
?>
<?php require APP_ROOT . '/views/layouts/header.php'; ?>

<div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 mb-6">
    <div class="flex items-center space-x-3">
        <a href="<?= APP_URL ?>/home" class="text-gray-400 dark:text-gray-500 hover:text-primary-600 dark:hover:text-primary-400 text-sm transition-colors"><?= Lang::get('home') ?></a>
        <span class="text-gray-400">/</span>
        <h1 class="text-xl sm:text-2xl font-bold"><?= Lang::get('literature') ?></h1>
    </div>
    <a href="<?= APP_URL ?>/home"
       class="btn-gradient btn-gradient-gray btn-gradient-icon btn-gradient-sm self-start">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
        <span class="hidden sm:inline"><?= Lang::get('back') ?></span>
    </a>
</div>

<!-- Upload Form -->
<div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6 mb-6">
    <h2 class="text-lg font-semibold mb-4"><?= Lang::get('add_literature') ?></h2>
    <form method="POST" action="<?= APP_URL ?>/literature" enctype="multipart/form-data" class="flex flex-col md:flex-row gap-4">
        <input type="hidden" name="csrf_token" value="<?= Session::getCsrfToken() ?>">
        <div class="flex-1">
            <input type="text" name="title" required placeholder="<?= Lang::get('literature_title') ?>"
                   class="w-full px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors">
        </div>
        <div class="flex-1">
            <input type="file" name="file" required accept=".pdf,.doc,.docx,.txt,.xls,.xlsx,.jpg,.jpeg,.png,.gif,.webp"
                   class="w-full px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 file:mr-4 file:py-1 file:px-3 file:rounded-md file:border-0 file:text-sm file:font-medium file:bg-primary-50 file:text-primary-600 dark:file:bg-primary-900/30 dark:file:text-primary-400 hover:file:bg-primary-100 transition-colors">
        </div>
        <button type="submit" class="btn-gradient btn-gradient-blue btn-gradient-icon btn-gradient-sm whitespace-nowrap">
            <?= Lang::get('upload') ?>
        </button>
    </form>
</div>

<!-- Documents List -->
<?php if (empty($documents)): ?>
    <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-12 text-center">
        <svg class="w-16 h-16 mx-auto text-gray-400 dark:text-gray-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/>
        </svg>
        <p class="text-gray-500 dark:text-gray-400 text-lg"><?= Lang::get('no_literature') ?></p>
    </div>
<?php else: ?>
    <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-gray-50 dark:bg-gray-750 border-b border-gray-200 dark:border-gray-700">
                        <th class="px-4 py-3 text-left font-medium text-gray-500 dark:text-gray-400"><?= Lang::get('literature_title') ?></th>
                        <th class="px-4 py-3 text-left font-medium text-gray-500 dark:text-gray-400"><?= Lang::get('file_type') ?></th>
                        <th class="px-4 py-3 text-left font-medium text-gray-500 dark:text-gray-400"><?= Lang::get('file_size') ?></th>
                        <th class="px-4 py-3 text-left font-medium text-gray-500 dark:text-gray-400"><?= Lang::get('created_at') ?></th>
                        <th class="px-4 py-3 text-left font-medium text-gray-500 dark:text-gray-400"><?= Lang::get('actions') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
                    <?php foreach ($documents as $doc): ?>
                        <?php
                            $isImage = str_starts_with($doc['file_type'] ?? '', 'image/');
                            $isPdf = ($doc['file_type'] ?? '') === 'application/pdf';
                            $fileUrl = APP_URL . '/uploads/literature/' . htmlspecialchars($doc['file_path']);
                            $sizeKB = round(($doc['file_size'] ?? 0) / 1024, 1);
                            $sizeMB = round($sizeKB / 1024, 1);
                            $sizeDisplay = $sizeKB > 1024 ? $sizeMB . ' MB' : $sizeKB . ' KB';
                            $ext = strtoupper(pathinfo($doc['file_path'], PATHINFO_EXTENSION));
                        ?>
                        <tr class="hover:bg-gray-50 dark:hover:bg-gray-750 transition-colors">
                            <td class="px-4 py-3">
                                <div class="flex items-center space-x-3">
                                    <?php if ($isImage): ?>
                                        <img src="<?= $fileUrl ?>" alt="" class="w-10 h-10 object-cover rounded border border-gray-200 dark:border-gray-600">
                                    <?php else: ?>
                                        <div class="w-10 h-10 rounded border border-gray-200 dark:border-gray-600 bg-gray-100 dark:bg-gray-700 flex items-center justify-center text-xs font-bold text-gray-500 dark:text-gray-400">
                                            <?= $ext ?>
                                        </div>
                                    <?php endif; ?>
                                    <div>
                                        <p class="font-medium text-gray-900 dark:text-gray-100"><?= htmlspecialchars($doc['title']) ?></p>
                                        <?php if ($doc['original_name']): ?>
                                            <p class="text-xs text-gray-400 dark:text-gray-500"><?= htmlspecialchars($doc['original_name']) ?></p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3 text-gray-500 dark:text-gray-400"><?= $ext ?></td>
                            <td class="px-4 py-3 text-gray-500 dark:text-gray-400"><?= $sizeDisplay ?></td>
                            <td class="px-4 py-3 text-gray-500 dark:text-gray-400"><?= date('d.m.Y H:i', strtotime($doc['created_at'])) ?></td>
                            <td class="px-4 py-3">
                                <div class="flex items-center space-x-3">
                                    <?php if ($isImage): ?>
                                        <button onclick="openImageModal('<?= $fileUrl ?>')" class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 transition-colors" title="<?= Lang::get('view') ?>">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                                        </button>
                                    <?php elseif ($isPdf): ?>
                                        <a href="<?= $fileUrl ?>" target="_blank" class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 transition-colors" title="<?= Lang::get('view') ?>">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                                        </a>
                                    <?php endif; ?>
                                    <a href="<?= $fileUrl ?>" download class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 transition-colors" title="<?= Lang::get('download') ?>">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
                                    </a>
                                    <a href="<?= APP_URL ?>/literature/<?= $doc['id'] ?>/delete" onclick="return confirm('<?= Lang::get('confirm_delete') ?>')" class="text-gray-400 hover:text-red-600 dark:hover:text-red-400 transition-colors" title="<?= Lang::get('delete') ?>">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php endif; ?>

<!-- Image Modal -->
<div id="imageModal" class="fixed inset-0 bg-black/70 z-50 hidden flex items-center justify-center p-4" role="dialog" aria-modal="true" aria-label="Image preview" onclick="closeImageModal()">
    <img id="modalImage" src="" alt="" class="max-w-full max-h-[90vh] rounded-lg shadow-2xl">
</div>

<?php $extraJs = ['literature.js']; ?>
<?php require APP_ROOT . '/views/layouts/footer.php'; ?>
