<!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">
                            <button type="button" data-toggle="modal" data-target="#create"
                                class="btn btn-block btn-primary btn-flat">
                                <i class="fa fa-plus"></i> Create Property Type </a>

                        </div>
                        <div class="modal fade" id="create">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title">Add New</h4>
                                    </div>
                                    <div class="modal-body">
                                        <form method="post" action="{{ route('property.types.store') }}"
                                            enctype="multipart/form-data">
                                            @csrf
                                            <div class="form-group">
                                                <input type="text" class="form-control"
                                                    placeholder="Enter Propert Type" required="" name="name">
                                            </div>
                                            <div class="form-group">
                                                <input type="file" class="form-control" required name="img"
                                                    id="imgInput" accept="image/*">
                                            </div>

                                            <!-- Image preview -->
                                            <div class="form-group">
                                                <img id="previewImage" src="{{ url('/dist/img/avatar-old.png') }}"
                                                    alt="Preview" style="max-width: 150px; display: none;" />
                                            </div>
                                            <div class="form-group">
                                                <input type="submit" class="btn btn-info" value="submit"
                                                    name="">
                                            </div>
                                        </form>
                                    </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>
                        <!-- /.modal -->

                    </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>Property Type</th>
                                            <th>Image</th>
                                            <th>Status</th>

                                            <th style="width: 200px">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($records as $record)
                                            <tr>
                                                <td>{{ $loop->iteration }}</td>
                                                <td>{{ $record->name ?? '' }}</td>
                                                <td>
                                                    <img src="{{ $record->img ?? url('/dist/img/avatar-old.png') }}"
                                                        alt="Property Image" width="50">
                                                </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>
                                                    <!-- Add your action buttons or links here -->
                                                    {{-- Example: --}}
                                                    <button type="button" data-toggle="modal"
                                                        data-target="#edit_{{ $record->id }}"
                                                        class="btn btn-primary btn-sm"><i class="fa fa-edit"></i></button>
                                                        <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 Modal -->
                                            <div class="modal fade" id="edit_{{ $record->id }}">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h4 class="modal-title">Edit Property Type</h4>
                                                            <button type="button" class="close"
                                                                data-dismiss="modal">&times;</button>
                                                        </div>
                                                        <div class="modal-body">
                                                            <form method="POST"
                                                                action="{{ route('property.types.update', $record->id) }}"
                                                                enctype="multipart/form-data">
                                                                @csrf
                                                                <input type="hidden" name="id"
                                                                    value="{{ $record->id }}">

                                                                <div class="form-group">
                                                                    <input type="text" class="form-control"
                                                                        name="name" value="{{ $record->name }}"
                                                                        required>
                                                                </div>

                                                                <div class="form-group">
                                                                    <label for="imgInput_{{ $record->id }}">Change
                                                                        Image</label>
                                                                    <input type="file" name="img"
                                                                        class="form-control img-input"
                                                                        id="imgInput_{{ $record->id }}"
                                                                        data-preview="previewImage_{{ $record->id }}"
                                                                        accept="image/*">
                                                                </div>

                                                                <div class="form-group">
                                                                    <label>Current Image:</label><br>
                                                                    <img id="previewImage_{{ $record->id }}"
                                                                        src="{{ $record->img ?? url('/dist/img/avatar-old.png') }}"
                                                                        alt="Property Image"
                                                                        style="max-width: 150px;">
                                                                </div>

                                                                <div class="form-group">
                                                                    <input type="submit" class="btn btn-info"
                                                                        value="Update">
                                                                </div>
                                                            </form>
                                                        </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>
        document.querySelectorAll('.img-input').forEach(input => {
            input.addEventListener('change', function(e) {
                const previewId = this.getAttribute('data-preview');
                const preview = document.getElementById(previewId);

                if (this.files && this.files[0]) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        preview.src = e.target.result;
                    };
                    reader.readAsDataURL(this.files[0]);
                }
            });
        });
    </script>
    <script>
        document.getElementById('imgInput').addEventListener('change', function(event) {
            const preview = document.getElementById('previewImage');
            const file = event.target.files[0];

            if (file) {
                const reader = new FileReader();

                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                };

                reader.readAsDataURL(file);
            } else {
                preview.src = '';
                preview.style.display = 'none';
            }
        });
    </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.types.status') }}", {
                            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>
