<div class="col card--ui" id="card--dashboard">

    @if(!$add && !$update)

    <a class="btn--ui lightGrey" href="/settings?type=anagrafica"><i class="fa-solid fa-arrow-left"></i></a><br>

    <header id="title--section"  class="d-flex align-items-center justify-content-between">
        <div class="title--section_name d-flex align-items-center justify-content-between" style="display:none !important" >
            <i class="ico--ui title_section utenti me-2"></i>
            <h2 class="primary">@if(!$add && !$update)Categorie @else Inserimento/modifica categoria @endif</h2>
        </div>

        @if(!$add && !$update)
            <div class="title--section_addButton text-right "  wire:click="add()" style="cursor: pointer;margin-top:20px;">
                <div class="btn--ui entrata d-flex justify-items-between">
                    <a href="#" wire:click="add()" style="color:white">AGGIUNGI</a>
                </div>
            </div>

        @endif

    </header>




        <section id="resume-table">
            <div class="compare--chart_wrapper d-none"></div>

            <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
                <thead>
                    <tr>
                        <th scope="col">Nome</th>
                        <th scope="col">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target">
                    @foreach($records as $record)
                        <tr>
                            <td style="font-weight: bold">{{$record->name}}</td>
                            <td>
                                <button type="button" class="btn btn-outline-success btn-sm" wire:click="addLevel({{ $record->id }})">Aggiungi livello</button>
                                <button type="button" class="btn" wire:click="edit({{ $record->id }})" data-bs-toggle="popover"  data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Modifica"><i class="fa-regular fa-pen-to-square"></i></button>
                                <button type="button" class="btn" onclick="confirm('Sei sicuro?') || event.stopImmediatePropagation()" wire:click="delete({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Elimina"><i class="fa-regular fa-trash-can"></i></button>
                            </td>
                        </tr>
                        @if(count($record->childs))
                            @include('livewire/category_child',['records' => $record->childs, 'indentation' => 1])
                        @endif
                    @endforeach

                </tbody>
            </table>

        </section>

    @else

        <div class="container">

            <a class="btn--ui lightGrey" href="/categories"><i class="fa-solid fa-arrow-left"></i></a><br><br>

            @if (session()->has('error'))
                <div class="alert alert-danger" role="alert">
                    {{ session()->get('error') }}
                </div>
            @endif

            <div class="row">
                <div class="col">

                    <form action="">

                        @if($parent != '')
                            <h3>Aggiungi livello a {{$parent}}</h3><br>
                        @endif

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="inputName" class="form-label">Nome</label>
                                    <input class="form-control js-keyupTitle @error('name') is-invalid @enderror" type="text" id="name" placeholder="Nome" wire:model="name">
                                    @error('name')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="form--item">
                            <button type="button" class="btn--ui lightGrey" wire:click="cancel()">Annulla</button>
                        @if($add)
                            <button type="submit" class="btn--ui" wire:click.prevent="store()">Salva</button>
                        @endif
                        @if($update)
                            <button type="submit" class="btn--ui" wire:click.prevent="update()">Salva</button>
                        @endif
                        </div>

                    </form>
                </div>
            </div>
        </div>

    @endif
</div>
