<x-filament-panels::page>
    <x-filament::section heading="主题列表">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:1.25rem">
            @forelse($this->themes as $theme)
                <x-filament::section :compact="true" class="!p-0">
                    <div class="flex flex-col">
                    {{-- 主题预览图 --}}
                    <div style="position:relative;aspect-ratio:4/3;overflow:hidden;border-top-left-radius:0.75rem;border-top-right-radius:0.75rem;background:rgba(128,128,128,.08)">
                        @if($theme['screenshot'])
                            <img src="{{ theme('asset', $theme['screenshot'], $theme['name']) }}"
                                 alt="{{ $theme['title'] }} 预览"
                                 style="width:100%;height:100%;object-fit:cover;object-position:top;transition:transform .3s" onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
                        @else
                            <div class="flex h-full items-center justify-center text-gray-400">
                                <x-filament::icon icon="heroicon-o-photo" class="h-10 w-10" />
                            </div>
                        @endif
                        @if($theme['active'])
                            <div class="absolute left-3 top-3">
                                <x-filament::badge color="success">使用中</x-filament::badge>
                            </div>
                        @endif
                    </div>

                    <div class="flex flex-1 flex-col p-5">
                        <div class="flex items-center justify-between">
                            <h3 class="font-semibold">{{ $theme['title'] }}</h3>
                            <span class="text-xs text-gray-400">v{{ $theme['version'] }}</span>
                        </div>
                        <p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $theme['description'] }}</p>
                        <p class="mt-1 text-xs text-gray-400">{{ $theme['author'] }}</p>

                        @if($theme['inject_points'])
                            <div class="mt-3 flex flex-wrap gap-1">
                                @foreach($theme['inject_points'] as $point)
                                    <x-filament::badge color="gray" size="xs">{{ $point }}</x-filament::badge>
                                @endforeach
                            </div>
                        @endif

                        <div class="mt-4 flex gap-2">
                            @if(! $theme['active'])
                                <x-filament::button size="sm" color="primary" wire:click="activateTheme('{{ $theme['name'] }}')">启用</x-filament::button>
                                <x-filament::button size="sm" color="danger" wire:click="uninstallTheme('{{ $theme['name'] }}')" wire:confirm="确定删除该主题？">删除</x-filament::button>
                            @endif
                        </div>
                    </div>
                    </div>
                </x-filament::section>
            @empty
                <p class="text-sm text-gray-500">暂无主题</p>
            @endforelse
        </div>
    </x-filament::section>

    <x-filament::section heading="安装主题（ZIP）">
        <form wire:submit="uploadThemeZip" class="flex items-end gap-3">
            <div class="flex-1">
                <x-filament::input.wrapper>
                    <x-filament::input type="file" wire:model="zip" accept=".zip" />
                </x-filament::input.wrapper>
                @error('zip') <p class="mt-1 text-sm text-danger-600">{{ $message }}</p> @enderror
            </div>
            <x-filament::button type="submit" color="primary">安装</x-filament::button>
        </form>
    </x-filament::section>

    <x-filament::section heading="主题市场" :description="config('market.url') ?: '未配置远程市场（config/market.php 的 MARKET_URL），仅支持本地 ZIP 安装'">
        <div class="mb-4">
            <x-filament::button color="gray" wire:click="loadThemeMarket">刷新市场</x-filament::button>
        </div>
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:1rem">
            @forelse($marketItems as $index => $item)
                @php $marketPaid = $item['price'] > 0; @endphp
                <x-filament::section :compact="true">
                    <div class="flex flex-col">
                        <div class="flex items-start justify-between gap-3">
                            <x-filament::icon icon="heroicon-o-swatch" class="h-8 w-8 text-primary-600" />
                            @if($marketPaid)
                                <x-filament::badge color="warning">¥{{ number_format($item['price'] / 100, 2) }}</x-filament::badge>
                            @else
                                <x-filament::badge color="success">免费</x-filament::badge>
                            @endif
                        </div>
                        <h3 class="mt-3 text-base font-semibold">{{ $item['title'] ?? $item['name'] }}</h3>
                        <p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $item['description'] ?? '' }}</p>
                        <p class="mt-2 text-xs text-gray-400">{{ $item['version'] ?? '' }} · {{ $item['author'] ?? '' }}</p>
                        <div class="mt-4 flex flex-wrap gap-2">
                            @if($marketPaid && ! $this->isThemeMarketPurchased($index))
                                <x-filament::button size="sm" color="primary" wire:click="purchaseThemeMarketItem({{ $index }})">购买</x-filament::button>
                            @elseif($marketPaid)
                                <x-filament::badge color="success">已购买</x-filament::badge>
                            @endif
                            <x-filament::button size="sm" color="gray" wire:click="installFromThemeMarket({{ $index }})">安装</x-filament::button>
                        </div>
                    </div>
                </x-filament::section>
            @empty
                <p class="text-sm text-gray-500">点击「刷新市场」拉取可用主题</p>
            @endforelse
        </div>
    </x-filament::section>
</x-filament-panels::page>
