@php($title = $company->name)
@extends('layouts.admin')
@section('content')
    <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4 p-3">

        <h4 class="fw-bold my-3">Редактировать данные агентства</h4>
        <form action="{{ route('admin.companies.update', ['company' => $company]) }}" 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="{{ old('name', $company->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="{{ old('email', $company->email) }}">
                    @error('email')
                        <div class="text-danger d-none">{{ $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="{{ old('phone', $company->phone) }}">
                    @error('phone')
                        <div class="text-danger d-none">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="innFormControl" class="form-label">ИНН</label>
                    <input type="text" disabled class="form-control" id="innFormControl" name="inn"
                        value="{{ old('inn', $company->inn) }}">
                    @error('inn')
                        <div class="text-danger d-none">{{ $message }}</div>
                    @enderror
                </div>
            </div>
            @if (env('APP_DEBUG'))
                <div class="mb-3 d-none">
                    <label for="tokenFormControl" class="form-label">Секретный токен для вебхука</label>
                    <input type="text" class="form-control" name="token" id="tokenFormControl"
                        value="{{ $company->bitrixy?->token }}">
                    <small style="font-size:10px">* Служебное поле доступно в режиме тестирования приложения</small>
                </div>
            @endif

            <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">
        <div class="d-flex align-items-center m-3">
            <h4 class="fw-bold">Администраторы</h4>
            <div class="ms-auto d-flex gap-3">
                <a href="" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createCompanyAdminModal">
                    <i class="bi bi-person-plus-fill"></i> <span class="d-none d-lg-inline">Добавить</span>
                </a>
            </div>
        </div>
        <table class="table m-0">
            <thead>
                <tr scope="col">
                    <th>ФИО
                    <th>Когда назначен
                    <th>
                </tr>
            </thead>
            <tbody class=" ">
                @foreach ($companyAdminsRelation as $companyAdmin)
                    <tr scope="row">
                        <td class="align-middle">
                            {{ $companyAdmin->user->name }}
                        </td>
                        <td class="align-middle">
                            {{ $companyAdmin->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.companies.admins.delete', [
                                            'companyAdmin' => $companyAdmin
                                        ]) }}">
                                        @csrf
                                        <button class="dropdown-item" type="submit">Удалить</button>
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4 mt-3 pb-3">
        <div class="d-flex align-items-center m-3">
            <h4 class="fw-bold">Агенты</h4>
            <div class="ms-auto d-flex gap-3">
                <a href="" class="btn btn-secondary">
                    <i class="bi bi-file-earmark-excel"></i> <span class="d-none d-lg-inline">Загрузить из Excel</span>
                </a>
                <a href="" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createAgentModal">
                    <i class="bi bi-person-plus-fill"></i> <span class="d-none d-lg-inline">Добавить</span>
                </a>
            </div>
        </div>
        <table class="table m-0">
            <thead>
                <tr scope="col">
                    <th>ФИО
                    <th>Когда назначен
                    <th>
                </tr>
            </thead>
            <tbody class=" ">
                @foreach ($companyAgentsRelation as $companyAgent)
                    <tr scope="row">
                        <td class="align-middle">
                            {{ $companyAgent->user->name }}
                        </td>
                        <td class="align-middle">
                            {{ $companyAgent->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.companies.agents.delete', [
                                            'agent' => $companyAgent
                                        ]) }}">
                                        @csrf
                                        <button class="dropdown-item" type="submit">Удалить</button>
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @livewire('company.agent.create', ['containerId' => 'createAgentModal', 'companyId' => $company->id])
    @livewire('company.admin.create', ['containerId' => 'createCompanyAdminModal', 'companyId' => $company->id])
@endsection