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

    <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)Province @else Inserimento/modifica provincia @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>


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

        <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">Nazione</th>
                        <th scope="col">Codice</th>
                        <th scope="col">Nome</th>
                        <th scope="col">Abilitato</th>
                        <th scope="col">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target">
                    @foreach($records as $record)
                        <tr>
                            <td>{{$record->nation}}</td>
                            <td>{{$record->code}}</td>
                            <td>{{$record->name}}</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>
                                <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>
                    @endforeach
                </tbody>
            </table>
            <!--
            <div class="paginator d-flex justify-content-center">
                <nav aria-label="Page navigation example">
                    <ul class="pagination">
                        <li class="page-item">
                        <a class="page-link" href="#" aria-label="Previous">
                            <span aria-hidden="true"></span>
                        </a>
                        </li>
                        <li class="page-item"><a class="page-link" href="#">1</a></li>
                        <li class="page-item"><a class="page-link" href="#">2</a></li>
                        <li class="page-item"><a class="page-link" href="#">3</a></li>
                        <li class="page-item"><a class="page-link" href="#">3</a></li>

                        <li class="page-item"><span class="more-page">...</span></li>

                        <li class="page-item">
                        <a class="page-link" href="#" aria-label="Next">
                            <span aria-hidden="true"></span>
                        </a>
                        </li>
                    </ul>
                    </nav>
            </div>
            -->
        </section>

    @else

        <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="code" class="form-label">Codice</label>
                                    <input class="form-control" type="text" id="code" placeholder="Codice" wire:model="code">
                                </div>
                            </div>
                            <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="row mb-3">
                            <div class="col">
                                <label for="nation_id" class="form-label">Nazione</label>
                                <select name="nation_id" class="form-select @error('nation_id') is-invalid @enderror" aria-label="Seleziona una nazione" wire:model="nation_id">
                                    <option value="">--Seleziona--
                                    @foreach($nations as $nation)
                                        <option value="{{$nation->id}}">{{$nation->name}}
                                    @endforeach
                                </select>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <div class="form-check form-check-inline">
                                    <label class="form-check-label" for="enabled">Abilitato</label><br>
                                    <input class="form-check-input" type="checkbox" id="enabled" wire:model="enabled">

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

@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'}
                },
                layout: {
                    topStart: {
                        buttons: [
                            {
                                extend: 'excelHtml5',
                                text: '<i class="fa-solid fa-file-excel"></i>',
                                action: newexportaction,
                                title: date_export + 'Provincie',
                                filename: date_export + 'Provincie',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'pdfHtml5',
                                text: '<i class="fa-solid fa-file-pdf"></i>',
                                action: newexportaction,
                                title: date_export + 'Provincie',
                                filename: date_export + 'Provincie',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'print',
                                action: newexportaction,
                                text: '<i class="fa-solid fa-print"></i>',
                                title: date_export + 'Provincie',
                                filename: date_export + 'Provincie',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            }
                        ]
                    },
                    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>',
                    }
                },
            });
            $('#tablesaw-350 thead tr th').addClass('col');
            $('#tablesaw-350 thead tr th').css("background-color", "#f6f8fa");

        }

    </script>
@endpush
