@php($title = 'Пользователи')
@extends('layouts.admin')
@section('content')
    <div>
        
            <form class="d-flex mb-3" method="GET" action="{{ route('admin.users') }}">
                <div class="p-2 border rounded-3 border-1 bg-white">
                    <input type="radio" class="btn-check" name="role" value="all" id="option_all" autocomplete="off"
                        onclick="this.form.submit()" {{ $role == 'all' || !$role ? 'checked' : '' }}>
                    <label class="btn p-2 fs-5" for="option_all">Все</label>
                    @foreach ($roles as $roleItem)
                        <input type="radio" class="btn-check" name="role" value="{{ $roleItem->id }}"
                            id="option_{{ $roleItem->id }}" autocomplete="off" onclick="this.form.submit()"
                            {{ $roleItem->id == $role ? 'checked' : '' }}>
                        <label class="btn p-2 fs-5" for="option_{{ $roleItem->id }}">{{ __($roleItem->name) }}</label>
                    @endforeach
                </div>

                <div class="ms-auto p-2">
                    @if(!isset($role) || $role == 'all' || $role == 1)
                        <button type="button" class="btn btn-primary py-2 px-3 fs-5" data-bs-toggle="modal"
                            data-bs-target="#createUserModal">
                            <i class="bi bi-person-plus"></i>
                        </button>
                    @endif
                </div>
            </form>
        @if ($users->count() == 0)
            <div class="text-center py-5">Нет данных для отображения</div>
        @else
            <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4 py-3">
                <table class="table m-0">
                    <thead>
                        <tr scope="col">
                            <th>ФИО
                            @if($role == 3)
                            <th>Агентство
                            @endif
                            <th>Email
                            <th>Телефон
                            <th>Создан
                            <th>
                        </tr>
                    </thead>
                    <tbody class=" ">
                        @foreach ($users as $user)
                            <tr scope="row">
                                <td class="align-middle">
                                    {{ $user->name }}
                                </td>
                                @if($role == 3)
                                <td class="align-middle">{{  AgentCompanyOfUser($user)?->name }}</td>
                                @endif
                                <td class="align-middle">
                                    {{ $user->email }}
                                </td>
                                <td class="align-middle">
                                    {{ $user->phone }}
                                </td>
                                <td class="align-middle">
                                    {{ $user->created_at?->diffForHumans() }}
                                </td>
                                <td>
                                    <div class="dropdown" style="">
                                        <button class="btn btn-light" type="button" id="dropdownMenuButton"
                                            data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <i class="bi bi-three-dots-vertical"></i>
                                        </button>
                                        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                            <a class="dropdown-item"
                                                href="{{ route('admin.users.edit', ['user' => $user]) }}">Редактировать</a>
                                            <form method="post"
                                                action="{{ route('admin.users.delete', ['user' => $user]) }}">
                                                @csrf
                                                <button class="dropdown-item" type="submit">Удалить</button>
                                            </form>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif

        @livewire('post.card')
    </div>

    <!-- Modal -->
    <div class="modal fade" id="createUserModal" tabindex="-1" aria-labelledby="createUserModalLabel" aria-hidden="true">
        <form class="modal-dialog modal-dialog-centered" action="{{ route('admin.users.create') }}" method="post"
            enctype="multipart/form-data">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="createUserModalLabel">Новый пользователь</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    @csrf
                    @if(isset($role))
                        @foreach ($roles as $roleItem)
                            @if($roleItem->id == $role)
                                <div class="alert alert-warning">
                                    Этому пользователю будет предоставлена роль "{{ __($roleItem->name) }}"
                                </div>
                                <input type="hidden" name="role" value="{{ $role }}">
                            @endif
                        @endforeach
                    @endisset
                    <div class="my-3">
                        <label for="nameInput" class="form-label">ФИО (полностью)</label>
                        <input class="form-control" type="text" id="nameInput" name="name" required>
                        @error('name')
                            <div class="text-danger">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="my-3">
                        <label for="nameInput" class="form-label">Телефон</label>
                        <input class="form-control" type="tel" id="phoneInput" name="phone" required>
                        @error('phone')
                            <div class="text-danger">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="my-3">
                        <label for="nameInput" class="form-label">Электронная почта</label>
                        <input class="form-control" type="text" id="emailInput" name="email" required>
                        @error('email')
                            <div class="text-danger">{{ $message }}</div>
                        @enderror
                    </div>
                    
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Добавить</button>
                </div>
            </div>
        </form>
    </div>

@endsection
