{% extends 'group/group_base.html' %}

{% load seahub_tags i18n %}
{% load url from future %}

{% block cur_wiki %}tab-cur{% endblock %}

{% block left_panel %}
    {{ block.super }}
    {% if wiki_exists %}
    <div class="wiki-index grp-wiki-index">
        {% if not wiki_index_exists %}
        <h3 class="hd">{% trans "Wiki Index" %}</h3>
        <p>{% trans "No index now." %}</p>
        {% if repo_perm == 'rw' %}
        <button id="create-index">{% trans "Create" %}</button>
        {% endif %}
        {% else %}
        <div class="hd ovhd">
            <h3 class="fleft">{% trans "Wiki Index" %}</h3>
            {% if repo_perm == 'rw' %}
            <a id="edit-index" class="wiki-index-edit-icon op-icon icon-pencil fright" title="{% trans "Edit" %}" aria    -label="{% trans "Edit" %}" href="#"></a>
            {% endif %}
        </div>
        <div id="wiki-index-con"></div>
        {% endif %}
    </div>
    {% endif %}
{% endblock %}

{% block right_panel %}

    {% if not wiki_exists %}
    <div class="empty-tips">
        <h2 class="alc">{% trans "This group currently has no wiki" %}</h2>
        <p>{% trans "Seafile Wiki enables you to organize your knowledge in a simple way. The contents of wiki is stored in a normal library with pre-defined file/folder structure. This enables you to edit your wiki in your desktop and then sync back to the server." %}</p>
        {% if is_staff %}
        <a id="wiki-create" href="#">{% trans "Create Wiki Now" %}</a>
        {% if group_repos %}
        <br /><a id="wiki-use-lib" href="#">{% trans "Choose a library as wiki" %}</a>
        {% endif %}
        {% endif %}
    </div>
    <form id="wiki-create-form" action="" method="post" class="hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "Create Wiki"%}</h3>
        <label for="wiki-name">{% trans "Name"%}</label><br/>
        <input type="text" name="repo_name" value="{{group.group_name}}-wiki" maxlength="{{max_file_name}}" class="input" id="wiki-name" /><br />
        <label for="wiki-desc">{% trans "Description"%}</label><br/>
        <textarea name="repo_desc" class="textarea" id="wiki-desc">{% trans 'Wiki Pages' %}</textarea><br/>
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
    </form>

    <form id="wiki-use-lib-form" action="{% url 'group_wiki_use_lib' group.id %}" method="post" class="file-choose-form hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "Choose a library as wiki:" %}</h3>
        <div class="dir-tree-cont">
          <div id="repos-dirs"></div>
        </div>
        <input type="hidden" name="dst_repo" value="" />
        <p class="error hide">{% trans "Please click and choose a library."%}</p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
        <button class="simplemodal-close">{% trans "Cancel"%}</button>
    </form>

    {% else %}
    <div class="wiki-top ovhd">
        <ul class="wiki-nav fleft">
            <li class="item{% if page == 'home' %} cur-item{% endif %}"><a href="{% url 'group_wiki' group.id %}" class="link">{% trans "Home" %}</a></li>
            <li class="item"><a href="{% url 'group_wiki_pages' group.id %}" class="link">{% trans "Pages" %}</a></li>
            {% if group.view_perm != "pub" %}
            <li class="item"><a href="{% url 'repo_history' repo_id %}" class="link" id="wiki-history">{% trans "Wiki History" %}</a></li>
            {% endif %}
        </ul>

        {% if repo_perm == 'rw' %}
        <button id="page-create" class="btn-white">{% trans "New Page" %}</button>
        <button id="page-delete" class="btn-white" data-url="{% url 'group_wiki_page_delete' group.id page %}">{% trans "Delete Page" %}</button>
        <a class="sf-btn-link btn-white" href="{% url 'group_wiki_page_edit' group.id page %}">{% trans "Edit Page" %}</a>
        <a class="sf-btn-link btn-white" href="{% url 'file_revisions' repo_id %}?p={{path|urlencode}}" id="page-history">{% trans "Page History" %}</a>
        {% endif %}
    </div>

    <div id="wiki-area" class="article">
        <h1 class="bold">{{ page|capfirst }}</h1>
        <div id="wiki-content"></div><!--content will be offered by js-->
        <p id="wiki-last-modified">{% blocktrans with modifier=latest_contributor|email2nickname modify_time=last_modified|translate_seahub_time %}Last modified by {{modifier}}, {{modify_time}}{% endblocktrans %}</p>
    </div>

    <form id="page-create-form" action="{% url 'group_wiki_page_new' group.id %}" method="post" class="hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "New Page"%}</h3>
        <label for="page-name">{% trans "Name"%}</label><br/>
        <input id="page-name" type="text" name="page_name" value="" maxlength="{{max_file_name}}" class="input" /><br />
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
    </form>
    {% endif %}

{% endblock %}

{% block extra_script %}{{block.super}}
<script type="text/javascript" src="{{MEDIA_URL}}js/editormd/lib/marked.min.js"></script>
<script type="text/javascript">
{% if wiki_exists %}
var renderer = new marked.Renderer();
// remove 'id' attribute for headings
renderer.heading = function (text, level) {
    return '<h' + level + '>' + text + '</h' + level + '>';
};
marked.setOptions({
    renderer: renderer,
    breaks: true, // keep linebreak
    smartLists: true,
    sanitize: true // Ignore any HTML that has been input
});
$('#wiki-content')
.html(addAnchorsToHeaders(marked('{{ content|escapejs }}')))
.find('h1, h2').hover(
    function() {
        $('.anchor', $(this)).removeClass('vh');
    },
    function() {
        $('.anchor', $(this)).addClass('vh');
    }
);

$('#page-create').click(function() {
    $('#page-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
});
$('#page-create-form').submit(function() {
    var page_name = $.trim($('[name="page_name"]', $(this)).val());
    if (!page_name) {
        return false;
    }
});

addConfirmTo($('#page-delete'), {
    'title': "{% trans "Delete Page" %}",
    'con': "{% trans "Are you sure you want to delete this page?" %}"
});
$('a.wiki-page-missing').click(function() {
    $('#page-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
    $('#page-name').val($(this).text());
    return false;
});

{% else %}
$('#wiki-create').click(function() {
    $('#wiki-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
    return false;
});
$('#wiki-create-form').submit(function () {
    var form = $(this),
        form_id = form.attr('id'),
        repo_name = $('[name="repo_name"]', form).val(),
        repo_desc = $('[name="repo_desc"]', form).val();
    if (!$.trim(repo_name)) {
         apply_form_error(form_id, "{% trans "Name is required." %}");
         return false;
    }
    if (!$.trim(repo_desc)) {
         apply_form_error(form_id, "{% trans "Description is required." %}");
         return false;
    }

    var submit_btn = $('[type="submit"]', form);
    disable(submit_btn);
    $.ajax({
        url: '{% url 'group_wiki_create' group.id %}',
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {
            'repo_name': repo_name,
            'repo_desc': repo_desc
        },
        success: function(data) {
            location.href = data['href'];
        },
        error: function(data, textStatus, jqXHR) {
            var errors = $.parseJSON(data.responseText);
            $.each(errors, function(index, value) {
                apply_form_error(form_id, value);
            });
            enable(submit_btn);
        }
    });

    return false;
});

{% if group_repos %}
var all_repos = [];
{% for a_repo in group_repos %}
    all_repos.push({
        'text': HTMLescape('{{ a_repo.props.name|escapejs }}'),
        'data': {
            'repo_id': '{{ a_repo.props.id }}',
            'path': '/'
        }
    });
{% endfor %}
$('#wiki-use-lib').click(function() {
    var $form = $('#wiki-use-lib-form');
    $form.modal({appendTo:'#main', autoResize:true, focus:false});
    $('#simplemodal-container').css({'width':'auto', 'height':'auto'});
    FileTree.renderDirTree($('#repos-dirs').data('site_root', '{{SITE_ROOT}}'), $form, all_repos);
    return false;
});
$('#wiki-use-lib-form').submit(function() {
    var dst_repo = $('[name="dst_repo"]', $(this)).val();
    if (!$.trim(dst_repo)) {
        $('.error', $(this)).removeClass('hide');
        return false;
    }
});
{% endif %}

{% endif %}                     // END wiki_exists

{% if wiki_exists %}
{% if wiki_index_exists %}
$('#wiki-index-con').html(marked('{{ index_content|escapejs }}'));
    {% if repo_perm == 'rw' %}
        {% with page_name='index' %}
    $('#edit-index').attr('href', "{% url 'group_wiki_page_edit' group.id page_name %}");
        {% endwith %}
    {% endif %}
{% else %}
    {% if repo_perm == 'rw' %}
    $('#create-index').click(function() {
        var form = $('#page-create-form');
        $('[name="page_name"]', form).val('index');
        form.submit();
    });
    {% endif %}
{% endif %}
{% endif %}

// for 'go back'
var $pageHistory = $('#page-history');
$pageHistory.attr('href', $pageHistory.attr('href') + '&referer=' + encodeURIComponent(location.href));
var $wikiHistory = $('#wiki-history');
$wikiHistory.attr('href', $wikiHistory.attr('href') + '?referer=' + encodeURIComponent(location.href));
</script>
{% endblock %}
