
<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)Sponsor @else Inserimento/modifica sponsor @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="subheader" class="d-flex align-items-center justify-content-between">

            <form action="" class="search--form d-flex align-items-center">
                <div class="input-group mb-3">
                    <input type="text" class="form-control" placeholder="Cerca sponsor" aria-label="cerca utent" aria-describedby="button-addon2" wire:model="search">
                    @if(false)
                        @if($showReset)
                            <button class="btn--ui" type="button" id="button-addon2" wire:click="resetSearch()"><i class="ico--ui search"></i>Reset</button>
                        @else
                            <button class="btn--ui" type="button" id="button-addon2" wire:click="search()"><i class="ico--ui search"></i>Cerca</button>
                        @endif
                    @endif
                </div>
            </form>
        </section>

        <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">Ragione sociale</th>
                        <th scope="col">Indirizzo</th>
                        <th scope="col">Email</th>
                        <th scope="col">Telefono</th>
                        <th scope="col">Partita IVA</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>{{$record->phone}}</td>
                            <td>{{$record->vat}}</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



            @if (session()->has('error'))
                <div class="alert alert-danger" role="alert">
                    {{ session()->get('error') }}
                </div>
            @endif

            <section id="card--container" class="">

                <div id="card--nuovoUtente">
                    <ul class="nav nav-tabs" id="myTab" role="tablist">
                        <li class="nav-item" role="presentation">
                        <button wire:ignore.self class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home-tab-pane" type="button" role="tab" aria-controls="home-tab-pane" aria-selected="true">INFO GENERICHE</button>
                        </li>
                        <li class="nav-item" role="presentation">
                        <button wire:ignore.self class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile-tab-pane" type="button" role="tab" aria-controls="profile-tab-pane" aria-selected="false">CONTRATTI</button>
                        </li>
                    </ul>
                    <div class="tab-content card--ui" id="myTabContent">

                        <!-- TAB INFO GENERICHE -->
                        <div class="tab-pane fade show active info-tab" id="home-tab-pane" role="tabpanel" aria-labelledby="home-tab" tabindex="0" wire:ignore.self>
                            <div class="form--wrapper">
                                <form class="form--utente">
                                    <div class="">
                                        <div class="row g-3">
                                            <div class="col">
                                                <div class="form--item">
                                                    <label for="inputName" class="form-label">Ragione sociale</label>
                                                    <input class="form-control js-keyupTitle @error('name') is-invalid @enderror" type="text" id="name" placeholder="Ragione sociale" 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="first_name" class="form-label">Nome</label>
                                                    <input class="form-control" type="text" id="first_name" placeholder="Nome" wire:model="first_name">
                                                </div>
                                            </div>
                                            <div class="col">
                                                <div class="form--item">
                                                    <label for="last_name" class="form-label">Cognome</label>
                                                    <input class="form-control" type="text" id="last_name" placeholder="Cognome" wire:model="last_name">
                                                </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" type="text" 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="phone" class="form-label">Telefono</label>
                                                    <input class="form-control" type="text" id="phone" placeholder="Telefono" wire:model="phone">
                                                </div>
                                            </div>
                                            <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>

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

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

                        <div class="tab-pane fade tasseramento-tab" id="profile-tab-pane" role="tabpanel" aria-labelledby="profile-tab" tabindex="0" wire:ignore.self>

                            @if(!$addContract && !$updateContract)


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

                                    <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350-1">
                                        <thead>
                                            <tr>
                                                <th scope="col">Data inizio</th>
                                                <th scope="col">Data fine</th>
                                                <th scope="col">Data sottoscrizione</th>
                                                <th scope="col">Valore</th>
                                                <th scope="col">Contratto</th>
                                                <th scope="col">...</th>
                                            </tr>
                                        </thead>
                                        <tbody id="checkall-target">
                                            @foreach($contracts as $contract)
                                                <tr>
                                                    <td>{{date("d/m/Y", strtotime($contract->from_date))}}</td>
                                                    <td>{{date("d/m/Y", strtotime($contract->to_date))}}</td>
                                                    <td>{{date("d/m/Y", strtotime($contract->subscription_date))}}</td>
                                                    <td>{{formatPrice($contract->amount)}}</td>
                                                    <td>
                                                        @if ($contract->file != '')
                                                            <a href="{{ asset('storage/app/public/'.$contract->file) }}" target="_blank">Visualizza</a>
                                                        @endif
                                                    </td>
                                                    <td>
                                                        <button type="button" class="btn" wire:click="editContract({{ $contract->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="deleteContract({{ $contract->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>

                                    <button type="submit" class="btn--ui" wire:click.prevent="addContract()">Aggiungi</button>

                                </section>

                            @else

                                <div class="container">

                                    <div class="row">
                                        <div class="col">

                                            <form action="">

                                                <div class="row mb-3">
                                                    <div class="col">
                                                        <div class="form--item">
                                                            <label class="form-label">Data inizio</label>
                                                            <input class="form-control @error('from_date') is-invalid @enderror" type="date"  wire:model="from_date">
                                                            @error('from_date')
                                                                <div class="invalid-feedback">{{ $message }}</div>
                                                            @enderror
                                                        </div>
                                                    </div>
                                                    <div class="col">
                                                        <div class="form--item">
                                                            <label class="form-label">Data fine</label>
                                                            <input class="form-control js-keyupTitle @error('to_date') is-invalid @enderror" type="date"  placeholder="Data fine" wire:model="to_date">
                                                            @error('to_date')
                                                                <div class="invalid-feedback">{{ $message }}</div>
                                                            @enderror
                                                        </div>
                                                    </div>
                                                    <div class="col">
                                                        <div class="form--item">
                                                            <label class="form-label">Data sottoscrizione</label>
                                                            <input class="form-control js-keyupTitle @error('subscription_date') is-invalid @enderror" type="date" id="subscription_date" placeholder="Data sottoscrizione" wire:model="subscription_date">
                                                            @error('subscription_date')
                                                                <div class="invalid-feedback">{{ $message }}</div>
                                                            @enderror
                                                        </div>
                                                    </div>
                                                    <div class="col">
                                                        <div class="form--item">
                                                            <label class="form-label">Importo</label>
                                                            <input class="form-control js-keyupTitle @error('amount') is-invalid @enderror" type="text" id="amount" placeholder="Importo" wire:model="amount" onkeyup="onlyNumberAmount(this)" placeholder="€ 0,00">
                                                            @error('amount')
                                                                <div class="invalid-feedback">{{ $message }}</div>
                                                            @enderror
                                                        </div>
                                                    </div>
                                                    <div class="col">
                                                        <div class="form--item">
                                                            <label class="form-label">Contratto</label>
                                                            <input class="form-control" type="file" wire:model="file" accept="image/*,.pdf,.doc,.docx">
                                                            @if ($file_old != '')
                                                                <br><a href="/storage/app/public/{{$file_old}}" target="_blank">Visualizza</a>&nbsp;&nbsp;&nbsp;(<a wire:click.prevent="removeFile()" 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></a>)
                                                            @endif
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="form--item">
                                                    <button type="button" class="btn--ui lightGrey" wire:click="cancelContract()">Annulla</button>
                                                @if($addContract)
                                                    <button type="submit" class="btn--ui" wire:click.prevent="storeContract()">Salva</button>
                                                @endif
                                                @if($updateContract)
                                                    <button type="submit" class="btn--ui" wire:click.prevent="updateContract()">Salva</button>
                                                @endif
                                                </div>

                                            </form>
                                        </div>
                                    </div>
                                </div>

                            @endif

                        </div>
                    </div>
                </div>

            </section>

            <section class="save--section d-flex justify-content-start">
                @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
                <button type="button" class="btn--ui lightGrey" onclick="annulla()">Annulla</button>
            </section>



    @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')
    <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>
        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;

        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'
                }
            });
        });

        function onlyNumberAmount(input) {
            let v = input.value.replace(/\D+/g, '');
            if (v.length > 14) v = v.slice(0, 14);
            input.value = "€ " + v.replace(/(\d)(\d\d)$/, "$1,$2").replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:,|$))/g, '$1.');
        }

    </script>

    <script>

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

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

        $(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,
                fixedHeader: false,
                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 + 'Sponsor',
                                filename: date_export + 'Sponsor',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'pdfHtml5',
                                text: '<i class="fa-solid fa-file-pdf"></i>',
                                action: newexportaction,
                                title: date_export + 'Sponsor',
                                filename: date_export + 'Sponsor',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'print',
                                action: newexportaction,
                                text: '<i class="fa-solid fa-print"></i>',
                                title: date_export + 'Sponsor',
                                filename: date_export + 'Sponsor',
                                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
