<div>
    <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4">

        @if ($clients->count() == 0)
            <div class="text-center py-5">Нет данных для отображения</div>
        @endif
        <div class="vstack gap-2">
            @foreach ($clients as $client)
                        <?php
                $complexesNames = [];
                $companiesNames = [];
                $agentsNames = [];
                $filterRow = [];
                $clientDeals = $client->deals;
                //echo 'cId:' . $client->id;
                foreach ($clientDeals as $deal) {
                    //echo 'dId:' . $deal->id;
                    $filterRow[] = 'filter[deal_id][]=' . $deal->id;
                    $complexesNames[] = $deal->complex?->name;
                    $companiesNames[] = $deal->agent?->company?->name;
                    $agentsNames[] = $deal->agent?->user?->name;
                }
                $filterRow = implode('&', $filterRow);
                $complexesNames = array_unique($complexesNames);
                $complexesNames = implode('<br>', array: $complexesNames);
                $companiesNames = array_unique($companiesNames);
                $companiesNames = implode('<br>', $companiesNames);
                $agentsNames = array_unique($agentsNames);
                $agentsNames = implode('<br>', $agentsNames);
                $dealsWithContracts = $client->deals()->whereHas('contract');
                        ?>
                        @if ($mode == 'full')
                            <div class="d-flex flex-row m-0 my-2 px-2 client-row">
                                <div class="d-flex flex-column flex-md-row w-100">
                                    <div class="col fw-semibold fs-5 align-middle text-start">
                                        {{ $client->name }}
                                        @if (env('APP_DEBUG'))
                                            @foreach($client->deals as $deal)
                                            <div class="text-secondary fs-6 fw-light">Bitrix ID: {{ $deal->bitrixId() }} </div>
                                            @endforeach
                                        @endif
                                    </div>

                                    <div class="col">
                                        {!! $complexesNames !!}
                                    </div>
                                    @if (auth()->user()->isCityManager())
                                        <div class="col col-md-2 align-middle text-truncate">
                                            {!! $companiesNames !!}
                                        </div>

                                        <div class="col col-md-2 align-middle text-truncate">
                                            {!! $agentsNames !!}
                                        </div>
                                    @endif

                                    <div class="col col-md-2 align-middle">
                                        {{ $client->phone }}
                                    </div>
                                </div>
                                <div class="col col-md-1 text-end contracts-link">
                                    @if ($dealsWithContracts->count() == 1)
                                        <a href="{{ route('contract', ['contract' => $dealsWithContracts->first()->contract]) }}"
                                            class="icon-link icon-link-hover">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
                                                class="bi bi-file-earmark-text" viewBox="0 0 16 16">
                                                <path
                                                    d="M5.5 7a.5.5 0 0 0 0 1h5a.5.5 0 0 0 0-1zM5 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m0 2a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1h-2a.5.5 0 0 1-.5-.5" />
                                                <path
                                                    d="M9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.5zm0 1v2A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1z" />
                                            </svg>
                                        </a>
                                    @elseif($dealsWithContracts->count() > 1)
                                        <a href="{{ route('contracts', [$filterRow]) }}" class="icon-link icon-link-hover">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
                                                class="bi bi-arrow-right" viewBox="0 0 16 16">
                                                <path fill-rule="evenodd"
                                                    d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
                                            </svg>
                                        </a>
                                    @endif
                                </div>
                            </div>
                        @else
                            <div class="m-2 my-1">
                                @if ($dealsWithContracts->count() == 1)
                                    <a href="{{ route('contract', ['contract' => $dealsWithContracts->first()->contract]) }}"
                                        class="icon-link icon-link-hover w-100 hstack gap-2 text-decoration-none">
                                        <span class="col-8 text-dark">{{ $client->name }}</span>
                                        @if (auth()->user()->isCityManager())
                                            <div class="col align-middle text-truncate">
                                                {!! $companiesNames !!}
                                            </div>
                                        @endif
                                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
                                            class="bi bi-file-earmark-text ms-auto" viewBox="0 0 16 16">
                                            <path
                                                d="M5.5 7a.5.5 0 0 0 0 1h5a.5.5 0 0 0 0-1zM5 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m0 2a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1h-2a.5.5 0 0 1-.5-.5" />
                                            <path
                                                d="M9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.5zm0 1v2A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1z" />
                                        </svg>
                                    </a>
                                @elseif($dealsWithContracts->count() > 1)
                                        <?php
                                    $filterRow = [];
                                    foreach ($client->deals as $deal) {
                                        $filterRow[] = 'filter[deal_id][]=' . $deal->id;
                                    }
                                    $filterRow = implode('&', $filterRow);
                                                                            ?>
                                        <a href="{{ route('contracts', [$filterRow]) }}"
                                            class="icon-link icon-link-hover w-100 hstack gap-2 text-decoration-none">
                                            <span class="col-8 text-dark">{{ $client->name }}</span>
                                            @if (auth()->user()->isCityManager())
                                                <div class="col align-middle text-truncate">
                                                    {!! $companiesNames !!}
                                                </div>
                                            @endif
                                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
                                                class="bi bi-arrow-right ms-auto" viewBox="0 0 16 16">
                                                <path fill-rule="evenodd"
                                                    d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
                                            </svg>
                                        </a>
                                @else
                                    <a href="" class=" icon-link icon-link-hover w-100 hstack gap-2 text-decoration-none">
                                        <span class="col-8 text-truncate text-dark">{{ $client->name }}</span>
                                        @if (auth()->user()->isCityManager())
                                            <div class="col align-middle text-truncate">
                                                {!! $companiesNames !!}
                                            </div>
                                        @endif
                                    </a>
                                @endif
                            </div>
                        @endif
            @endforeach
        </div>
    </div>
    @if ($mode == 'full')
        <div class="mt-3">
            {{ $clients->links('vendor.pagination.bootstrap-5', ['scrollTo' => false]) }}
        </div>
    @endif

</div>