<!DOCTYPE html>
<html lang="en">
<head>
    <meta property="og:url" content="{{ url()->current() }}"/>
    <meta property="og:type" content="website"/>
    <meta property="og:title" content="itro.vn"/>
    <meta property="fb:app_id" content="1072839196191686"/>
    <meta property="og:description"
          content="Hóa đơn"/>
    <meta property="og:image" content="https://itro.vn/invoice-bg.jpg"/>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Hóa đơn | iTro</title>
    <script src="/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
    @if(!isset($isPrint)&&(count($itemArr)>0))
        <script>
            $(document).ready(function () {
                window.print();
            });
        </script>
    @endif

    @php

            @endphp

    <style>

        /*RESET*/
        * {
            margin: 0;
            padding: 0;
        }

        *, *:before, *:after {
            box-sizing: inherit;
        }

        img, object, embed {
            max-width: 100%;
        }

        img {
            border: 0;
        }

        a {
            background: transparent;
        }

        /*PRINT*/
        @page {
            size: auto;
            margin: 1cm;
        }

        @media print {
            .header, .hide {
                visibility: hidden
            }
        }

        /*BASE*/

        @if(isset($font))
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 9pt;
            line-height: 12pt;
            font-weight: 400;
        }

        @else
        body {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-size: 9pt;
            line-height: 12pt;
            font-weight: 400;
        }

        @endif

        table {
            margin: 0;
            padding: 0;
            border: 0;
            background: transparent;
            width: 100%;
        }

        table {
            table-layout: auto;
            border-collapse: collapse;
            width: 100%;
        }

        table .absorbing-column {
            width: 100%;
        }

        /*CLASSES*/
        .text-nowrap {
            white-space: nowrap !important;
        }

        .text-center {
            text-align: center !important;
        }

        .text-left {
            text-align: left !important;
        }

        .text-right {
            text-align: right !important;
        }

        .text-uppercase {
            text-transform: uppercase !important;
        }

        .pa-12 {
            padding: 12pt !important;
        }

        .pt-12 {
            padding-top: 12pt !important;
        }

        .pb-12 {
            padding-bottom: 12pt !important;
        }

        .pl-12 {
            padding-left: 12pt !important;
        }

        .pr-12 {
            padding-right: 12pt !important;
        }

        .pa-24 {
            padding: 24pt !important;
        }

        .pt-24 {
            padding-top: 24pt !important;
        }

        .pb-24 {
            padding-bottom: 24pt !important;
        }

        .pl-24 {
            padding-left: 24pt !important;
        }

        .pr-24 {
            padding-right: 24pt !important;
        }

        .ma-12 {
            padding: 12pt !important;
        }

        .mt-12 {
            padding-top: 12pt !important;
        }

        .mb-12 {
            padding-bottom: 12pt !important;
        }

        .ml-12 {
            padding-left: 12pt !important;
        }

        .mr-12 {
            padding-right: 12pt !important;
        }

        .ma-24 {
            padding: 24pt !important;
        }

        .mt-24 {
            padding-top: 24pt !important;
        }

        .mb-24 {
            padding-bottom: 24pt !important;
        }

        .ml-24 {
            padding-left: 24pt !important;
        }

        .mr-24 {
            padding-right: 24pt !important;
        }

        /*MAIN*/
        body {
            /*padding: 1cm;*/
            margin: 0;
            padding: 0;
        }

        @php use App\User;$ownerColor = User::query()->where('type', User::ADMIN)
         ->where('phone', '0908372472')->first();
         @endphp
         @if(auth('backend')->check())
        @if(auth('backend')->user()->id == optional($ownerColor)->id ||  auth('backend')->user()->staff_owner_id == optional($ownerColor)->id)
        body {
            /*padding: 1cm;*/
            color: #00388c
        }

        table {
            border-color: #00388c;
        }

        @endif
        @endif

        @media screen {
            body {
                background-color: #eee;
            }

            .page-container {
                max-width: 1200px;
                margin: 24px auto;
                padding: 24px;
                background-color: #fff;
            }
        }

        /*Header*/
        .invoice__header {
            margin: 0 0 8pt;
        }

        .invoice__header .company__logo img {
            display: block;
            max-width: 100pt;
            max-height: 50pt;
        }

        .invoice__header .company__detail {
            padding: 0 12pt 0 8pt;
        }

        .invoice__header .company__detail .detail-title {
            margin: 0 0 2pt;
            font-size: 12pt;
        }

        .invoice__header .company__detail .detail-info {
            margin: 1pt 0 0;
        }

        .invoice__header .invoice__date-issue {
            margin: 0 0 4pt;
        }

        /*Title*/
        .invoice__title {
            margin: 0 0 8pt;
        }

        .invoice__title .invoice-title {
            margin: 0;
            line-height: 20pt;
            font-size: 14pt;
            font-weight: 700;
        }

        .invoice__title .invoice-subtitle {
            margin: 2pt 0 0;
            line-height: 14pt;
            font-size: 10pt;
            font-weight: 700;
        }

        /*Customer*/
        .invoice__customer {
            margin: 0 0 4pt;
        }

        .invoice__customer td {
            padding-top: 1pt;
            vertical-align: top;
        }

        /*Detail*/
        .invoice__detail {
            width: 100%;
            max-width: 100%;
            margin: 0 0 8pt;
            font-size: 9pt;
            border: 1pt solid #333;
        }

        .invoice__detail th,
        .invoice__detail td {
            line-height: 14pt;
            text-align: center;
            border: 1pt solid #333;
        }

        .invoice__detail th {
            padding: 2pt;
        }

        .invoice__detail td {
            padding: 2pt;
        }

        /*Summary*/
        .invoice__summary {
            margin: 0 0 8pt;
        }

        .invoice__summary td {
            vertical-align: top;
            padding-top: 1pt;
        }

        /*Payment*/
        .invoice__payments {
            margin: 0 0 8pt;
            table-layout: fixed;
        }

        .invoice__payments td {
            vertical-align: top;
        }

        .invoice__payments table td {
            padding-top: 1pt;
        }

        /*Footer*/
        .invoice__footer {
            margin: 12pt 0 8pt;
            table-layout: fixed;
        }

        .invoice__footer td {
            vertical-align: top;
        }

        /*Notes*/
        .invoce__notes {
            margin-top: 8pt;
        }

        html,
        body {
            height: 100%;
            width: 100%;
        }

        .container {
            align-items: center;
            display: flex;
            justify-content: center;
            height: 100%;
            width: 100%;
        }

        .content {
            text-align: center;
        }
    </style>
</head>
<body>

@if(count($itemArr)==0)
    <div class="container">
        <div class="content">
            <h1>Không có hoá đơn nào chưa thanh toán.</h1>
        </div>
    </div>
@else
    @for($i=0; $i<count($itemArr); $i++)
        @php

            $item = $itemArr[$i];
            $details=  $detailArr[$i];
             if($details->count() == 0)
    {
        continue;
    }
            $pay= $payArr[$i];
            $text =$textArr[$i];
            $f=$fArr[$i];



$electricPrice = 0;
$waterPrice = 0;

$hostelFee = \App\Models\HostelFee::where('type', \App\Models\HostelFee::ELECTRIC)
->where('hostel_id', optional(optional($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
        <p style="page-break-before: always">

        </p>

        <div class="page-container">

            <table class="invoice__header">
                <tbody>
                <tr>
                    {{--<td>--}}
                    {{--<div class="company__logo"><img src="http://teachersjobworld.com/employer/upload_logo/sample_logo.png" alt=""></div>--}}
                    {{--</td>--}}
                    @php $hostel = $item->room->hostel;
            if(!isset($discount))
            {
                $discount = $item->discount;
            }
                    @endphp
                    <td class="absorbing-column">
                        <div class="company__detail">
                            <h2 class="detail-title">{{ $hostel->name }}</h2>
                            <div class="detail-info">Địa chỉ/Address: {{ $hostel->address_text }}
                            </div>
                            <div class="detail-info">SĐT/Phone: {{ $item->hostel->getHotlinePhoneAttribute() }}</div>
                            <div class="invoice__date-issue text-nowrap">Ngày in / Statement Date: {{ \Carbon\Carbon::now()->format('d/m/Y') }}</div>
                        </div>
                    </td>
                    <td>
                        @php $setting = \App\Models\Config::query()->where('owner_id', $hostel->owner_id)->first() @endphp
                        @if($setting)
                            @if(!empty($setting->logo))

                                <img src="/files/{{ $setting->logo }}" style="max-width: 250px"/>

                            @endif
                        @endif


                    </td>
                </tr>
                </tbody>
            </table>

            <table class="invoice__title">
                <tbody>
                <tr>
                    <td>
                        <div class="text-center">
                            <h1 class="invoice-title">HÓA ĐƠN <br> INVOICE</h1>
                            @if(!empty($item->date_aciton))
                                <div class="invoice-subtitle">Kỳ thanh toán:
                                    Tháng {{ $item->date_action->format('m/Y') }}</div>
                            @endif
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>

            <table class="invoice__customer">
                <tbody>
                <tr>
                    <td class="pr-24">
                        <table>
                            <tbody>
                            <tr>
                                <td class="text-nowrap pr-24"><b>Tên khách hàng/Customer name:</b></td>

                                @if($item instanceof App\Models\Contract)
                                    <td class="absorbing-column"><b>{{ $item->name }}</b></td>
                                @else
                                    <td class="absorbing-column"><b>{{ optional($item->contract)->name }}</b></td>
                                @endif

                            </tr>
                            <tr>
                                <td class="text-nowrap pr-24">Phòng/Room: </td>
                                <td class="absorbing-column">{{ $item->room->name }}
                                    @if($item instanceof App\Models\Contract)
                                        @if($item->bed) ({{ $item->bed->name }}) @endif
                                    @else
                                        @if(optional($item->contract)->bed) ({{ optional($item->contract)->bed->name }}) @endif
                                    @endif
                                </td>
                            </tr>

                            </tbody>
                        </table>
                    </td>
                    {{--<td>--}}
                    {{--<table>--}}
                    {{--<tbody>--}}
                    {{--<tr>--}}
                    {{--<td class="text-nowrap pr-24">Số hợp đồng:</td>--}}
                    {{--<td class="text-nowrap absorbing-column">1234234123423423A</td>--}}
                    {{--</tr>--}}
                    {{--<tr>--}}
                    {{--<td class="text-nowrap pr-24">Vị trí:</td>--}}
                    {{--<td class="text-nowrap absorbing-column">12312</td>--}}
                    {{--</tr>--}}
                    {{--<tr>--}}
                    {{--<td class="text-nowrap pr-24">Mã kế toán:</td>--}}
                    {{--<td class="text-nowrap absorbing-column">ÁA-1A12312</td>--}}
                    {{--</tr>--}}
                    {{--</tbody>--}}
                    {{--</table>--}}
                    {{--</td>--}}
                </tr>
                </tbody>
            </table>

            <table class="invoice__detail">
                <thead>
                <tr>
                    <th rowspan="2" class="text-left" style="min-width: 100pt">Khoản thu <br>(Description)</th>
                    {{--            <th colspan="2">Ngày tính phí</th>--}}
                    <th colspan="2">Chỉ số điện / nước <br>(Electricity/Water)</th>
                    <th rowspan="2">Số lượng <br>(Quantity)</th>
                    <th rowspan="2">Đơn vị <br> (Unit)</th>
                    <th rowspan="2">Đơn giá <br> (Unit Price)</th>
                    <th rowspan="2">Thành tiền <br>(Total)</th>
                </tr>
                <tr>
                    {{--            <th>Từ ngày</th>--}}
                    {{--            <th>Đến ngày</th>--}}
                    <th>Đầu<br>(Previous number)</th>
                    <th>Cuối <br>(Present number)</th>

                </tr>
                </thead>
                <tbody>
                @php $sum = 0;

                $discount = App\Models\MoneyInfo::query()->whereIn('id', $details->pluck('money_info_id')->toArray())
                ->sum('discount');
                $notes = App\Models\MoneyInfo::query()->whereIn('id', $details->pluck('money_info_id')->toArray())
                ->pluck('note')->toArray();
                @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)) !!} @if(optional($detail->moneyInfo)->type == \App\Models\MoneyInfo::VOUCHER_SERVICE)
                                ({{ $detail->moneyInfo->date_action->format('m/Y') }}) @endif </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>{{ $startEnd['end'] - $startEnd['start'] }}</td>
                        @else
                            <td>0</td>
                            <td>0</td>

                            <td>{{ $detail->qty }}</td>
                        @endif

                        @if($detail->is_electric == true)
                            @if($hostelFee)
                                @if(in_array(optional($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
                            @else
                                <td>người</td>
                            @endif
                        @elseif($detail->is_water)
                            @if($hostelFee)
                                @if(in_array(optional($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>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
                    </tr>
                @endforeach
                </tbody>
                <tbody>
                <tbody>
                <tr>
                    <td colspan="6" class="text-left"><b>Tổng cộng/Total amount</b></td>
                    <td><b>{{  number_format($sum, 0, '.', '.') }}</b></td>

                </tr>
                <tr>
                    <td colspan="6" class="text-left"><b>Giảm giá/Discount</b></td>
                    <td><b>{{  number_format($discount, 0, '.', '.') }}</b></td>

                </tr>
                <tr>
                    <td colspan="6" class="text-left"><b>Đã trả/Paid</b></td>
                    <td><b>{{  number_format($pay, 0, '.', '.') }}</b></td>

                </tr>

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

                </tr>
                </tbody>
            </table>


            <table class="invoice__summary">
                <tbody>
                @if(isset($notes))
                    @php $notes = array_filter($notes, function($note) {
   return !empty($note);
}); @endphp
                    <tr>
                        <td class="text-nowrap pr-24"><b>Ghi chú: </b>{{ implode(', ', $notes) }}</td>
                    </tr>
                @else
                    <tr>
                        <td class="text-nowrap pr-24"><b>Ghi chú: </b>{{ $item->note }}</td>
                    </tr>
                @endif
                <tr>
                    <td class="text-nowrap pr-24"><b>Tổng số tiền phải thanh toán kì này (Total)</b></td>
                    <td class="text-nowrap pr-24">{{  number_format($sum, 0, '.', '.') }}đ</td>
                </tr>
                <tr>
                    <td class="text-nowrap pr-24"><b>Tổng số tiền đã thanh toán trong kỳ (Paid)</b></td>
                    <td class="text-nowrap pr-24">{{  number_format($pay, 0, '.', '.') }}đ</td>
                </tr>
                <tr>
                    <td class="text-nowrap pr-24"><b>Tổng số tiền còn phải thanh toán (Remaining)</b></td>
                    <td class="text-nowrap pr-24">{{  number_format($sum - $pay - $discount, 0, '.', '.') }}đ</td>
                </tr>
                <tr>
                    <td class="">Bằng chữ/In words: {{ $f->format($sum - $pay- $discount) }} đồng</td>
                </tr>
                </tbody>
            </table>

            <table class="invoice__footer">
                <tbody>
                <tr>
                    <td>
                        @if(isset($text))
                            {!! $text !!}
                        @endisset

                    </td>
                    <td>
                        <div class="invoice__footer__signature text-center" style="margin-left: 40%">
                            <div><b>Người lập hóa đơn <br>(Seller)
                                    @if($item->hostel->owner->phone === '0979652628')
                                        <br> {{ $item->hostel->owner->name }}
                                    @endif
                                </b></div>
                            <div style="height: 2cm"></div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>

        </div>
    @endfor
@endif


</body>
</html>