<?php 
$pageTitle = Lang::get('lists') . ' - ' . Lang::get('app_name');
$pageDescription = Lang::get('lists_seo_description');
$pageKeywords = Lang::get('lists_seo_keywords');
$robotsMeta = 'index, follow';

// Structured data for lists page
$structuredData = [
    '@type' => 'CollectionPage',
    '@id' => APP_URL . '/lists#webpage',
    'name' => Lang::get('lists'),
    'description' => Lang::get('lists_seo_description'),
    'url' => APP_URL . '/lists',
    'isPartOf' => ['@id' => Schema::webSiteId()],
    'inLanguage' => Lang::getLocale(),
    'mainEntity' => [
        '@type' => 'ItemList',
        'numberOfItems' => count($lists ?? []),
        'itemListElement' => array_map(function($list) {
            return [
                '@type' => 'Thing',
                'name' => htmlspecialchars($list['name'] ?? ''),
                'description' => htmlspecialchars($list['description'] ?? ''),
                'dateCreated' => $list['created_at'] ?? '',
                'author' => [
                    '@type' => 'Person',
                    'name' => htmlspecialchars($list['display_name'] ?? $list['username'] ?? '')
                ]
            ];
        }, $lists ?? [])
    ]
];

$extraJsonLd = Schema::render([
    $structuredData,
    Schema::breadcrumbs([
        ['name' => Lang::get('home'), 'item' => APP_URL . '/'],
        ['name' => Lang::get('lists'), 'item' => APP_URL . '/lists'],
    ]),
]);
?>
<?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('lists') ?></h1>
    </div>
    <div class="flex items-center gap-2">
        <a href="<?= APP_URL ?>/home"
           class="btn-gradient btn-gradient-gray btn-gradient-icon btn-gradient-sm">
            <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>
        <a href="<?= APP_URL ?>/lists/create"
           class="btn-gradient btn-gradient-blue btn-gradient-icon btn-gradient-sm">
            <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 6v6m0 0v6m0-6h6m-6 0H6"/></svg>
            <span class="hidden sm:inline"><?= Lang::get('add_list') ?></span>
        </a>
    </div>
</div>

<?php if (empty($lists)): ?>
    <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="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
        </svg>
        <p class="text-gray-500 dark:text-gray-400 text-lg"><?= Lang::get('no_lists') ?></p>
    </div>
<?php else: ?>
    <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6">
        <?php foreach ($lists as $list): ?>
            <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6 hover:shadow-lg transition-shadow duration-200">
                <div class="flex items-start justify-between mb-3">
                    <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100"><?= htmlspecialchars($list['title']) ?></h3>
                    <div class="flex space-x-1">
                        <button type="button" onclick="moveListItem(<?= $list['id'] ?>, 'up', this)" class="p-1 text-gray-400 hover:text-green-600 dark:hover:text-green-400 rounded transition-colors" title="<?= Lang::get('move_up') ?>">
                            <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="M5 15l7-7 7 7"/></svg>
                        </button>
                        <button type="button" onclick="moveListItem(<?= $list['id'] ?>, 'down', this)" class="p-1 text-gray-400 hover:text-blue-600 dark:hover:text-blue-400 rounded transition-colors" title="<?= Lang::get('move_down') ?>">
                            <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 9l-7 7-7-7"/></svg>
                        </button>
                        <a href="<?= APP_URL ?>/lists/<?= $list['id'] ?>/edit" class="p-1 text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 rounded transition-colors" title="<?= Lang::get('edit') ?>">
                            <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="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
                        </a>
                        <a href="<?= APP_URL ?>/lists/<?= $list['id'] ?>/delete" onclick="return confirm('<?= Lang::get('confirm_delete') ?>')" class="p-1 text-gray-400 hover:text-red-600 dark:hover:text-red-400 rounded 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>
                </div>
                <?php if (!empty($list['content'])): ?>
                    <div class="text-sm text-gray-600 dark:text-gray-400 whitespace-pre-line line-clamp-6"><?= htmlspecialchars($list['content']) ?></div>
                <?php endif; ?>
                <div class="mt-4 pt-3 border-t border-gray-100 dark:border-gray-700 text-xs text-gray-400 dark:text-gray-500">
                    <?= Lang::get('updated_at') ?>: <?= date('d.m.Y H:i', strtotime($list['updated_at'])) ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

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