<div class="m-3">

    <div class="row mb-3">
        <div class="col-6">
            <h2>{{$title}}</h2>
        </div>
        <div class="col-md-6 text-right">
            @if(!$add && !$update)
                <button wire:click="add()" type="button" class="btn btn-block btn-success float-right"
                    style="max-width:150px">Aggiungi</button><br>
            @endif
        </div>
    </div>

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

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            @foreach ($errors->all() as $e)
                {{$e}}<br>
            @endforeach
        </div>
    @endif

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

        <div>

            <div class="card">

                <div class="card-header">

                    <div class="card-tools">
                        <div class="input-group input-group-sm" style="width: 300px;">
                            <input type="text" name="search" wire:model="search" class="form-control float-right"
                                placeholder="Cerca">

                            <div class="input-group-append">
                                <button type="submit" class="btn btn-default">
                                    <i class="fas fa-search"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card-body table-responsive p-0">
                    <table class="table table-hover text-nowrap">
                        <thead>
                            <tr>
                                <th>Nome</th>
                                <th>Cognome</th>
                                <th>Gruppo</th>
                                <th style="width:150px">&nbsp;</th>
                                <th style="width:150px">&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($records as $record)
                                <tr>
                                    <td>{{$record->firstname}}</td>
                                    <td>{{$record->lastname}}</td>
                                    <td>{{$record->group()}}</td>
                                    <td>
                                        @if($record->username != 'admin')
                                            <button wire:click="edit({{ $record->id }})" type="button"
                                                class="btn btn-primary btn-block"><i class="fa fa-edit"></i>&nbsp;Modifica</button>
                                        @endif
                                    </td>
                                    <td>
                                        @if($record->username != 'admin')
                                            <button onclick="confirm('Sei sicuro?') || event.stopImmediatePropagation()"
                                                wire:click="delete({{ $record->id }})" type="button"
                                                class="btn btn-danger btn-block"><i class="fa fa-trash"></i>&nbsp;Elimina</button>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>

                    </table>

                </div>

                <div class="card-footer clearfix">

                    <ul class="pagination pagination-sm m-0 float-right">

                        {{ $records->links() }}

                    </ul>

                </div>


            </div>
        </div>

    @else

        <div class="card card-primary">

            <form>

                <div class="card-header">
                    <h3 class="card-title"><b>Inserimento/Modifica</b></h3>
                </div>

                <div class="card-body">
                    <div class="form-group">
                        <label for="firstname">Nome</label>
                        <input class="form-control @error('firstname') is-invalid @enderror" type="text" id="firstname"
                            placeholder="Nome" wire:model="firstname">
                        @error('firstname')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label for="lastname">Cognome</label>
                        <input class="form-control @error('lastname') is-invalid @enderror" type="text" id="lastname"
                            placeholder="Cognome" wire:model="lastname">
                        @error('lastname')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label for="username">Username</label>
                        <input class="form-control @error('username') is-invalid @enderror" type="text" id="username"
                            placeholder="Username" wire:model="username">
                        @error('username')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label for="email">Email</label>
                        <input class="form-control @error('email') is-invalid @enderror" type="text" id="email"
                            placeholder="Email" wire:model="email">
                        @error('email')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label for="group">Gruppo</label>
                        <select id="group_id" wire:model="group_id" class="form-control">
                            <option value="">
                                @foreach($groups as $group)
                                    <option value="{{$group->id}}">{{$group->name}}
                                @endforeach
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="password">Password</label>
                        <small>Lasciare vuoto per non aggiornarla</small>
                        <input class="form-control @error('password') is-invalid @enderror" type="text" id="password"
                            placeholder="Password" wire:model="password">
                        @error('password')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    @if($add)
                        <button type="submit" class="btn btn-primary" wire:click.prevent="store()">Salva</button>
                    @endif
                    @if($update)
                        <button type="submit" class="btn btn-primary" wire:click.prevent="update()">Salva</button>
                    @endif
                    <button type="submit" class="btn btn-default" wire:click.prevent="cancel()">Annulla</button>
                </div>
            </form>
        </div>

    @endif

</div>
