{% extends "dashboard/base.html" %}
{% load staticfiles %}
{% load i18n %}
{% load pipeline %}
{% load instance_tags %}

{% block title-page %}{{ instance.name }} | vm{% endblock %}

{% block content %}

{% if instance.is_base %}
<div class="alert alert-info alert-new-template" id="alert-new-template" style="position: relative;">
  <form action="{% url "dashboard.views.vm-toggle-tutorial" pk=instance.pk %}"
    method="POST">
    {% csrf_token %}
    <input name="hidden" type="hidden"
     value="{{ hide_tutorial|yesno:"false,true" }}"/>
    <button type="submit"
      id="dashboard-tutorial-toggle" class="btn btn-sm pull-right btn-success">
      <i class="fa fa-caret-{% if hide_tutorial %}down{% else %}up{% endif %}"></i>
      {% trans "Toggle tutorial panel" %}
    </button>

    <a href="#" class="btn btn-default btn-sm pull-right"
      id="vm-details-start-template-tour">
      <i class="fa fa-play"></i> {% trans "Start template tutorial" %}
    </a>
  </form>
  <strong>{% trans "This is the master vm of your new template" %}</strong>
  <ol {% if hide_tutorial %}style="display: none;"{% endif %}>
    <li>{% trans "Modify the virtual machine to suit your needs <strong>(optional)</strong>" %}
      <ul>
        <li>{% trans "Change the description" %}</li>
        <li>{% trans "Change resources (CPU and RAM)" %}</li>
        <li>{% trans "Attach or detach disks" %}</li>
        <li>{% trans "Add or remove network interfaces" %}</li>
      </ul>
    </li>
    <li>{% trans "Deploy the virtual machine" %}</li>
    <li>{% trans "Connect to the machine" %}</li>
    <li>{% trans "Do all the needed installations/customizations" %}</li>
    <li>{% trans "Log off from the machine" %}</li>
    <li>
      {% trans "Press the Save as template button" %}
    </li>
    <li>
      {% trans "Delete this virtual machine <strong>(optional)</strong>" %}
    </li>
  </ol>
</div>
{% endif %}
<div class="body-content">
  <div class="page-header">
    <div class="pull-right" id="ops">
      {% include "dashboard/vm-detail/_operations.html" %}
    </div>
    <h1>
      <div id="vm-details-rename" class="vm-details-home-rename-form-div">
        <form action="{{ op.rename.get_url }}" method="POST" id="vm-details-rename-form">
          {% csrf_token %}
          <div class="input-group vm-details-home-name">
            <input id="vm-details-rename-name" class="form-control input-sm" name="new_name" type="text" value="{{ instance.name }}"/>
            <span class="input-group-btn">
              <button type="submit" class="btn btn-sm vm-details-rename-submit
                {% if not is_operator %}disabled{% endif %}">
                {% trans "Rename" %}
              </button>
            </span>
          </div>
        </form>
      </div>
      <div id="vm-details-h1-name" class="vm-details-home-edit-name">
        {{ instance.name }}
      </div>
      <small>{{ instance.primary_host.get_fqdn }}</small>
      <small class="dashboard-vm-favourite" style="line-height: 39.6px;" data-vm="{{ instance.id }}">
        {% if fav %}
          <i class="fa fa-star text-primary title-favourite" title="{% trans "Unfavourite" %}"></i>
        {% else %}
          <i class="fa fa-star-o text-primary title-favourite" title="{% trans "Mark as favorite" %}"></i>
        {% endif %}
      </small>
    </h1>
    <div style="clear: both;"></div>
  </div>
  {% if instance.node and not instance.node.online %}
    <div class="alert alert-warning">
      {% if user.is_superuser %}
        {% blocktrans with name=instance.node.name %}
        The node <strong>{{ name }}</strong> is missing.
        {% endblocktrans %}
      {% else %}
        {% trans "Currently you cannot execute any operations because the virtual machine's node is missing." %}
      {% endif %}
    </div>
  {% endif %}
  <div class="row">
    <div class="col-md-4" id="vm-info-pane">
      <div class="big">
        <span id="vm-details-state" class="label label-success" data-status="{{ instance.status }}">
          <i class="fa
            {% if is_new_state %}
            fa-spinner fa-spin
            {% else %}
            {{ instance | get_status_icon }}
            {% endif %}"></i>
          <span>{{ instance|get_status_display|upper }}</span>
        </span>
      </div>

      <h3>{% trans "Connection details" %}</h3>
      <dl class="dl-horizontal vm-details-connection">
        <dt>{% trans "Protocol" %}</dt>
        <dd>{{ access_method|upper }}</dd>
          <dt>{% trans "Host" %}</dt>
          <dd>
            {% if instance.get_connect_port %}
              {{ instance.get_connect_host }}:<strong>{{ instance.get_connect_port }}</strong>
            {% elif instance.interface_set.count < 1%}
              <strong>{% trans "The VM doesn't have any network interface." %}</strong>
            {% else %}
              <strong>{% trans "The required port for this protocol is not forwarded." %}</strong>
            {% endif %}
          </dd>

        {% if instance.ipv6 and instance.get_connect_port %}
          <dt>{% trans "Host (IPv6)" %}</dt>
          <dd>{{ ipv6_host }}:<strong>{{ ipv6_port }}</strong></dd>
        {% endif %}

        <dt>{% trans "Username" %}</dt>
        <dd>cloud</dd>
        <dt>{% trans "Password" %}</dt>
        <dd>
          <div class="input-group">
            <input type="text" id="vm-details-pw-input" class="form-control input-sm input-tags"
            value="{{ instance.pw }}" spellcheck="false" autocomplete="new-password"/>
            <span class="input-group-addon btn btn-default input-tags vm-details-show-password"
              title="{% trans "Show password" %}" data-container="body">
              <i class="fa fa-eye vm-details-password-eye"></i>
            </span>
          </div>
        </dd>
        <dd style="font-size: 10px; text-align: right; padding-top: 8px;">
          <div id="vm-details-pw-reset">
          {% with op=op.password_reset %}{% if op %}
          <a href="{% if op.disabled %}#{% else %}{{op.get_url}}{% endif %}"
            class="operation operation-{{op.op}}"
            {% if op.disabled %}
            data-disabled="true"
            title="{% if instance.has_agent %}{% trans "Start the VM to change the password." %}{% else %}{% trans "This machine has no agent installed." %}{% endif %}"
            {% endif %}>
            {% trans "Generate new password!" %}
          </a>
          {% endif %}{% endwith %}
          </div>
        </dd>
      </dl>
      {% for c in connect_commands %}
      <div class="input-group dashboard-vm-details-connect-command">
        <span class="input-group-addon input-tags">{% trans "Command" %}</span>
        <input type="text" spellcheck="false"
        value="{{ c.cmd }}"
        id="vm-details-connection-string-{{ c.id }}" class="form-control input-tags" />
        <span class="input-group-addon btn btn-default input-tags vm-details-show-password"
          title="{% trans "Show password" %}" data-container="body">
          <i class="fa fa-eye vm-details-password-eye"></i>
        </span>
        <span class="input-group-addon input-tags btn btn-default vm-details-connection-string-copy"
          title="{% trans "Copy to clipboard" %}"
          data-container="body"
          data-clipboard-target="#vm-details-connection-string-{{ c.id }}">
          <i class="fa fa-copy"></i>
        </span>
      </div>
      {% empty %}
      <div class="input-group dashboard-vm-details-connect-command">
        <span class="input-group-addon input-tags">{% trans "Command" %}</span>
        <input type="text" spellcheck="false" value="{% trans "Connection is not possible." %}"
        id="vm-details-connection-string" class="form-control input-tags" />
      </div>
      {% endfor %}
      {% if instance.get_connect_uri %}
      <div id="dashboard-vm-details-connect" class="operation-wrapper">
        {% if client_download %}
        <a id="dashboard-vm-details-connect-button" class="btn btn-xs btn-default operation " href="{{ instance.get_connect_uri}}" title="{% trans "Connect via the CIRCLE Client" %}">
                <i class="fa fa-external-link"></i> {% trans "Connect" %}
        </a>
        <a href="{% url "dashboard.views.client-check" %}?vm={{ instance.pk }}">{% trans "Download client" %}</a>
        {% else %}
        <a id="dashboard-vm-details-connect-download-button" class="btn btn-xs btn-default operation " href="{% url "dashboard.views.client-check" %}?vm={{ instance.pk }}" title="{% trans "Download the CIRCLE Client" %}">
                <i class="fa fa-external-link"></i> {% trans "Connect (download client)" %}
        </a>
        {% endif %}
      </div>
      {% endif %}
    </div>
    <div class="col-md-8" id="vm-detail-pane">
      <div class="big" id="vm-detail-successful-boot">
        <div class="label label-info" data-status="{{ instance.status }}">
          <i class="fa fa-check"></i>
          {% trans "The virtual machine successfully started, you can connect now." %}
        </div>
      </div>
      <div class="panel panel-default" id="vm-detail-panel">
        <ul class="nav nav-pills panel-heading">
          <li class="active">
            <a href="#home" data-toggle="pill" data-target="#_home" class="text-center">
              <i class="fa fa-compass fa-2x"></i><br>
              {% trans "Home" %}</a>
          </li>
          <li>
            <a href="#resources" data-toggle="pill" data-target="#_resources" class="text-center">
              <i class="fa fa-tasks fa-2x"></i><br>
              {% trans "Resources" %}</a>
          </li>
          <li{% if not vnc_url %} class="disabled"{% endif %}>
            <a href="#console" data-toggle="pill" data-target="#_console" class="text-center">
              <i class="fa fa-desktop fa-2x"></i><br>
              {% trans "Console" %}</a></li>
          <li>
            <a href="#network" data-toggle="pill" data-target="#_network" class="text-center">
              <i class="fa fa-globe fa-2x"></i><br>
              {% trans "Network" %}</a>
          </li>
          <!--
          <li>
            <a href="#activity" data-toggle="pill" data-target="#_activity" class="text-center"
               data-activity-url="{% url "dashboard.views.vm-activity-list" instance.id %}">
              <i class="fa fa-clock-o fa-2x"></i><br>
              {% trans "Activity" %}</a>
          </li>
          -->
        </ul>

        <div class="tab-content panel-body">
          <div class="not-tab-pane active" id="_home">{% include "dashboard/vm-detail/home.html" %}</div>
          <hr class="js-hidden"/>
          <div class="not-tab-pane" id="_resources">{% include "dashboard/vm-detail/resources.html" %}</div>
          <div class="tab-pane" id="_console">{% include "dashboard/vm-detail/console.html" %}</div>
          <hr class="js-hidden"/>
          <div class="not-tab-pane" id="_access">{% include "dashboard/vm-detail/access.html" %} </div>
          <hr class="js-hidden"/>
          <div class="not-tab-pane" id="_network">{% include "dashboard/vm-detail/network.html" %}</div>
          <hr class="js-hidden"/>
          <div class="not-tab-pane" id="_activity">{% include "dashboard/vm-detail/activity.html" %}</div>
          <hr class="js-hidden"/>
        </div>
      </div>
    </div>
  </div>
</div>

{% endblock %}

{% block extra_js %}
  {% javascript 'vm-detail' %}
{% endblock %}
