<x-filament-panels::page>
    <x-filament::section heading="已安装插件">
        <div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
            @forelse($this->plugins as $plugin)
                <div class="flex flex-col rounded-xl border border-gray-200 bg-white p-5 shadow-sm dark:border-white/10 dark:bg-white/5">
                    <div class="flex items-start justify-between gap-3">
                        <div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-lg bg-primary-50 text-primary-600 dark:bg-primary-500/10">
                            <x-filament::icon icon="heroicon-o-puzzle-piece" class="h-6 w-6" />
                        </div>
                        <div class="text-right">
                            @if($plugin['enabled'])
                                <x-filament::badge color="success">已启用</x-filament::badge>
                            @else
                                <x-filament::badge color="gray">已停用</x-filament::badge>
                            @endif
                        </div>
                    </div>

                    <h3 class="mt-3 font-semibold">{{ $plugin['title'] }}</h3>
                    <p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $plugin['description'] }}</p>
                    <p class="mt-1 text-xs text-gray-400">v{{ $plugin['version'] }} · {{ $plugin['vendor'] }}.{{ $plugin['name'] }}</p>

                    <div class="mt-4 flex gap-2">
                        @if($plugin['enabled'])
                            <x-filament::button size="sm" color="gray" wire:click="disablePlugin('{{ $plugin['key'] }}')">停用</x-filament::button>
                        @else
                            <x-filament::button size="sm" color="success" wire:click="enablePlugin('{{ $plugin['key'] }}')">启用</x-filament::button>
                        @endif
                        @if(! in_array($plugin['key'], config('plugins.enabled', []), true))
                            <x-filament::button size="sm" color="danger" wire:click="uninstallPlugin('{{ $plugin['key'] }}')" wire:confirm="确定卸载该插件？">卸载</x-filament::button>
                        @endif
                    </div>
                </div>
            @empty
                <p class="text-sm text-gray-500">暂无插件</p>
            @endforelse
        </div>
    </x-filament::section>

    <x-filament::section heading="安装插件（ZIP）">
        <form wire:submit="uploadZip" 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="loadMarket">刷新市场</x-filament::button>
        </div>
        <div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
            @forelse($marketItems as $index => $item)
                <div class="flex flex-col rounded-xl border border-gray-200 bg-white p-5 shadow-sm dark:border-white/10 dark:bg-white/5">
                    <div class="flex h-11 w-11 items-center justify-center rounded-lg bg-primary-50 text-primary-600 dark:bg-primary-500/10">
                        <x-filament::icon icon="heroicon-o-cloud-arrow-down" class="h-6 w-6" />
                    </div>
                    <h3 class="mt-3 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-1 text-xs text-gray-400">{{ $item['version'] ?? '' }} · {{ $item['author'] ?? '' }}</p>
                    <div class="mt-4">
                        <x-filament::button size="sm" color="primary" wire:click="installFromMarket({{ $index }})">安装</x-filament::button>
                    </div>
                </div>
            @empty
                <p class="text-sm text-gray-500">点击「刷新市场」拉取可用插件/主题</p>
            @endforelse
        </div>
    </x-filament::section>
</x-filament-panels::page>
