@php $user = auth('backend')->user() @endphp
@foreach($rooms as $room)
    @php $isEmptyRoom = \App\Components\Functions::ifEmptyRoom($room); @endphp
    @php $contracts = \App\Models\Contract::where('room_id', $room->id)->where('status', '<>', \App\Models\Contract::LIQUIDATED)->get();
     $reserves = \App\Models\RoomReservation::where('room_id', $room->id)->get();
    @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($room->date_available))
         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($room->date_available))
                                Sắp trống
                                ({{ \Carbon\Carbon::createFromFormat('Y-m-d', $room->date_available)->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::where('room_id',$room->id)->where('status',\App\Models\RoomBed::UNAVAILABLE)->get()->count();
                            $deposit = \App\Models\RoomBed::where('room_id',$room->id)->where('status',\App\Models\RoomBed::DEPOSIT)->get()->count();
                        @endphp

                        [Đang ở: {{$unavailable}}] - [Đang cọc: {{$deposit}}] - [Đang
                        trống: {{$room->max_renters-($unavailable+$deposit)}}]
                    @endif
                </span>
            </div>
            <div class="tools">
                <a href="javascript:void(0);" class="collapse" data-original-title="" title=""> </a>
            </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::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>

                                        @php
                                        $date = null;
                                               $contract = \App\Models\Contract::query()
                                        ->where('room_id', $room->id)
                                        ->where('status', '<>', \App\Models\Contract::LIQUIDATED)
                                        ->first();

                                        if($contract)
                                            {
                                                $date = $contract->leave_day->format('d/m/Y');
                                            }
                                                @endphp
                                        {{ $date }}
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            @endif

            @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)

                                            <tr>
                                                <td>
                                                    <p class="bed_name_show">{{ $bed->name}}</p>
                                                    <input type="text" value="{{ $bed->name}}" class="bed_name" hidden>
                                                </td>
                                                <td>
                                                    @if($bed->status==0)Đang trống @elseif($bed->status==1)Đã đặt
                                                    cọc @elseif($bed->status==2)Đang ở @endif
                                                </td>
                                                <td>
                                                    @if($bed->status==0||$bed->status==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($bed->status == \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>
                                                            @endif
                                                        @endforeach

                                                    @elseif($bed->status == \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">
                                                    @php
                                                        $date = null;
                                                               $contract = \App\Models\Contract::query()
                                                                 ->where('status', '<>', \App\Models\Contract::LIQUIDATED)
                                                        ->where('room_id', $room->id)
                                                        ->where('bed_id', $bed->id)
                                                        ->first();

                                                        if($contract)
                                                            {
                                                                $date = $contract->leave_day->format('d/m/Y');
                                                            }
                                                    @endphp

                                                </td>
                                                <td>
                                                    <p class="date_avail_show">{{ $date }}</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 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');
    })

</script>