{% extends "minecraft_manager/base.html" %}
{% load static %}
{% load csrf_html %}
{% load sidebar %}
{% load template_settings %}
{% block bootstrap %}
    {% if user.usersettings.default_theme == 'DA' %}
        <link rel="stylesheet" href="{% static "minecraft_manager/css/bootswatch-darkly.css" %}">
    {% elif user.usersettings.default_theme == 'SO' %}
        <link rel="stylesheet" href="{% static "minecraft_manager/css/bootswatch-solar.css" %}">
    {% elif user.usersettings.default_theme == 'SL' %}
        <link rel="stylesheet" href="{% static "minecraft_manager/css/bootswatch-slate.css" %}">
    {% else %}
        <link rel="stylesheet" href="{% static "minecraft_manager/css/bootswatch-flatly.css" %}">
    {% endif %}
{% endblock bootstrap %}
{% block content %}
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          {% template_settings 'DASHBOARD_TITLE' as DASHBOARD_TITLE %}
          <a class="navbar-brand">{% if DASHBOARD_TITLE %}{{ DASHBOARD_TITLE }}{% else %}Minecraft Manager{% endif %}</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
          {% template_settings 'COREPROTECT_WEB_URL' as CPW %}
          {% template_settings 'COREPROTECT_ACTIVITY_URL' as CPA %}
            {% if CPW %}
                {% if perms.auth.coreprotect_activity and CPA %}
                <li class="dropdown">
                    <a class="dropdown-toggle " href="#" role="button" data-toggle="dropdown" id="cpDropdown">CoreProtect</a>
                    <ul class="dropdown-menu" aria-labelledby="cpDropdown">
                        <li><a href="{% url 'coreprotect' %}">Web GUI</a></li>
                        <li><a href="{% url 'activity' %}">Activity Monitor</a></li>
                    </ul>
                </li>
                {% elif perms.auth.coreprotect_partial or perms.auth.coreprotect_full %}
                    <li><a class="dropdown-item btn" href="{% url 'coreprotect' %}">CoreProtect</a></li>
                {% endif %}
            {% endif %}
            <li class="dropdown">
                <a class="dropdown-toggle " href="#" role="button" data-toggle="dropdown" id="accountDropdown">{{ user.username }}</a>
                <ul class="dropdown-menu" role="menu" aria-labelledby="accountDropdown">
                    <li><a style="cursor:pointer;" data-toggle="modal" data-target="#settingsModal">Settings</a></li>
                    <li><a style="cursor:pointer;" data-toggle="modal" data-target="#passwordModal">Change Password</a></li>
                    <li><a href="{% url "logout"  %}">Logout</a></li>
                </ul>
            </li>
          </ul>
          <form method="GET" action="{% url 'player' %}" class="navbar-form navbar-right">
            <input type="text" class="form-control" name="search" placeholder="Player Search..." value="{% if 'search' in request.GET %}{{ request.GET.search }}{% endif %}">
            <button class="btn btn-default" type="submit">Search</button>
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            {% autoescape off %}{% get_sidebar current_app|safe request %}{% endautoescape %}
          </ul>
        </div>
        <div class="col-sm-9 col-md-10 main">
          <h1 class="page-header">{{ current_app|capfirst }} {% block after_h1 %}{% endblock %}</h1>
          <div id="alert" class="popup"></div>
          {% block section %}{% endblock section %}
        </div>
      </div>
    </div>
    <div id="settingsModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">User Settings</h4>
              </div>
              <form id="settingsForm" method="POST" action="{% url 'api-web' keyword='settings' %}">{% autoescape off %}{% get_csrf_html request %}{% endautoescape %}
              <div class="modal-body">
                    {% get_form 'usersettings' request %}
                    {% if user.is_staff %}
                        <br/><a target="_blank" href="{% url "admin:index" %}">Admin Site</a>
                    {% endif %}
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  <button type="button" class="btn btn-primary" onclick="saveSettings();">Save</button>
              </div>
              </form>
            </div>

        </div>
    </div>
    <div id="passwordModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Change Password</h4>
              </div>
              <form id="passwordForm" method="POST" action="{% url 'api-web' keyword='password' %}">{% autoescape off %}{% get_csrf_html request %}{% endautoescape %}
              <div id="passwordDiv" class="modal-body">
                    {% get_form 'password' request %}
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  <button type="button" class="btn btn-primary" onclick="savePassword();">Save</button>
              </div>
              </form>
            </div>

        </div>
    </div>
    <script>

        $('.table.link-table > tbody > tr').click(function() {
            var base_url = $(location).attr('href').split("?")[0];
            var id = $(this).attr('data-id');
            var url = $(this).attr('data-url');
            if (url != null && id != null) {
                if (url.endsWith("/")) {
                    window.location.href = url + id
                } else {
                    window.location.href = url + "/" + id
                }
            } else if (id != null) {
                if (base_url.endsWith("/")) {
                    window.location.href = base_url + id
                } else {
                    window.location.href = base_url + "/" + id
                }
            }

        });

        function showAlert(message, flavor) {
            var $alert = $("#alert");
            $alert.hide();
            $alert.html(message);
            $alert.removeClass();
            $alert.addClass("popup alert alert-" + flavor);
            $alert.fadeIn().delay(1000).fadeOut();
        }

        function saveSettings() {
            var form = $("#settingsForm").serialize();
            form = form.replace("search_player_ip=on", "search_player_ip=True");
            if (!form.includes("search_player_ip")) {
                form += "&search_player_ip=False";
            }
            form = form.replace("show_timestamp_chat=on", "show_timestamp_chat=True");
			if (!form.includes("show_timestamp_chat")) {
                form += "&show_timestamp_chat=False";
            }
            $.ajax({
                type: 'POST',
                url: '{% url 'api-web' keyword='settings' %}',
                data: form,
                success: function() {
                    location.reload()
                }
            });
        }

        function savePassword() {
            var form = $("#passwordForm").serialize();
            $.ajax({
                type: 'POST',
                url: '{% url 'api-web' keyword='password' %}',
                data: form,
                success: function(data) {
                    if (data === "success") {
                        alert("Password Changed!");
                        location.reload();
                    } else {
                        $("#passwordDiv").html(data);
                    }
                }
            });
        }

        $(document).ready(function() {
            $(".paginate_button").each(function() {
                $(this).addClass("btn");
            });

            $("#model-table").on('draw.dt', function() {
                $(".paginate_button").each(function() {
                    $(this).addClass("btn");
                });
            });
        });

    </script>
{% endblock content %}
