@php
    $electricPrice = 0;
    $waterPrice = 0;


$hostelFee = \App\Models\HostelFee::where('type', \App\Models\HostelFee::ELECTRIC)
    ->where('hostel_id', $item->room->hostel->id)
    ->first();

    $hostelFeeWater = \App\Models\HostelFee::where('type', \App\Models\HostelFee::WATER)
    ->where('hostel_id', $item->room->hostel->id)
    ->first();

if ($hostelFee) {
    $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)
    ->where('fee_id', $hostelFee->id)
    ->first();

    if(!$electric)
    {
    $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)->first();
    }

} else {
    $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)->first();
   // $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();
}

if($hostelFeeWater)
{
$water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)
->where('fee_id', $hostelFeeWater->id)
->first();
if(!$water)
{
  $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();
}
} else {
$water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();

}

if ($electric) {
    $electricPrice = $electric->price_quota_1;
}

if($water)
{
    $waterPrice = $water->price_quota_1;
}
@endphp
<table class="invoice__detail">
    <thead>
    <tr>
        <th rowspan="2" class="text-left" style="min-width: 100pt">Khoản thu</th>
        {{--            <th colspan="2">Ngày tính phí</th>--}}
        <th colspan="3">Chỉ số điện / nước</th>
        <th rowspan="2">Số lượng</th>
        <th rowspan="2">Đơn vị</th>
        <th rowspan="2">Đơn giá</th>
        <th rowspan="2">Thành tiền</th>
        <th rowspan="2" class="text-left">Diễn giải</th>
    </tr>
    <tr>
        {{--            <th>Từ ngày</th>--}}
        {{--            <th>Đến ngày</th>--}}
        <th>Đầu</th>
        <th>Cuối</th>
        <th>Hệ số</th>
    </tr>
    </thead>
    <tbody>
    @php $sum = 0; @endphp
    @foreach($details as $detail)
        @php if(!empty($detail->sum_amount)) {
            $sum+= $detail->sum_amount;
            } else {$sum+= $detail->amount;}
            $hostelFee = \App\Models\HostelFee::find($detail->hostel_fee_id);

        @endphp
        <tr>
            <td class="text-left">{!! nl2br(ucfirst($detail->name)) !!} </td>
            {{--                <td>{{ $item->date_action->startOfMonth()->format('d/m/Y') }}</td>--}}
            {{--                <td>{{ $item->date_action->endOfMonth()->format('d/m/Y') }}</td>--}}
            @if(!empty($detail->start_end))
                @php $startEnd = $detail->start_end @endphp

                <td>{{ $startEnd['start'] }}</td>
                <td>{{ $startEnd['end'] }}</td>
                <td>1</td>
                <td>{{ $startEnd['end'] - $startEnd['start'] }}</td>
            @else
                <td>0</td>
                <td>0</td>
                <td>1</td>
                <td>{{ $detail->qty }}</td>
            @endif

            @if($detail->is_electric == true)
                @if(in_array($hostelFee->type, [\App\Models\HostelFee::ELECTRIC, \App\Models\HostelFee::ELECTRIC_BY_CLOCK, \App\Models\HostelFee::ELECTRIC_DYNAMIC ]))
                    <td>kwH</td>
                @else
                    <td>người</td>
                @endif
            @elseif($detail->is_water)
                @if(in_array($hostelFee->type, [\App\Models\HostelFee::WATER, \App\Models\HostelFee::WATER_BY_CLOCK, \App\Models\HostelFee::WATER_DYNAMIC ]))
                    <td>m3</td>
                @else
                    <td>người</td>
                @endif
            @else
                <td></td>
            @endif
            @if(!empty($hostelFee))
                @if(!in_array($hostelFee->type, [\App\Models\HostelFee::WATER, \App\Models\HostelFee::ELECTRIC]))

                    @if(in_array($hostelFee->type, [
                    \App\Models\HostelFee::WATER_BY_PEOPLE, \App\Models\HostelFee::WATER_BY_CLOCK, \App\Models\HostelFee::ELECTRIC_BY_PEOPLE,
                    \App\Models\HostelFee::ELECTRIC_BY_CLOCK
                    ]))

                        @if($detail->value > 1)
                            <td>{{ number_format($detail->value, 0, '.', '.') }}</td>
                        @else

                            <td>{{ number_format($hostelFee->fee, 0, '.', '.') }}</td>
                        @endif
                    @else
                        <td>{{ number_format($detail->value, 0, '.', '.') }}</td>
                    @endif
                @else
                    @if($detail->is_electric == true)
                        <td>{{ number_format($electricPrice, 0, '.', '.') }}</td>
                    @elseif($detail->is_water == true)
                        <td>{{ number_format($waterPrice, 0, '.', '.') }}</td>
                    @else
                        <td>1</td>
                    @endif
                @endif
            @else
                <td>{{ number_format($detail->value, 0, '.', '.') }}</td>
            @endif

            @if(!empty($detail->sum_amount))
                <td>{{ number_format($detail->sum_amount, 0, '.', '.') }}</td>
            @else
                <td>{{ number_format($detail->amount, 0, '.', '.') }}</td>
            @endif
            <td class="text-left">{{ $detail->note }}</td>
        </tr>
    @endforeach
    </tbody>
    <tbody>
    <tr>
        <td colspan="7" class="text-left"><b>Tổng cộng</b></td>
        <td><b>{{  number_format($sum, 0, '.', '.') }}</b></td>
        <td></td>
    </tr>
    <tr>
        <td colspan="7" class="text-left"><b>Giảm giá</b></td>
        <td><b>{{  number_format($item->discount, 0, '.', '.') }}</b></td>
        <td></td>
    </tr>
    <tr>
        <td colspan="7" class="text-left"><b>Đã trả</b></td>
        <td><b>{{  number_format($pay, 0, '.', '.') }}</b></td>
        <td></td>
    </tr>

    <tr>
        <td colspan="7" class="text-left"><b>Còn lại</b></td>
        <td><b>{{  number_format($sum - $pay - $item->discount, 0, '.', '.') }}</b></td>
        <td></td>
    </tr>
    </tbody>
</table>


<table class="invoice__summary">
    <tbody>
    <tr>
        <td class="text-nowrap pr-24"><b>Tổng số tiền phải thanh toán kì này</b></td>
        <td class="text-nowrap pr-24">{{  number_format($sum, 0, '.', '.') }}{{\App\Components\Functions::moneyUnit()}}</td>
    </tr>
    <tr>
        <td class="text-nowrap pr-24"><b>Tổng số tiền đã thanh toán trong kỳ</b></td>
        <td class="text-nowrap pr-24">{{  number_format($pay, 0, '.', '.') }}{{\App\Components\Functions::moneyUnit()}}</td>
    </tr>
    <tr>
        <td class="text-nowrap pr-24"><b>Tổng số tiền còn phải thanh toán</b></td>
        <td class="text-nowrap pr-24">{{  number_format($sum - $pay - $item->discount, 0, '.', '.') }}đ</td>
        <td class="">Bằng chữ: {{ $f->format($sum - $pay- $item->discount) }} {{\App\Components\Functions::moneyUnit()}}</td>
    </tr>
    </tbody>
</table>