<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)comuni @else Inserimento/modifica comuni @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">Provincia</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->province_name}}</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>

        </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="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="province_id" class="form-label">Provincia</label>
                                <select name="province_id" class="form-select  @error('province_id') is-invalid @enderror" aria-label="Seleziona una provincia" wire:model="province_id">
                                    <option value="">--Seleziona--
                                    @foreach($provinces as $province)
                                        <option value="{{$province->id}}">{{$province->name}}
                                    @endforeach
                                </select>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <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>
                        </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()}${date.getDate()}_`;

            if ( $.fn.DataTable.isDataTable('#tablesaw-350') ) {
                $('#tablesaw-350').DataTable().destroy();
            }
            $('#tablesaw-350').DataTable({
                processing: 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 + 'Comuni',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'pdfHtml5',
                                text: '<i class="fa-solid fa-file-pdf"></i>',
                                action: newexportaction,
                                title: date_export + 'Comuni',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'print',
                                action: newexportaction,
                                text: '<i class="fa-solid fa-print"></i>',
                                title: date_export + 'Comuni',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            }
                        ]
                    }
                },
                pagingType: 'numbers',
                "language": {
                    "url": "/assets/js/Italian.json"
                }
            });
            $('#tablesaw-350 thead tr th').addClass('col');
            $('#tablesaw-350 thead tr th').css("background-color", "#f6f8fa");

        }

    </script>
@endpush
