{% extends ../base.html %}

{% block meta %}
<!--  <meta http-equiv="refresh" content="60">-->
{% end %}

{% block title %}Crafty Controller - {{ translate('serverDetails', 'serverDetails', data['lang']) }}{% end %}

{% block content %}

<div class="content-wrapper">

    <!-- Page Title Header Starts-->
  <div class="row page-title-header">
    <div class="col-12">
      <div class="page-header">
        <h4 class="page-title">
          {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
            <br />
            <small>UUID: {{ data['server_stats']['server_id']['server_uuid']  }}</small>
        </h4>
      </div>
    </div>

  </div>
  <!-- Page Title Header Ends-->

 {% include "parts/details_stats.html %}

  <div class="row">

    <div class="col-sm-12 grid-margin">
      <div class="card">
        <div class="card-body  pt-0">
            {% include "parts/server_controls_list.html %}

            <div class="row">
                <div class="col-md-6 col-sm-12">
                    <form class="forms-sample" method="post" action="/panel/server_backup">
                      {% raw xsrf_form_html() %}
                      <input type="hidden" name="id" value="{{ data['server_stats']['server_id']['server_id'] }}">
                      <input type="hidden" name="subpage" value="backup">

                      <div class="form-group">
                        <a href="/panel/backup_now?id={{ data['server_stats']['server_id']['server_id'] }}" class="btn btn-primary" onclick="backup_started()">{{ translate('serverBackups', 'backupNow', data['lang']) }}</a>
                      </div>
                      <div class="form-group">
                        {% if data['super_user'] %}
                        <label for="server_name">{{ translate('serverBackups', 'storageLocation', data['lang']) }} <small class="text-muted ml-1"> - {{ translate('serverBackups', 'storageLocationDesc', data['lang']) }}</small> </label>
                        <input type="text" class="form-control" name="backup_path" id="backup_path" value="{{ data['server_stats']['server_id']['backup_path']  }}" placeholder="{{ translate('serverBackups', 'storageLocation', data['lang']) }}" >
                        {% end %}
                      </div>

                      <div class="form-group">
                        <label for="server_path">{{ translate('serverBackups', 'maxBackups', data['lang']) }} <small class="text-muted ml-1"> - {{ translate('serverBackups', 'maxBackupsDesc', data['lang']) }}</small> </label>
                        <input type="text" class="form-control" name="max_backups" id="max_backups" value="{{ data['backup_config']['max_backups']  }}" placeholder="{{ translate('serverBackups', 'maxBackups', data['lang']) }}" >
                      </div>

                      <button type="submit" class="btn btn-success mr-2">{{ translate('serverBackups', 'save', data['lang']) }}</button>
                      <button type="reset" class="btn btn-light">{{ translate('serverBackups', 'cancel', data['lang']) }}</button>
                    </form>
                </div>

                <div class="col-md-6 col-sm-12">
                    <div class="card">
                      <div class="card-body">
                        <h4 class="card-title">{{ translate('serverBackups', 'currentBackups', data['lang']) }}</h4>
                        
                      </div>
                    </div>
                    <div class="text-center">

                      <table class="table table-responsive dataTable" id="backup_table">
                        <thead>
                        <tr>
                            <th width="10%">{{ translate('serverBackups', 'options', data['lang']) }}</th>
                            <th>{{ translate('serverBackups', 'path', data['lang']) }}</th>
                            <th width="20%">{{ translate('serverBackups', 'size', data['lang']) }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for backup in data['backup_list'] %}
                            <tr>
                            <td>
                                <a href="/panel/download_backup?file={{ backup['path'] }}&id={{ data['server_stats']['server_id']['server_id'] }}" class="btn btn-primary">
                                    <i class="fas fa-download" aria-hidden="true"></i>
                                    {{ translate('serverBackups', 'download', data['lang']) }}
                                </a>
                                <br>
                                <br>
                                <button data-file="{{ backup['path'] }}" class="btn btn-danger del_button">
                                    <i class="fas fa-trash" aria-hidden="true"></i>
                                    {{ translate('serverBackups', 'delete', data['lang']) }}
                                </button>
                                <button data-file="{{ backup['path'] }}" class="btn btn-warning restore_button">
                                  <i class="fas fa-undo-alt" aria-hidden="true"></i>
                                  {{ translate('serverBackups', 'restore', data['lang']) }}
                              </button>
                            </td>
                            <td>{{ backup['path'] }}</td>
                            <td>{{ backup['size'] }}</td>
                            </tr>
                        {% end %}

                        </tbody>
                    </table>

                    </div>
                </div>
            </div>

        </div>
      </div>
    </div>
  </div>



</div>
<!-- content-wrapper ends -->

{% end %}

{% block js %}
<script>


    //used to get cookies from browser - this is part of tornados xsrf protection - it's for extra security
    function getCookie(name) {
        var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
        return r ? r[1] : undefined;
    }

    function backup_started(time='5-10') {
            bootbox.alert({
            message: "{{ translate('serverBackups', 'backupTask', data['lang']) }}",
            backdrop: true
        });
    }

    function del_backup(filename, id){
        var token = getCookie("_xsrf")

        data_to_send = { file_name :filename}

        console.log('Sending Command to delete backup: ' + filename)
        $.ajax({
          type: "DELETE",
          headers: {'X-XSRFToken': token},
          url: '/ajax/del_backup?server_id='+id,
          data: {
          file_path: filename,
          id: id
          },
          success: function(data) {
            location.reload();
          },
        });
    }

    function restore_backup(filename, id){
      var token = getCookie("_xsrf")

      console.log('Sending Command to restore backup: ' + filename)
      $.ajax({
        type: "POST",
        headers: {'X-XSRFToken': token},
        url: '/ajax/restore_backup?server_id='+id,
        data: {
        zip_file: filename,
        id: id
        },
        success: function(data) {
          var dialog = bootbox.dialog({
            message: '<i class="fa fa-spin fa-spinner"></i> {{ translate('serverBackups', 'restoring', data['lang']) }}',
            closeButton: false
        });
        setTimeout(function(){
          location.href=('/panel/dashboard');
      }, 15000);
        },
      });
  }


    $( document ).ready(function() {
        console.log( "ready!" );
        $("#backup_config_box").hide();
        $("#backup_save_note").hide();

        $("#show_config").click(function () {
            $("#backup_config_box").toggle();
            $('#backup_button').hide();
            $('#backup_save_note').show();
            $('#backup_data').hide();
        });

        $('#backup_table').DataTable({
            "order": [[ 1, "desc" ]],
            "paging":true,
            "lengthChange": false,
            "searching": true,
            "ordering": true,
            "info": true,
            "autoWidth": false,
            "responsive": true,
        });

        $( ".del_button" ).click(function() {
            var file_to_del = $(this).data("file");

            console.log("file to delete is" + file_to_del);

            bootbox.confirm({
                title: "{% raw translate('serverBackups', 'destroyBackup', data['lang']) %}",
                message: "{{ translate('serverBackups', 'confirmDelete', data['lang']) }}",
                buttons: {
                    cancel: {
                        label: '<i class="fas fa-times"></i> {{ translate("serverBackups", "cancel", data['lang']) }}'
                    },
                    confirm: {
                        label: '<i class="fas fa-check"></i> {{ translate("serverBackups", "confirm", data['lang']) }}'
                    }
                },
                callback: function (result) {
                    console.log(result);
                    if (result == true) {
                      var full_path = '{{ data['backup_path'] }}' + '/' + file_to_del;
                        del_backup(full_path, {{ data['server_stats']['server_id']['server_id'] }} );
                    }
                }
            });
        });
        
      $( ".restore_button" ).click(function() {
        var file_to_restore = $(this).data("file");


        bootbox.confirm({
            title: "{{ translate('serverBackups', 'restore', data['lang']) }} "+file_to_restore,
            message: "{{ translate('serverBackups', 'confirmRestore', data['lang']) }}",
            buttons: {
                cancel: {
                    label: '<i class="fas fa-times"></i> {{ translate("serverBackups", "cancel", data['lang']) }}'
                },
                confirm: {
                    label: '<i class="fas fa-check"></i> {{ translate("serverBackups", "restore", data['lang']) }}'
                }
            },
            callback: function (result) {
                console.log(result);
                if (result == true) {
                    restore_backup(file_to_restore, {{ data['server_stats']['server_id']['server_id'] }} );
                }
            }
        });
    });

  });


</script>

{% end %}