{% extends "documents/document_base.html" %}
{% load i18n %}

{% block title %} {% trans "Upload Document" %} - {{ block.super }} {% endblock %}

{% block body_class %}documents upload{% endblock body_class %}

{% block head %}
    {% include "geonode/ext_header.html" %}
    {% include "geonode/app_header.html" %}
    {{ block.super }}
{% endblock %}

{% block body %}
    <div class="block">
        <div class="span8">
          <h2 class="page-title">{% trans "Upload Documents" %}</h2>
            <div>
                <form id="upload_form"  method="post" enctype="multipart/form-data" action="{% url "document_upload" %}">
                    <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" />
                    {{ form.as_table }}
                    <div><br/><input class="btn" type="submit" value="Upload" /></div>
                </form>
            </div>
        </div>
    </div>
{% endblock %}

{% block sidebar %}

<h3>{% trans "Permissions"  %}</h3>
<div>
  <form id="permission_form">
        {% include "_permissions.html" %}
  </form>
</div>

{% endblock %}

{% block extra_script %}
{% with "none" as document %}
{% include "_permissions_form_js.html" %}
{% endwith %}

<script type="text/javascript">
    $('#id_doc_file').on('change', function(){
        if($('#id_title').val() == ''){
            $('#id_title').val($('#id_doc_file').val());
        }
    });
    $("#resource").select2({
        minimumInputLength: 1,
        placeholder: 'Select layer, map or empty',
        width: 'element',
        ajax: {
          url: '{% url "search_api" %}',
          data: function(term,page){
            return {
              type: 'layer,map',
              q: term.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '')
            }
          },
          results: function(data) {
            return {
              results: $.map(data.results,function(object){
                return {id: object.id, text: object.title, type: object._type};
              })
            }
          }          
        }
      });
    $('#resource').on('change',function(e){$('#resource_type').val($('#resource').select2('data').type)});
    $('#upload_form').submit(function(){
      $('#permissions').val(JSON.stringify(permissionsString($('#permission_form'),'documents')));
    });
</script>
{% endblock extra_script %}
