<form action="#" id="form-edit-contract">
    {!! csrf_field() !!}
    <div class="row">
        <div class="col-md-12">
            <div class="row">
                <div class="col-md-12">
                    <div class="text-center">
                        <h4><strong>Cập nhật thông tin hợp đồng mã {{ $contract->code }}</strong></h4>
                    </div>
                    <!-- BEGIN Portlet PORTLET-->

                    <input type="hidden" name="id" value="{{ $contract->id }}">
                    <div class="form-body">
                        <div class="row">

                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Ngày hợp đồng <span class="required"
                                                               aria-required="true"> (*) </span></label>
                                    <input name="date_contract" type="text" class="form-control datepicker2"
                                           value="{{ $contract->date_contract->format('d/m/Y') }}">
                                    <span id="emailHelp" class="form-text text-muted" style="color: red">Sửa ngày hợp đồng sẽ không thay đổi/tác động gì đến các hóa đơn tiền nhà</span>
                                </div>
                            </div>
                            {{--<div class="col-md-4">--}}
                            {{--<div class="form-group">--}}
                            {{--<label>Thuê từ ngày <span class="required" aria-required="true"> (*) </span></label>--}}
                            {{--<input name="start_date" type="text" class="form-control datepicker"--}}
                            {{--value="{{ \Carbon\Carbon::now()->format('d/m/Y') }}">--}}
                            {{--</div>--}}
                            {{--</div>--}}
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Tiền phòng <span class="required" aria-required="true"> (*) </span></label>
                                    <input name="room_price" type="text" class="form-control price-r"
                                           value="{{ number_format($contract->room_price, 0, '.', '.') }}">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Tiền cọc <span class="required" aria-required="true"> (*) </span></label>
                                    <input name="deposit" type="text" class="form-control price-r"
                                           value="{{ number_format($contract->deposit, 0, '.', '.') }}">
                                </div>
                            </div>
                            {{--<div class="col-md-4">--}}
                            {{--<div class="form-group">--}}
                            {{--<label>Thuê đến ngày <span class="required"--}}
                            {{--aria-required="true"> (*) </span></label>--}}
                            {{--<input name="end_date" type="text" class="form-control datepicker"--}}
                            {{--value="{{ \Carbon\Carbon::now()->addDay(180)->format('d/m/Y') }}">--}}
                            {{--</div>--}}
                            {{--</div>--}}
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Ngày hết hạn</label>
                                    <input name="end_date" type="text" class="form-control datepicker2"
                                           value="{{ $contract->end_date->format('d/m/Y') }}">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Ngày chốt tiền phòng hàng tháng </label>
                                    <select class="form-control select2" name="day_collect">
                                        @for($i = 1; $i <=31; $i++)
                                            <option @if(!empty($contract->day_collect) && $contract->day_collect == $i) selected
                                                    @endif value="{{ $i }}">{{ $i }}</option>
                                        @endfor
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Ngày rời đi</label>
                                    <input name="leave_day" type="text" class="form-control datepicker2"
                                           value="{{ optional($contract->leave_day)->format('d/m/Y') }}">
                                </div>
                            </div>
{{--                            @if(auth('backend')->user()->can('change-room'))--}}
{{--                                <div class="col-md-12">--}}
{{--                                    <div class="form-group">--}}
{{--                                        <label>Nhà</label>--}}
{{--                                        <select name="hostel_id" class="form-control select2" id="hostel_id">--}}
{{--                                            @php--}}
{{--                                                if(auth('backend')->user()->type == \App\User::STAFF)--}}
{{--        {--}}
{{--            $hostelArrs = \App\Components\Functions::getHostelArrStaff();--}}
{{--        } else {--}}
{{--        $hostelArrs = \App\Models\Hostel::query()--}}
{{--        ->where('owner_id', auth('backend')->user()->id)--}}
{{--        ->pluck('id')--}}
{{--        ->toArray();--}}
{{--        }--}}

{{--                                                    $hostels = \App\Models\Hostel::query()--}}
{{--                                                    ->where('type_rent', $contract->hostel->type_rent)--}}
{{--    ->whereIn('id', $hostelArrs)--}}
{{--    ->get(); @endphp--}}

{{--                                            @foreach($hostels as $hostel)--}}
{{--                                                <option @if($contract->hostel_id == $hostel->id) selected--}}
{{--                                                        @endif value="{{ $hostel->id }}">{{ $hostel->name }}--}}
{{--                                                </option>--}}
{{--                                            @endforeach--}}

{{--                                        </select>--}}
{{--                                    </div>--}}
{{--                                </div>--}}
{{--                                <div class="col-md-12">--}}
{{--                                    <div class="form-group">--}}
{{--                                        <label>Phòng</label>--}}
{{--                                        <select name="room_id" class="form-control select2" id="room_id">--}}

{{--                                            @php $rooms = \App\Models\Room::query()--}}
{{--->where('hostel_id', $contract->hostel_id)--}}
{{--->get(); @endphp--}}

{{--                                            @foreach($rooms as $room)--}}
{{--                                                <option @if($contract->room_id == $room->id) selected--}}
{{--                                                        @endif value="{{ $room->id }}">{{ $room->name }}--}}
{{--                                                </option>--}}
{{--                                            @endforeach--}}

{{--                                        </select>--}}
{{--                                    </div>--}}
{{--                                </div>--}}

{{--                                @if($contract->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)--}}
{{--                                    <div class="col-md-12">--}}
{{--                                        <div class="form-group">--}}
{{--                                            <label>Giường</label>--}}
{{--                                            <select name="bed_id" class="form-control select2" id="bed_id">--}}

{{--                                                @php $beds = \App\Models\RoomBed::query()->where('room_id', $contract->room_id)--}}
{{--->where(function($q) use ($contract) {--}}
{{--    $q->orWhere('status', \App\Models\RoomBed::AVAILABLE);--}}
{{--    $q->orWhere('id', $contract->bed_id);--}}
{{--})--}}
{{--->get(); @endphp--}}

{{--                                                @foreach($beds as $bed)--}}
{{--                                                    <option @if($contract->bed_id == $bed->id) selected--}}
{{--                                                            @endif value="{{ $bed->id }}">{{ $bed->name }}--}}
{{--                                                    </option>--}}
{{--                                                @endforeach--}}

{{--                                            </select>--}}
{{--                                        </div>--}}
{{--                                    </div>--}}
{{--                                @endif--}}
{{--                            @endif--}}
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Chu kỳ trả tiền phòng</label>
                                    <select name="period" class="form-control">

                                        <option @if($contract->period == \App\Models\Room::ONE_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::ONE_MONTH_PERIOD }}">1 tháng
                                        </option>
                                        <option @if($contract->period == \App\Models\Room::TWO_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::TWO_MONTH_PERIOD }}">2 tháng
                                        </option>
                                        <option @if($contract->period == \App\Models\Room::THREE_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::THREE_MONTH_PERIOD }}">3 tháng
                                        </option>

                                        <option @if($contract->period == \App\Models\Room::FOUR_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::FOUR_MONTH_PERIOD }}">4 tháng
                                        </option>
                                        <option @if($contract->period == \App\Models\Room::FIVE_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::FIVE_MONTH_PERIOD }}">5 tháng
                                        </option>
                                        <option @if($contract->period == \App\Models\Room::SIX_MONTH_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::SIX_MONTH_PERIOD }}">6 tháng
                                        </option>
                                        <option @if($contract->period == \App\Models\Room::ONE_YEAR_PERIOD) selected
                                                @endif value="{{ \App\Models\Room::ONE_YEAR_PERIOD }}">1 năm
                                        </option>

                                    </select>
                                </div>
                            </div>

                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Mã hợp đồng tham chiếu </label>
                                    <input name="reference" type="text" class="form-control"
                                           value="{{ $contract->reference }}">
                                </div>
                            </div>
                            @php $renter = \App\Models\Renter::where('user_id', $contract->renter_id)->first(); @endphp
                            {{--                            <div class="col-md-12">--}}
                            {{--                                <div class="form-group">--}}
                            {{--                                    <label>Ghi chú</label>--}}
                            {{--                                    <textarea name="note" class="form-control">@if(!empty($renter)) {{ $contract->note }} @endif</textarea>--}}
                            {{--                                </div>--}}
                            {{--                            </div>--}}
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Ghi chú hợp đồng</label>
                                    <textarea name="contract_note"
                                              class="form-control">{{ $contract->contract_note }}</textarea>
                                </div>
                            </div>

                        </div>
                        {{--                        <div class="row">--}}
                        {{--                            <div class="col-md-12">--}}
                        {{--                                <div class="form-group">--}}
                        {{--                                    <label>Ghi chú hợp đồng</label>--}}
                        {{--                                    <textarea name="contract_note" class="form-control">{{ $contract->contract_note }}</textarea>--}}
                        {{--                                </div>--}}
                        {{--                            </div>--}}
                        {{--                        </div>--}}

                        <div class="row">
                            <div class="col-md-12">
                                <hr style="border-top: 1px dashed">
                            </div>
                            <div class="text-center">
                                <h4><strong>Thông tin dịch vụ</strong></h4>
                            </div>
                            <div class="col-md-12">
                                <!-- BEGIN Portlet PORTLET-->
                                @php
                                    $fees = \App\Models\HostelFee::where('hostel_id', $room->hostel->id)->get();
                            $hostel = $room->hostel;
                                @endphp

                                <div class="table-scrollable">
                                    <table class="table table-bordered table-hover">
                                        <thead>
                                        <tr>
                                            <th><label class="mt-checkbox">
                                                    <input type="checkbox" class="checkbox-all-contract"> <span></span>
                                                </label>
                                            </th>
                                            <th> Tên</th>
                                            <th> Loại phí</th>
                                            <th> Đơn giá</th>
                                            <th> Đơn vị</th>
                                            <th> Số lượng</th>
                                            <th>Chỉ số đầu</th>
                                        </tr>
                                        </thead>
                                        <tbody id="table-hostel-fees">
                                        @php $qty = \App\Models\ContractFee::where('contract_id', $contract->id)->pluck('qty', 'fee_id')->toArray();

                                          $feeUsed = \App\Models\ContractFee::where('contract_id', $contract->id)->pluck('fee_id')->toArray();
                                        @endphp
                                        @foreach($fees as $fee)
                                            <tr>
                                                <td>

                                                    <label class="mt-checkbox">
                                                        <input type="checkbox" class="checkbox-contract-fee-c-contract"
                                                               @if(in_array($fee->id, $feeUsed)) checked @endif
                                                               name="fee-contracts[]"
                                                               value="{{ $fee->id }}"> <span></span>
                                                    </label>

                                                </td>
                                                <td> {{ $fee->name }} </td>
                                                <td> {{ $fee->type_text }} </td>
                                                <td>
                                                    @if($fee->type == \App\Models\HostelFee::ELECTRIC || $fee->type == \App\Models\HostelFee::WATER || $fee->type == \App\Models\HostelFee::WATER_DYNAMIC
                                       || $fee->type == \App\Models\HostelFee::ELECTRIC_DYNAMIC
                                       )
                                                        Biến động
                                                    @else
                                                        {{ number_format($fee->fee, 0, '.', '.' ) }}
                                                    @endif
                                                </td>
                                                <td>{{ $fee->unit }}</td>
                                                <td>
                                                    @if($fee->type != \App\Models\HostelFee::ELECTRIC && $fee->type != \App\Models\HostelFee::WATER
                                       && $fee->type != \App\Models\HostelFee::ELECTRIC_BY_CLOCK && $fee->type != \App\Models\HostelFee::WATER_BY_CLOCK)
                                                        <input type="number" class="form-control"
                                                               name="qty[{{ $fee->id }}][]"
                                                               @if(isset($qty[$fee->id])) value="{{ $qty[$fee->id] }}"
                                                               @else    value="1" @endif>
                                                    @else
                                                        <input type="number" class="form-control" disabled
                                                               value="0">
                                                    @endif

                                                </td>
                                                <td>

                                                    @if($fee->type != \App\Models\HostelFee::ELECTRIC && $fee->type != \App\Models\HostelFee::WATER
                                       && $fee->type != \App\Models\HostelFee::ELECTRIC_BY_CLOCK && $fee->type != \App\Models\HostelFee::WATER_BY_CLOCK)
                                                        <input type="number" class="form-control" disabled
                                                               value="0">
                                                    @else

                                                        <input type="number" class="form-control"
                                                               name="qty[{{ $fee->id }}][]"
                                                               @if(isset($qty[$fee->id])) value="{{ $qty[$fee->id] }}"
                                                               @else    value="1" @endif>
                                                    @endif
                                                </td>

                                            </tr>
                                        @endforeach
                                        </tbody>

                                    </table>

                                </div>
                                <!-- END Portlet PORTLET-->
                            </div>
                        </div>
                    </div>


                </div>
            </div>

        </div>
    </div>
</form>

