{#-
       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 page = "new_projects" %}
{% set hide_left_bar = True %}

{% extends 'allura:templates/site_admin.html' %}

{% block title %}New Projects{% endblock %}
{% block header %}New Projects{% endblock %}

{% macro _paging() %}
<div class="paging">
    <a class="newer" title="Newer" href="{{ newer_url }}">&lt;</a>&nbsp;&hellip;&nbsp;
    <a class="newer" title="Older" href="{{ older_url }}">&gt;</a>
</div>
{% endmacro %}

{% block content %}
  <div class="grid-24">
  <form method="GET">
    <div class="grid-13">
    <label for="start-dt">From: </label><input type="text" name="start-dt" id="start-dt" value="{{ window_start.strftime('%Y/%m/%d %H:%M:%S') }}">
    <label for="end-dt">To: </label><input type="text" name="end-dt" id="end-dt" value="{{ window_end.strftime('%Y/%m/%d %H:%M:%S') }}">
    </div>
    <div class="grid-3"><input type="submit" value="Filter"></div>
  </form>
  </div>
  {{ _paging() }}
  <div style="clear:both;"></div>
  <table id="new_projects">
    <thead>
      <tr>
        <th class="narrow"></th>
        <th>Created</th>
        <th>Shortname</th>
        <th>Name</th>
        <th>Short description</th>
        <th>Summary</th>
        <th>Homepage</th>
        <th>Admins</th>
      </tr>
    </thead>
    {% for p in projects %}
    <tr>
      <td class="narrow">
        <input type="checkbox" class="js-select-project" data-shortname="{{ p.shortname }}" data-url="{{ p.url() }}" />
      </td>
      <td class="narrow">
        <small>{{ p._id.generation_time.strftime('%Y-%m-%d %H:%M:%S') }}</small>
      </td>
      <td><small>{{ p.shortname }}</small></td>
      <td><small><a href="{{ p.url() }}">{{ p.name }}</a></small></td>
      <td><small>{{ p.short_description|truncate(100) }}</small></td>
      <td><small>{{ p.summary }}</small></td>
      <td><small>{{ p.external_homepage|urlize(22) }}</small></td>
      <td>{% for a in p.admins() %}<small><a href="/nf/admin/user/{{ a.username }}">{{ a.username }}</a></small> {% endfor %}</td>
    </tr>
    <tr class="tablesorter-childRow">
        <td colspan="8" class="child-row">
            {% set note = p.get_tool_data('notes', 'note') %}
            <a class="add-note" title="click to add a not for this project" data-project="{{ p.shortname }}" href="#"><i class="fa fa-edit"></i> {% if note %}edit note{% else %}add note{%endif %}</a>
            <br>
            {% if note %}
                <strong>Notes:</strong> <span class="project-note">{{ note }}</span>
            {% endif %}
        </td>
    </tr>
    {% endfor %}
  </table>
  {{ _paging() }}
  <div id="selected-projects"></div>
  <div id="delete-projects-url" class="grid-19"></div>
{% endblock %}

{% block extra_css %}
<style type="text/css">
    .paging {
        float: right;
        margin: 1em;
    }
    .pad table {
      width: 915px;
    }
    td {
        max-width: 120px;
    }
    th.tablesorter-header {
        background-image: url("{{g.forge_static('images/bg.gif')}}");
        cursor: pointer;
        font-weight: bold;
        background-repeat: no-repeat;
        background-position: center left;
        padding-left: 20px;
    }
    th.tablesorter-headerAsc{
        background-image: url("{{g.forge_static('images/asc.gif')}}");
        background-repeat: no-repeat;
        background-position: 100% 50%;
        background-color: #6bb3fd;
    }
    th.tablesorter-headerDesc{
        background-image: url("{{g.forge_static('images/desc.gif')}}");
        background-repeat: no-repeat;
        background-position: 100% 50%;
        background-color: #6bb3fd;
    }
    td.child-row{ text-align:center}
    .note-buttons{
        display: flex;
        justify-content: center;
    }
</style>
{% endblock %}

{% block extra_js %}
  <script type="text/javascript" src="{{g.forge_static('js/site_admin_new_projects.js')}}"></script>
  <script type="text/javascript" src="{{g.forge_static('js/jquery.tablesorter.js')}}"></script>
  <script type="text/JavaScript">$(document).ready(function() {
      $(function(){
        $('#new_projects').tablesorter({
            usNumberFormat : false,
            sortReset      : false,
            sortRestart    : false
        });
    });
    $('.add-note').each(function(el){
        var el = $(this);
        el.on('click', function(evt){
            evt.preventDefault();
            var project_shortname = el.data('project');
            var form_exists = el.closest('tr').find('td:first').find('form');
            var notes = el.closest('tr').find('td:first').find('.project-note')
           if (form_exists.length > 0) {
               return;
           }
            var form = $('<form></form>')
            var textarea = $('<textarea class="note" rows="5" cols="100"></textarea>');
            var submit_btn = $('<button type="submit" class="button blue right">Submit</button>');
            var cancel_btn = $('<button class="" >Cancel</button>');
            var div = $('<div class="note-buttons"></div>')
            if (notes){
                textarea.val(notes.text());
            }
            form.append(textarea)
            div.append(submit_btn);
            div.append(cancel_btn);
            form.append(div);
            el.closest('tr').find('td:first').append(form)
            form.on('submit', function(evt){
                evt.preventDefault();
                var url = "{{ request.path.replace('new_projects', 'save_project_note')}}";
                var data = {'_csrf_token': $.cookie('_csrf_token'), 'note': textarea.val(), 'shortname': project_shortname}
                $.post(url,data).done(function(response){
                    flash(response.message, 'success');
                    form.remove();
                    var td = el.closest('tr').find('td:first');
                    td.html('<p><strong>Notes:</strong>' + escape_html(textarea.val()) + '</p>');
                    el.remove();
                }).fail(function (response){
                    flash(response.message, 'error');
                })
            })
            cancel_btn.on('click', function(evt){
                evt.preventDefault();
                form.remove();
            })
        })
    })
  } );</script>
{% endblock %}
