{% extends "myhome_base.html" %}
{% load seahub_tags i18n %}

{% block extra_style %}
{% include 'snippets/file_view_style.html' %}
<style type="text/css">
.CodeMirror { margin-bottom:0; }
</style>
{% endblock %}

{% block main_panel %}
    <h2 id="view-hd">{{ file_name }}</h2>
    <div id="file">
        <div id="file-op">
            <p class="fleft">
            {% if zipped %}
              {% trans "Current path: "%} 
              {% for name, link in zipped %}
                {% if not forloop.last %}
                <a href="{{ SITE_ROOT }}d/{{ token }}/?p={{ link|urlencode }}">{{ name }}</a> /               
                {% else %}
                {{ name }}
                {% endif %}
              {% endfor %}
            {% else %}
            {% trans "Shared by: " %}{{ shared_by|email2nickname }}
            {% endif %}
            </p>

            {% if request.user.is_authenticated and request.user.username != shared_by %}
              {% if save_to_link %}
              <button data="{{save_to_link}}" id="save">{% trans "Save to..."%}</button>
              {% endif %}
            {% endif %}
            <a href="{{ SITE_ROOT }}repo/{{ repo.id }}/{{ obj_id }}/?file_name={{ file_name|urlencode }}&op=download&t={{ shared_token }}&p={{path|urlencode}}" class="obv-btn">{% trans "Download" %} ({{file_size|filesizeformat}})</a>
        </div>
        {% include 'snippets/file_content_html.html' %}
    </div>


    <form id="file-save-form" action="{{save_to_link}}" method="post" class="file-choose-form hide">{% csrf_token %}
        <h3>{% trans "Save To:" %}</h3>
        <div class="dir-tree-cont">
            <div id="repos-dirs"></div>
        </div>
        <input type="hidden" name="s_token" value="{{shared_token}}" />
        <input type="hidden" name="dst_repo" value="" />
        <input type="hidden" name="dst_path" value="" />
        <p class="error hide">{% trans "Please click and choose a directory."%}</p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
        <button class="simplemodal-close">{% trans "Cancel"%}</button>
    </form>
    
{% endblock %}

{% block extra_script %}
{% include "snippets/file_view_js.html" %}
<script type="text/javascript">
var all_repos = [];
{% for a_repo in accessible_repos %}
    all_repos.push({
        'data': '{{ a_repo.props.name }}',
        'attr': {'repo_id': '{{ a_repo.props.id }}', 'root_node': true},
        'state': 'closed'
    });  
{% endfor %}

$('#save').click(function() {
    var form = $('#file-save-form'),
        file_tree = new FileTree();
    form.modal({appendTo:'#main', autoResize:true, focus:false});
    file_tree.renderDirTree($('#repos-dirs').data('site_root', '{{SITE_ROOT}}'), form, all_repos);
});
$('#file-save-form').submit(function() {
    var form = $(this),
        dst_repo = $('[name="dst_repo"]', form).val(),
        dst_path = $('[name="dst_path"]', form).val();

    if (!$.trim(dst_repo) || !$.trim(dst_path)) {
        $('.error', form).removeClass('hide');
        return false;
    }
});
{% include "snippets/file_content_js.html" %}
</script>
{% endblock %}
