{% load i18n static richmin admin_urls %}
{% get_current_language as LANGUAGE_CODE %}
{% get_current_language_bidi as LANGUAGE_BIDI %}
{% get_richmin_settings request as richmin_settings %}
{% get_richmin_ui_tweaks as richmin_ui %}
{% get_selected_filters request as selected_filters %}

<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <!--  Prevent admin panel being crawled by search engines -->
  <meta name="robots" content="none, noarchive">

  <title>{% block title %}{{ title }} | {{ richmin_settings.site_title }}{% endblock %}</title>

  <!-- Font Awesome Icons -->
  <link rel="stylesheet" href="{% static "vendor/fontawesome-free/css/all.min.css" %}">

  <!-- Bootstrap and adminLTE -->
  <link rel="stylesheet" href="{% static "vendor/adminlte/css/adminlte.min.css" %}" id="adminlte-css">

  <!-- Bootswatch theme -->
  {% if richmin_ui.theme.name != 'default' %}
    <link rel="stylesheet" href="{{ richmin_ui.theme.src }}" id="richmin-theme"/>
  {% endif %}

  {% if richmin_ui.dark_mode_theme %}
    <link rel="stylesheet" href="{{ richmin_ui.dark_mode_theme.src }}" id="richmin-dark-mode-theme"
          media="(prefers-color-scheme: dark)"/>
  {% endif %}

  <!-- Custom fixes for django -->
  <link rel="stylesheet" href="{% static "richmin/css/main.css" %}">
  <link rel="stylesheet" href="{% static "richmin/css/dark-mode.css" %}">
  <script src="{% static "richmin/js/theme.js" %}"></script>

  {% if not is_popup and is_nav_sidebar_enabled %}
    <script src="{% static 'richmin/js/nav_sidebar.js' %}" defer></script>
  {% endif %}

  {% if LANGUAGE_BIDI %}
    <link rel="stylesheet" href="{% static "richmin/css/bootstrap-rtl.min.css" %}">
    <link rel="stylesheet" href="{% static "richmin/css/rtl.css" %}">
  {% else %}
    <link rel="stylesheet" href="{% static "richmin/css/ltr.css" %}">
  {% endif %}

  {% if richmin_settings.custom_css %}
    <!-- Custom CSS -->
    <link rel="stylesheet" href="{% static richmin_settings.custom_css %}">
  {% endif %}

  <!-- favicons -->
  <link rel="shortcut icon" href="{% static richmin_settings.site_icon %}" type="image/png">
  <link rel="icon" href="{% static richmin_settings.site_icon %}" sizes="32x32" type="image/png">

  {% if richmin_settings.use_google_fonts_cdn %}
    <!-- Google Font: Source Sans Pro -->
    <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
  {% endif %}

  <link rel="stylesheet" href="{% static 'admin/css/vendor/select2/select2.min.css' %}">

  {% block extrastyle %} {% endblock %}
  {% block extrahead %} {% endblock %}
</head>
<body
  class="hold-transition{% if not richmin_settings.show_sidebar %} no-sidebar{% else %} sidebar-mini{% endif %} {% sidebar_status request %} {% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %} {{ richmin_ui.body_classes }} {% if richmin_ui.dark_mode_theme %}theme-dark{% endif %}"
  data-admin-utc-offset="{% now "Z" %}">

<div class="wrapper">

  {% if not is_popup %}
    <nav class="main-header navbar navbar-expand {{ richmin_ui.navbar_classes }}" id="richy-navbar">
      <ul class="navbar-nav">

        {% if richmin_settings.show_sidebar %}
          <li class="nav-item">
            <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
          </li>
        {% else %}
          <li class="nav-item">
            <a href="{% url 'admin:index' %}" class="brand-link">
              <img src="{% static richmin_settings.site_logo %}" alt="{{ richmin_settings.site_header }} Logo"
                   class="{{ richmin_settings.site_logo_classes }} brand-image" style="opacity: .8; margin: 0 0 0 5px;">
            </a>
          </li>
        {% endif %}

        {% get_top_menu user request.current_app|default:"admin" as top_menu %}
        {% for link in top_menu %}
          <li class="nav-item d-none d-sm-inline-block{% if link.children %} dropdown{% endif %}">
            {% if link.children %}
              <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true"
                 aria-expanded="false">
                {{ link.name }}
              </a>
              <div class="dropdown-menu">
                {% for child in link.children %}
                  <a class="dropdown-item" href="{{ child.url }}"
                     {% if link.new_window %}target="_blank"{% endif %}>{{ child.name }}</a>
                {% endfor %}
              </div>
            {% else %}
              <a href="{{ link.url }}" class="nav-link"
                 {% if link.new_window %}target="_blank"{% endif %}>{{ link.name }}</a>
            {% endif %}
          </li>
        {% endfor %}
      </ul>

      {% if richmin_settings.search_model %}
        {% for search_model in richmin_settings.search_models_parsed %}
          <form action="{{ search_model.search_url }}" method="GET" class="form-inline ml-3">
            <div class="input-group input-group-sm">
              <input class="form-control form-control-navbar" name="q" type="search"
                     placeholder="{% trans 'Search' %} {{ search_model.search_name }}..."
                     aria-label="{% trans 'Search' %} {{ search_model.search_name }}...">
              <div class="input-group-append">
                <button class="btn btn-navbar" type="submit">
                  <i class="fas fa-search"></i>
                </button>
              </div>
            </div>
          </form>
        {% endfor %}
      {% endif %}

      {% if richmin_settings.filter_model %}
        {% for model_name, label_and_queryset in richmin_settings.filter_models_parsed.items %}
          <div class="form-inline ml-3">
            <div class="input-group input-group-sm">
              <select class="form-control search-filter global-filter-item" data-name="{{ model_name }}">
                <option value="">{{ label_and_queryset.0 }}</option>
                {% for obj in label_and_queryset.1 %}
                  {% with selected_key_id=selected_filters|get_item:model_name %}
                    <option value="{{ obj.id }}" {% if obj.id == selected_key_id|to_int %}selected{% endif %}>
                      {{ obj }}
                    </option>
                  {% endwith %}
                {% endfor %}
              </select>
            </div>
          </div>
        {% endfor %}
      {% endif %}


      <ul class="navbar-nav ml-auto">

        <li class="nav-item">
          <a class="nav-link theme-toggle" href="#" role="button">
            <div
              class="visually-hidden theme-label-when-auto">{% translate 'Toggle theme (current theme: auto)' %}</div>
            <div
              class="visually-hidden theme-label-when-light">{% translate 'Toggle theme (current theme: light)' %}</div>
            <div
              class="visually-hidden theme-label-when-dark">{% translate 'Toggle theme (current theme: dark)' %}</div>
            <svg aria-hidden="true" class="theme-icon-when-auto">
              <use xlink:href="#icon-auto"/>
            </svg>
            <svg aria-hidden="true" class="theme-icon-when-dark">
              <use xlink:href="#icon-moon"/>
            </svg>
            <svg aria-hidden="true" class="theme-icon-when-light">
              <use xlink:href="#icon-sun"/>
            </svg>
          </a>
        </li>
        {% if richmin_settings.show_ui_builder %}
          <li class="nav-item">
            <a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#" role="button">
              <i class="fas fa-th-large"></i>
            </a>
          </li>
        {% endif %}
        {% if 'django.contrib.admindocs'|app_is_installed %}
          <li class="nav-item">
            <a class="nav-link" href="{% url 'django-admindocs-docroot' %}" role="button">
              <i class="fas fa-book"></i>
            </a>
          </li>
        {% endif %}

        {% if richmin_settings.language_chooser %}
          {% get_available_languages as LANGUAGES %}
          {% get_language_info_list for LANGUAGES as languages %}

          <li class="nav-item dropdown">
            <a class="nav-link btn" data-toggle="dropdown" href="#" title="Choose language">
              <i class="fas fa-globe" aria-hidden="true"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-lg dropdown-menu-left" id="richy-languagemenu">
              <form action="{% url 'set_language' %}" method="post">
                {% csrf_token %}
                <input name="next" type="hidden" value="{{ request.get_full_path|remove_lang:LANGUAGE_CODE }}"/>
                {% for language in languages %}
                  <button
                    type="submit"
                    name="language"
                    value="{{ language.code }}"
                    class="dropdown-item {% if language.code == LANGUAGE_CODE %}active{% endif %}"
                    lang="{{ language.code }}"
                  >
                    {{ language.name_local|title }}
                  </button>
                {% endfor %}
              </form>
            </div>
          </li>
        {% endif %}

        <li class="nav-item dropdown">
          <a class="nav-link btn" data-toggle="dropdown" href="#" title="{{ request.user }}">
            <i class="far fa-user" aria-hidden="true"></i>
          </a>
          <div class="dropdown-menu dropdown-menu-lg dropdown-menu-left" id="richy-usermenu">
            <span class="dropdown-header">{% trans 'Account' %}</span>
            <div class="dropdown-divider"></div>
            <a href="{% url 'admin:password_change' %}" class="dropdown-item">
              <i class="fas fa-key mr-2"></i> {% trans 'Change password' %}
            </a>
            <div class="dropdown-divider"></div>
            <a href="{% url 'admin:logout' %}" class="dropdown-item">
              <i class="fas fa-users mr-2"></i> {% trans 'Log out' %}
            </a>
            {% get_user_menu user request.current_app|default:"admin" as user_menu %}
            {% for link in user_menu %}
              <div class="dropdown-divider"></div>
              <a href="{{ link.url }}" class="dropdown-item" {% if link.new_window %}target="_blank"{% endif %}>
                <i class="{{ link.icon }} mr-2"></i> {% trans link.name %}
              </a>
            {% endfor %}
            <div class="dropdown-divider"></div>
            {% if perms|can_view_self %}
              <a href="{% richy_admin_url request.user request.current_app|default:"admin" %}"
                 class="dropdown-item dropdown-footer">{% trans 'See Profile' %}</a>
            {% endif %}
          </div>
        </li>
      </ul>
    </nav>
    {% block sidebar %}
      {% if richmin_settings.show_sidebar %}
        {% get_side_menu as side_menu_list %}

        <aside class="main-sidebar elevation-4 {{ richmin_ui.sidebar_classes }}" id="richy-sidebar">
          <a href="{% url 'admin:index' %}" class="brand-link {{ richmin_ui.brand_classes }}" id="richy-logo">
            <img src="{% static richmin_settings.site_logo %}" alt="{{ richmin_settings.site_header }} Logo"
                 class="{{ richmin_settings.site_logo_classes }} brand-image elevation-3" style="opacity: .8">
            <span class="brand-text font-weight-light">{{ richmin_settings.site_brand }}</span>
          </a>

          <div class="sidebar">
            <div class="user-panel mt-3 pb-3 mb-3 d-flex">
              <div class="image">
                {% if richmin_settings|has_richmin_setting:"user_avatar" %}
                  <img src="{% get_user_avatar request.user %}" width="160px" class="img-circle elevation-2"
                       alt="User Image">
                {% else %}
                  <i class="fas fa-inverse user-profile fa-user-circle"></i>
                {% endif %}
              </div>
              <div class="info">
                {% if perms|can_view_self %}
                  <a href="{% richy_admin_url request.user request.current_app|default:"admin" %}"
                     class="d-block">{{ request.user }}</a>
                {% else %}
                  <span class="d-block" style="color: white;">{{ request.user }}</span>
                {% endif %}
              </div>
            </div>

            <nav class="mt-2">
              <ul class="nav nav-pills nav-sidebar flex-column {{ richmin_ui.sidebar_list_classes }}"
                  data-widget="treeview" role="menu" data-collapsible="false">

                {% if not is_popup and is_nav_sidebar_enabled %}
                  <li class="nav-item mb-3">
                    <div id="search-nav" class="input-search input-group input-group-sm">
                      <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fa fa-search"></i></span>
                      </div>
                      <input type="search" id="nav-filter" class="form-control" placeholder="{% trans 'Filter menu…' %}"
                             aria-label="Filter navigation items">
                    </div>
                  </li>
                {% endif %}

                <li class="nav-item">
                  <a href="{% url 'admin:index' %}" class="nav-link">
                    <i class="nav-icon fas fa-th-large"></i>
                    <p>{% trans 'Dashboard' %}</p>
                  </a>
                </li>

                {% if richmin_settings.navigation_expanded %}
                  {% for app in side_menu_list %}
                    <li class="nav-header">{{ app.name }}</li>
                    {% for model in app.models %}
                      <li class="nav-item">
                        {% if model.url %}
                          <a href="{{ model.url }}" class="nav-link">
                            <i class="nav-icon {{ model.icon }}"></i>
                            <p>{{ model.name }}</p>
                          </a>
                        {% else %}
                          <span class="nav-link disabled">
                                                <i class="nav-icon {{ model.icon }}"></i> <p>{{ model.name }}</p>
                                            </span>
                        {% endif %}
                      </li>
                    {% endfor %}
                  {% endfor %}
                {% else %}
                  {% for app in side_menu_list %}
                    <li class="nav-item has-treeview">
                      <a href="#" class="nav-link">
                        <i class="nav-icon {{ app.icon }}"></i>
                        <p>{{ app.name|truncatechars:21 }} <i class="fas fa-angle-left right"></i></p>
                      </a>
                      <ul class="nav nav-treeview" style="display: none;">
                        {% for model in app.models %}
                          <li class="nav-item">
                            <a href="{% if model.url %}{{ model.url }}{% else %}javascript:void(0){% endif %}"
                               class="nav-link">
                              <i class="nav-icon {{ model.icon }}"></i>
                              <p>{{ model.name }}</p>
                            </a>
                          </li>
                        {% endfor %}
                      </ul>
                    </li>
                  {% endfor %}
                {% endif %}
              </ul>
            </nav>
          </div>
        </aside>
      {% endif %}
    {% endblock %}
  {% endif %}

  <div class="content-wrapper" {% if is_popup %}style="margin-left:0px; padding-top: 20px;"{% endif %}>
    {% block page_content %}
      {% if not is_popup %}
        <div class="content border-bottom mb-2">
          <div class="container-fluid">
            <div class="row">
              <div class="col-12 col-md-auto d-flex flex-grow-1 align-items-center">
                <h1 class="h4 m-0 pr-3 mr-3 border-right">{% block content_title %}{% endblock %}</h1>
                {% block breadcrumbs %}{% endblock %}
              </div>
              {% block page_actions %}{% endblock %}
            </div>
          </div>
        </div>
      {% endif %}

      <div class="content">
        <div class="container-fluid">
          <section id="content" class="content">
            {% block messages %}
              {% for message in messages %}
                {% if message.tags == 'success' %}
                  <div class="alert alert-success alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
                    </button>
                    <i class="icon fa fa-check"></i>{{ message|capfirst }}
                  </div>
                {% elif message.tags == 'error' %}
                  <div class="alert alert-danger alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
                    </button>
                    <i class="icon fa fa-ban"></i>{{ message|capfirst }}
                  </div>
                {% elif message.tags == 'warning' %}
                  <div class="alert alert-warning alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
                    </button>
                    <i class="icon fa fa-exclamation-triangle"></i>{{ message|capfirst }}
                  </div>
                {% elif message.tags == 'info' %}
                  <div class="alert alert-info alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
                    </button>
                    <i class="icon fa fa-info"></i>{{ message|capfirst }}
                  </div>
                {% endif %}
              {% endfor %}
            {% endblock messages %}
            <div class="row">
              {% block content %} {% endblock %}
            </div>
          </section>
        </div>
      </div>


    {% endblock %}
  </div>

  {% block footer %}
    {% if not is_popup %}
      <footer class="main-footer {{ richmin_ui.footer_classes }}">
        <div class="float-right d-none d-sm-inline">
          <b>{% trans 'version' %}</b> {% get_richmin_version %}
        </div>
        {% autoescape off %}
          <strong>{% trans 'Copyright' %} &copy; {% now 'Y' %} {{ richmin_settings.copyright }}.</strong>
          {% trans 'All rights reserved.' %}
        {% endautoescape %}
      </footer>
      {% if richmin_settings.show_ui_builder %}
        {% include 'richmin/includes/ui_builder_panel.html' %}
      {% endif %}
    {% endif %}
  {% endblock %}

</div>

{% if richmin_settings.show_ui_builder %}
  <div id="codeBox" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">{% trans 'UI Configuration' %}</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">
          <p>{% trans 'Copy this info your settings file to persist these UI changes' %}</p>
          <pre><code></code></pre>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn {{ richmin_ui.button_classes.danger }}"
                  data-dismiss="modal">{% trans 'Close' %}</button>
        </div>
      </div>

    </div>
  </div>
{% endif %}

<!-- jQuery -->
<script src="{% static "admin/js/vendor/jquery/jquery.js" %}"></script>
<!-- Bootstrap 4 -->
<script src="{% static "vendor/bootstrap/js/bootstrap.min.js" %}"></script>
<!-- AdminLTE App -->
<script src="{% static "vendor/adminlte/js/adminlte.min.js" %}"></script>
<!-- Django customisations -->
<script src="{% static "richmin/js/main.js" %}"></script>
<script type="text/javascript" src="{% static 'admin/js/vendor/select2/select2.full.min.js' %}"></script>
<script>
  {% comment %} set filterInputLength default and custom values {% endcomment %}
  window.filterInputLengthDefault = 0;
  window.filterInputLength = {
    {% for k,v in cl.model_admin.filter_input_length.items %}
      '{{ k }}': {{ v }},
    {% endfor %}
  }
  window.enableCtrlSSave = {{ richmin_settings.enable_ctrl_s_save|yesno:'true,false' }};
</script>

{% block change_list_js %}
  <script type="text/javascript" src="{% static 'richmin/js/change_list.js' %}"></script>
{% endblock %}

{% if richmin_settings.custom_js %}
  <script src="{% static richmin_settings.custom_js %}"></script>
{% endif %}

{% if richmin_settings.show_ui_builder %}
  <script>
    window.ui_changes = {{ richmin_ui.raw|as_json|safe }};
  </script>
  <script src="{% static "richmin/js/ui-builder.js" %}"></script>
{% endif %}

{% block extrajs %}{% endblock %}

<!-- SVGs -->
<svg xmlns="http://www.w3.org/2000/svg" class="base-svgs">
  <symbol viewBox="0 0 24 24" width="1.3rem" height="1.3rem" id="icon-auto">
    <path d="M0 0h24v24H0z" fill="currentColor"/>
    <path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm0-2V4a8 8 0 1 0 0 16z"/>
  </symbol>
  <symbol viewBox="0 0 24 24" width="1.3rem" height="1.3rem" id="icon-moon">
    <path d="M0 0h24v24H0z" fill="currentColor"/>
    <path
      d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/>
  </symbol>
  <symbol viewBox="0 0 24 24" width="1.3rem" height="1.3rem" id="icon-sun">
    <path d="M0 0h24v24H0z" fill="currentColor"/>
    <path
      d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/>
  </symbol>
</svg>
<!-- END SVGs -->
</body>
</html>
