@extends('agency.layouts.app')

@section('title', 'Klasörler')

@section('content')
    <div class="container-fluid px-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h1 class="h3 mb-0 text-gray-800">Klasör Yönetimi</h1>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item"><a href="{{ route('agency.disk.index') }}">Disk</a></li>
                        <li class="breadcrumb-item active">Klasörler</li>
                    </ol>
                </nav>
            </div>
            <div>
                <a href="{{ route('agency.disk.folders.create') }}" class="btn btn-primary">
                    <i class="fas fa-folder-plus"></i> Yeni Klasör
                </a>
            </div>
        </div>

        <!-- Filtreler -->
        <div class="card shadow-sm mb-4">
            <div class="card-body py-2">
                <form method="GET" class="row g-3 align-items-end">
                    <div class="col-md-3">
                        <label class="form-label small">Klasör Türü</label>
                        <select name="type" class="form-select form-select-sm">
                            <option value="">Tüm Türler</option>
                            <option value="general" {{ request('type') == 'general' ? 'selected' : '' }}>Genel</option>
                            <option value="tour" {{ request('type') == 'tour' ? 'selected' : '' }}>Tur</option>
                            <option value="hotel" {{ request('type') == 'hotel' ? 'selected' : '' }}>Otel</option>
                            <option value="transport" {{ request('type') == 'transport' ? 'selected' : '' }}>Ulaşım
                            </option>
                            <option value="activity" {{ request('type') == 'activity' ? 'selected' : '' }}>Aktivite
                            </option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label small">Seviye</label>
                        <select name="level" class="form-select form-select-sm">
                            <option value="">Tüm Seviyeler</option>
                            <option value="0" {{ request('level') === '0' ? 'selected' : '' }}>Ana Klasörler</option>
                            <option value="1" {{ request('level') == '1' ? 'selected' : '' }}>Alt Klasörler</option>
                            <option value="2" {{ request('level') == '2' ? 'selected' : '' }}>2. Seviye</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small">Arama</label>
                        <input type="text" name="search" class="form-control form-control-sm"
                               placeholder="Klasör adı veya açıklama..."
                               value="{{ request('search') }}">
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-outline-primary btn-sm me-1">
                            <i class="fas fa-search"></i> Filtrele
                        </button>
                        <a href="{{ route('agency.disk.folders.index') }}" class="btn btn-outline-secondary btn-sm">
                            <i class="fas fa-times"></i>
                        </a>
                    </div>
                </form>
            </div>
        </div>

        @if($folders->count() > 0)
            <!-- Klasör Listesi -->
            <div class="card shadow">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">
                        Klasörler ({{ $folders->count() }})
                    </h6>
                    <div class="btn-group btn-group-sm" role="group">
                        <input type="radio" class="btn-check" name="view" id="grid-view" checked>
                        <label class="btn btn-outline-secondary" for="grid-view">
                            <i class="fas fa-th"></i>
                        </label>
                        <input type="radio" class="btn-check" name="view" id="list-view">
                        <label class="btn btn-outline-secondary" for="list-view">
                            <i class="fas fa-list"></i>
                        </label>
                    </div>
                </div>
                <div class="card-body">
                    <!-- Grid Görünüm -->
                    <div id="grid-content">
                        <div class="row">
                            @foreach($folders as $folder)
                                <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 mb-4">
                                    <div class="card border-0 shadow-sm h-100 folder-card">
                                        <div class="card-body text-center p-3">
                                            <div class="position-relative mb-3">
                                                <i class="fas fa-folder fa-4x mb-2"
                                                   style="color: {{ $folder->color_code ?? '#6c757d' }}"></i>
                                                @if($folder->icon)
                                                    <i class="fas fa-{{ $folder->icon }} position-absolute"
                                                       style="top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; font-size: 1.2rem;"></i>
                                                @endif
                                                @if($folder->level > 0)
                                                    <span class="position-absolute top-0 start-0 badge bg-secondary"
                                                          style="font-size: 0.6rem;">
                                                    L{{ $folder->level }}
                                                </span>
                                                @endif
                                            </div>

                                            <h6 class="card-title mb-2 text-truncate">{{ $folder->name }}</h6>

                                            @if($folder->parent)
                                                <p class="small text-muted mb-1">
                                                    <i class="fas fa-level-up-alt"></i> {{ $folder->parent->name }}
                                                </p>
                                            @endif

                                            <div class="row text-center mb-2">
                                                <div class="col-6">
                                                    <div class="small text-muted">Dosyalar</div>
                                                    <div class="font-weight-bold">{{ $folder->item_count }}</div>
                                                </div>
                                                <div class="col-6">
                                                    <div class="small text-muted">Boyut</div>
                                                    <div class="font-weight-bold">
                                                        @php
                                                            $bytes = $folder->total_size_bytes ?? 0;
                                                            $units = ['B', 'KB', 'MB', 'GB'];
                                                            $i = 0;
                                                            while ($bytes > 1024 && $i < count($units) - 1) {
                                                                $bytes /= 1024;
                                                                $i++;
                                                            }
                                                        @endphp
                                                        {{ $bytes > 0 ? number_format($bytes, 1) . ' ' . $units[$i] : '-' }}
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="small text-muted mb-2">
                                            <span
                                                class="badge badge-outline-{{ $folder->folder_type == 'tour' ? 'primary' : ($folder->folder_type == 'hotel' ? 'success' : 'secondary') }}">
                                                {{ ucfirst($folder->folder_type) }}
                                            </span>
                                                @if($folder->is_public)
                                                    <span class="badge badge-outline-success">Public</span>
                                                @endif
                                            </div>

                                            @if($folder->description)
                                                <p class="card-text small text-muted mb-2">
                                                    {{ Str::limit($folder->description, 50) }}
                                                </p>
                                            @endif
                                        </div>

                                        <div class="card-footer bg-transparent border-top-0 text-center p-2">
                                            <div class="btn-group btn-group-sm w-100" role="group">
                                                <a href="{{ route('agency.disk.folders.show', $folder) }}"
                                                   class="btn btn-outline-primary">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('agency.disk.folders.edit', $folder) }}"
                                                   class="btn btn-outline-warning">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-outline-danger"
                                                        onclick="deleteFolder({{ $folder->id }}, '{{ $folder->name }}')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    </div>

                    <!-- Liste Görünüm -->
                    <div id="list-content" style="display: none;">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                <tr>
                                    <th>Klasör</th>
                                    <th>Tür</th>
                                    <th>Ana Klasör</th>
                                    <th>Dosyalar</th>
                                    <th>Boyut</th>
                                    <th>Oluşturulma</th>
                                    <th>İşlemler</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($folders as $folder)
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-folder me-2"
                                                   style="color: {{ $folder->color_code ?? '#6c757d' }}"></i>
                                                <div>
                                                    <div class="font-weight-bold">{{ $folder->name }}</div>
                                                    @if($folder->description)
                                                        <small
                                                            class="text-muted">{{ Str::limit($folder->description, 60) }}</small>
                                                    @endif
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <span class="badge badge-outline-secondary">
                                                {{ ucfirst($folder->folder_type) }}
                                            </span>
                                        </td>
                                        <td>{{ $folder->parent?->name ?? '-' }}</td>
                                        <td>{{ $folder->item_count }}</td>
                                        <td>
                                            @php
                                                $bytes = $folder->total_size_bytes ?? 0;
                                                $units = ['B', 'KB', 'MB', 'GB'];
                                                $i = 0;
                                                while ($bytes > 1024 && $i < count($units) - 1) {
                                                    $bytes /= 1024;
                                                    $i++;
                                                }
                                            @endphp
                                            {{ $bytes > 0 ? number_format($bytes, 1) . ' ' . $units[$i] : '-' }}
                                        </td>
                                        <td>{{ $folder->created_at->format('d.m.Y') }}</td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{{ route('agency.disk.folders.show', $folder) }}"
                                                   class="btn btn-outline-primary">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('agency.disk.folders.edit', $folder) }}"
                                                   class="btn btn-outline-warning">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-outline-danger"
                                                        onclick="deleteFolder({{ $folder->id }}, '{{ $folder->name }}')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        @else
            <!-- Boş Durum -->
            <div class="card shadow">
                <div class="card-body text-center py-5">
                    <i class="fas fa-folder-open fa-4x text-gray-400 mb-4"></i>
                    <h4 class="text-gray-600">Klasör Bulunamadı</h4>
                    <p class="text-gray-500 mb-4">
                        @if(request()->hasAny(['search', 'type', 'level']))
                            Arama kriterlerinize uygun klasör bulunamadı.
                        @else
                            Henüz hiç klasör oluşturulmamış.
                        @endif
                    </p>
                    <div>
                        @if(request()->hasAny(['search', 'type', 'level']))
                            <a href="{{ route('agency.disk.folders.index') }}"
                               class="btn btn-outline-secondary me-2">
                                <i class="fas fa-times"></i> Filtreleri Temizle
                            </a>
                        @endif
                        <a href="{{ route('agency.disk.folders.create') }}" class="btn btn-primary">
                            <i class="fas fa-folder-plus"></i> Yeni Klasör Oluştur
                        </a>
                    </div>
                </div>
            </div>
        @endif
    </div>

    <!-- Delete Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Klasörü Sil</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <p>Bu klasörü silmek istediğinizden emin misiniz?</p>
                    <p><strong id="folderName"></strong></p>
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle"></i>
                        Bu işlem geri alınamaz ve klasördeki tüm dosyalar da silinecektir.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
                    <form id="deleteForm" method="POST" style="display: inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger">Sil</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            // Görünüm değiştirme
            document.getElementById('grid-view').addEventListener('change', function () {
                if (this.checked) {
                    document.getElementById('grid-content').style.display = 'block';
                    document.getElementById('list-content').style.display = 'none';
                }
            });

            document.getElementById('list-view').addEventListener('change', function () {
                if (this.checked) {
                    document.getElementById('grid-content').style.display = 'none';
                    document.getElementById('list-content').style.display = 'block';
                }
            });
        });

        function deleteFolder(id, name) {
            document.getElementById('folderName').textContent = name;
            document.getElementById('deleteForm').action = '/agency/disk/folders/' + id;
            new bootstrap.Modal(document.getElementById('deleteModal')).show();
        }
    </script>
@endpush

@push('styles')
    <style>
        .folder-card {
            transition: transform 0.2s;
        }

        .folder-card:hover {
            transform: translateY(-2px);
        }

        .badge-outline-primary {
            color: #007bff;
            border: 1px solid #007bff;
        }

        .badge-outline-success {
            color: #28a745;
            border: 1px solid #28a745;
        }

        .badge-outline-secondary {
            color: #6c757d;
            border: 1px solid #6c757d;
        }
    </style>
@endpush
