@extends("admin.admin_app")

@section("content")

  <div class="content-page">
      <div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-lg-12">
              <div class="card-box">
                 <div class="row">
                  <div class="col-sm-6">
                      <a href="{{ URL::to('admin/property') }}"><h4 class="header-title m-t-0 m-b-30 text-primary pull-left" style="font-size: 20px;"><i class="fa fa-arrow-left"></i> Back to Properties</h4></a>
                  </div>
                </div>

                {{ html()->form('POST', url('/admin/property/generate'))
                     ->attributes(['class' => 'form-horizontal', 'id' => 'generate_form', 'name' => 'generate_form', 'role' => 'form', 'enctype' => 'multipart/form-data'])->open() }}

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">How many properties?</label>
                    <div class="col-sm-3">
                      <input type="number" name="count" value="{{ old('count', 10) }}" class="form-control" min="1" max="200" step="1">
                    </div>
                  </div>

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Property Type</label>
                    <div class="col-sm-3">
                      <select class="form-control select2" name="type_id" id="type_id">
                        <option value="">Select type</option>
                        @foreach($type_list as $type_data)
                          <option value="{{$type_data->id}}" @if(old('type_id')==$type_data->id) selected @endif>{{$type_data->type_name}}</option>
                        @endforeach
                      </select>
                    </div>

                    <label class="col-sm-2 col-form-label">Purpose</label>
                    <div class="col-sm-3">
                      <select class="form-control" name="purpose" id="purpose">
                        <option value="Sale" @if(old('purpose')=="Sale") selected @endif>Sale</option>
                        <option value="Rent" @if(old('purpose')=="Rent") selected @endif>Rent</option>
                      </select>
                    </div>
                  </div>

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Price Range</label>
                    <div class="col-sm-3">
                      <input type="number" name="price_min" value="{{ old('price_min', 100000) }}" class="form-control" min="1" step="1" placeholder="Min price">
                    </div>
                    <div class="col-sm-3">
                      <input type="number" name="price_max" value="{{ old('price_max', 500000) }}" class="form-control" min="1" step="1" placeholder="Max price">
                    </div>
                  </div>

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Country/Location</label>
                    <div class="col-sm-3">
                      <select class="form-control select2" name="location_id" id="location_id">
                        <option value="">Select location</option>
                        @foreach($location_list as $location_data)
                          <option value="{{$location_data->id}}" @if(old('location_id')==$location_data->id) selected @endif>{{$location_data->name}}</option>
                        @endforeach
                      </select>
                    </div>
                  </div>

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Images (used randomly)</label>
                    <div class="col-sm-8">
                      <textarea name="images" rows="5" class="form-control" placeholder="One path per line or comma-separated. Supports relative paths (upload/...) or full URLs.">{{ old('images', 'upload/placeholder_img.jpg') }}</textarea>
                      <small class="form-text text-muted">These images are used for featured, gallery, and floor plan images.</small>
                      <div class="mt-2" id="generator-image-uploads">
                        <input type="file" name="image_files[]" class="form-control mb-2" accept="image/*" multiple>
                      </div>
                      <button type="button" class="btn btn-light btn-sm" id="add-generator-image">Add another image</button>
                      <small class="form-text text-muted">Upload multiple images to use randomly (optional). You can select multiple files at once or add more inputs.</small>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="offset-sm-3 col-sm-9 pl-1">
                      <button type="submit" class="btn btn-primary waves-effect waves-light">Generate</button>
                    </div>
                  </div>

                {{ html()->form()->close() }}
              </div>
            </div>
          </div>
        </div>
      </div>

      @include("admin.copyright")

    </div>

  <script type="text/javascript">
    'use strict';

    @if (count($errors) > 0)
      Swal.fire({
        icon: 'error',
        title: 'Oops...',
        html: '<p>@foreach ($errors->all() as $error) {{$error}}<br/> @endforeach</p>',
        showConfirmButton: true,
        confirmButtonColor: '#10c469',
        background:"#1a2234",
        color:"#fff"
      })
    @endif

    document.getElementById('add-generator-image').addEventListener('click', function () {
      var wrapper = document.getElementById('generator-image-uploads');
      var input = document.createElement('input');
      input.type = 'file';
      input.name = 'image_files[]';
      input.className = 'form-control mb-2';
      input.accept = 'image/*';
      wrapper.appendChild(input);
    });
  </script>

@endsection
