@extends('layouts.app')

@section('title', $showingDeleted ? 'Deleted Categories' : 'Asset Categories')

@section('content')
    @php
        $indexUrl = route('masters.categories.index');
        $listUrl = $showingDeleted ? route('masters.categories.index', ['trashed' => 1]) : $indexUrl;
    @endphp

    <x-page-header
        :title="$showingDeleted ? 'Deleted Categories' : 'Asset Categories'"
        :subtitle="trans_choice(':count category|:count categories', $categories->total(), ['count' => $categories->total()])">

        @if ($showingDeleted)
            <a href="{{ $indexUrl }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left" aria-hidden="true"></i>Back to Categories
            </a>
        @else
            @can('access-admin')
                @if ($deletedCount > 0)
                    <a href="{{ route('masters.categories.index', ['trashed' => 1]) }}"
                       class="btn btn-outline-secondary">
                        <i class="bi bi-trash3" aria-hidden="true"></i>Deleted
                        <x-badge color="secondary">{{ $deletedCount }}</x-badge>
                    </a>
                @endif
            @endcan

            <a href="{{ route('masters.categories.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-lg" aria-hidden="true"></i>Add Category
            </a>
        @endif
    </x-page-header>

    <x-table.wrapper>
        <x-slot:toolbar>
            <x-filter-bar
                :action="$listUrl"
                :active="(bool) array_filter($filters)"
                :reset-url="$listUrl">

                @if ($showingDeleted)
                    <input type="hidden" name="trashed" value="1">
                @endif

                <x-filter.search :value="$filters['search']" placeholder="Search name or code…" />

                <x-filter.select
                    name="level"
                    label="Level"
                    placeholder="Any level"
                    :selected="$filters['level']"
                    :options="['root' => 'Top-level only', 'sub' => 'Subcategories only']" />

                <x-filter.select
                    name="status"
                    label="Status"
                    placeholder="Any status"
                    :selected="$filters['status']"
                    :options="['active' => 'Active', 'inactive' => 'Inactive']" />
            </x-filter-bar>
        </x-slot:toolbar>

        <x-slot:head>
            <tr>
                <th scope="col">Category</th>
                <th scope="col">Code</th>
                <th scope="col">Warranty</th>
                <th scope="col">Depreciation</th>
                <th scope="col">Status</th>
                <th scope="col" class="text-end">Actions</th>
            </tr>
        </x-slot:head>

        @forelse ($categories as $category)
            <tr>
                <td>
                    <div class="d-flex align-items-center flex-wrap gap-1">
                        @if ($category->parent)
                            <span class="ams-cell-secondary">{{ $category->parent->name }}</span>
                            <i class="bi bi-chevron-right ams-text-xs text-body-tertiary" aria-hidden="true"></i>
                        @endif

                        <span class="ams-cell-primary">{{ $category->name }}</span>

                        @if ($category->children_count > 0)
                            <x-badge color="light">
                                {{ trans_choice(':count subcategory|:count subcategories', $category->children_count, ['count' => $category->children_count]) }}
                            </x-badge>
                        @endif
                    </div>
                </td>

                <td><span class="ams-cell-code">{{ $category->code }}</span></td>

                <td class="text-nowrap ams-cell-secondary">
                    @if ($category->default_warranty_months)
                        {{ trans_choice(':count month|:count months', $category->default_warranty_months, ['count' => $category->default_warranty_months]) }}
                    @else
                        <span class="text-body-tertiary">—</span>
                    @endif
                </td>

                <td class="text-nowrap ams-cell-secondary">
                    @if ($category->depreciation_years)
                        {{ trans_choice(':count year|:count years', $category->depreciation_years, ['count' => $category->depreciation_years]) }}
                    @else
                        <span class="text-body-tertiary">—</span>
                    @endif
                </td>

                <td>
                    @if ($category->is_active)
                        <x-badge color="success" dot>Active</x-badge>
                    @else
                        <x-badge color="secondary" dot>Inactive</x-badge>
                    @endif
                </td>

                <td class="text-end text-nowrap">
                    <x-table.actions>
                        @if ($showingDeleted)
                            @can('access-admin')
                                <form method="POST"
                                      action="{{ route('masters.categories.restore', $category) }}"
                                      data-ams-confirm="Restore the category {{ $category->name }}?"
                                      data-ams-confirm-title="Restore category"
                                      data-ams-confirm-action="Restore">
                                    @csrf
                                    @method('PATCH')
                                    <button type="submit" class="btn btn-sm btn-outline-success">
                                        <i class="bi bi-arrow-counterclockwise" aria-hidden="true"></i>Restore
                                    </button>
                                </form>
                            @endcan
                        @else
                            <a href="{{ route('masters.categories.edit', $category) }}"
                               class="btn btn-sm btn-icon"
                               data-bs-toggle="tooltip"
                               title="Edit">
                                <i class="bi bi-pencil" aria-hidden="true"></i>
                                <span class="visually-hidden">Edit {{ $category->name }}</span>
                            </a>

                            @can('access-admin')
                                <form method="POST"
                                      action="{{ route('masters.categories.destroy', $category) }}"
                                      data-ams-confirm="Delete the category {{ $category->name }}? You can restore it afterwards from the deleted list."
                                      data-ams-confirm-title="Delete category"
                                      data-ams-confirm-action="Delete">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit"
                                            class="btn btn-sm btn-icon btn-outline-danger"
                                            data-bs-toggle="tooltip"
                                            title="Delete">
                                        <i class="bi bi-trash3" aria-hidden="true"></i>
                                        <span class="visually-hidden">Delete {{ $category->name }}</span>
                                    </button>
                                </form>
                            @endcan
                        @endif
                    </x-table.actions>
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="6" class="p-0">
                    <x-table.empty-state
                        icon="tags"
                        :title="$showingDeleted ? 'Nothing in the deleted list' : 'No categories match these filters'"
                        :message="array_filter($filters)
                            ? 'Try clearing the filters to see every category.'
                            : ($showingDeleted
                                ? 'Deleted categories appear here and can be restored.'
                                : 'Add the kinds of asset Aumni owns — laptops, monitors, phones.')">
                        @if (array_filter($filters))
                            <a href="{{ $listUrl }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-lg" aria-hidden="true"></i>Clear filters
                            </a>
                        @elseif (! $showingDeleted)
                            <a href="{{ route('masters.categories.create') }}" class="btn btn-primary">
                                <i class="bi bi-plus-lg" aria-hidden="true"></i>Add Category
                            </a>
                        @endif
                    </x-table.empty-state>
                </td>
            </tr>
        @endforelse

        @if ($categories->hasPages())
            <x-slot:footer>
                <x-table.count :paginator="$categories" noun="category" />
                {{ $categories->links() }}
            </x-slot:footer>
        @endif
    </x-table.wrapper>
@endsection
