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

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

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

    <header id="title--section" class="d-flex align-items-center justify-content-end mb-2">
        <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)causali @else Inserimento/modifica causale @endif</h2>
        </div>

        @if(!$add && !$update)
            <div class="title--section_addButton"  wire:click="add()" style="cursor: pointer;">
                <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" style="max-height: unset !important">
            <div class="compare--chart_wrapper d-none"></div>

            <h1>Entrata</h1>
            <div class="mb-3">
                <button type="button" class="btn btn-outline-secondary btn-sm" wire:click="toggleHidden()">
                    @if($showHidden)
                        <i class="fa-regular fa-eye-slash"></i> Nascondi elementi nascosti
                    @else
                        <i class="fa-regular fa-eye"></i> Mostra elementi nascosti
                    @endif
                </button>
            </div>
            <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
                <thead>
                    <tr>
                        <th scope="col" width="50%">Nome</th>
                        <th scope="col" width="10%">Tipologia</th>
                        <th scope="col" width="10%">Abilitato</th>
                        <th scope="col" width="30%">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target" wire:sortable="reorder" wire:sortable.options="{ animation: 100 }" wire:sortable-group="reorderGroup">
                    @foreach($recordsIn as $record)
                        <tr wire:sortable.item="{{ $record->id }}" wire:sortable.triggers="reorder">
                            <td style="font-weight: bold;">{{$record->name}}</td>
                            <td>{{$record->type == 'IN' ? 'Entrata' : 'Uscita'}}</td>
                            <td> <span class="tablesaw-cell-content"><span class="badge tessera-badge {{$record->enabled ? 'active' : 'suspended'}}">{{$record->enabled ? 'attivo' : 'disattivo'}}</span></span></td>
                            <td>
                                @if (!in_array($record->name, ['PAGAMENTO CORSO', 'PAGAMENTO ISCRIZIONE', 'PAGAMENTO ABBONAMENTO']))
                                    <button type="button" class="btn btn--ui btn-sm" wire:click="addLevel({{ $record->id }})">Aggiungi livello</button>
                                    <button type="button" class="btn btn--ui btn-sm" wire:click="duplicate({{ $record->id }})">Duplica</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>
                                    @if($record->hidden)
                                        <button type="button" class="btn" wire:click="show({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Ripristina"><i style="color: #3b994b" class="fa-regular fa-eye"></i></button>
                                    @else
                                        <button type="button" class="btn" onclick="confirm('Sei sicuro di voler nascondere questo elemento?') || event.stopImmediatePropagation()" wire:click="hide({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Nascondi"><i style="color: #be2828" class="fa-regular fa-eye-slash"></i></button>
                                    @endif
                                @endif
                            </td>
                        </tr>
                        @if(count($record->childs))
                            @include('livewire/causal_child',['records' => $record->childs, 'indentation' => 1])
                        @endif
                    @endforeach

                </tbody>
            </table>

            <br>
            <h1>Uscita</h1>
            <div class="mb-3">
                <button type="button" class="btn btn-outline-secondary btn-sm" wire:click="toggleHidden()">
                    @if($showHidden)
                        <i class="fa-regular fa-eye-slash"></i> Nascondi elementi nascosti
                    @else
                        <i class="fa-regular fa-eye"></i> Mostra elementi nascosti
                    @endif
                </button>
            </div>
            <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
                <thead>
                    <tr>
                        <th scope="col" width="50%">Nome</th>
                        <th scope="col" width="10%">Tipologia</th>
                        <th scope="col" width="10%">Abilitato</th>
                        <th scope="col" width="30%">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target" wire:sortable="reorder" wire:sortable.options="{ animation: 100 }" wire:sortable-group="reorderGroup">
                    @foreach($recordsOut as $record)
                        <tr wire:sortable.item="{{ $record->id }}" wire:sortable.triggers="reorder" class="record-level-0">
                            <td>
                                <span class="level-indicator level-0">{{$record->name}}</span>
                            </td>
                            <td>{{$record->type == 'IN' ? 'Entrata' : 'Uscita'}}</td>
                            <td>
                                <span class="tablesaw-cell-content">
                                    <span class="badge tessera-badge {{$record->enabled ? 'active' : 'suspended'}}">
                                        {{$record->enabled ? 'attivo' : 'disattivo'}}
                                    </span>
                                </span>
                            </td>
                            <td>
                                <button type="button" class="btn btn--ui btn-sm" wire:click="addLevel({{ $record->id }})">Aggiungi livello</button>
                                <button type="button" class="btn btn--ui btn-sm" wire:click="duplicate({{ $record->id }})">Duplica</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>
                                @if($record->hidden)
                                    <button type="button" class="btn" wire:click="show({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Mostra"><i style="color: #3b994b" class="fa-regular fa-eye"></i></button>
                                @else
                                    <button type="button" class="btn" onclick="confirm('Sei sicuro di voler nascondere questo elemento?') || event.stopImmediatePropagation()" wire:click="hide({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Nascondi"><i style="color: #be2828" class="fa-regular fa-eye-slash"></i></button>
                                @endif
                                </td>
                        </tr>
                        @if(count($record->childs))
                            @include('livewire/causal_child',['records' => $record->childs, 'indentation' => 1])
                        @endif
                    @endforeach
                </tbody>
            </table>

        </section>

    @else

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

        <div class="container">

            @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-md-6 col-sm-12">
                                <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 class="col-md-6 col-sm-12">
                                <label for="next_month_expire" class="form-label">Tipologia</label>
                                <select name="type" class="form-select  @error('type') is-invalid @enderror" aria-label="Seleziona una tipologia" wire:model="type">
                                    <option value="">--Seleziona--
                                    <option value="IN">Entrata
                                    <option value="OUT">Uscita
                                </select>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:0px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="enabled" wire:model="enabled">
                                <label class="form-check-label" for="enabled">Abilitato</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="money" wire:model="money">
                                <label class="form-check-label" for="money">Incrementa borsellino virtuale</label>
                            </div>
                        </div>



                         <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="no_receipt" wire:model="no_receipt">
                                <label class="form-check-label" for="no_receipt">Non generare ricevuta</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="user_status" wire:model="user_status">
                                <label class="form-check-label" for="user_status">Impatta stato utente</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="no_first" wire:model="no_first">
                                <label class="form-check-label" for="no_first">Escludi dalla prima nota</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="no_records" wire:model="no_records">
                                <label class="form-check-label" for="no_records">Escludi da entrate/uscite gestionale</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                @if($corrispettivo_causal_id == 0 || $corrispettivo_causal_id == $dataId)
                                    <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="corrispettivo_fiscale" wire:model="corrispettivo_fiscale">
                                @else
                                    <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="corrispettivo_fiscale" wire:model="corrispettivo_fiscale" disabled="disabled">
                                @endif
                                <label class="form-check-label" for="corrispettivo_fiscale">Utilizza per corrispettivo fiscale</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline" style="padding-left:28px !important;">
                                <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="no_reports" wire:model="no_reports">
                                <label class="form-check-label" for="no_reports">Escludi da reports</label>
                            </div>
                        </div>

                        <!-- // inline input field -->

                        <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
    <br>
</div>

@push('scripts')
    <!--
    <script src="https://cdn.jsdelivr.net/gh/livewire/sortable@v0.x.x/dist/livewire-sortable.js"></script>
    -->
@endpush
