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

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

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

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

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

            <!-- Toggle button to show/hide hidden items -->
            <div class="mb-3 mt-3">
                <button type="button" class="btn btn-outline-secondary btn-sm" wire:click="toggleShowHidden">
                    @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">Origine</th>
                        <th scope="col">Destinazione</th>
                        <th scope="col">Nome</th>
                        <th scope="col">Tipo</th>
                        <th scope="col">Abilitato</th>
                        <th scope="col">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target">
                    @foreach($records as $record)
                        <tr @if(isset($record->hidden) && $record->hidden) style="opacity: 0.6; font-style: italic;" @endif>
                            <td>{{$record->origin_id ? $record->origin->name : ''}}</td>
                            <td>{{$record->destination_id ? $record->destination->name : ''}}</td>
                            <td>
                                {{$record->name}}
                                @if(isset($record->hidden) && $record->hidden)
                                    <span class="text-muted">(nascosto)</span>
                                @endif
                            </td>
                            <td>
                                @php
                                switch ($record->type) {
                                    case 'IN':
                                        echo "Entrate";
                                        break;
                                    case 'OUT':
                                        echo "Uscite";
                                        break;
                                    case 'ALL':
                                        echo "Entrate/Uscite";
                                        break;
                                }
                                @endphp
                            </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" 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(isset($record->hidden) && $record->hidden)
                                    <button type="button" class="btn btn-success" wire:click="show({{ $record->id }})" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Mostra"><i class="fa-regular fa-eye"></i></button>
                                @else
                                    <button type="button" class="btn btn-secondary" onclick="confirm('Sei sicuro di voler nascondere questo metodo di pagamento?') || 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 class="fa-regular fa-eye-slash"></i></button>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </section>

    @else

    <a class="btn--ui lightGrey" href="/payment_methods"><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="">

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="name" class="form-label">Nome</label>
                                    <select class="form-control @error('name') is-invalid @enderror" id="name" wire:model="name">
                                        <option value="">Seleziona un metodo di pagamento</option>
                                        @foreach($paymentMethods as $method)
                                            <option value="{{ $method['name'] }}">{{ $method['name'] }} ({{ $method['code'] }})</option>
                                        @endforeach
                                    </select>
                                    @error('name')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <label for="type" class="form-label">Tipologia</label>
                                <select name="type" class="form-select" aria-label="Seleziona una tipologia" wire:model="type">
                                    <option value="ALL">Entrate/Uscite</option>
                                    <option value="IN">Entrate</option>
                                    <option value="OUT">Uscite</option>
                                </select>
                            </div>
                        </div>
                        <div class="row mb-3">

                            <div class="col">
                                <label for="origin_id" class="form-label">Origine</label>
                                <select name="origin_id" class="form-select" aria-label="Seleziona un'origine" wire:model="origin_id">
                                    <option value="">--Seleziona--
                                    @foreach($origins as $origin)
                                        <option value="{{$origin->id}}">{{$origin->name}}
                                    @endforeach
                                </select>
                            </div>

                            <div class="col">
                                <label for="destination_id" class="form-label">Destinazione</label>
                                <select name="destination_id" class="form-select" aria-label="Seleziona una destinazione" wire:model="destination_id">
                                    <option value="">--Seleziona--
                                    @foreach($destinations as $destination)
                                        <option value="{{$destination->id}}">{{$destination->name}}
                                    @endforeach
                                </select>
                            </div>
                        </div>

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

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="checkbox" id="money" wire:model="money">
                                <label class="form-check-label" for="money">Decrementa borsellino virtuale</label>
                            </div>
                        </div>

                        <div class="form--item mb-3">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="checkbox" id="corrispettivo_fiscale" wire:model="corrispettivo_fiscale">
                                <label class="form-check-label" for="corrispettivo_fiscale">Utilizza per corrispettivo fiscale</label>
                            </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>

@if (session()->has('success'))
    <div class="alert alert-success alert-dismissible fade show mt-3" role="alert">
        {{ session()->get('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

@if (session()->has('error'))
    <div class="alert alert-danger alert-dismissible fade show mt-3" role="alert">
        {{ session()->get('error') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

@push('scripts')
    <link href="/css/datatables.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="/assets/js/datatables.js"></script>
    <script src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.dataTables.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
@endpush

@push('scripts')
    <script>

        $(document).ready(function() {
            loadDataTable();
        });

        Livewire.on('load-data-table', () => {
            loadDataTable();
        });

        function loadDataTable(){
            let date = new Date();
            let date_export = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}_`;

            if ($.fn.DataTable.isDataTable('#tablesaw-350')) {
                $('#tablesaw-350').DataTable().destroy();
            }

            $('#tablesaw-350').DataTable({
                processing: true,
                stateSave: true,
                thead: {
                'th': {'background-color': 'blue'}
                },
                order: [
                    [2, 'asc']
                ],
                layout: {
                    topStart : null,
                    topEnd : null,
                    top1A: {
                        // buttons: [
                        //     {
                        //         extend: 'collection',
                        //         text: 'ESPORTA',
                                buttons: [
                                    {
                                        extend: 'excelHtml5',
                                        text: '<i class="fa-solid fa-file-excel"></i>',
                                        action: newexportaction,
                                        title: date_export + 'Metodi di pagamento',
                                        filename: date_export + 'Metodi di pagamento',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        }
                                    },
                                    {
                                        extend: 'pdfHtml5',
                                        text: '<i class="fa-solid fa-file-pdf"></i>',
                                        action: newexportaction,
                                        title: date_export + 'Metodi di pagamento',
                                        filename: date_export + 'Metodi di pagamento',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        }
                                    },
                                    {
                                        extend: 'print',
                                        action: newexportaction,
                                        text: '<i class="fa-solid fa-print"></i>',
                                        title: date_export + 'Metodi di pagamento',
                                        filename: date_export + 'Metodi di pagamento',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        }
                                    }
                                ],
                        //         dropup: true
                        //     }
                        // ]
                    },
                    top1B : {
                        pageLength: {
                            menu: [[10, 25, 50, 100, 100000], [10, 25, 50, 100, "Tutti"]]
                        }
                    },
                    top1C :'search',
                    bottomEnd: {
                        paging: {
                            boundaryNumbers: false
                        }
                    }
                },
                pagingType: 'first_last_numbers',
                language: {
                    "url": "/assets/js/Italian.json",
                    paginate: {
                        first: '<i class="fa-solid fa-angles-left"></i>',
                        last: '<i class="fa-solid fa-angles-right"></i>',
                    }
                },
                "fnInitComplete": function (oSettings, json) {
                    var html = '&nbsp;<a href="#" class="addData btn--ui"><i class="fa-solid fa-plus"></i></a>';
                    $(".dt-search").append(html);
                }
            });
            $('#tablesaw-350 thead tr th').addClass('col');
            $('#tablesaw-350 thead tr th').css("background-color", "#f6f8fa");

            $(document).ready(function() {
                $(document).on("click",".addData",function() {
                    $(".title--section_addButton").trigger("click")
                });
            });

        }

    </script>
@endpush
