{% load gadjo %}
<form class="quixote" method="POST" action="{{ site_base }}{% url 'combo-public-ajax-page-cell' page_pk=cell.page.id cell_reference=cell.get_reference %}">
  {% csrf_token %}
  <input type="hidden" name="action" value="create"/>
  <div class="grid-1-1">
    <label for="new_place_nom">Nom du lieu <span title="Ce champ est obligatoire." class="required">*</span></label>
    <input type="text" required id="new_place_nom" name="nom" placeholder="ex: mon domicile, mon travail"
      {% if prefill and userdata.address_street %}value="Mon domicile"{% endif %}>
  </div>
  <div class="grid-1-1">
    <label for="new_place_adresse">Adresse <span title="Ce champ est obligatoire." class="required">*</span></label>
    <input type="text" required id="new_place_adresse" name="adresse" {% if prefill and userdata.address_street %}value="{{userdata.address_number}} {{userdata.address_street}}"{% endif %}>
  </div>
  <div class="newline grid-1-4">
    <label for="new_place_codepostal">Code postal <span title="Ce champ est obligatoire." class="required">*</span></label>
    <input type="text" required id="new_place_codepostal" name="codepostal" {% if prefill and userdata.address_zipcode %}value="{{userdata.address_zipcode}}"{% endif %}>
  </div>
  <div class="grid-3-4 ville">
    <label for="new_place_ville">Ville <span title="Ce champ est obligatoire." class="required">*</span></label>
    <input type="text" required id="new_place_ville" name="ville" {% if prefill and userdata.address_city %}value="{{userdata.address_city}}"{% endif %}>
  </div>
  <div class="newline buttons">
    <button>{{ button_label }}</button>
  </div>
</form>
<script>
$(function() {
  $('input[name=adresse]').autocomplete({
    source: function( request, response ) {
      $.ajax({
        url: '{{streets_url}}',
        dataType: 'jsonp',
        data: {
          q: request.term
        },
        success: function( data ) {
          response($.map(data.data, function(item) {
            return {label: item.text, value: item.label};
          }));
        }
      });
    },
    minLength: 3
  });
  $('input[name=ville]').autocomplete({
    source: function( request, response ) {
      $.ajax({
        url: '{{communes_url}}?case-insensitive=1',
        dataType: 'jsonp',
        data: {
          q: request.term
        },
        success: function( data ) {
          response($.map(data.data, function(item) {
            return {label: item.text, value: item.label};
          }));
        }
      });
    },
    minLength: 2
  });
});
</script>
