{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
{% block title %}{% trans "Storage" %} - {{ pool }}{% endblock %}
{% block style %}
    <link rel="stylesheet" href="{% static "css/sortable-theme-bootstrap.css" %}"/>
{% endblock %}
{% block content %}

<!-- Page Heading -->
<div class="row">
    <div class="col-lg-12">
        {% include 'create_stg_vol_block.html' %}
        <h1 class="page-header">{% trans "Storage:" %} {{ pool }}</h1>
        <ol class="breadcrumb">
            <li class="active">
                <i class="fa fa-dashboard"></i> <a href="{% url 'overview' compute.id %}">{% trans "Overview" %}</a>
            </li>
            <li>
                <i class="fa fa-server"></i> <a href="{% url 'instances' compute.id %}">{% trans "Instances" %}</a>
            </li>
            <li>
                <i class="fa fa-hdd-o"></i> <a href="{% url 'storages' compute.id %}">{% trans "Storages" %}</a>
            </li>
            <li>
                <i class="fa fa-sitemap"></i> <a href="{% url 'networks' compute.id %}">{% trans "Networks" %}</a>
            </li>
            <li>
                <i class="fa fa-wifi"></i> <a href="{% url 'interfaces' compute.id %}">{% trans "Interfaces" %}</a>
            </li>
            <li>
                <i class="fa fa-filter"></i> <a href="{% url 'nwfilters' compute.id %}">{% trans "NWFilters" %}</a>
            </li>
            <li>
                <i class="fa fa-key"></i> <a href="{% url 'secrets' compute.id %}">{% trans "Secrets" %}</a>
            </li>
        </ol>
    </div>
</div>
<!-- /.row -->

{% include 'errors_block.html' %}
{% include 'messages_block.html' %}

<div class="row">
    <div class="col-xs-6 col-sm-6">
        <p>{% trans "Pool name:" %}</p>
        <p>{% trans "Pool type:" %}</p>
        <p>{% trans "Pool path:" %}</p>
        <p>{% trans "Pool status:" %}</p>
        <p>{% trans "Size:" %} ({{ size|filesizeformat }} / {{ used|filesizeformat }})</p>
        <p>{% trans "State:" %}</p>
        <p>{% trans "Autostart:" %}</p>
    </div>
    <div class="col-xs-6 col-sm-6">
        <p>{{ pool }}</p>
        <p>{% if not type %}{% trans "None" %}{% else %}{{ type }}{% endif %}</p>
        <p>{% if not path %}{% trans "None" %}{% else %}{{ path }}{% endif %}</p>
        <p>{% if not status %}{% trans "None" %}{% else %}{{ status }}{% endif %}</p>
        <p>{% trans "Usage:" %} {{ percent }}%</p>
        <p>
        <form action="" method="post" role="form">{% csrf_token %}
            {% ifequal state 0 %}
                <input type="submit" class="btn btn-xs btn-default" name="start" value="{% trans "Start" %}">
                <input type="submit" class="btn btn-xs btn-default" name="delete" value="{% trans "Delete" %}"
                       onclick="return confirm('{% trans "Are you sure?" %}')">
                {% else %}
                <input type="submit" class="btn btn-xs btn-default" name="stop" value="{% trans "Stop" %}"
                       onclick="return confirm('{% trans "Are you sure?" %}')">
            {% endifequal %}
        </form>
        </p>
        <p>
        <form action="" method="post" role="form">{% csrf_token %}
            {% ifequal autostart 0 %}
                <input type="submit" class="btn btn-xs btn-default" name="set_autostart"
                       value="{% trans "Enable" %}">
                {% else %}
                <input type="submit" class="btn btn-xs btn-default" name="unset_autostart"
                       onclick="return confirm('{% trans "Are you sure?" %}')" value="{% trans "Disable" %}">
            {% endifequal %}
        </form>
        </p>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        {% if state %}
        <div class="row">
            <div class="pull-right">
                <input id="filter" class="form-control" type="text" placeholder="Search">
            </div>
            <h3 class="page-header">{% trans "Volumes" %}</h3>

        </div>
        {% if volumes  %}
        <div class="table-responsive">
            <table class="table table-striped sortable-theme-bootstrap" data-sortable>
                <thead>
                <tr>
                    <th style="width: 45px;">#</th>
                    <th>{% trans "Name" %}</th>
                    <th>{% trans "Allocated" %}</th>
                    <th>{% trans "Size" %}</th>
                    <th>{% trans "Format" %}</th>
                    <th data-sortable="false" colspan="2">{% trans "Action" %}</th>
                </tr>
                </thead>
                <tbody class="searchable">
                {% for volume in volumes %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ volume.name }}</td>
                    <td>{{ volume.allocation|filesizeformat }}</td>
                    <td>{{ volume.size|filesizeformat }}</td>
                    <td>{{ volume.type }}</td>
                    <td style="width:30px;">
                        <!-- Modal Clone -->
                        <div class="modal fade" id="Clone{{ forloop.counter }}" tabindex="-1" role="dialog"
                             aria-labelledby="addHostLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title">{% trans "Clone image" %} <span class="text-danger">{{ volume.name }}</span></h4>
                                    </div>
                                    <form class="form-horizontal" method="post" role="form">{% csrf_token %}
                                        <div class="modal-body">
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">{% trans "Name" %}</label>
                                                <div class="col-sm-6">
                                                    <input type="text" class="form-control" name="name" placeholder="{% trans "Name" %}" required pattern="[a-zA-Z0-9\.\-_]+">
                                                    <input type="hidden" name="image" value="{{ volume.name }}">
                                                </div>
                                            </div>
                                            <div class="form-group" id="image_format">
                                                <label class="col-sm-3 control-label">{% trans "Convert" %}</label>
                                                <div class="col-sm-6">
                                                    <input class="volume-convert" type="checkbox" name="convert" value="true">
                                                </div>
                                            </div>
                                            <div class="form-group format-convert">
                                                <label class="col-sm-3 control-label">{% trans "Format" %}</label>
                                                <div class="col-sm-6">
                                                    <select name="format" class="form-control image-format">
                                                        <option value="raw">{% trans "raw" %}</option>
                                                        <option value="qcow">{% trans "qcow" %}</option>
                                                        <option value="qcow2">{% trans "qcow2" %}</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group meta-prealloc" style="display: none;">
                                                <label class="col-sm-3 control-label">{% trans "Metadata" %}</label>
                                                <div class="col-sm-6">
                                                    <input type="checkbox" name="meta_prealloc" value="true">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
                                            <button type="submit" class="btn btn-primary" name="cln_volume" onclick="showPleaseWaitDialog();">{% trans "Clone" %}</button>
                                        </div>
                                    </form>
                                </div> <!-- /.modal-content -->
                            </div> <!-- /.modal-dialog -->
                        </div> <!-- /.modal -->
                        {% ifnotequal volume.type "iso" %}
                        <a data-toggle="modal" href="#Clone{{ forloop.counter }}" class="btn btn-sm btn-default" title="{% trans "Clone" %}"><i class="fa fa-files-o"></i></a>
                        {% else %}
                        <a class="btn btn-sm btn-default disabled"><i class="fa fa-files-o"></i></a>
                        {% endifnotequal %}
                    </td>
                    <td style="width:30px;">
                        <form action="" method="post" style="height:10px" role="form">{% csrf_token %}
                            <input type="hidden" name="volname" value="{{ volume.name }}">
                            <button type="submit" class="btn btn-sm btn-default" name="del_volume" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                <i class="fa fa-trash"></i>
                            </button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="col-lg-12">
            <div class="alert alert-warning alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <i class="fa fa-exclamation-triangle"></i> <strong>{% trans "Warning:" %}</strong> {% trans "Hypervisor doesn't have any Volumes" %}
            </div>
        </div>
        {% endif %}
        {% endif %}
    </div>
</div>
{% include 'pleasewaitdialog.html' %}
{% endblock %}
{% block script %}
    <script src="{% static "js/sortable.min.js" %}"></script>
    <script>
        $('.format-convert').hide();
        $(document).on('change', '.volume-convert', function () {
            if ($(this).prop('checked')) {
                $('.format-convert').show();
                if ($('.image-format').val() == 'qcow2') {
                    $('.meta-prealloc').show();
                }
            } else {
                $('.format-convert').hide();
                $('.meta-prealloc').hide();
            }
        });
        $(document).on('change', '.image-format', function () {
            if ($(this).val() == "qcow2") {
                $('.meta-prealloc').show();
            } else {
                $('.meta-prealloc').hide();
            }
        });
    </script>
    <script>
        $(document).ready(function () {
            (function ($) {
                $('#filter').keyup(function () {
                    var rex = new RegExp($(this).val(), 'i');
                    $('.searchable tr').hide();
                    $('.searchable tr').filter(function () {
                        return rex.test($(this).text());
                    }).show();
                })
            }(jQuery));
        });
    </script>
{% endblock %}
