{% extends "waliki/base.html" %}
{% load i18n %}
{% load waliki_tags %}


{% block extra_head %}
  {{ block.super }}
 {% entry_point 'detail_extra_head' %}
{% endblock %}



{% block header %}
    {% entry_point 'detail_pre_header' %}

    <div class="pull-right">
    {% entry_point 'detail_actions' %}

    {% block actions %}
        <div class="btn-group">

            {% block main_action %}

            <a href="{% if page %}{% url 'waliki_edit' slug=page.slug|default:slug %}{% else %}#{% endif %}" class="btn btn-default {% if not page %}disabled{% endif %}">{% trans "Edit" %}</a>


            {% endblock main_action %}
            <button class="btn dropdown-toggle btn-default" data-toggle="dropdown">
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
            <!-- dropdown menu links -->

            {% if page %}
                {% extra_page_actions page %}

                <li><a href="{% url "waliki_detail_raw" page.slug %}">{% trans "Raw" %}</a></li>


                {% check_perms "delete_page" for request.user in page.slug as "can_delete" %}
                {% if can_delete %}
                <li class="divider"></li>
                <li><a id="btn-waliki_delete" class="text-error" href="{% url 'waliki_delete' page.slug %}">{% trans "Delete" %}</a></li>
                {% endif %}

            {% endif %}
            </ul>
        </div>
    {% endblock actions %}
    </div>
    {% block page_title %}<h1>{{ page.title|default:'' }}</h1>{% endblock page_title %}
{% endblock header %}

{% block content %}
    {% entry_point 'detail_pre_content' %}

    {% if page %}
        {{ page.body|safe }}
    {% else %}
        <p>{% trans "This page doesn't exist yet." %}</p>
        <p><form action="{% url 'waliki_edit' slug=page.slug|default:slug %}" method="post">{% csrf_token %}<button type="submit" class="btn btn-success">{% trans "Create it" %}</button></form></p>
    {% endif %}


    {% entry_point 'detail_post_content' %}


{% endblock content %}

{% block extra_content %}
    {% if page %}
    <div id="delete-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <form id="delete-form" method="post" class="form" action="{% url 'waliki_delete' slug=page.slug %}">

          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">{% trans "Close" %}</span></button>
            <h4 class="modal-title">{% trans "Delete" %}</h4>
          </div>
          <div class="modal-body">

          </div>
          <div class="modal-footer">
           <input id='btn-delete-submit' class="btn btn-danger" value="{% trans "Delete" %}" type="submit">

            <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Cancel" %}</button>
          </div>
          </form>
        </div>
      </div>
    </div>
    {% endif %}


<div id="new-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <form id="new-form" method="post" class="form" action="{% url 'waliki_new' %}">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">{% trans "Create a new page" %}</span></button>
        <h4 class="modal-title" id="myModalLabel">{% trans "New page" %}</h4>
      </div>
      <div class="modal-body">

      </div>
      <div class="modal-footer">
       <input id='btn-new-submit' data-slug-url="{% url 'waliki_get_slug' %}" class="btn btn-success" value="{% trans "Create page" %}" type="submit">
      </div>
      </form>
    </div>
  </div>
</div>


{% endblock extra_content %}


{% block extra_script %}
    {% if WALIKI_USE_MATHJAX %}
      <script type="text/javascript"
        src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
      </script>
    {% endif %}

    {% if page %}
      <script type="text/javascript">
        $('#btn-waliki_delete').on('click', function(e) {

                e.preventDefault();
                var url = $(this).attr('href');
                console.log(url);
                $.get(url, function(data){
                    console.log(data);
                    $('#delete-modal .modal-body').empty().append(data.data);
                    $('#delete-modal').modal('show');
                });
        });

        $("#delete-form").submit(function(e) {
            e.preventDefault();
            var url = $(this).attr('action');
            $.post(url, $(this).serialize(), function(data){
                  if (data.redirect !== undefined){
                    window.location = data.redirect;
                  }else {
                      $('#delete-modal .modal-body').empty().append(data.data);
                      $('#delete-modal').modal('show');
                  }
                });
        });
      </script>
    {% endif %}
{% endblock extra_script %}