@extends('layouts.app')

@section('title', 'Edit Category')

@section('container-class', 'ams-container--narrow')

@section('content')
    <x-page-header
        title="Edit Asset Category"
        :subtitle="$category->qualifiedName()">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('masters.categories.index') }}">Asset Categories</a></li>
            <li class="breadcrumb-item active" aria-current="page">{{ $category->name }}</li>
        </x-slot:breadcrumbs>
    </x-page-header>

    <div class="row g-3">
        <div class="col-xl-8">
            <div class="card">
                <div class="card-body p-4">
                    <form method="POST" action="{{ route('masters.categories.update', $category) }}" novalidate>
                        @csrf
                        @method('PUT')

                        @include('masters.categories._form', ['category' => $category, 'parents' => $parents])

                        <div class="ams-form-actions">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check2" aria-hidden="true"></i>Save Changes
                            </button>
                            <a href="{{ route('masters.categories.index') }}" class="btn btn-ghost">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-4">
            @php $children = $category->children()->orderBy('name')->get(); @endphp

            <div class="d-flex flex-column gap-3">
                @if ($children->isNotEmpty())
                    <x-panel
                        title="Subcategories"
                        :subtitle="trans_choice(':count subcategory sits under this one.|:count subcategories sit under this one.', $children->count(), ['count' => $children->count()])"
                        flush>

                        <ul class="list-group list-group-flush">
                            @foreach ($children as $child)
                                <li class="list-group-item d-flex align-items-center justify-content-between gap-2">
                                    <span class="min-width-0">
                                        <span class="ams-cell-primary">{{ $child->name }}</span>
                                        <span class="ams-cell-code text-body-tertiary ms-1">{{ $child->code }}</span>
                                    </span>
                                    <a href="{{ route('masters.categories.edit', $child) }}"
                                       class="btn btn-sm btn-outline-secondary flex-shrink-0">Edit</a>
                                </li>
                            @endforeach
                        </ul>
                    </x-panel>
                @endif

                @can('access-admin')
                    @if ($children->isNotEmpty())
                        {{--
                            A parent with children cannot be deleted, so the panel
                            explains rather than offers. Showing a button that is
                            guaranteed to be refused would be worse than showing none.
                        --}}
                        <x-panel title="Delete category" tone="warning">
                            <p class="text-secondary mb-0">
                                This category has
                                {{ trans_choice(':count subcategory|:count subcategories', $children->count(), ['count' => $children->count()]) }}
                                and cannot be deleted. Delete or move them first, or
                                deactivate this category instead.
                            </p>
                        </x-panel>
                    @else
                        <x-panel
                            title="Delete category"
                            subtitle="Kept on record and restorable from the deleted list."
                            tone="danger">

                            <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-outline-danger">
                                    <i class="bi bi-trash3" aria-hidden="true"></i>Delete Category
                                </button>
                            </form>
                        </x-panel>
                    @endif
                @endcan
            </div>
        </div>
    </div>
@endsection
