{% extends "base.html" %}
{% block content %}


<table id="result_table" class="table table-sm tableRowHover">
  <thead class="thead-dark">
    <tr>
      <th style="width:5%; text-align:center;">IDX</th>
      <th style="width:8%; text-align:center;">Plugin</th>
      <th style="width:10%; text-align:center;">시작시간</th>
      <th style="width:20%; text-align:center;">파일명</th>
      <th style="width:8%; text-align:center;">상태</th>
      <th style="width:15%; text-align:center;">진행률</th>
      <th style="width:5%; text-align:center;">길이</th>
      <th style="width:5%; text-align:center;">PF</th>
      <th style="width:8%; text-align:center;">배속</th>
      <th style="width:8%; text-align:center;">진행시간</th>
      <th style="width:8%; text-align:center;">Action</th>
    </tr>
  </thead>
  <tbody id="list"></tbody>
</table>

<script type="text/javascript">

$(document).ready(function(){
  var socket = io.connect(window.location.href);

  socket.on('on_start', function(data){
    document.getElementById("log").innerHTML += data.data;
    document.getElementById("log").scrollTop = document.getElementById("log").scrollHeight;
    document.getElementById("log").style.visibility = 'visible';
    $('#loading').hide();
  });

  socket.on('add', function(data){
    str = make_item(data);
    if (current_data == null || current_data.length == 0) {
      current_data = Array();
      $("#list").html(str);
    } else {
      $("#list").html($("#list").html() + str);
    }
    current_data.push(data);
  });

  socket.on('status_change', function(data) {
    button_html(data);
  });

  socket.on('status', function(data){
    status_html(data);
  });

  socket.on('last', function(data){
    status_html(data);
    button_html(data);
  });

  globalSendCommand('list', null, null, null, function(data) {
    current_data = data;
    $("#list").html('');
    console.log(data)
    if (data.length == 0) {
      str = "<tr><td colspan='10'><h4>작업이 없습니다.</h4><td><tr>";
    } else {
      str = ''
      for(i in data) {
        str += make_item(data[i]);
      }
    }
    $("#list").html(str);
  });
});


$("body").on('click', '#stop_btn', function(e){
  e.stopPropagation();
  e.preventDefault();
  globalSendCommand('stop', $(this).data('idx'), null, null, function(ret){
    refresh_item(ret.data);
  });
});


function refresh_item(data) {
  $('#tr1_'+data.idx).html(make_item1(data));
  $('#collapse_'+data.idx).html(make_item2(data));
}

function make_item(data) {
  str = '<tr id="tr1_'+data.idx+'" style="cursor: pointer;" data-toggle="collapse" data-target="#collapse_'+ data.idx + '" aria-expanded="true" >';
  str += make_item1(data);
  str += '</tr>';
  str += '<tr class="collapse tableRowHoverOff" style="cursor: pointer;"  id="collapse_' + data.idx + '">';
  str += make_item2(data);
  str += '</tr>';
  return str;
}

function make_item1(data) {
  //console.log(data);
  str = '';
  str += '<td scope="col" style="width:5%; text-align:center;">'+ data.idx + '</td>';
  str += '<td scope="col" style="width:8%; text-align:center;">'+ data.callback_id + '</td>';
  str += '<td scope="col" style="width:10%; text-align:center;">'+ data.start_time + '</td>';
  str += '<td scope="col" style="width:20%; text-align:center;">'+ data.filename + '</td>';
  str += '<td id="status_'+data.idx+'" scope="col" style="width:8%; text-align:center;">'+ data.status_kor + '</td>';
  var visi = 'hidden';
  if (parseInt(data.percent) > 0) {
    visi = 'visible';
  }
  str += '<td scope="col" style="width:20%; text-align:center;"><div class="progress"><div id="progress_'+data.idx+'" class="progress-bar" style="visibility: '+visi+'; width:'+data.percent+'%">'+data.percent +'%</div></div></td>';
  str += '<td scope="col" style="width:5%; text-align:center;">'+ data.duration_str + '</td>';
  str += '<td id="current_pf_count_'+data.idx+'" scope="col" style="width:5%; text-align:center;">'+ data.current_pf_count + '</td>';
  str += '<td id="current_speed_'+data.idx+'" scope="col" style="width:8%; text-align:center;">'+ data.current_speed + '</td>';
  str += '<td id="download_time_'+data.idx+'" scope="col" style="width:8%; text-align:center;">'+ data.download_time + '</td>';
  str += '<td id="button_'+data.idx+'" scope="col" style="width:8%; text-align:center;">';
  if (data.status_str == 'DOWNLOADING') {
    str += j_button('stop_btn', '중지', {'idx':data.idx}, 'danger', false, false);
  }
  str += '</td>'
  return str;
}

function make_item2(data) {
  str = '';
  str += '<td colspan="11">';
  str += '<div id="detail_'+data.idx+'">';
  str += get_detail(data);
  str += '</div>';
  str += '</td>';
  return str
}


function get_detail(data) {
  var str = j_row_info('URL', data.url);
  str += j_row_info('임시경로', data.temp_fullpath);
  str += j_row_info('저장경로', data.save_fullpath);
  str += j_row_info('진행률(current/total)', data.percent+ '% (' + data.current_duration + ' / ' + data.duration + ')');
  str += j_row_info('현재 비트레이트', data.current_bitrate);
  str += j_row_info('종료시간', data.end_time);
  str += j_row_info('허용 Packet Fail 수', data.max_pf_count);
  str += j_row_info('파일 Exist', data.exist);
  if (data.status_str == 'COMPLETED') {
    str += j_row_info('파일 크기', data.filesize_str);
    str += j_row_info('다운 속도', data.download_speed);
  }
  return str;
}

function button_html(data) {
  //console.log(data)
  str = '';
  if (data.status_str == 'DOWNLOADING') {
    str = j_button('stop_btn', '중지', {'idx':data.idx}, 'danger', false, false);
  }
  $("#button_" + data.idx).html(str);
}

function status_html(data) {
  var progress = document.getElementById("progress_" + data.idx);
  progress.style.width = data.percent+ '%';
  progress.innerHTML = data.percent+ '%';
  progress.style.visibility = 'visible';
  document.getElementById("status_" + data.idx).innerHTML = data.status_kor;
  document.getElementById("current_pf_count_" + data.idx).innerHTML = data.current_pf_count;
  document.getElementById("current_speed_" + data.idx).innerHTML = data.current_speed;
  document.getElementById("download_time_" + data.idx).innerHTML = data.download_time;
  document.getElementById("detail_" + data.idx).innerHTML = get_detail(data);
}

</script>
{% endblock %}
