{% extends "base.html" %}
{% load freeadmin %}

{% block menubar %}
<div data-dojo-type="dijit.MenuBar" id="navMenu">
  <div class="menu_icon" data-dojo-type="dijit.MenuBarItem">
  <p style="font-size: 200%; margin-left: 10px;">Standby Node</p>
  </div>

<div id="failover_menuitem" data-dojo-type="dijit.MenuBarItem" onClick="" class="menu_icon right">
  <div data-dojo-type="dijit.layout.ContentPane" id="failover_status" class="failover_{% if status == "MASTER" %}ok{% elif status == "BACKUP" %}passive{% elif status == "IMPORTING" %}importing{% elif status == "ELECTING" %}electing{% elif status == "ERROR" %}error{% else %}passive{% endif %}"></div>
  <span><span id="failover_status_name" status="{{ status }}">{% if status == "MASTER" %}Active{% elif status == "BACKUP" %}Standby{% else %}{{ status }}{% endif %}</span></span>
</div>

</div>
{% endblock %}

{% block dojango_header_extra %}
<script type="text/javascript" src="{{ STATIC_URL }}lib/js/failover.js"></script>
<link rel="stylesheet" href="{{ STATIC_URL }}css/failover.css" type="text/css" />
{% endblock %}

{% block dojango_content %}
<script type="text/javascript">
    require([
        "dojo/ready",
        "dijit/registry",
        "dojo/parser",
        "dojo/fx/easing",
        "dijit/_base/manager",
        "dijit/form/TextBox",
        "dijit/layout/BorderContainer",
        "dijit/layout/ContentPane",
        "dijit/layout/TabContainer",
        "dijit/Dialog",
        "dijit/MenuBar",
        "dijit/MenuBarItem"
        ], function(ready, registry) {

        ready(function() {
          checkFailoverStatus();
        });
    });
</script>
<div data-dojo-type="dijit.layout.ContentPane" style="width:100%; border-top: 1px solid #eee;" data-dojo-props="doLayout: true, region: 'center'">

<div style="margin: 0 auto; width: 500px; text-align: center;">
{% if ips %}
<h2>Active IP: {% for ip in ips %}{{ ip }}{% if not forloop.last %}, {% endif %}{% endfor %}</h2>
{% endif %}

{% if failover.disabled %}
<div data-dojo-type="dijit.form.Button">
Takeover
<script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
dijit.byId("takeover_dialog").show();
</script>
</div>

<div data-dojo-type="dijit/Dialog" data-dojo-props="title: 'Takeover'" id="takeover_dialog">

  <form data-dojo-type="dijit/form/Form">
    <div data-dojo-type="dijit/layout/ContentPane" id="takeover_error" style="color: red;"></div>
    {% admin_form form %}

    <div data-dojo-type="dijit.form.Button">
    OK
      <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">

        var me = this;
        var cancel = dijit.byId("takeover_cancel");
        require([
        "dojo/request/xhr"
        ], function(xhr) {

           me.set('disabled', true);
           cancel.set('disabled', true);

           xhr.post('{% url "failover_takeover" %}', {
             handleAs: 'json',
             headers: {"X-CSRFToken": '{{ csrf_token }}'},
             data: {
               password: dijit.byId("id_password").get("value")
             }
           }).then(function(data) {
             if(data.error) {
               dijit.byId("takeover_error").set('content', data.message);
               me.set('disabled', false);
               cancel.set('disabled', false);
             } else {
               dijit.byId("takeover_dialog").hide();
               window.location = '/';
             }
           });
        });

      </script>
    </div>

    <div data-dojo-type="dijit.form.Button" id="takeover_cancel">
    Cancel
      <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
	 dijit.byId("takeover_error").set('content', '');
         dijit.byId("id_password").set('value', '');
         dijit.byId("takeover_dialog").hide();
      </script>
    </div>
  </form>

</div>
{% endif %}

</div>
</div>
{% endblock %}
