{{-- Main Content --}}
@if(!Auth::check() || !Auth::user()->landlord || !Auth::user()->is_admin)
@endif
@php
// Lấy giá thấp nhất của mỗi motel (giá phòng thấp nhất trong tất cả các phòng)
$motels->each(function ($motel) {
$prices = $motel->rooms->flatMap(function ($room) {
return $room->roomPrices->pluck('price');
});
// Lưu giá thấp nhất vào object $motel
$motel->price_min = $prices->min() ?? 0;
});
@endphp
@if(isset($motels) && $motels->count() > 0)
{{-- Ẩn map đối với chủ trọ --}}
@if(!Auth::check() || !Auth::user()->landlord || !Auth::user()->is_admin)
@if(isset($motels) && $motels->count() > 0)
@else
@endif
@php
$motelsData = $motels->map(function($motel) {
$rooms = $motel->rooms->map(function($room) {
$roomPrices = $room->roomPrices ?? collect([]);
$minPrice = $roomPrices->isNotEmpty()
? $roomPrices->min('price')
: ($room->price ?? 0);
return [
'id' => $room->id,
'name' => $room->name,
'price' => $room->price ?? null,
'price_min' => $minPrice,
'room_prices' => $roomPrices->map(function($rp) {
return [
'id' => $rp->id,
'price' => $rp->price,
];
})->toArray(),
];
})->toArray();
// Lấy ảnh đầu tiên từ rooms của motel
$firstImage = null;
foreach ($motel->rooms as $room) {
if (isset($room->images) && is_array($room->images) && count($room->images) > 0) {
$img = $room->images[0];
if (!empty($img) && trim($img) !== '') {
$firstImage = str_starts_with($img, 'http')
? $img
: asset('storage/' . ltrim($img, '/'));
break;
}
}
}
// Nếu không tìm thấy ảnh từ rooms, dùng ảnh mặc định
if (!$firstImage) {
$firstImage = 'https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=400&h=300&fit=crop';
}
return [
'id' => $motel->id,
'name' => $motel->name,
'address' => $motel->address,
'full_address' => $motel->full_address,
'latitude' => $motel->latitude,
'longitude' => $motel->longitude,
'price_min' => $motel->price_min ?? null,
'image_url' => $firstImage,
'description' => $motel->description ?? null,
'rating' => $motel->rating ?? null,
'reviews_count' => $motel->reviews_count ?? null,
'rooms' => $rooms,
];
})->toArray();
@endphp
@vite('resources/js/pages/model-map.js')
@include('components.motel-map', ['motels' => $motels])
{{-- Livewire component để geocode bất đồng bộ --}}
@livewire('geocode-motels', ['motelIds' => $motels->pluck('id')->toArray()])
@endif
@endif