
<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)Fornitori @else Inserimento/modifica fornitore @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" width="100%" id="tablesaw-350" width="100%">
                <thead>
                    <tr>
                        <th scope="col">Nome</th>
                        <th scope="col">Indirizzo</th>
                        <th scope="col">Email</th>
                        <th scope="col">Abilitato</th>
                        <th scope="col">...</th>
                    </tr>
                </thead>
                <tbody id="checkall-target">
                    @foreach($records as $record)
                        <tr>
                            <td>{{$record->name}}</td>
                            <td>{{$record->address}}</td>
                            <td>{{$record->email}}</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

    <a style="margin-top:20px" class="btn--ui lightGrey" href="/suppliers"><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="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">
                                <div class="form--item">
                                    <label for="address" class="form-label">Indirizzo</label>
                                    <input class="form-control" type="text" id="address" placeholder="Indirizzo" wire:model="address">
                                </div>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <label for="zip_code" class="form-label">CAP</label>
                                    <input class="form-control" id="zip_code" placeholder="CAP" wire:model="zip_code" maxlength="5">
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6 mb-3">
                                <label for="nation_id" class="form-label">Nazione</label>
                                <select name="nation_id" class="form-select nationClass" aria-label="Seleziona una nazione" wire:model="nation_id">
                                    <option value="{{$nation_id}}">{{$this->getNation($nation_id)}}
                                </select>
                            </div>
                            @if($isItaly)
                                <div class="col-md-6 mb-3">
                                    <label for="province_id" class="form-label">Provincia</label>
                                    <select name="province_id" class="form-select provinceClass" aria-label="Seleziona una provincia" wire:model="province_id">
                                        <option value="{{$province_id}}">{{$this->getProvince($province_id)}}
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="city_id" class="form-label">Comune</label>
                                    <select name="city_id" class="form-select cityClass" aria-label="Seleziona un comune" wire:model="city_id">
                                        <option value="{{$city_id}}">{{$this->getCity($city_id)}}
                                    </select>
                                </div>
                            @else
                                <div class="col"></div>
                                <div class="col"></div>
                            @endif
                        </div>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="fiscal_code" class="form-label">Codice fiscale</label>
                                    <input class="form-control" type="text" id="fiscal_code" placeholder="Codice fiscale" wire:model="fiscal_code" maxlength="16">
                                </div>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <label for="vat" class="form-label">Partita iva</label>
                                    <input class="form-control" type="text" id="vat" placeholder="Partita iva" wire:model="vat" maxlength="11">
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="website" class="form-label">Sito web</label>
                                    <input class="form-control" type="text" id="website" placeholder="Sito web" wire:model="website">
                                </div>
                            </div>
                            <div class="col"></div>
                        </div>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="email" class="form-label">Email</label>
                                    <input class="form-control" type="text" id="email" placeholder="Email" wire:model="email">
                                </div>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <label for="phone" class="form-label">Telefono</label>
                                    <input class="form-control" type="text" id="phone" placeholder="Telefono" wire:model="phone">
                                </div>
                            </div>
                        </div>

                        <span class="title-form primary d-block w-100">Referente</span>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="referent_first_name" class="form-label">Nome</label>
                                    <input class="form-control" type="text" id="referent_first_name" placeholder="Nome" wire:model="referent_first_name">
                                </div>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <label for="referent_last_name" class="form-label">Cognome</label>
                                    <input class="form-control" type="text" id="referent_last_name" placeholder="Cognome" wire:model="referent_last_name">
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="referent_phone" class="form-label">Telefono</label>
                                    <input class="form-control" type="text" id="referent_phone" placeholder="Telefono" wire:model="referent_phone">
                                </div>
                            </div>
                            <div class="col">
                                <div class="form--item">
                                    <label for="referent_mobile" class="form-label">Cellulare</label>
                                    <input class="form-control" type="text" id="referent_mobile" placeholder="Cellulare" wire:model="referent_mobile">
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col">
                                <div class="form--item">
                                    <label for="referent_email" class="form-label">Email</label>
                                    <input class="form-control" type="text" id="referent_email" placeholder="Email" wire:model="referent_email">
                                </div>
                            </div>
                            <div class="col"></div>
                        </div>


                        <div class="row mb-3">
                            <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" onclick="annulla()">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="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>
        .select2-container--default .select2-selection--single{
            background-color: #E9F0F5;
            border: 0.0625rem solid #DFE5EB;
            font-size: 0.75rem;
        }
        .select2-selection
        {
            height: 38px !important;
        }
        .select2-selection__rendered
        {
            padding-top:3px;
        }
    </style>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
@endpush

@push('scripts')
    <script>
        var isEdit = false;
        Livewire.on('setEdit', (x) =>
        {
            isEdit = x;
            console.log(isEdit);
        });

        function matchStart(params, data) {
            params.term = params.term || '';
            if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
                return data;
            }
            return false;
        }
        var nation_id = 0;
        var province_id = 0;

        function annulla()
        {
            window.onbeforeunload = null;
            document.location.href = '/suppliers';
        }

        window.onbeforeunload = function(){
            if (isEdit) {
                return 'Cambiando pagina le eventuali modifiche andranno perse';
            }
        };

        Livewire.on('load-select', () => {

            $('.nationClass').select2({
                ajax: {
                    url: '/nations',
                    dataType: 'json'
                }
            });

            $('.nationClass').on('change', function (e) {
                nation_id = $('.nationClass').select2("val");
                province_id = 0;
                @this.set('nation_id', nation_id);
                @this.set('province_id', 0);
                @this.set('city_id', 0);
                @this.checkIsItaly();
            });

            $('.provinceClass').select2({
                ajax: {
                    url: '/provinces/' + nation_id,
                    dataType: 'json'
                }
            });
            $('.provinceClass').on('change', function (e) {
                province_id = $('.provinceClass').select2("val");
                @this.set('province_id', province_id);
                @this.set('city_id', 0);
            });

            $('.cityClass').select2({
                ajax: {
                    url: '/cities/' + province_id,
                    dataType: 'json'
                }
            });
            $('.cityClass').on('change', function (e) {
                var data = $('.cityClass').select2("val");
                @this.set('city_id', data);

            });

            $('.nationBirthClass').select2({
                ajax: {
                    url: '/nations',
                    dataType: 'json'
                }
            });

        });

        Livewire.on('load-provinces', (nation_id, element) => {
            $('.' + element).select2({
                ajax: {
                    url: '/provinces/' + nation_id,
                    dataType: 'json'
                }
            });
        });

        Livewire.on('load-cities', (province_id, element) => {
            $('.' + element).select2({
                ajax: {
                    url: '/cities/' + province_id,
                    dataType: 'json'
                }
            });
        });

    </script>

    <link href="/css/datatables.css" rel="stylesheet" />
    <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>
    <script>

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

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

        function loadDataTable(){
            if ( $.fn.DataTable.isDataTable('#tablesaw-350') ) {
                $('#tablesaw-350').DataTable().destroy();
            }
            $('#tablesaw-350').DataTable({
                thead: {
                'th': {'background-color': 'blue'}
                },
                layout: {
                    topStart : null,
                    topEnd : null,
                    top1A: {
                        buttons: [
                            {
                                extend: 'collection',
                                text: 'ESPORTA',
                                buttons: [
                                    {
                                    extend: 'excelHtml5',
                                        title: 'Fornitori',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        }
                                    },
                                    {
                                        extend: 'pdfHtml5',
                                        title: 'Fornitori',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        },
                                        customize: function(doc) {
                                            doc.styles.tableHeader.alignment = 'left';
                                        }
                                    },
                                    {
                                        extend: 'print',
                                        text: 'Stampa',
                                        title: 'Fornitori',
                                        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
