{#-
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-#}
{% set hide_left_bar = True %}
{% extends g.theme.master %}
{% import 'allura:templates/jinja_master/lib.html' as lib with context %}

{% block title %}{{importer.source}} Project Importer{% endblock %}
{% block header %}{{importer.source}} Project Importer{% endblock %}

{% block extra_css %}
    {{ super() }}
    <style type="text/css">
        #project-import-form #project-fields input {
            width: 88%;
        }

        .hidden { display: none; }
    </style>
{% endblock %}

{% block extra_js %}
    {{ super() }}
    <script type="text/javascript">
        var manual = false;
        function suggest_name() {
            var $project_shortname = $('#project_shortname');
            if (!manual) {
                $project_shortname.val($('#project_name').val().replace(/[^A-Za-z0-9]+/g, '-').toLowerCase());
            }
            $project_shortname.trigger('change');
        }

        function check_names() {
            var data = {};
            $('#project-import-form input').each(function() {
              data[$(this).attr('name')] = $(this).val();
            });
            $.getJSON('check_names', data, function(result) {
                $('#project_name_error').addClass('hidden');
                $('#project_shortname_error').addClass('hidden');
                for(var field in result) {
                    $('#'+field+'_error').text(result[field]).removeClass('hidden');
                }
            });
        }

        function update_url() {
            $('#url-fragment').text($('#project_shortname').val());
        }

        $(function() {
            $('#project_name').on('change keyup', suggest_name);
            $('#project_shortname').on('change keyup', function(event) {
                if (event.type == 'keyup') {
                    manual = true;
                }
                update_url();
            });
            $('#project_name').on('blur', check_names);
            $('#project_shortname').on('blur', check_names);
            if($('#project_url').length === 1) {
                $('#project_url').on('blur', check_names);
            }
        });
    </script>
{% endblock %}

{% block content %}
<form id="project-import-form" method="POST" action="process" enctype="multipart/form-data">
    <input type="hidden" id="neighborhood" name="neighborhood" value="{{importer.neighborhood.name}}"/>

    <fieldset id="project-fields">
      {% block project_fields %}
      <div class="grid-6" style="clear:left">
          <label>Project Name</label>
      </div>
      <div class="grid-10">
          <input id="project_name" name="project_name" value="{{request.validation.values['project_name']}}"/>
          <div id="project_name_error" class="error{% if not request.validation.errors['project_name'] %} hidden{% endif %}">
              {{request.validation.errors['project_name']}}
          </div>
      </div>

      <div class="grid-6" style="clear:left">
          <label>{{config.site_name}} URL Name</label>
      </div>
      <div class="grid-10">
          <input id="project_shortname" name="project_shortname" value="{{request.validation.values['project_shortname']}}"/>
          <div id="project_shortname_error" class="error{% if not request.validation.errors['project_shortname'] %} hidden{% endif %}">
              {{request.validation.errors['project_shortname']}}
          </div>
          <div id="project-url">
              http://{{request.environ['HTTP_HOST']}}{{importer.neighborhood.url()}}<span id="url-fragment">{{request.validation.values['project_shortname']}}</span>
          </div>
      </div>
      {% endblock %}
    </fieldset>

    <div id="tool-fields">
        {% if request.validation.errors['tools'] %}
        <div class="error">{{request.validation.errors['tools']}}</div>
        {% endif %}
        {% for name, tool_importer in importer.tool_importers.items() %}
        <div class="tool">
            <img src="{{ tool_importer.tool_icon(g.theme, 48) }}" alt="{{ tool_importer.tool_label }} icon">
            <label>
                <input name="tools" value="{{name}}" type="checkbox"{% if not request.validation.errors or name in request.validation.values['tools'] %} checked="checked"{% endif %}/>
                {{tool_importer.tool_label}}
            </label>
            {% for option_name, option_label in tool_importer.tool_option.items() %}
              <label>
                <input name="tool_option" value="{{option_name}}" type="checkbox"{% if not request.validation.errors or name in request.validation.values['tool_option'] %} checked="checked"{% endif %}/>
                {{option_label}}
              </label>
            {% endfor %}
            {{tool_importer.tool_description}}
        </div>
        {% endfor %}
    </div>

    <div style="margin:5px">
      You'll receive an email after each tool import finishes.
      Import time varies depending on the amount of data being imported.
      {% if config['doc.url.importers.{}'.format(importer.source) ] %}
          <br/><br/>
          For more information on what will be imported and post-import configuration,
          please see the <a href="{{ config['doc.url.importers.{}'.format(importer.source) ] }}">{{importer.source}} import documentation</a>.
      {% endif %}
    </div>

    <input type="submit" value="Import"/>
    {{lib.csrf_token()}}
</form>
{% endblock %}
