{% extends ../base.html %}

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

{% block title %}Crafty Controller - {{ translate('dashboard', 'dashboard', 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('dashboard', 'dashboard', data['lang']) }}</h4>
      </div>
    </div>

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

  <div class="row">
    <div class="col-md-12 grid-margin">
      <div class="card">
        <div class="card-body">
          <div class="row">
            <div class="col-lg-4 col-md-6">
              <div class="d-flex">
                <div class="wrapper">
                  <h5 class="mb-1 font-weight-medium text-primary"> {{ translate('dashboard', 'host', data['lang']) }}</h5>
                  <h3 class="mb-0 font-weight-semibold"> <i class="fas fa-chart-line"></i></h3>

                </div>
                <div class="wrapper my-auto ml-auto ml-lg-4">
                  <p id="cpu_data" class="mb-0 text-success" data-toggle="tooltip" data-placement="top" data-html="true" title="{% raw translate('dashboard', 'cpuCores', data['lang']) %}: {{ data.get('hosts_data').get('cpu_cores') }} <br /> {% raw translate('dashboard', 'cpuCurFreq', data['lang']) %}: {{ data.get('hosts_data').get('cpu_cur_freq') }} <br /> {% raw translate('dashboard', 'cpuMaxFreq', data['lang']) %}: {{ data.get('hosts_data').get('cpu_max_freq') }}" >
                  {{ translate('dashboard', 'cpuUsage', data['lang']) }}: <span id="cpu_usage">{{ data.get('hosts_data').get('cpu_usage') }}</span>
                  </p>
                  <p id="mem_usage" class="mb-0 text-danger" data-toggle="tooltip" data-placement="top" title="{{ translate('dashboard', 'memUsage', data['lang']) }}: {{ data.get('hosts_data').get('mem_usage') }}" >
                    {{ translate('dashboard', 'memUsage', data['lang']) }}: <span id="mem_percent">{{ data.get('hosts_data').get('mem_percent') }}%</span>
                  </p>
                </div>
              </div>
            </div>
            <div class="col-lg-4 col-md-6 mt-md-0 mt-4">
              <div class="d-flex">
                <div class="wrapper">
                  <h5 class="mb-1 font-weight-medium text-primary">{{ translate('dashboard', 'servers', data['lang']) }}</h5>
                  <h3 class="mb-0 font-weight-semibold">{{ data['server_stats']['total'] }}</h3>

                </div>
                <div class="wrapper my-auto ml-auto ml-lg-4">
                  <p class="mb-0 text-success">{{ data['server_stats']['running'] }} {{ translate('dashboard', 'online', data['lang']).lower() }}</p>
                  <p class="mb-0 text-warning"> {{ data['server_stats']['stopped'] }} {{ translate('dashboard', 'offline', data['lang']).lower() }}</p>
                </div>
              </div>
            </div>
            <div class="col-lg-4 col-md-6 mt-md-0 mt-4">
              <div class="d-flex">
                <div class="wrapper">
                  <h5 class="mb-1 font-weight-medium text-primary">{{ translate('dashboard', 'players', data['lang']) }}</h5>
                  <h3 class="mb-0 font-weight-semibold">{{ data['num_players'] }}</h3>

                </div>
                <div class="wrapper my-auto ml-auto ml-lg-4">
                  <p class="mb-0 text-success">35 {{ translate('dashboard', 'max', data['lang']) }}</p>
                  <p class="mb-0 text-warning">10 {{ translate('dashboard', 'avg', data['lang']) }}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12 col-lg-12 grid-margin stretch-card">
      <div class="card">
        <div class="card-header header-sm d-flex justify-content-between align-items-center">
          <h4 class="card-title"><i class="fas fa-server"></i> &nbsp;{{ translate('dashboard', 'allServers', data['lang']) }}</h4>
          {% if len(data['servers']) > 0 %}
          <span class="too_small" title="{{ translate('dashboard', 'cannotSeeOnMobile', data['lang']) }}", data-content="{{ translate('dashboard', 'cannotSeeOnMobile2', data['lang']) }}", data-placement="top"></span>
          {% end %}
          <div><a class="nav-link" href="/server/step1"><i class="fas fa-plus-circle"></i> &nbsp; {{ translate('dashboard', 'newServer', data['lang']) }}</a></div>
        </div>
        <div class="card-body">

          <div class="table-responsive">
                {% if len(data['servers']) == 0%}
                <div style="text-align: center; color: grey;">
                <h1>{{ translate('dashboard', 'welcome', data['lang']) }}</h1>
                <br>
                <h7>{{ translate('dashboard', 'no-servers', data['lang']) }} {{ translate('dashboard', 'newServer', data['lang']) }}.</h7>
                </div>
                
              {% end %}
          {% if len(data['servers']) > 0 %}
            <table class="table table-hover">
              <thead>
                <tr class="rounded">
                  <th>{{ translate('dashboard', 'server', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'actions', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'cpuUsage', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'memUsage', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'world', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'players', data['lang']) }}</th>
                  <th>{{ translate('dashboard', 'status', data['lang']) }}</th>
                </tr>
              </thead>
              <tbody>
                {% for server in data['servers'] %}
                <tr>
                  <td>
                    <i class="fas fa-server"></i>
                    <a href="/panel/server_detail?id={{server['server_data']['server_id']}}">
                      {{ server['server_data']['server_name'] }}
                    </a>
                  </td>

                  <td id="controls{{server['server_data']['server_id']}}" class="actions_serverlist">
                    {% if server['user_command_permission'] %}
                      {% if server['stats']['running'] %}
                        <a class="stop_button" data-id="{{server['server_data']['server_id']}}" data-toggle="tooltip" title={{ translate('dashboard', 'stop', data['lang']) }}> <i class="fas fa-stop"></i></a> &nbsp;
                        <a class="restart_button" data-id="{{server['server_data']['server_id']}}" data-toggle="tooltip" title={{ translate('dashboard', 'restart', data['lang']) }}> <i class="fas fa-sync"></i></a> &nbsp;
                        <a class="kill_button" data-id="{{server['server_data']['server_id']}}" class="kill_button" data-toggle="tooltip" title={{ translate('dashboard', 'kill', data['lang']) }}> <i class="fas fa-skull"></i></a> &nbsp;
                      {% elif server['stats']['updating']%}
                        <a data-id="{{server['server_data']['server_id']}}" class="">{{ translate('serverTerm', 'updating', data['lang']) }}</i></a>
                      {% elif server['stats']['waiting_start']%}
                        <a data-id="{{server['server_data']['server_id']}}" class="" title={{ translate('dashboard', 'delay-explained', data['lang'])}}>{{ translate('dashboard', 'starting', data['lang']) }}</i></a>
                      {% else %}
                      <a data-id="{{server['server_data']['server_id']}}" class="play_button"><i class="fas fa-play" data-toggle="tooltip" title={{ translate('dashboard', 'start', data['lang']) }}></i></a> &nbsp;
                        <a data-id="{{server['server_data']['server_id']}}" class="clone_button"> <i class="fas fa-clone" data-toggle="tooltip" title={{ translate('dashboard', 'clone', data['lang']) }}></i></a>&nbsp;
                        <a class="kill_button" data-id="{{server['server_data']['server_id']}}" class="kill_button" data-toggle="tooltip" title={{ translate('dashboard', 'kill', data['lang']) }}> <i class="fas fa-skull"></i></a> &nbsp;
                      {% end %}
                    {% end %}
                  </td>

                  <td>
                    <div class="progress mb-1" data-toggle="tooltip" data-placement="top" title="{{server['stats']['cpu']}}">
                      <div class="progress-bar
                      {% if server['stats']['cpu'] <= 33 %}
                      bg-success
                      {% elif 34 <= server['stats']['cpu'] <= 66 %}
                      bg-warning
                      {% else %}
                      bg-danger
                      {% end %}
                      " role="progressbar" style="width: {{server['stats']['cpu']}}%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
                    {{server['stats']['cpu']}}%
                  </td>

                  <td>
                    <div class="progress mb-1" data-toggle="tooltip" data-placement="top" title="{{server['stats']['mem']}}">
                      <div class="progress-bar
                      {% if server['stats']['mem_percent'] <= 33 %}
                      bg-success
                      {% elif 34 <= server['stats']['mem_percent'] <= 66 %}
                      bg-warning
                      {% else %}
                      bg-danger
                      {% end %}
                      " role="progressbar" style="width: {{server['stats']['mem_percent']}}%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
                    {{server['stats']['mem_percent']}}% -

                    {% if server['stats']['mem'] == 0 %}
                      0 MB
                    {% else %}
                      {{server['stats']['mem']}}
                    {% end %}
                  </td>
                  <td>
                    {{ server['stats']['world_name'] }} : {{ server['stats']['world_size'] }}
                  </td>
                  <td>
                    {% if server['stats']['int_ping_results'] %}
                      {{ server['stats']['online'] }} / {{ server['stats']['max'] }} {{ translate('dashboard', 'max', data['lang']) }}<br />

                      {% if server['stats']['desc'] != 'False' %}
                        {{ server['stats']['desc'] }} <br />
                      {% end %}

                      {% if server['stats']['version'] != 'False' %}
                        {{ server['stats']['version'] }}
                      {% end %}
                    {% end %}

                  </td>
                  <td>
                    {% if server['stats']['running'] %}
                    <i class="fas fa-thumbs-up"></i> <span class="text-success">{{ translate('dashboard', 'online', data['lang']) }}</span>
                    {% else %}
                    <i class="fas fa-thumbs-down"></i> <span class="text-danger">{{ translate('dashboard', 'offline', data['lang']) }}</span>
                    {% end %}
                  </td>
                </tr>
                {% end %}

              </tbody>
            </table>
            {% end %}
          </div>
        </div>
      </div>
    </div>
  </div>


</div>
<!-- content-wrapper ends -->
<style>
  .popover-body{
    color: white !important;;
}
</style>


{% end %}

{% block js %}
<script>
$(document).ready(function(){ 
  $('[data-toggle="popover"]').popover();
    if($(window).width() < 1000){
    $('.too_small').popover("show");
  }
  
});
 $(window).ready(function(){
        $('body').click(function(){
            $('.too_small').popover("hide");
    });
});
$(window).resize(function() {
  // This will execute whenever the window is resized
  if($(window).width() < 1000){
    $('.too_small').popover("show");
  }
  else{
    $('.too_small').popover("hide");
  } // New width
});
</script>
<script>

function send_command (server_id, command){
  /* this getCookie function is in base.html */
  var token = getCookie("_xsrf");

  $.ajax({
    type: "POST",
    headers: {'X-XSRFToken': token},
    url: '/server/command?command=' + command + '&id=' + server_id,
    success: function(data){
      console.log("got response:");
      console.log(data);
      setTimeout(function(){
      if (command != 'start_server'){
            location.reload();
          }
      }, 10000);

    }
  });
}

function send_kill (server_id){
  /* this getCookie function is in base.html */
  var token = getCookie("_xsrf");

  $.ajax({
    type: "POST",
    headers: {'X-XSRFToken': token},
    url: '/ajax/kill?id=' + server_id,
    success: function(data){
      console.log("got response:");
      console.log(data);
      setTimeout(function(){
            location.reload();
      }, 10000);

    }
  });
}

$( document ).ready(function() {
  console.log('ready for JS!')

  $( ".play_button" ).click(function() {
    server_id = $(this).attr("data-id");
    send_command(server_id, 'start_server');
    bootbox.alert({
      backdrop: true,
      title: '{% raw translate("dashboard", "sendingCommand", data['lang']) %}',
      message: '<div align="center"><i class="fas fa-spin fa-spinner"></i> &nbsp; {% raw translate("dashboard", "bePatientStart", data['lang']) %} </div>'
    });
  });

  $( ".stop_button" ).click(function() {
    console.log("stopping server");
    server_id = $(this).attr("data-id");
    send_command(server_id, 'stop_server');
    bootbox.alert({
      backdrop: true,
      title: '{% raw translate("dashboard", "sendingCommand", data['lang']) %}',
      message: '<div align="center"><i class="fas fa-spin fa-spinner"></i> &nbsp; {% raw translate("dashboard", "bePatientStop", data['lang']) %} </div>'
    });
  });

  $( ".restart_button" ).click(function() {
    server_id = $(this).attr("data-id");
    send_command(server_id, 'restart_server');
    bootbox.alert({
      backdrop: true,
      title: '{% raw translate("dashboard", "sendingCommand", data['lang']) %}',
      message: '<div align="center"><i class="fas fa-spin fa-spinner"></i> &nbsp; {% raw translate("dashboard", "bePatientRestart", data['lang']) %} </div>'
    });
  });
    $( ".kill_button" ).click(function() {
      server_id = $(this).attr("data-id");
      bootbox.confirm({
    message: "This will kill the server process and all it's subprocesses. Killing a process can potentially corrupt files. Only do this in extreme circumstances. Are you sure you would like to continue?",
    buttons: {
        confirm: {
            label: '{% raw translate("dashboard", "kill", data['lang']) %}',
            className: 'btn-danger'
        },
        cancel: {
            label: '{% raw translate("panelConfig", "cancel", data['lang']) %}',
            className: 'btn-secondary'
        }
    },
    callback: function (result) {
      if(result){
          send_kill(server_id);
          var dialog = bootbox.dialog({
    title: '{% raw translate("dashboard", "killing", data['lang']) %}',
    message: '<p><i class="fa fa-spin fa-spinner"></i> Loading...</p>'
});
            
dialog.init(function(){
    setTimeout(function(){
        location.reload();
    }, 15000);
});
      }
    }
});
  });
  if (webSocket) {
    cpu_data = document.getElementById('cpu_data');
    cpu_usage = document.getElementById('cpu_usage');
    mem_usage = document.getElementById('mem_usage');
    mem_percent = document.getElementById('mem_percent');

    webSocket.on('update_host_stats', function (hostStats) {
      var cpuDataTitle = `{% raw translate('dashboard', 'cpuCores', data['lang']) %}: ${hostStats.cpu_cores} <br /> {% raw translate("dashboard", "cpuCurFreq", data['lang']) %}: ${hostStats.cpu_cur_freq} <br /> {% raw translate("dashboard", "cpuMaxFreq", data['lang']) %}: ${hostStats.cpu_max_freq}`;
      cpu_data.setAttribute('data-original-title', cpuDataTitle);
      cpu_usage.textContent = hostStats.cpu_usage;
      mem_usage.setAttribute('data-original-title', `{% raw translate("dashboard", "memUsage", data['lang']) %}: ${hostStats.mem_usage}`);
      mem_percent.textContent = hostStats.mem_percent + '%';
    });
  }

    if (webSocket) {
    webSocket.on('send_start_reload', function () {
    location.reload()
    });
  }

      if (webSocket) {
    webSocket.on('update_button_status', function (updateButton) {
    var id = 'controls';
    var dataId = updateButton.server_id;
    var string = updateButton.string
    var id = id.concat(updateButton.server_id);
      if (updateButton.isUpdating){
        console.log(updateButton.isUpdating)
        document.getElementById(id).innerHTML = string;
      }
      else{
      window.location.reload()
        }
    });
  }

  $( ".clone_button" ).click(function() {
    server_id = $(this).attr("data-id");
    send_command(server_id, 'clone_server');
    bootbox.alert({
      backdrop: true,
      title: '{% raw translate("dashboard", "sendingCommand", data['lang']) %}',
      message: '<div align="center"><i class="fas fa-spin fa-spinner"></i> &nbsp; {% raw translate("dashboard", "bePatientClone", data['lang']) %} </div>'
    });
  });

});
</script>

{% end %}