{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block custom_head_css_js %}
    <link href='{% static "css/plugins/select2/select2.min.css" %}' rel="stylesheet">
    <link href='{% static "css/plugins/sweetalert/sweetalert.css" %}' rel="stylesheet">
    <script src='{% static "js/plugins/select2/select2.full.min.js" %}'></script>
    <script src='{% static "js/plugins/sweetalert/sweetalert.min.js" %}'></script>
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a href="{% url 'assets:asset-detail' pk=asset.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Asset detail' %} </a>
                            </li>
                            {% if user.is_superuser %}
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'assets:asset-update' pk=asset.id %}"><i class="fa fa-edit"></i>Update</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-delete-asset">
                                    <i class="fa fa-edit"></i>Delete
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-7" style="padding-left: 0">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span class="label"><b>{{ asset.hostname }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="20%">{% trans 'Hostname' %}:</td>
                                            <td><b>{{ asset.hostname }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'IP' %}:</td>
                                            <td><b>{{ asset.ip }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Public IP' %}:</td>
                                            <td><b>{{ asset.public_ip|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Port' %}:</td>
                                            <td><b>{{ asset.port }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Admin user' %}:</td>
                                            {% if asset.admin_user_avail %}
                                            <td><b>{{ asset.admin_user_avail.name }}</b></td>
                                            {% else %}
                                            <td></td>
                                            {% endif %}
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Remote card IP' %}:</td>
                                            <td><b>{{ asset.remote_card_ip|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Cluster' %}:</td>
                                            <td><b>{{ asset.cluster.name }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Cabinet number' %}:</td>
                                            <td><b>{{ asset.cabinet_no|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Cabinet position' %}:</td>
                                            <td><b>{{ asset.cabinet_pos|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Vendor' %}:</td>
                                            <td><b>{{ asset.vendor|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Model' %}:</td>
                                            <td><b>{{ asset.model|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'CPU' %}:</td>
                                            <td><b>{{ asset.cpu_model|default:"" }} {{ asset.cpu_count|default:"" }}*{{ asset.cpu_cores|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Memory' %}:</td>
                                            <td><b>{{ asset.memory|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Disk' %}:</td>
                                            <td><b>{{ asset.disk_total|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Platform' %}:</td>
                                            <td><b>{{ asset.platform|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'OS' %}:</td>
                                            <td><b>{{ asset.os|default:"" }} {{ asset.os_version|default:"" }} {{ asset.os_arch|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset status' %}:</td>
                                            <td><b>{{ asset.status }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Is active' %}:</td>
                                            <td><b>{{ asset.is_active|yesno:"Yes,No" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset type' %}:</td>
                                            <td><b>{{ asset.type }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset environment' %}:</td>
                                            <td><b>{{ asset.env }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Serial number' %}:</td>
                                            <td><b>{{ asset.sn|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset number' %}:</td>
                                            <td><b>{{ asset.number|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Created by' %}:</td>
                                            <td><b>{{ asset.created_by }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date joined' %}:</td>
                                            <td><b>{{ asset.date_joined|date:"Y-m-j H:i:s" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ asset.comment }}</b></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        {% if user.is_superuser %}
                        <div class="col-sm-5" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Quick modify' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                          <td width="50%">{% trans 'Active' %}:</td>
                                            <td>
                                              <span class="pull-right">
                                                  <div class="switch">
                                                      <div class="onoffswitch">
                                                          <input type="checkbox" {% if asset.is_active %} checked {% endif %}  class="onoffswitch-checkbox" id="is_active">
                                                          <label class="onoffswitch-label" for="is_active">
                                                              <span class="onoffswitch-inner"></span>
                                                              <span class="onoffswitch-switch"></span>
                                                          </label>
                                                      </div>
                                                  </div>
                                              </span>
                                          </td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Refresh hardware' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" class="btn btn-primary btn-xs" id="btn_refresh_asset" style="width: 54px">{% trans 'Refresh' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Test is alive' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" class="btn btn-primary btn-xs" id="btn-test-is-alive" style="width: 54px">{% trans 'Test' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            <div class="panel panel-info">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Asset groups' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table group_edit" id="add-asset2group">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Join asset groups' %}" id="groups_selected" class="select2 groups" style="width: 100%" multiple="" tabindex="4">
                                                        {% for asset_group in asset_groups_remain %}
                                                        <option value="{{ asset_group.id }}" id="opt_{{ asset_group.id }}" >{{ asset_group.name }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-info btn-sm" id="btn-add-user-group">{% trans 'Confirm' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for asset_group in asset_groups %}
                                        <tr>
                                          <td ><b class="bdg_group" data-gid={{ asset_group.id }}>{{ asset_group.name }}</b></td>
                                          <td>
                                              <button class="btn btn-danger pull-right btn-xs btn-leave-group" type="button"><i class="fa fa-minus"></i></button>
                                          </td>
                                        </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
jumpserver.groups_selected = {};
function updateAssetGroups(groups) {
    var the_url = "{% url 'api-assets:asset-detail' pk=asset.id %}";
    var body = {
        groups: Object.assign([], groups)
    };
    var success = function(data) {
        // remove all the selected groups from select > option and rendered ul element;
        $('.select2-selection__rendered').empty();
        $('#groups_selected').val('');
        $.map(jumpserver.groups_selected, function(group_name, index) {
            $('#opt_' + index).remove();
            // change tr html of user groups.
            $('#add-asset2group tbody').append(
                '<tr>' +
                '<td><b class="bdg_group" data-gid="' + index + '">' + group_name + '</b></td>' +
                '<td><button class="btn btn-danger btn-xs pull-right btn-leave-group" type="button"><i class="fa fa-minus"></i></button></td>' +
                '</tr>'
            )
        });
        // clear jumpserver.groups_selected
        jumpserver.groups_selected = {};
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}

function refreshAssetHardware() {
    var the_url = "{% url 'api-assets:asset-refresh' pk=asset.id %}";
    var success = function (data) {
        location.reload();
    };
    var error = function (data) {
        alert(data)
    };
    APIUpdateAttr({
        url: the_url,
        success: success,
        error: error,
        method: 'GET'
    });
}


$(document).ready(function () {
    $('.select2.groups').select2().on('select2:select', function(evt) {
        var data = evt.params.data;
        jumpserver.groups_selected[data.id] = data.text;
    }).on('select2:unselect', function(evt) {
        var data = evt.params.data;
        delete jumpserver.groups_selected[data.id]
    });
}).on('click', '#is_active', function () {
    var the_url = '{% url "api-assets:asset-detail" pk=asset.id %}';
    var checked = $(this).prop('checked');
    var body = {
        'is_active': checked
    };
    var success = '{% trans "Update successfully!" %}';
    var status = $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").text();
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success_message: success
    });
    if (status == "False") {
            $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('True');
    }else{
            $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('False');
    }
}).on('click', '#btn-add-user-group', function () {
    if (Object.keys(jumpserver.groups_selected).length === 0) {
       return false;
    }
    var groups = $('.bdg_group').map(function() {
        return $(this).data('gid');
    }).get();
    $.map(jumpserver.groups_selected, function(value, index) {
        groups.push(index);
        $('#opt_' + index).remove();
    });
    updateAssetGroups(groups)
}).on('click', '.btn-leave-group', function() {
    var $this = $(this);
    var $tr = $this.closest('tr');
    var $badge = $tr.find('.bdg_group');
    var gid = $badge.data('gid');
    var group_name = $badge.html() || $badge.text();
    $('#groups_selected').append(
        '<option value="' + gid + '" id="opt_' + gid + '">' + group_name + '</option>'
    );
    $tr.remove();
    var groups = $('.bdg_group').map(function () {
        return $(this).data('gid');
    }).get();
    updateAssetGroups(groups)
}).on('click', '.btn-delete-asset', function () {
    var $this = $(this);
    var name = "{{ asset.hostname }}";
    var uid = "{{ asset.id }}";
    var the_url = '{% url "api-assets:asset-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    var redirect_url = "{% url 'assets:asset-list' %}";
    objectDelete($this, name, the_url, redirect_url);
}).on('click', '#btn_refresh_asset', function () {
    alert('关闭alert, 等待完成, 自动刷新页面');
    refreshAssetHardware()
}).on('click', '#btn-test-is-alive', function () {
    var the_url = "{% url 'api-assets:asset-alive-test' pk=asset.id %}";
    var error = function (data) {
        alert(data)
    };
    alert('关闭alert, 等待完成');
    APIUpdateAttr({
        url: the_url,
        error: error,
        method: 'GET',
        success_message: "{% trans "Pong" %}"
    });
})

</script>
{% endblock %}
