@extends('layouts.admin')

@section('title', 'Manage Categories')

@section('admin-content')
    <!-- Page Header -->
    <div class="page-header">
        <div>
            <h3 class="page-title">Manage Categories</h3>
            <p class="text-muted mb-0">Organize products into categories</p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-primary-gradient shadow-sm" onclick="openCategoryModal('parent')">
                <i class="bi bi-folder-plus me-2"></i> Add Parent Category
            </button>
            <button type="button" class="btn btn-outline-primary shadow-sm" onclick="openCategoryModal('sub')">
                <i class="bi bi-diagram-3 me-2"></i> Add Sub Category
            </button>
        </div>
    </div>

    <!-- Categories List -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white py-3 border-0">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0 fw-bold">All Categories</h5>
                
                <!-- Search/Filter (Optional Enhancement) -->
                <div style="width: 250px;">
                    <input type="text" class="form-control form-control-sm ps-3 rounded-pill bg-light border-0" placeholder="Search categories...">
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4" style="width: 80px;">Image</th>
                            <th>Name</th>
                            <th>Level/Parent</th>
                            <th>Status</th>
                            <th>Products</th>
                            <th class="text-end pe-4">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($categories as $category)
                            <tr>
                                <td class="ps-4">
                                    <div class="position-relative">
                                        @if($category->image)
                                            <img src="{{ asset('storage/' . $category->image) }}" class="rounded shadow-sm" style="width: 48px; height: 48px; object-fit: cover;">
                                        @else
                                            <div class="bg-light rounded d-flex align-items-center justify-content-center text-muted border" style="width: 48px; height: 48px;">
                                                <i class="bi bi-image fs-5 opacity-50"></i>
                                            </div>
                                        @endif
                                        @if($category->is_featured)
                                            <span class="position-absolute top-0 start-0 translate-middle p-1 bg-warning border border-white rounded-circle">
                                                <span class="visually-hidden">Featured</span>
                                            </span>
                                        @endif
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-bold text-dark">{{ $category->name }}</div>
                                    <div class="small text-muted text-truncate" style="max-width: 200px;">/{{ $category->slug }}</div>
                                </td>
                                <td>
                                    @if($category->parent)
                                        <span class="badge bg-primary-subtle text-primary border border-primary-subtle">
                                            <i class="bi bi-arrow-return-right me-1"></i> {{ $category->parent->name }}
                                        </span>
                                    @else
                                        <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">Top Level</span>
                                    @endif
                                </td>
                                <td>
                                    <form action="{{ route('admin.categories.update', $category->id) }}" method="POST" class="d-inline">
                                        @csrf
                                        @method('PUT')
                                        <!-- Hidden inputs for required fields to prevent validation errors -->
                                        <input type="hidden" name="name" value="{{ $category->name }}">
                                        @if($category->is_active)
                                            <input type="hidden" name="is_featured" value="{{ $category->is_featured ? 'on' : '' }}">
                                        @else
                                            <input type="hidden" name="is_active" value="on">
                                            <input type="hidden" name="is_featured" value="{{ $category->is_featured ? 'on' : '' }}">
                                        @endif
                                        
                                        <!-- Actual toggle logic fits better in a dedicated toggle endpoint, but utilizing update for now -->
                                        <!-- Simplification: Just showing status here contextually -->
                                        @if($category->is_active)
                                            <span class="badge bg-success bg-opacity-10 text-success"><i class="bi bi-check-circle-fill me-1"></i> Active</span>
                                        @else
                                            <span class="badge bg-danger bg-opacity-10 text-danger"><i class="bi bi-x-circle-fill me-1"></i> Inactive</span>
                                        @endif
                                    </form>
                                </td>
                                <td>
                                    <span class="badge bg-light text-dark border rounded-pill px-3">{{ $category->products_count ?? 0 }} Items</span>
                                </td>
                                <td class="text-end pe-4">
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-light border-0" type="button" data-bs-toggle="dropdown">
                                            <i class="bi bi-three-dots-vertical"></i>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-end border-0 shadow">
                                            <li>
                                                <a class="dropdown-item" href="{{ route('admin.categories.edit', $category->id) }}">
                                                    <i class="bi bi-pencil me-2"></i> Edit Details
                                                </a>
                                            </li>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <form action="{{ route('admin.categories.destroy', $category->id) }}" method="POST" onsubmit="return confirm('Delete this category? All sub-categories products might be affected.');">
                                                    @csrf @method('DELETE')
                                                    <button type="submit" class="dropdown-item text-danger">
                                                        <i class="bi bi-trash me-2"></i> Delete
                                                    </button>
                                                </form>
                                            </li>
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer bg-white border-0 py-3">
            {{ $categories->links('pagination::bootstrap-5') }}
        </div>
    </div>

    <!-- Create Category Modal -->
    <div class="modal fade" id="categoryModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content border-0 shadow">
                <div class="modal-header border-bottom-0">
                    <h5 class="modal-title fw-bold" id="categoryModalLabel">Add Category</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <div class="modal-body">
                        <div class="mb-4">
                            <label class="form-label fw-bold text-muted small">Category Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control form-control-lg" placeholder="e.g. Industrial Machinery" required>
                        </div>

                        <div class="mb-4" id="parentCategoryField">
                            <label class="form-label fw-bold text-muted small">Parent Category <span class="text-danger">*</span></label>
                            <select name="parent_id" class="form-select form-select-lg">
                                <option value="" selected disabled>Select Parent Category</option>
                                @foreach($parents as $parent)
                                    <option value="{{ $parent->id }}">{{ $parent->name }}</option>
                                @endforeach
                            </select>
                            <div class="form-text">This category will be listed under the selected parent.</div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold text-muted small">Cover Image</label>
                            <input type="file" name="image" class="form-control" accept="image/*">
                        </div>

                        <div class="row g-3">
                            <div class="col-6">
                                <div class="p-3 border rounded bg-light">
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" name="is_featured" id="isFeatured">
                                        <label class="form-check-label fw-bold" for="isFeatured">Featured</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="p-3 border rounded bg-light">
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" name="is_active" id="isActive" checked>
                                        <label class="form-check-label fw-bold" for="isActive">Active</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer border-top-0 pt-0">
                        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary-gradient px-4">Create Category</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        function openCategoryModal(type) {
            const modal = new bootstrap.Modal(document.getElementById('categoryModal'));
            const parentField = document.getElementById('parentCategoryField');
            const title = document.getElementById('categoryModalLabel');
            const parentSelect = parentField.querySelector('select');

            if (type === 'parent') {
                title.innerText = 'Add Parent Category';
                parentField.style.display = 'none';
                parentSelect.value = ''; // Ensure null is sent
            } else {
                title.innerText = 'Add Sub Category';
                parentField.style.display = 'block';
            }
            
            modal.show();
        }
    </script>
    @endpush
@endsection
