@extends('layout')

@section('title', __('store.downloads_title'))

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            <x-clientarea.page-header
                icon="fas fa-download"
                :title="__('store.downloads_title')"
                :subtitle="__('store.downloads_description')"
            />

            
            @if($downloads->count() > 0)
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body py-3">
                        <div class="row g-3 align-items-center">
                            <div class="col-md-6">
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-search"></i></span>
                                    <input type="text" class="form-control" id="downloads-search" placeholder="{{ __('store.downloads_search') }}">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <select class="form-select" id="downloads-category-filter">
                                    <option value="">{{ __('store.downloads_filter_category') }}</option>
                                    @foreach($categories as $cat)
                                        <option value="{{ $cat }}">{{ __('store.downloads_category_' . $cat) }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            @endif

            @if($downloads->count() > 0)
                @foreach($downloads as $category => $files)
                    <div class="card border-0 shadow-sm mb-4 downloads-category-card" data-category="{{ $category }}">
                        <div class="card-header border-0">
                            <h5 class="mb-0">
                                @switch($category)
                                    @case('software')
                                        <i class="fas fa-laptop-code me-2"></i>
                                        @break
                                    @case('documentation')
                                        <i class="fas fa-book me-2"></i>
                                        @break
                                    @case('tools')
                                        <i class="fas fa-tools me-2"></i>
                                        @break
                                    @default
                                        <i class="fas fa-folder me-2"></i>
                                @endswitch
                                {{ __('store.downloads_category_' . $category) }}
                            </h5>
                        </div>
                        <div class="card-body p-0">
                            <div class="list-group list-group-flush">
                                @foreach($files as $download)
                                    <div class="list-group-item d-flex justify-content-between align-items-start px-4 py-3 download-item" data-title="{{ strtolower($download->title) }}">
                                        <div class="d-flex align-items-start flex-grow-1">
                                            <div class="me-3 mt-1">
                                                @php
                                                    $iconClass = match(strtolower($download->file_type)) {
                                                        'pdf' => 'fas fa-file-pdf text-danger',
                                                        'doc', 'docx' => 'fas fa-file-word text-primary',
                                                        'xls', 'xlsx' => 'fas fa-file-excel text-success',
                                                        'zip', 'rar', '7z', 'tar', 'gz' => 'fas fa-file-archive text-warning',
                                                        'exe', 'msi', 'dmg' => 'fas fa-file-alt text-info',
                                                        'jpg', 'jpeg', 'png', 'gif', 'svg' => 'fas fa-file-image text-purple',
                                                        default => 'fas fa-file text-secondary',
                                                    };
                                                @endphp
                                                <i class="{{ $iconClass }} fa-2x"></i>
                                            </div>
                                            <div class="flex-grow-1">
                                                <div class="d-flex align-items-center gap-2 mb-1">
                                                    <h6 class="mb-0">{{ $download->title }}</h6>
                                                    @if($download->version)
                                                        <span class="badge bg-primary bg-opacity-75">v{{ $download->version }}</span>
                                                    @endif
                                                    @if($download->isNew())
                                                        <span class="badge bg-success">{{ __('store.downloads_new_badge') }}</span>
                                                    @endif
                                                </div>
                                                @if($download->description)
                                                    <p class="text-muted mb-1 small">{{ $download->description }}</p>
                                                @endif

                                                
                                                @if($download->changelog)
                                                    <div class="mb-1">
                                                        <a class="small text-decoration-none" data-bs-toggle="collapse" href="#changelog-{{ $download->id }}" role="button" aria-expanded="false">
                                                            <i class="fas fa-list-ul me-1"></i>{{ __('store.downloads_changelog') }}
                                                            <i class="fas fa-chevron-down ms-1" style="font-size: 0.65rem;"></i>
                                                        </a>
                                                        <div class="collapse mt-1" id="changelog-{{ $download->id }}">
                                                            <div class="small text-muted p-2 rounded" style="border: 1px dashed var(--bs-border-color);">
                                                                {!! nl2br(e($download->changelog)) !!}
                                                            </div>
                                                        </div>
                                                    </div>
                                                @endif

                                                <div class="d-flex flex-wrap align-items-center text-muted small gap-3">
                                                    <span>
                                                        <i class="fas fa-hdd me-1"></i>
                                                        {{ __('store.downloads_file_size') }}: {{ $download->getFormattedSize() }}
                                                    </span>
                                                    <span>
                                                        <i class="fas fa-download me-1"></i>
                                                        {{ __('store.downloads_download_count', ['count' => $download->download_count]) }}
                                                    </span>
                                                    @if($download->file_hash)
                                                        <span class="download-hash-container">
                                                            <i class="fas fa-fingerprint me-1"></i>
                                                            {{ __('store.downloads_file_hash') }}:
                                                            <code class="small user-select-all" title="{{ $download->file_hash }}">{{ Str::limit($download->file_hash, 16) }}</code>
                                                            <button type="button" class="btn btn-link btn-sm p-0 ms-1 btn-copy-hash" data-hash="{{ $download->file_hash }}" title="{{ __('store.downloads_copy_hash') }}">
                                                                <i class="fas fa-copy" style="font-size: 0.7rem;"></i>
                                                            </button>
                                                        </span>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div class="ms-3">
                                            <a href="{{ route('store.auth.downloads.file', $download->id) }}" class="btn btn-outline-primary btn-sm">
                                                <i class="fas fa-download me-1"></i>
                                                {{ __('store.downloads_download') }}
                                            </a>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                @endforeach
            @else
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-download fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.downloads_no_files') }}</h5>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('scripts')
@vite('resources/js/store/downloads.js')
@endpush
