<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>
@php $services = App\Models\HostelFee::query()->where('hostel_id', $hostel->id)->get(); @endphp
<table class="table table-striped table-bordered table-hover" id="tb">
    <tr>
        <th colspan="{{ 13 +  $services->count()}}" class="uppercase">Báo cáo thu tiền
            <br>
            Tháng {{ $date->copy()->month }} năm 2020
        </th>
    </tr>

    <tr>

        <th rowspan="2" style="width: 10px">STT</th>
        <th rowspan="2">Phòng</th>
        <th rowspan="2">Tiền phòng</th>
        <th colspan="4">Điện</th>
        <th colspan="4">Nước</th>
        <th colspan="{{ $services->count() }}">Dịch vụ</th>
        <th rowspan="2">Tổng số phải nộp</th>
        <th rowspan="2">Ký nhận</th>
    </tr>
    <tr>
        <th>SM</th>
        <th>SC</th>
        <th>SL</th>
        <th>$</th>
        <th>SM</th>
        <th>SC</th>
        <th>SL</th>
        <th>$</th>
        @foreach($services as $service)
            <th>{{ $service->name }}</th>
        @endforeach
    </tr>

    @foreach($rooms as $room)

        @php
            $moneyInfoRoom = App\Models\MoneyInfo::query()
    ->where('room_id', $room->id)
    ->whereIn('type', [
    App\Models\MoneyInfo::VOUCHER_ROOM_PRICE,
    App\Models\MoneyInfo::VOUCHER_CONTRACT,
    ])
    ->whereBetween('date_action', [
        $date->copy()->startOfMonth(),
        $date->copy()->endOfMonth(),
    ])
    ->first();



    $moneyInfoService = App\Models\MoneyInfo::query()
    ->where('room_id', $room->id)
    ->where('type',  App\Models\MoneyInfo::VOUCHER_SERVICE)
    ->whereBetween('date_action', [
        $date->copy()->startOfMonth(),
        $date->copy()->endOfMonth(),
    ])
    ->first();


    $e = null;
    $w = null;

    if($moneyInfoService)
    {
        $e = App\Models\MoneyDetail::query()
        ->where('money_info_id', $moneyInfoService->id)
        ->where('is_electric', 1)
        ->first();

         $w = App\Models\MoneyDetail::query()
        ->where('money_info_id', $moneyInfoService->id)
        ->where('is_water', 1)
        ->first();
    }
        @endphp

        <tr>
            <td>{{ $loop->index + 1 }}</td>
            <td>{{ $room->name }}</td>
            <td>{{ number_format(optional($moneyInfoRoom)->amount, 0, '.', '.') }}</td>
            <td>{{ optional($e)->start_end['end'] }}</td>
            <td>{{ optional($e)->start_end['start'] }}</td>
            <td>{{ optional($e)->start_end['end'] - optional($e)->start_end['start'] }}</td>
            <td>{{ number_format(optional($e)->amount , 0, '.', '.')}}</td>

            <td>{{ optional($w)->start_end['end'] }}</td>
            <td>{{ optional($w)->start_end['start'] }}</td>
            <td>{{ optional($w)->start_end['end'] - optional($w)->start_end['start'] }}</td>
            <td>{{ number_format(optional($w)->amount , 0, '.', '.')}}</td>

            @foreach($services as $service)
                @php
                    $itemService = App\Models\MoneyDetail::query()
      ->whereHas('moneyInfo', function($q) use ($date, $room) {
           $q->whereBetween('date_action', [
        $date->copy()->startOfMonth(),
        $date->copy()->endOfMonth()
        ])->where('room_id', $room->id);
      })->where('hostel_fee_id', $service->id)
        ->first();
                @endphp
                <td>{{
number_format(optional($itemService)->amount, 0, '.', '.')
            }}</td>
            @endforeach

            <td>
                {{ number_format(optional($e)->amount
                + optional($w)->amount
                + optional($itemService)->amount
                + optional($moneyInfoRoom)->amount

                , 0, '.', '.')}}
            </td>
            <td></td>
        </tr>
    @endforeach
</table>