<!--
Copyright 2019 EveryUP srl

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 "project_descriptors.html" %}
{% load staticfiles %}


{% block head_block %}
  	{{ block.super }}
<link rel="stylesheet" href="/static/node_modules/select2/dist/css/select2.min.css">
{% endblock %}

{% block title_header_big %}
    {{ block.super }}
  {% if package_type  %}
     {{ package_type|upper }}
  {% else%}
    Undefined
  {% endif %} Packages
{% endblock %}

{% block left_sidebar %}
    {% include 'osm/osm_project_left_sidebar.html' %}
{% endblock %}

{% block breadcrumb_body %}
  	{{ block.super }}
    <li><a href="{% url 'projects:open_project' %}">{{project_name}}</a></li>
    <li><a> {% if package_type  %}
     {{ package_type }}
  {% else%}
    Undefined
  {% endif %} Packages</a></li>

{% endblock %}

{% block content_body %}
  	{{ block.super }}
{% include 'osm/modal/files_list.html' %}
{% include 'modal/instance_create.html' %}
{% include 'modal/choose_package_name.html' %}
<div class="row">
    {% if package_type == 'ns' %}
        {% include "package_list_ns.html" %}
    {% elif package_type == 'vnf' %}
        {% include "package_list_vnf.html" %}
    {% endif %}
    
</div>
{% endblock %}


{% block resource_block %}
  	{{ block.super }}
     <script src="/static/node_modules/select2/dist/js/select2.js"></script>
    <script src="/static/src/instancehandler/instance_create.js"></script>
    <script src="/static/src/packagehandler/packages_list.js"></script>
    <script src="/static/src/drop_zone.js"></script>
    <script src="/static/src/packagehandler/onboard_package.js"></script>

    <script>
    var csrf_token = '{{csrf_token}}';
    var descr_list_url = '{% url "packages:list_packages"   package_type=package_type%}';
    var new_desc_url = '{% url "packages:onboard_package"   package_type=package_type%}'
    var vim_list_url = '{% url "vims:list"  %}';
    var package_type = '{{ package_type | safe }}';
    var project_id = '{{ project_id }}';
    var table;
    var row_builder = {
        'ns': [
                {
                    "render": function (data, type, row) {
                       return row['short-name'] || '';
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                       return row['_id'];
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                       return row['description'] || '';
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row) {
                       return row['vendor'] || '';
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                       return row['version'] || '';
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row) {
                       var result = '<div class="btn-group">\n' ;
                       
                           result += '    <button type="button" class="btn btn-default" data-container="body"\n' +
                               'data-toggle="tooltip" data-placement="top" title="Instantiate NS"\n' +
                               'onclick="javascript:openModalCreateNS({ \'project_id\':\'' + project_id +'\', \'package_type\': \''+ package_type +'\', \'descriptor_name\':\''+row["name"]+'\', \'descriptor_id\':\''+row["_id"]+'\',\'vim_list_url\': \''+vim_list_url+'\',  \'nsd_list_url\': \''+descr_list_url+'\' })">\n' +
                               '<i class="fa fa-paper-plane"></i></button>\n';
                        result += '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Edit"\n' +
                           '        onclick="javascript:openDescriptorView(\'nsd\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-edit"></i></button>\n' +
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Show content"\n' +
                           '        onclick="javascript:openPackageContentList(\''+ package_type +'\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-folder-open"></i></button>\n' +
                            '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Clone"\n' +
                           '        onclick="javascript:clonePackage(\''+ package_type +'\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-clone"></i></button>\n';
                           result += '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Show Graph"\n' +
                           '  onclick="location.href=\'/projects/descriptors/composer?type=nsd&id='+row["_id"] +'\'"\n' +
                           '        ><i class="fa fa-sitemap fa-fw"></i></button>\n';
                        result +=
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Download package"\n' +
                           '        onclick="location.href=\'/packages/'+package_type+'/'+ row["_id"] +'/download\'">\n' +
                           '    <i class="fa fa-download fa-fw"></i></button>\n' +
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Delete"\n' +
                           '        onclick="javascript:deletePackage( \'' + package_type + '\', \''+row["_id"] + '\', \''+row["name"] + '\')">\n' +
                           '    <i class="far fa-trash-alt"></i></button>\n' +
                           '</div>';

                               return result
                    },
                    "targets": 5,
                    "orderable": false
                }],
        'vnf': [
                {
                    "render": function (data, type, row) {
                       return row['short-name'] || '';
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                       return row['_id'];
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                       return row['_admin']['type'] || '';
                    },
                    "targets": 2,
                    "orderable": false
                },
                {
                    "render": function (data, type, row) {
                       return row['description'] || '';
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                       return row['vendor'] || '';
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row) {
                       return row['version'] || '';
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row) {
                       var result = '<div class="btn-group">\n' ;
                        result += '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Edit"\n' +
                           '        onclick="javascript:openDescriptorView(\'vnfd\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-edit"></i></button>\n' +
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Show content"\n' +
                           '        onclick="javascript:openPackageContentList(\''+ package_type +'\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-folder-open"></i></button>\n' +
                            '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Clone"\n' +
                           '        onclick="javascript:clonePackage(\''+ package_type +'\', \''+row["_id"]+'\')">\n' +
                           '    <i class="fa fa-clone"></i></button>\n';
                           result += '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Show Graph"\n' +
                           '  onclick="location.href=\'/projects/descriptors/composer?type=vnfd&id='+row["_id"] +'\'"\n' +
                           '        ><i class="fa fa-sitemap fa-fw"></i></button>\n';
                        result +=
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Download package"\n' +
                           '        onclick="location.href=\'/packages/'+package_type+'/'+ row["_id"] +'/download\'">\n' +
                           '    <i class="fa fa-download fa-fw"></i></button>\n' +
                           '<button type="button" class="btn btn-default" data-container="body"\n' +
                           '        data-toggle="tooltip" data-placement="top" title="Delete"\n' +
                           '        onclick="javascript:deletePackage( \'' + package_type + '\', \''+row["_id"] + '\', \''+row["name"] + '\')">\n' +
                           '    <i class="far fa-trash-alt"></i></button>\n' +
                           '</div>';

                               return result
                    },
                    "targets": 6,
                    "orderable": false
                }
            ]
    };
    function refreshTable() {
        var type_vnf = $('#type_vnf').val()
        var url = (type_vnf && type_vnf !== '') ? ("/packages/" +package_type+"/list?type="+type_vnf) : ("/packages/" +package_type+"/list")
        table.ajax.url(url);
        table.ajax.reload();
    }

    $(document).ready( function () {
        table = $('#descriptors_table').DataTable({
            responsive: true,
            "ajax": {
                "url": "/packages/" +package_type+"/list",
                "dataSrc": function (json) {
                    return json['descriptors'];
                },
                statusCode: {
                    401: function () {
                        console.log("no auth");
                        moveToLogin(window.location.pathname);
                    }
                },
                "error": function (hxr, error, thrown) {

                }

            },
            "columns": row_builder[package_type]

        });

        setInterval(refreshTable, 10000);


        $("#formCreatePackage").submit(function (event) {
                event.preventDefault(); //prevent default action
                var post_url = $(this).attr("action"); //get form action url
                var request_method = $(this).attr("method");
                var form_data = new FormData(this); //Encode form elements for submission

                $.ajax({
                    url: post_url,
                    type: request_method,
                    data: form_data,
                    headers: {
                        "Accept": 'application/json'
                    },
                    contentType: false,
                    processData: false
                }).done(function (response, textStatus, jqXHR) {
                    var map = {
                        ns: 'nsd',
                        vnf: 'vnfd'
                    };
                    location.href='/projects/descriptors/composer?type=' + map[response['data']['type']]+'&id=' + response['data']['id'];
                }).fail(function (result) {
                    var data = result.responseJSON;
                    var title = "Error " + (data.code ? data.code : 'unknown');
                    var message = data.detail ? data.detail : 'No detail available.';
                    bootbox.alert({
                        title: title,
                        message: message
                    });
                });
            });
    });

    $(document).on('change', '#type_vnf', function(){
        refreshTable();
    });
    </script>


{% endblock %}

{% block footer %}
    {% include "footer.html"  %}
{% endblock %}
