<!DOCTYPE html>
<html>

@include('includes.head')


<body class="hold-transition skin-blue fixed sidebar-mini">
    <!-- Site wrapper -->
    <div class="wrapper">

        @include('includes.header')
        <!-- =============================================== -->

        <!-- Left side column. contains the sidebar -->
        @include('includes.side-nav')

        <!-- =============================================== -->

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <h1>
                    Property Management
                    <small>Manage Property</small>
                </h1>
                <ol class="breadcrumb">
                    <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                    <li><a href="#">Property</a></li>
                    <li class="active">Property</li>
                </ol>
            </section>

            <!-- Main content -->
            <section class="content">
                @include('includes.messages')
                <!-- Default box -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Property Management</h3>
                        <div class="box-tools pull-right">
                            <a href="{{ route('property.add') }}" class="btn btn-block btn-primary btn-flat">
                                <i class="fa fa-plus"></i> Add New Property </a>

                        </div>


                    </div>
                    <div class="row">
                        <div class="col-md-12">

                            <div class="box-body">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>Sl No</th>
                                            <th>Name</th>
                                            <th>Property Type</th>
                                            <th>Property For</th>
                                            <th>Price</th>
                                            <th>Location</th>
                                            <th>Status</th>
                                            <th>Image Upload</th>
                                            <th>Features Upload</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($records as $record)
                                            <tr>
                                                <td>{{ $loop->iteration }}</td>
                                                <td>{{ $record->name ?? '' }}</td>
                                                <td>
                                                    {{ $record->property_types->name ?? '' }}
                                                </td>
                                                <td>
                                                    @if ($record->property_for_id == 1)
                                                        Rent
                                                    @else
                                                        Sale
                                                    @endif
                                                </td>
                                                <td>
                                                    ₹ {{ $record->price ?? '0' }} /
                                                    @if ($record->price_break_id == 1)
                                                        Monthly
                                                    @elseif ($record->price_break_id == 2)
                                                        Yearly
                                                    @elseif ($record->price_break_id == 3)
                                                        One Time
                                                    @else
                                                        N/A
                                                    @endif
                                                </td>
                                                <td>
                                                    <b>BHK :</b> {{ $record->bhk->name ??"" }}<br>
                                                   <b>City :</b> {{ $record->city->name ??"" }}<br>
                                                    <b>Full Address : {{ $record->address }}</b>
                                                </td>





                                                <td>


                                                    <label class="switch">
                                                        <input type="checkbox" class="toggle-status"
                                                            data-id="{{ $record->id }}" data-field="status"
                                                            {{ $record->status ? 'checked' : '' }}>
                                                        <span class="slider round"></span>
                                                    </label>
                                                </td>
                                                <td>
                                                    <button type="button" data-toggle="modal"
                                                    data-target="#features_upload{{ $record->id }}"
                                                    class="btn btn-sm btn-block mt-2 btn-warning"><i class="fa fa-list"></i>
                                                    Features</button>
                                                </td>
                                                <td>  <button type="button" data-toggle="modal"
                                                    data-target="#file_upload{{ $record->id }}"
                                                    class="btn btn-sm btn-info btn-block mt-2"><i class="fa fa-image"></i> Upload
                                                    Image</button></td>
                                                <td>
                                                    <!-- Add your action buttons or links here -->
                                                    {{-- Example: --}}
                                                    <a href="{{ route('property.edit', $record->id) }}"
                                                        class="btn btn-primary btn-sm"><i class="fa fa-edit"></i></a>

                                                    <a href="#" class="btn btn-danger btn-sm"
                                                        onclick="confirmDelete('{{ route('property.types.delete', $record->id) }}')"><i
                                                            class="fa fa-trash"></i></a>

                                                    <script>
                                                        function confirmDelete(url) {
                                                            if (confirm('Are you sure you want to delete this?')) {
                                                                window.location.href = url;
                                                            }
                                                        }
                                                    </script>





                                                </td>
                                            </tr>

                                            <!-- Edit file upload Modal -->
                                            <div class="modal fade" id="file_upload{{ $record->id }}">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h4 class="modal-title">Upload Image</h4>
                                                            <button type="button" class="close"
                                                                data-dismiss="modal">&times;</button>
                                                        </div>
                                                        <div class="modal-body">
                                                            <form method="POST"
                                                                action="{{ route('property.update.img', $record->id) }}"
                                                                enctype="multipart/form-data">
                                                                @csrf
                                                                <input type="hidden" name="id"
                                                                    value="{{ $record->id }}">



                                                                <div class="form-row">
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_1_{{ $record->id }}"
                                                                                src="{{ $record->img_1 ? url($record->img_1) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_1"
                                                                                onchange="previewImage(event, 'preview_img_1_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_2_{{ $record->id }}"
                                                                                src="{{ $record->img_2 ? url($record->img_2) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_2"
                                                                                onchange="previewImage(event, 'preview_img_2_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_3_{{ $record->id }}"
                                                                                src="{{ $record->img_3 ? url($record->img_3) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_3"
                                                                                onchange="previewImage(event, 'preview_img_3_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_4_{{ $record->id }}"
                                                                                src="{{ $record->img_4 ? url($record->img_4) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_4"
                                                                                onchange="previewImage(event, 'preview_img_4_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_5_{{ $record->id }}"
                                                                                src="{{ $record->img_5 ? url($record->img_5) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_5"
                                                                                onchange="previewImage(event, 'preview_img_5_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group col-md-4">
                                                                        <div class="bg-gray p-2  text-center">
                                                                            <img id="preview_img_6_{{ $record->id }}"
                                                                                src="{{ $record->img_6 ? url($record->img_6) : url('/dist/img/image.png') }}"
                                                                                style="max-width: 100%">
                                                                            <input type="file" class="form-control"
                                                                                name="img_6"
                                                                                onchange="previewImage(event, 'preview_img_6_{{ $record->id }}')">
                                                                        </div>
                                                                    </div>
                                                                </div>



                                                                <div class="form-group">
                                                                    <input type="submit"
                                                                        class="btn btn-info btn-block"
                                                                        value="Upload Image">
                                                                </div>
                                                            </form>
                                                        </div>
                                                        <div class="modal-footer">

                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- Edit Modal -->
                                            <div class="modal fade" id="features_upload{{ $record->id }}">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h4 class="modal-title">Features Update</h4>
                                                            <button type="button" class="close"
                                                                data-dismiss="modal">&times;</button>
                                                        </div>
                                                        <div class="modal-body">
                                                            <form method="POST"
                                                                action="{{ route('property.update.features', $record->id) }}"
                                                                enctype="multipart/form-data">
                                                                @csrf
                                                                <input type="hidden" name="id"
                                                                    value="{{ $record->id }}">



                                                                <div class="form-group">
                                                                    <select name="features_id[]"
                                                                        class="select2 form-control" style="width:100%" multiple>
                                                                        @foreach ($all_features as $feature)
                                                                            <option value="{{ $feature->id }}"
                                                                                {{ $record->feature_mappings->pluck('features_id')->contains($feature->id) ? 'selected' : '' }}>
                                                                                {{ $feature->name }}
                                                                            </option>
                                                                        @endforeach
                                                                    </select>
                                                                </div>



                                                                <div class="form-group">
                                                                    <input type="submit"
                                                                        class="btn btn-info btn-block"
                                                                        value="Update Features">
                                                                </div>
                                                            </form>
                                                        </div>
                                                        <div class="modal-footer">

                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        @endforeach

                                    </tbody>

                                </table>

                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer">

                                {{ $records->links() }}

                            </div>

                        </div>

                    </div>

                </div>
                <!-- /.box -->

            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

        @include('includes.footer')

    </div>
    <!-- ./wrapper -->

    @include('includes.scripts')

    <script>
        function previewImage(event, previewId) {
            const input = event.target;
            const reader = new FileReader();

            reader.onload = function() {
                const img = document.getElementById(previewId);
                img.src = reader.result;
            }

            if (input.files && input.files[0]) {
                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            document.querySelectorAll(".toggle-status").forEach(function(toggle) {
                toggle.addEventListener("change", function() {
                    let eventId = this.getAttribute("data-id");
                    let field = this.getAttribute("data-field");
                    let value = this.checked ? 1 : 0;

                    fetch("{{ route('property.status.update') }}", {
                            method: "POST",
                            headers: {
                                "Content-Type": "application/json",
                                "X-CSRF-TOKEN": "{{ csrf_token() }}"
                            },
                            body: JSON.stringify({
                                id: eventId,
                                field: field,
                                value: value
                            })
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (!data.success) {
                                alert(data.message);
                                this.checked = !this.checked; // Revert change on failure
                            }
                        })
                        .catch(error => {
                            alert("Error updating status!");
                            this.checked = !this.checked;
                        });
                });
            });
        });
    </script>
</body>

</html>
