<style>
    .red {
        color: #eb4034;
    }

    .uppercase {
        text-transform: uppercase
    }

    @if(isset($isExcel))
    #tb td, #tb th {
        border: 1px solid #000000;
    }

    td {
        text-align: center;
    }

    th {
        text-align: center;
    }
    @endif
</style>
@if(isset($api))
<link href="https://itro.vn/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <style>
        td {
            text-align: center;
        }

        th {
            text-align: center;
        }
    </style>
@endif

<table class="table table-striped table-bordered table-hover" id="tb">
    <tr>
        <th colspan="9" class="uppercase">Báo cáo tổng hợp hiện trạng thuê</th>
    </tr>
    <tr>
        <th>STT</th>
        <th>Nhà</th>
        <th>Số phòng</th>
        <th>Tổng tiền</th>
        <th>Đang thuê</th>
        <th>Tiền phòng thuê</th>
        <th>Còn trống</th>
        <th>Tiền phòng trống</th>
        <th>Đang cọc giữ chỗ</th>
    </tr>
    @php $cntRoomHostel = 0;
$sumAllHostel = 0;
$renterRoomCntHostel = 0;
$sumRenterRoomHostel = 0;
$sumRenterEmptyHostel=0;
$reserveCntHostel=0;
            @endphp

    @foreach($hostels as $hostel)
        @php

$sumRenterRoom=0;
$sumRenterEmpty=0;

               $cntRoom =  $hostel->rooms->count();
                if($hostel->type_rent == App\Models\Hostel::TYPE_RENT_ALL) {
                    $sumAll=$hostel->rooms->sum('price');
                        $renterRoomCntArr = App\Models\RenterRoom::query()
                ->where('hostel_id', $hostel->id)
                ->groupBy('room_id')
               ->get();
                $renterRoomCnt = $renterRoomCntArr->count();
                $sumRenterRoom = \App\Models\Contract::query()
                  ->where('hostel_id', $hostel->id)
                  ->where('status', '=', \App\Models\Contract::VALIDATED)
                ->whereIn('room_id', $renterRoomCntArr->pluck('room_id')->toArray())
                ->sum('room_price');

                $sumRenterEmpty = \App\Models\Room::query()
                  ->where('hostel_id', $hostel->id)
                ->whereNotIn('id', $renterRoomCntArr->pluck('room_id')->toArray())
                ->sum('price');

                $reserveCntArr = App\Models\RoomReservation::query()
                 ->where('hostel_id', $hostel->id)
                 ->has('room')
                ->groupBy('room_id')
                ->pluck('room_id')->toArray();
                //dump($reserveCntArr);
                $reserveCnt = count($reserveCntArr);
                }
                else {
$sumAll = 0;
                $cntRoom = $hostel->rooms->sum('max_renters');

                     $renterRoomCntArr = App\Models\RenterRoom::query()
                     ->with('room')
                       ->has('room')
                ->where('hostel_id', $hostel->id)
                ->get();
                     //dd($renterRoomCntArr->pluck('room_id')->toArray());
                     $renterRoomCnt = $renterRoomCntArr->count();

                    foreach ($renterRoomCntArr as $item)
                        {
                            $sumRenterRoom+= $item->room->price;
                        }

                       foreach ($hostel->rooms as $room) {
                            $sumAll+=$room->max_renters * $room->price;
                           $used = \App\Models\RoomBed::query()
                           ->where('room_id', $room->id)
                           ->whereHas('contract', function($q) {
                               $q->where('status', '<>', \App\Models\Contract::LIQUIDATED);
                           })
                           ->count();
                           $empty = $room->beds->count() - $used;
                           $sumRenterEmpty += $empty * $room->price;
                       }

                $reserveCnt = App\Models\RoomReservation::query()
                  ->has('room')
                 ->where('hostel_id', $hostel->id)
                ->count();
                }
        @endphp
        <tr>
            <td>{{ $loop->index + 1 }}</td>

            <td>{{ $hostel->name }}</td>
            <td>{{ $cntRoom}}</td>
            <td>{{ number_format($sumAll, 0, '.', '.') }}</td>
            <td>{{ $renterRoomCnt }}</td>
            <td>{{ number_format($sumRenterRoom, 0, '.', '.') }}</td>
            <td>{{ $cntRoom - $renterRoomCnt }}</td>
            <th>{{ number_format($sumRenterEmpty, 0, '.', '.') }}</th>
            <td>{{ $reserveCnt }}</td>

        </tr>
        @php
                $cntRoomHostel += $cntRoom;
$sumAllHostel+=$sumAll;
$renterRoomCntHostel+=$renterRoomCnt;
$sumRenterRoomHostel+=$sumRenterRoom;
$sumRenterEmptyHostel+=$sumRenterEmpty;
$reserveCntHostel+=$reserveCnt;
                @endphp
    @endforeach
    <tr>
        <td><strong>Tổng</strong></td>
        <td></td>
        <td>{{ $cntRoomHostel }}</td>
        <td>{{ number_format($sumAllHostel, 0, '.', '.') }}</td>
        <td>{{ $renterRoomCntHostel }}</td>
        <td>{{ number_format($sumRenterRoomHostel, 0, '.', '.') }}</td>
        <td>{{ $cntRoomHostel - $renterRoomCntHostel }}</td>
        <th>{{ number_format($sumRenterEmptyHostel, 0, '.', '.') }}</th>
        <td>{{ $reserveCntHostel }}</td>
    </tr>
</table>