<!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>
                    <div class="row">
                        <div class="col-md-12">

                            <div class="box-body">
                                <form action="{{ route('property.update', $property->id) }}" method="PUT" enctype="multipart/form-data">
                                    @csrf

                                    <div class="form-row">
                                        {{-- Show validation errors --}}
                                        @if ($errors->any())
                                            <div class="col-12 mb-3">
                                                <div class="alert alert-danger w-100">
                                                    <ul class="mb-0">
                                                        @foreach ($errors->all() as $error)
                                                            <li>{{ $error }}</li>
                                                        @endforeach
                                                    </ul>
                                                </div>
                                            </div>
                                        @endif

                                        <div class="form-group col-md-12">
                                            <label>Name</label>
                                            <input type="text" name="name" class="form-control" placeholder="Enter Property Name"
                                                value="{{ old('name', $property->name ?? '') }}">
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Type</label>
                                            <select name="property_type_id" class="form-control" required>
                                                <option value="">Select Type</option>
                                                @foreach ($types as $type)
                                                    <option value="{{ $type->id }}"
                                                        {{ old('property_type_id', $property->property_type_id ?? '') == $type->id ? 'selected' : '' }}>
                                                        {{ $type->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Property For</label>
                                            <select name="property_for_id" class="form-control" required>
                                                <option value="">Select</option>
                                                <option value="1" {{ old('property_for_id', $property->property_for_id ?? '') == 1 ? 'selected' : '' }}>Rent</option>
                                                <option value="2" {{ old('property_for_id', $property->property_for_id ?? '') == 2 ? 'selected' : '' }}>Sale</option>
                                                <option value="3" {{ old('property_for_id', $property->property_for_id ?? '') == 3 ? 'selected' : '' }}>Mortgage</option>
                                                
                                            </select>
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Price</label>
                                            <input type="text" name="price" class="form-control" required
                                                value="{{ old('price', $property->price ?? '') }}" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Price Break</label>
                                            <select name="price_break_id" class="form-control" required>
                                                <option value="">Select</option>
                                                <option value="1" {{ old('price_break_id', $property->price_break_id ?? '') == 1 ? 'selected' : '' }}>Monthly</option>
                                                <option value="2" {{ old('price_break_id', $property->price_break_id ?? '') == 2 ? 'selected' : '' }}>Yearly</option>
                                                <option value="3" {{ old('price_break_id', $property->price_break_id ?? '') == 3 ? 'selected' : '' }}>One Time</option>
                                            </select>
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>BHK</label>

                                            <select name="bhk_id" class="form-control" required>
                                                <option value="">Select BHK</option>
                                                @foreach ($bhks as $c)
                                                    <option value="{{ $c->id }}" {{ old('bhk_id', $property->bhk_id) == $c->id ? 'selected' : '' }}>
                                                        {{ $c->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                        </div>


                                        <div class="form-group col-md-6">
                                            <label>City</label>
                                            <select name="city_id" class="form-control" required>
                                                <option value="">Select City</option>
                                                @foreach ($city as $c)
                                                    <option value="{{ $c->id }}" {{ old('city_id', $property->city_id) == $c->id ? 'selected' : '' }}>
                                                        {{ $c->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                        </div>

                                        <div class="form-group col-md-12">
                                            <label>Address</label>
                                            <textarea name="address" class="form-control">{{ old('address', $property->address ?? '') }}</textarea>
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Year Built</label>
                                            <input type="text" name="year_of_built" class="form-control"
                                                value="{{ old('year_of_built', $property->year_of_built ?? '') }}" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Postal Code</label>
                                            <input type="text" name="postal_code" class="form-control"
                                                value="{{ old('postal_code', $property->postal_code ?? '') }}" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Property Sqft</label>
                                            <input type="text" name="property_sqft" class="form-control"
                                                value="{{ old('property_sqft', $property->property_sqft ?? '') }}" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label>Land Sqft</label>
                                            <input type="text" name="land_sqft" class="form-control"
                                                value="{{ old('land_sqft', $property->land_sqft ?? '') }}" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        </div>

                                        <div class="form-group col-md-3">
                                            <label>How Many Bedrooms</label>
                                            <input type="number" name="bedroom_count" class="form-control" value="{{ old('bedroom_count', $property->bedroom_count ?? '') }}">
                                        </div>

                                        <div class="form-group col-md-3">
                                            <label>How Many Bathrooms</label>
                                            <input type="number" name="bathroom_count" class="form-control" value="{{ old('bathroom_count', $property->bathroom_count ?? '') }}">
                                        </div>

                                        <div class="form-group col-md-3">
                                            <label>How Many Kitchens</label>
                                            <input type="number" name="kitchen_coint" class="form-control" value="{{ old('kitchen_coint', $property->kitchen_coint ?? '') }}">
                                        </div>

                                        <div class="form-group col-md-3">
                                            <label>How Many Garage</label>
                                            <input type="number" name="garage_count" class="form-control" value="{{ old('garage_count', $property->garage_count ?? '') }}">
                                        </div>
                                        <div class="form-group col-md-12">
                                            <label>Video Link</label>
                                            <input type="text" name="video_link" class="form-control" value="{{ old('video_link', $property->video_link ?? '') }}">
                                        </div>

                                        <div class="form-group col-md-12">
                                            <label>Map Link</label>
                                            <input type="text" name="map_link" class="form-control" value="{{ old('map_link', $property->map_link ?? '') }}">
                                        </div>

                                        @php
                                            $nearby = [
                                                'school',
                                                'college',
                                                'grocery_center',
                                                'market',
                                                'hospital',
                                                'nursinghome',
                                                'rail_station',
                                                'metro_station',
                                                'main_road',
                                                'bus_stand',
                                                'sea',
                                                'river',
                                            ];
                                        @endphp

                                        @foreach ($nearby as $place)
                                            <div class="form-group col-md-6">
                                                <label>Near by {{ ucwords(str_replace('_', ' ', $place)) }} (KM / Meters)</label>
                                                <input type="text" name="near_by_{{ $place }}" class="form-control"
                                                    value="{{ old('near_by_'.$place, $property->{'near_by_'.$place} ?? '') }}">
                                            </div>
                                        @endforeach
                                        <div class="form-group col-md-12">
                                            <label>Details</label>

                                            <textarea class="form-control" id="editor1" name="details"><?php echo $property->details ?></textarea>
                                        </div>

                                        <div class="form-group col-md-12 text-right">
                                            <button type="submit" class="btn btn-success">
                                                {{ isset($property) ? 'Update' : 'Submit' }}
                                            </button>
                                        </div>
                                    </div>
                                </form>


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


                        </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>

</body>

</html>
