@extends('agency.layouts.app')

@section('title', 'Ürün Kategorileri')
@section('page_title', 'Ürün Kategorileri')

@php
    $breadcrumbs = [
        ['title' => 'Ana Sayfa', 'url' => route('agency.dashboard')],
        ['title' => 'Hizmet Türleri', 'url' => route('agency.product-types.index')],
        ['title' => 'Kategoriler', 'url' => '']
    ];
@endphp

@push('styles')
    <style>
        .category-card {
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-lg);
            background: white;
            transition: all 0.3s ease;
        }

        .category-card:hover {
            box-shadow: var(--shadow-md);
            border-color: var(--primary-color);
        }

        .category-header {
            padding: 1rem 1.5rem;
            border-bottom: 1px solid var(--gray-200);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .category-icon {
            width: 40px;
            height: 40px;
            border-radius: var(--radius-md);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            margin-right: 1rem;
        }

        .category-level-0 {
            background: linear-gradient(135deg, var(--primary-color), #667eea);
            color: white;
        }

        .category-level-1 {
            background: linear-gradient(135deg, var(--success-color), #10b981);
            color: white;
        }

        .category-info {
            flex: 1;
        }

        .category-name {
            font-size: 16px;
            font-weight: 600;
            color: var(--gray-900);
            margin-bottom: 4px;
        }

        .category-description {
            font-size: 13px;
            color: var(--gray-600);
            margin: 0;
        }

        .category-badge {
            padding: 4px 10px;
            border-radius: var(--radius-md);
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .badge-active {
            background: #d1fae5;
            color: #065f46;
        }

        .badge-inactive {
            background: var(--gray-100);
            color: var(--gray-600);
        }

        .badge-parent {
            background: #dbeafe;
            color: #1e40af;
        }

        .badge-child {
            background: #fef3c7;
            color: #92400e;
        }

        .subcategory-list {
            padding: 0.5rem 1.5rem 1rem 4rem;
        }

        .subcategory-item {
            padding: 0.75rem;
            border-left: 3px solid var(--gray-200);
            margin-bottom: 0.5rem;
            background: var(--gray-50);
            border-radius: var(--radius-md);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .subcategory-item:hover {
            border-left-color: var(--primary-color);
            background: var(--gray-100);
        }

        .filter-bar {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-lg);
            padding: 1.25rem;
            margin-bottom: 1.5rem;
        }

        .empty-state {
            text-align: center;
            padding: 3rem 1rem;
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-lg);
        }

        .empty-state i {
            font-size: 48px;
            color: var(--gray-300);
            margin-bottom: 1rem;
        }

        .empty-state h4 {
            color: var(--gray-700);
            margin-bottom: 0.5rem;
        }

        .empty-state p {
            color: var(--gray-500);
            margin-bottom: 1.5rem;
        }
    </style>
@endpush

@section('content')
    <div class="container-fluid">
        <!-- Page Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h1 class="h3 mb-1">Ürün Kategorileri</h1>
                <p class="text-muted mb-0">Ürünlerinizi kategorilere ayırarak düzenleyin</p>
            </div>
            <div>
                <a href="{{ route('agency.categories.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>
                    Yeni Kategori Ekle
                </a>
            </div>
        </div>

        <!-- Filter Bar -->
        <div class="filter-bar">
            <form method="GET" action="{{ route('agency.categories.index') }}" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label small fw-bold">Hizmet Türü</label>
                    <select name="product_type_id" class="form-select">
                        <option value="">Tümü</option>
                        @foreach($productTypes as $type)
                            <option value="{{ $type->id }}" {{ request('product_type_id') == $type->id ? 'selected' : '' }}>
                                {{ $type->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label small fw-bold">Durum</label>
                    <select name="status" class="form-select">
                        <option value="">Tümü</option>
                        <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Aktif</option>
                        <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Pasif</option>
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label small fw-bold">Arama</label>
                    <input type="text" name="search" class="form-control" placeholder="Kategori ara..." value="{{ request('search') }}">
                </div>

                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-filter me-2"></i>
                        Filtrele
                    </button>
                </div>
            </form>
        </div>

        <!-- Categories List -->
        @if($categories->count() > 0)
            <div class="row g-3">
                @foreach($categories as $category)
                    @if($category->parent_id === null)
                        <!-- Ana Kategori -->
                        <div class="col-12">
                            <div class="category-card">
                                <div class="category-header">
                                    <div class="d-flex align-items-center flex-grow-1">
                                        <div class="category-icon category-level-0">
                                            <i class="fas {{ $category->icon ?? 'fa-folder' }}"></i>
                                        </div>
                                        <div class="category-info">
                                            <div class="category-name">{{ $category->name }}</div>
                                            @if($category->short_description)
                                                <p class="category-description">{{ $category->short_description }}</p>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="d-flex align-items-center gap-2">
                                        <span class="category-badge {{ $category->is_active ? 'badge-active' : 'badge-inactive' }}">
                                            {{ $category->is_active ? 'Aktif' : 'Pasif' }}
                                        </span>
                                        <span class="category-badge badge-parent">
                                            Ana Kategori
                                        </span>
                                        @if($category->productType)
                                            <span class="badge bg-info text-white">
                                                {{ $category->productType->name }}
                                            </span>
                                        @endif

                                        <div class="btn-group">
                                            <button type="button" class="btn btn-sm btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown">
                                                <i class="fas fa-ellipsis-v"></i>
                                            </button>
                                            <ul class="dropdown-menu dropdown-menu-end">
                                                <li>
                                                    <a class="dropdown-item" href="{{ route('agency.categories.edit', $category) }}">
                                                        <i class="fas fa-edit me-2"></i> Düzenle
                                                    </a>
                                                </li>
                                                <li>
                                                    <form action="{{ route('agency.categories.toggle-status', $category) }}" method="POST" class="d-inline toggle-status-form">
                                                        @csrf
                                                        <button type="submit" class="dropdown-item">
                                                            <i class="fas fa-toggle-{{ $category->is_active ? 'off' : 'on' }} me-2"></i>
                                                            {{ $category->is_active ? 'Pasif Et' : 'Aktif Et' }}
                                                        </button>
                                                    </form>
                                                </li>
                                                <li><hr class="dropdown-divider"></li>
                                                <li>
                                                    <form action="{{ route('agency.categories.destroy', $category) }}" method="POST" class="d-inline delete-form">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="dropdown-item text-danger">
                                                            <i class="fas fa-trash me-2"></i> Sil
                                                        </button>
                                                    </form>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>

                                <!-- Alt Kategoriler -->
                                @php
                                    $subcategories = $category->children()->get();
                                @endphp
                                @if($subcategories->count() > 0)
                                    <div class="subcategory-list">
                                        <h6 class="text-muted mb-2" style="font-size: 12px; text-transform: uppercase; font-weight: 700;">
                                            Alt Kategoriler ({{ $subcategories->count() }})
                                        </h6>
                                        @foreach($subcategories as $subcategory)
                                            <div class="subcategory-item">
                                                <div class="d-flex align-items-center">
                                                    <div class="category-icon category-level-1" style="width: 32px; height: 32px; font-size: 14px;">
                                                        <i class="fas {{ $subcategory->icon ?? 'fa-folder' }}"></i>
                                                    </div>
                                                    <div>
                                                        <div style="font-weight: 600; font-size: 14px;">{{ $subcategory->name }}</div>
                                                        @if($subcategory->short_description)
                                                            <div style="font-size: 12px; color: var(--gray-600);">{{ $subcategory->short_description }}</div>
                                                        @endif
                                                    </div>
                                                </div>
                                                <div class="d-flex align-items-center gap-2">
                                                    <span class="category-badge {{ $subcategory->is_active ? 'badge-active' : 'badge-inactive' }}">
                                                        {{ $subcategory->is_active ? 'Aktif' : 'Pasif' }}
                                                    </span>
                                                    <div class="btn-group">
                                                        <button type="button" class="btn btn-sm btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown">
                                                            <i class="fas fa-ellipsis-v"></i>
                                                        </button>
                                                        <ul class="dropdown-menu dropdown-menu-end">
                                                            <li>
                                                                <a class="dropdown-item" href="{{ route('agency.categories.edit', $subcategory) }}">
                                                                    <i class="fas fa-edit me-2"></i> Düzenle
                                                                </a>
                                                            </li>
                                                            <li>
                                                                <form action="{{ route('agency.categories.toggle-status', $subcategory) }}" method="POST" class="d-inline toggle-status-form">
                                                                    @csrf
                                                                    <button type="submit" class="dropdown-item">
                                                                        <i class="fas fa-toggle-{{ $subcategory->is_active ? 'off' : 'on' }} me-2"></i>
                                                                        {{ $subcategory->is_active ? 'Pasif Et' : 'Aktif Et' }}
                                                                    </button>
                                                                </form>
                                                            </li>
                                                            <li><hr class="dropdown-divider"></li>
                                                            <li>
                                                                <form action="{{ route('agency.categories.destroy', $subcategory) }}" method="POST" class="d-inline delete-form">
                                                                    @csrf
                                                                    @method('DELETE')
                                                                    <button type="submit" class="dropdown-item text-danger">
                                                                        <i class="fas fa-trash me-2"></i> Sil
                                                                    </button>
                                                                </form>
                                                            </li>
                                                        </ul>
                                                    </div>
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>
                                @endif
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>

            <!-- Pagination -->
            @if($categories->hasPages())
                <div class="mt-4 d-flex justify-content-center">
                    {{ $categories->links() }}
                </div>
            @endif
        @else
            <div class="empty-state">
                <i class="fas fa-folder-open"></i>
                <h4>Henüz Kategori Eklenmemiş</h4>
                <p>Bu hizmet türü için henüz kategori oluşturulmamış.</p>
                <a href="{{ route('agency.categories.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>
                    İlk Kategoriyi Oluştur
                </a>
            </div>
        @endif
    </div>
@endsection

@push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Toggle status forms
            document.querySelectorAll('.toggle-status-form').forEach(form => {
                form.addEventListener('submit', async function(e) {
                    e.preventDefault();

                    try {
                        const response = await fetch(this.action, {
                            method: 'POST',
                            headers: {
                                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
                                'Accept': 'application/json',
                                'Content-Type': 'application/json'
                            }
                        });

                        const data = await response.json();

                        if (data.success) {
                            location.reload();
                        } else {
                            alert(data.message || 'Bir hata oluştu');
                        }
                    } catch (error) {
                        console.error('Error:', error);
                        alert('Bir hata oluştu');
                    }
                });
            });

            // Delete forms
            document.querySelectorAll('.delete-form').forEach(form => {
                form.addEventListener('submit', function(e) {
                    if (!confirm('Bu kategoriyi silmek istediğinizden emin misiniz?')) {
                        e.preventDefault();
                    }
                });
            });
        });
    </script>
@endpush
