<!--
Copyright 2019 ETSI

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an  BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

{% extends "base.html" %}
{% load get %}
{% load staticfiles %}


{% block head_block %}
    {{ block.super }}
    <link rel="stylesheet" href="/static/node_modules/datatables.net-bs/css/dataTables.bootstrap.min.css">
{% endblock %}
{% block title_header_big %}
    {{ block.super }}
{% endblock %}
{% block left_sidebar %}

   {% include 'osm/osm_project_left_sidebar.html' %}

{% endblock %}


{% block breadcrumb_body %}
    {{ block.super }}
    <li><a href="{% url "wims:list"   %}">VIMS</a></li>
{% endblock %}

{% block content_body %}
    {{ block.super }}
    {% include 'modal/wim_details.html' %}
    {% include 'modal/wim_create.html' %}
    {% csrf_token %}
    <div class="row">
        <div class="col-md-12">

            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">Registered WIM</h3>
                    <div class="box-tools">
                        <button type="button" class="btn btn-default" data-container="body"
                            data-toggle="tooltip" data-placement="top" title="New PDU"
                            onclick="javascript:openModalCreateWIM()">
                        <i class="fa fa-plus"></i> <span> New WIM</span>
                        </button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="wims_table" class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Identifier</th>
                            <th>Type</th>
                            <th>Operational State</th>
                            <th>Description</th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>
{% endblock %}

{% block resource_block %}
    {{ block.super }}
    <script src="/static/node_modules/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="/static/node_modules/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script>
    $(document).ready( function () {
        var table = $('#wims_table').DataTable({
            responsive: true,
            "ajax": {
                "url": "/wims/list/",
                "dataSrc": function (json) {
                    return json['datacenters'];
                },
                statusCode: {
                    401: function () {
                        console.log("no auth");
                        moveToLogin(window.location.pathname);
                    }
                },
                "error": function (hxr, error, thrown) {
                    console.log(hxr)
                    console.log(thrown)
                    console.log(error);
                }

            },
            "columns": [
                {
                    "render": function (data, type, row) {
                        return row["name"];
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                        return row['_id'];
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                        return row["wim_type"];
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row) {
                        return row["_admin"]['operationalState'];
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        return row['description'] || '';
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row) {
                        return '<div class="btn-group"><button type="button" class="btn btn-default" ' +
                            'onclick="javascript:showWIM( \''+row['_id'] + '\', \''+row['name'] +'\')" data-toggle="tooltip" data-placement="top" data-container="body" title="Show Info">' +
                        '<i class="fa fa-info"></i>' +
                        '</button> ' +
                        '<button type="button" class="btn btn-default"' +
                        'onclick="javascript:deleteWim(\''+row['_id']+'\', \''+ row["name"] +'\')" data-toggle="tooltip" data-placement="top" data-container="body" title="Delete">' +
                        '<i class="far fa-trash-alt" ></i></button></div>';
                    },
                    "targets": 5,
                    "orderable": false
                }
            ]
        });

        setInterval(function () {
                table.ajax.reload();
            }, 10000);
    });

        function openModalCreateWIM(){
            $('#modal_new_wim').modal('show');
        }
        function deleteWim(wim_id, wim_name) {
            var url = "/wims/"+wim_id+"/delete";
            bootbox.confirm("Are you sure want to delete " + wim_name + "?", function (result) {
                if (result) {
                    var dialog = bootbox.dialog({
                        message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
                        closeButton: true
                    });
                    $.ajax({
                        url: url,
                        type: 'GET',
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        success: function (result) {
                            if (result['error'] == true) {
                                dialog.modal('hide');
                                bootbox.alert("An error occurred.");
                            }
                            else {
                                dialog.modal('hide');
                                location.reload();
                            }
                        },
                        error: function (error) {
                            dialog.modal('hide');
                            bootbox.alert("An error occurred.");
                        }
                    });
                }
            })
        }

        function showWIM(wim_uuid) {
            var dialog = bootbox.dialog({
                message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
                closeButton: true
            });

            $.ajax({
                url: '/wims/' + wim_uuid ,
                type: 'GET',
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (result) {
                    //$('#modal_show_vim_body').empty();
                    var wim = result.wim;
                    
                    if (wim) {
                        $('#modal_show_wim_body').find('span').text('-');
                        for (var k in wim) {
                            $('#' + k).text(wim[k])
                        }
                        if (wim['_admin']) {
                            for (var i in wim['_admin']) {
                                if (i === 'modified' || i === 'created') {
                                    //$('#' + i).text(new Date(wim['_admin'][i]* 1000).toUTCString());
                                    $('#' + i).text(moment(wim['_admin'][i] * 1000).format('DD/MM/YY hh:mm:ss'));
                                }
                                else if (i === 'deployed') {
                                    $('#' + i).text(JSON.stringify(wim['_admin'][i]))
                                }
                                else
                                    $('#' + i).text(wim['_admin'][i])
                            }
                        }
                        dialog.modal('hide');
                        $('#modal_show_wim').modal('show');
                    }
                    else {
                        dialog.modal('hide');
                        bootbox.alert("An error occurred while retrieving the WIM info.");
                    }

                },
                error: function (result) {
                    dialog.modal('hide');
                    bootbox.alert("An error occurred while retrieving the WIM info.");
                }
            });

        }
    </script>

{% endblock %}
