@php($title = 'Пользователи')
@extends('layouts.admin')
@section('content')
    <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4 p-3">
        <form action="{{ route('admin.users.update', ['user' => $user]) }}" method="post" enctype="multipart/form-data">
            @csrf
            <div class="mb-3">
                <label for="nameFormControl" class="form-label">ФИО</label>
                <input type="text" class="form-control" id="nameFormControl" name="name" value="{{ $user->name }}">
                @error('name')
                    <div class="text-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="row">
                <div class="mb-3">
                    <label for="emailFormControl" class="form-label">Email</label>
                    <input type="text" class="form-control" id="emailFormControl" name="email"
                        value="{{ $user->email }}">
                    @error('email')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="phoneFormControl" class="form-label">телефон</label>
                    <input type="tel" class="form-control" id="phoneFormControl" name="phone"
                        value="{{ $user->phone }}">
                    @error('phone')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
            </div>
            <button type="submit" class="btn btn-primary">Сохранить</button>
        </form>
    </div>

    <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4 mt-3 pb-3">
        <h4 class="fw-bold m-3">Роли пользователя</h4>
        <table class="table m-0">
            <thead>
                <tr scope="col">
                    <th>Роль
                    <th>Когда назначена
                    <th>
                </tr>
            </thead>
            <tbody class=" ">
                @foreach ($userRoles as $userRole)
                    <tr scope="row">
                        <td class="align-middle">
                            {{ __($userRole->role->name) }}
                            @if($userRole->role->id == $roles::COMPANY_ADMIN)
                                <div class="fs-6 hstack gap-1">
                                    @foreach($companyAdmins as $admin) 
                                    <span class="badge text-bg-secondary">{{ $admin->company?->name }}</span>
                                    @endforeach
                                </div>
                            @endif
                            @if($userRole->role->id == $roles::AGENT)
                            <div class="fs-6 hstack gap-1">
                                @foreach($companyAgents as $agent) 
                                <span class="badge text-bg-secondary">{{ $agent->company?->name }}</span>
                                @endforeach
                            </div>
                            @endif
                        </td>
                        <td class="align-middle">
                            {{ $userRole->created_at?->diffForHumans() }}
                        </td>
                        <td class="text-end">
                            <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">
                                    <form method="post"
                                        action="{{ route('admin.users.role.delete', ['userRole' => $userRole]) }}">
                                        @csrf
                                        <button class="dropdown-item" type="submit">Удалить</button>
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
@endsection
