@php $user = auth('backend')->user() @endphp
@foreach($rooms as $room)
    @php $isEmptyRoom = \App\Components\Functions::ifEmptyRoom($room); @endphp
    @php $contracts = \App\Models\Contract::query()
    ->has('renterWithTrashed')
    ->where('room_id', $room->id)
    ->where('status', '<>', \App\Models\Contract::LIQUIDATED)
    ->get();
    $hostel = $room->hostel;
    if($hostel->type_rent == App\Models\Hostel::TYPE_RENT_EVERY)
    {
        $contracts = \App\Models\Contract::query()
    ->where('room_id', $room->id)
    ->where('status', '<>', \App\Models\Contract::LIQUIDATED)
    ->get();
    }
     $reserves = $room->reserves;
    @endphp
    <div class="portlet box"
         @if(!$isEmptyRoom)
         @if($reserves->count() > 0)
         style="background-color: #F08700; border: 1px solid #F08700"
         @else style="background-color: #183446; border: 1px solid #183446"
         @endif
         @else

         @if(!empty($contracts->last()->leave_day))
         style="background-color: #046E8F ; border: 1px solid #046E8F"
         @else
         style="background-color: #1ABC9C ; border: 1px solid #1ABC9C"
            @endif

            @endif>
        <div class="portlet-title">
            <div class="caption">
                <span> Phòng: {{ $room->name }} - @if(isset($isShowHostel)) {{ $room->hostel->name }} -  @endif
                    @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_ALL)
                        @if(!$isEmptyRoom)
                            @if($reserves->count() > 0)
                                Đang cọc
                            @else
                                Đang trống
                            @endif
                        @else

                            @if(!empty($contracts->last()->leave_day))
                                Sắp trống
                                ({{  optional($contracts->last()->leave_day)->format('d/m/Y') }})
                            @else
                                Đang ở
                            @endif

                        @endif
                    @endif
                    @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
                        @php
                            $unavailable = \App\Models\RoomBed::query()->where('room_id',$room->id)
->whereHas('contract', function($q) {
    $q->where('status', '=', \App\Models\Contract::VALIDATED);
})
->get()->count();
                            $deposit = \App\Models\RoomBed::query()
                            ->where('room_id',$room->id)
                            ->has('reserve')
                            ->doesntHave('contract')
                            ->count();
                        @endphp

                        [Đang ở: {{$unavailable}}] - [Đang cọc: {{$deposit}}] - [Đang
                        trống: {{$room->beds->count()-($unavailable+$deposit)}}]
                    @endif
                </span>
            </div>
            <div class="tools">
                <a href="javascript:void(0);" class="collapse" data-original-title="" title=""> </a>
            </div>
            <div class="actions">
                @if($user->can('edit-room'))
                    <a href="#edit-room" data-toggle="modal" data-room="{{ $room->id }}"
                       class="btn btn-default btn-sm btn-update-room">
                        <i class="fa fa-pencil"></i> Cập nhật thông tin </a>
                @endif

                @if($user->can('delete-room'))
                    <a href="#" data-room="{{ $room->id }}"
                       class="btn btn-default btn-sm btn-delete-room">
                        <i class="fa fa-trash"></i> Xóa phòng </a>
                @endif
            </div>
        </div>
        <div class="portlet-body">
            @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_ALL)
                <div class="row">
                    <div class="col-md-12">
                        <div class="table-scrollable">
                            <table class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    {{--<th>Tên</th>--}}
                                    {{--<th> Loại phòng</th>--}}
                                    <th> Giá ({{ strtolower($room->hostel->type_rent_text) }})</th>
                                    <th> Diện tích (m2)</th>
                                    <th> Số người tối đa</th>
                                    <th> Số người hiện tại</th>
                                    @if($contracts->count() > 0)
                                        <th> Hợp đồng đang có</th>
                                    @endif
                                    @if($reserves->count() > 0)
                                        <th>Thông tin giữ chỗ</th>
                                    @endif
                                    <th>Số tiền chưa thanh toán</th>
                                    <th>Ngày trống</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    {{--<td> <a href="#detail-room" data-room="{{ $room->id }}" data-toggle="modal"--}}
                                    {{--class="btn-detail-room">{{ $room->name }}</a> </td>--}}
                                    {{--<td> {{ $room->type_text }}</td>--}}
                                    <td> {{ number_format($room->price, 0, '.', '.') }} </td>
                                    <td> {{ $room->size }} </td>
                                    <td> {{ $room->max_renters }} </td>
                                    <td><a href="#detail-room" data-room="{{ $room->id }}"
                                           class="btn-number-people-room"
                                           data-toggle="modal"> {{ \App\Models\RenterRoom::query()->where('room_id', $room->id)->count() }}</a>
                                    </td>
                                    @if($contracts->count() > 0)
                                        <td>
                                            @foreach($contracts as $contract)
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <a href="#detail-room" data-room="{{ $room->id }}"
                                                           data-toggle="modal"
                                                           class="btn-detail-contract"
                                                           data-contract="{{ $contract->id }}"
                                                           data-code="{{ $contract->code }}">Hợp đồng:
                                                            {{--{{$contract->code}}--}}
                                                            {{ $contract->name }} @if($contract->end_date < \Carbon\Carbon::now()->toDateString())
                                                                - Hết hạn @endif</a>


                                                    </div>
                                                </div>
                                            @endforeach
                                        </td>
                                    @endif
                                    @if($reserves->count() > 0)
                                        <td>  @foreach($reserves as $reserve)
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <a href="#detail-reserve" data-reserve="{{ $reserve->id }}"
                                                           data-toggle="modal"
                                                           class="btn-detail-reserve"
                                                        >Giữ chỗ:
                                                            {{$reserve->name}}
                                                            - {{ $reserve->phone }}</a> (<a href="#add-contract-reserve"
                                                                                            class="btn-add-contract-reserve"
                                                                                            data-reserve="{{ $reserve->id }}"
                                                                                            data-room="{{ $room->id }}"
                                                                                            data-toggle="modal">Tạo Hợp
                                                            đồng</a>)

                                                    </div>
                                                </div>
                                            @endforeach </td>
                                    @endif
                                    <td> {{ \App\Components\Functions::getRemainRoom($room)  }} </td>
                                    <td>{{!empty($room->date_available)?date('d-m-Y', strtotime($room->date_available)):'Không xác định'}}</td>
                                </tr>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            @endif

            <div class="row" style="margin-top: 15px">

                <div class="col-md-12">
                    {{--@if($room->hostel->type_rent != \App\Models\Hostel::TYPE_RENT_EVERY)--}}
                    {{--<a href="#ew" data-toggle="modal" class="btn btn-success btn-ew" data-room="{{ $room->id }}">Chốt--}}
                    {{--số--}}
                    {{--điện nước</a>--}}
                    {{--@endif--}}
                    <a href="#detail-room" data-toggle="modal" data-room="{{ $room->id }}"
                       class="btn-detail-room btn blue-steel">Thông tin phòng</a>
                    @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
                        @if($user->can('add-contract'))
                            {{--<a href="#add-contract" data-toggle="modal" class="btn-add-contract btn red"--}}
                            {{--data-room="{{ $room->id }}">Tạo hợp đồng</a>--}}
                        @endif

                    @else
                        @php $checkAddRenter = \App\Components\Functions::checkAddRenter($room);  @endphp


                        @if($checkAddRenter === 1)
                            @if($user->can('add-renter'))
                                <a href="#add-renter" data-toggle="modal" class="btn-add-renter btn red"
                                   data-type-rent="{{ \App\Models\Hostel::TYPE_RENT_ALL }}"
                                   data-room="{{ $room->id }}">Thêm người phụ thuộc</a>
                            @endif

                        @elseif($checkAddRenter === 2)


                            <a href="#end-contract" data-toggle="modal" class="btn-end-contract btn red"
                               data-room="{{ $room->id }}">Thanh lý hợp đồng</a>

                        @else

                            @if($user->can('add-contract'))
                                <a href="#add-contract" data-toggle="modal" class="btn-add-contract btn red"
                                   data-room="{{ $room->id }}">Tạo hợp đồng</a>
                            @endif

                        @endif
                    @endif

                    @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_ALL)

                        @php $checkContractRoom = \App\Components\Functions::checkContractRoom($room);  @endphp
                        @if($user->can('add-reserve'))
                            <a href="#reserve" data-toggle="modal" class="btn-reserve btn green-jungle"
                               data-room="{{ $room->id }}">Giữ chỗ</a>
                        @endif
                    @else
                        @php $checkFull = \App\Components\Functions::checkFull($room);  @endphp
                        @if(!$checkFull)
                            @if($user->can('add-reserve'))
                                {{--<a href="#reserve" data-toggle="modal" class="btn-reserve btn green-jungle"--}}
                                {{--data-room="{{ $room->id }}">Giữ--}}
                                {{--chỗ</a>--}}
                            @endif

                        @endif
                    @endif
                    @if($isEmptyRoom)
                        @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
                            @if($user->can('add-voucher'))
                                <a href="#voucher" data-toggle="modal" class="btn yellow-crusta btn-voucher"
                                   data-room="{{ $room->id }}">Lập hóa đơn dịch vụ</a>
                            @endif
                        @else
                            @if($user->can('add-voucher'))
                                <a href="#voucher-all" data-toggle="modal" class="btn yellow-crusta btn-voucher-all"
                                   data-room="{{ $room->id }}">Lập hóa đơn dịch vụ</a>
                            @endif
                        @endif

                    @endif


                </div>

            </div>
            @if($room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
            <!-- BEGIN PAGE CONTENT-->
                <div class="clearfix" style="margin-top: 10px"></div>
                <div class="row">
                    <div class="col-md-12">
                        <!-- BEGIN EXAMPLE TABLE PORTLET-->
                        <div class="portlet box blue">
                            <div class="portlet-title">
                                <div class="caption">
                                    <i class=""></i>Danh sách giường
                                </div>
                            </div>
                            <div class="portlet-body">
                                <div class="table-toolbar">
                                </div>
                                <table class="table table-striped table-hover table-bordered" id="sample_editable_1">
                                    <thead>
                                    <tr>
                                        <th>
                                            Giường
                                        </th>
                                        <th>
                                            Tình trạng
                                        </th>
                                        <th>
                                            Người thuê
                                        </th>
                                        <th>
                                            Số tiền chưa thanh toán
                                        </th>
                                        <th>Ngày trống</th>
                                        <th>Hành động</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    @if(!empty($room->beds))
                                        @foreach($room->beds as $bed)
                                            @php $bedStatus = App\Components\Functions::checkRoomBedStatus($bed); @endphp

                                            <tr>
                                                <td>
                                                    <p class="bed_name_show">{{ $bed->name}}</p>
                                                    <input type="text" value="{{ $bed->name}}" class="bed_name" hidden>
                                                </td>
                                                <td data-bed="{{ $bed->id }}">
                                                    @if($bedStatus==0)
                                                        Đang trống
                                                    @elseif($bedStatus==1)
                                                        Đã đặt cọc
                                                    @elseif($bedStatus==2)

                                                        @if($bed->contract)
                                                            @if(!empty($bed->contract->leave_day))
                                                                Sắp trống
                                                                ({{ $bed->contract->leave_day->format('d/m/Y') }})
                                                            @else
                                                                Đang ở
                                                            @endif
                                                        @endif
                                                    @endif
                                                </td>
                                                <td>
                                                    @if($bedStatus==0||$bedStatus==null)
                                                        <a href="#add-contract" data-toggle="modal"
                                                           class="btn-add-contract btn red"
                                                           data-room="{{ $room->id }}" data-bed="{{$bed->id}}">Tạo hợp
                                                            đồng</a>
                                                        <a href="#reserve" data-toggle="modal"
                                                           class="btn-reserve btn green-jungle"
                                                           data-room="{{ $room->id }}" data-bed="{{$bed->id}}">Giữ
                                                            chỗ</a>
                                                    @elseif($bedStatus == \App\Models\RoomBed::UNAVAILABLE)
                                                        @foreach($contracts as $contract)
                                                            @if($contract->bed_id == $bed->id)
                                                                <a href="#detail-room" data-room="{{ $room->id }}"
                                                                   data-toggle="modal"
                                                                   class="btn-detail-contract"
                                                                   data-contract="{{ $contract->id }}"
                                                                   data-code="{{ $contract->code }}">Hợp đồng:
                                                                    {{--{{$contract->code}}--}}
                                                                    {{ $contract->name }} @if($contract->end_date < \Carbon\Carbon::now()->toDateString())
                                                                        - Hết hạn @endif</a>
                                                                <a href="#reserve" data-toggle="modal"
                                                                   class="btn-reserve" data-room="{{$room->id}}"
                                                                   data-bed="{{$bed->id}}">(Giữ
                                                                    chỗ)</a>
                                                            @endif
                                                        @endforeach

                                                    @elseif($bedStatus == \App\Models\RoomBed::DEPOSIT)
                                                        @foreach($reserves as $reserve)
                                                            @if($reserve->bed_id == $bed->id)
                                                                <a href="#detail-reserve"
                                                                   data-reserve="{{ $reserve->id }}"
                                                                   data-toggle="modal"
                                                                   class="btn-detail-reserve"
                                                                >Giữ chỗ:
                                                                    {{$reserve->name}}
                                                                    - {{ $reserve->phone }}</a> (<a
                                                                        href="#add-contract-reserve"
                                                                        class="btn-add-contract-reserve"
                                                                        data-reserve="{{ $reserve->id }}"
                                                                        data-room="{{ $room->id }}"
                                                                        data-bed="{{$bed->id}}"
                                                                        data-toggle="modal">Tạo Hợp
                                                                    đồng</a>)
                                                            @endif
                                                        @endforeach
                                                    @endif

                                                </td>
                                                <td class="center">
                                                    {{ \App\Components\Functions::getRemainBed($room,$bed)}}
                                                </td>
                                                <td>
                                                    <p class="date_avail_show">{{!empty($bed->date_available)? date("d/m/Y", strtotime($bed->date_available)) :'Không xác định'}}</p>
                                                    <input type="text" class="form-control datepicker date_room_bed"
                                                           name="date_avai"
                                                           value="{{!empty($bed->date_available)?date("d/m/Y", strtotime($bed->date_available)):''}}"
                                                           style="display: none;">
                                                </td>
                                                <td>
                                                    <a href="javascript:void(0);"
                                                       class="btn btn-icon-only red edit_room_bed"
                                                       style="float: left">
                                                        <i class="fa fa-edit"></i>
                                                    </a>
                                                    <a href="javascript:void(0);"
                                                       class="btn btn-icon-only red delete_room_bed"
                                                       data-id="{{ $bed->id }}"
                                                       style="float: left">
                                                        <i class="fa fa-trash"></i>
                                                    </a>
                                                    <a href="javascript:void(0);"
                                                       class="btn btn-icon-only purple cancel-edit"
                                                       style="display: none; float:left;">
                                                        <i class="fa fa-times"></i>
                                                    </a>
                                                    <a href="javascript:void(0);"
                                                       class="btn btn-icon-only blue store_room_bed"
                                                       data-id="{{$bed->id}}" style="display: none; float: left">
                                                        <i class="glyphicon glyphicon-floppy-saved"></i>
                                                    </a>

                                                </td>
                                            </tr>
                                        @endforeach
                                    @endif
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- END EXAMPLE TABLE PORTLET-->
                    </div>
                </div>
                <!-- END PAGE CONTENT -->
            @endif
        </div>
    </div>
@endforeach

<script>
    $(document).ready(function () {
        $('.datepicker').datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true
        });
    })

    $(document).on('click', '.store_room_bed', function () {
        var bed_id = $(this).attr('data-id');
        var bed_name = $(this).parents('tr').find('.bed_name').val();
        var bed_available = $(this).parents('tr').find('.date_room_bed').val();
        $.ajax({
            url: '{{url("admin2/room/edit-room-bed")}}',
            data: {
                id: bed_id,
                name: bed_name,
                date_available: bed_available,
            },
            type: 'post',
            dataType: 'json',
            success: function (response) {
                if (response.status == 1) {
                    swal('Thành công', response.message, 'success');
                    location.reload();
                } else {
                    swal('Thông báo', response.message, 'info');
                }
            }
        });
    });

    $(document).on('click', '.edit_room_bed', function () {
        $(this).parents('tr').find('.bed_name').removeAttr('hidden');
        $(this).parents('tr').find('.date_room_bed').css('display', 'block');
        $(this).parents('tr').find('.bed_name_show').css('display', 'none');
        $(this).parents('tr').find('.date_avail_show').css('display', 'none');
        $(this).css('display', 'none');
        $(this).parents('tr').find('.cancel-edit').css('display', 'block');
        $(this).parents('tr').find('.store_room_bed').css('display', 'block');
    })

    $(document).on('click', '.cancel-edit', function () {
        $(this).parents('tr').find('.bed_name').attr('hidden', true);
        $(this).parents('tr').find('.date_room_bed').css('display', 'none');
        $(this).parents('tr').find('.bed_name_show').css('display', 'block');
        $(this).parents('tr').find('.date_avail_show').css('display', 'block');
        $(this).parents('tr').find('.edit_room_bed').css('display', 'none');
        $(this).css('display', 'none');
        $(this).parents('tr').find('.edit_room_bed').css('display', 'block');
        $(this).parents('tr').find('.store_room_bed').css('display', 'none');
    })

    $(document).off('click', '.delete_room_bed').on('click', '.delete_room_bed', function () {
        var id = $(this).attr('data-id');

        bootbox.confirm({
            message: "Bạn có chắc chắn muốn xóa giường này ?",
            buttons: {
                confirm: {
                    label: 'Có',
                    className: 'btn-success'
                },
                cancel: {
                    label: 'Không',
                    className: 'btn-danger'
                }
            },
            callback: function (result) {
                if (result == true) {
                    $.ajax({
                        url: '{{url("admin2/room/delete-room-bed")}}',
                        data: {
                            id: id,
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (response) {
                            if (response.status == 1) {
                                swal('Thành công', response.message, 'success');
                                $('button[data-block-name="' + blockGlobalName + '"]').trigger('click');

                            } else {
                                swal('Thông báo', response.message, 'info');
                            }
                        }
                    })
                }
            }
        });

    })

</script>