{% extends "base.html" %}
{% block content %}
<div>
  {{ macros.m_hr_head_top() }}
  {{ macros.m_row_start('0') }}
  {{ macros.m_col_wide(1, macros.m_strong('Idx'), 'center') }}
  {{ macros.m_col_wide(2, macros.m_strong('Title')) }}
  {{ macros.m_col_wide(2, macros.m_strong('Package Name')) }}
  {{ macros.m_col_wide(1, macros.m_strong('Dev.')) }}
  {{ macros.m_col_wide(1, macros.m_strong('Version')) }}
  {{ macros.m_col_wide(5, macros.m_strong('Description')) }}
  {{ macros.m_row_end() }}
  {{ macros.m_hr_head_bottom() }}
  <div id="plugin_list_div"></div>
</div>


<script type="text/javascript">

$(document).ready(function(){
  globalSendCommand('get_plugin_list', null, null, null, null, function(data){
    make_plugin_list(data.data);
  });
});

function make_plugin_list(data) {
  current_data = data;
  console.log(data);
  str = ''
  console.log(data)
  for (i in data) {
    console.log(data[i]);
    str += m_row_start();
    str += m_col_wide(1, (parseInt(i)+1), 'center')
    if (data[i].title == null) {
      str += m_col_wide(2, '');
      str += m_col_wide(2, data[i].package_name);
      str += m_col_wide(5, '');
      tmp = m_button_small('uninstall_btn', '삭제', [{'key':'package_name', 'value':data[i].package_name}]);
    } else {
      str += m_col_wide(2, data[i].title);
      
      str += m_col_wide(2, data[i].package_name);
      str += m_col_wide(1, data[i].developer);
      str += m_col_wide(1, data[i].version);
      if (data[i].loading == false) {
        tmp = data[i].description + '<br>' + text_color('[로딩 실패] ') + data[i].status;
        str += m_col_wide(3, tmp);

      } else {
        str += m_col_wide(3, data[i].description);
      }
      
      tmp = ''
      
      tmp += m_button_small('globalOpenBtn', '홈페이지', [{'key':'url', 'value':data[i].home}], 'primary');
      tmp += m_button_small('uninstall_btn', '삭제', [{'key':'package_name', 'value':data[i].package_name}, {'key':'title', 'value':data[i].title}]);
      tmp += m_button_small('json_btn', 'JSON', [{'key':'idx', 'value':i}], 'info');
    }
    tmp = m_button_group(tmp)
    str += m_col_wide(2, tmp, 'right')
    str += m_row_end();
    if (i != current_data.length -1) str += m_hr(0);
  }
  document.getElementById("plugin_list_div").innerHTML = str;
}

$("body").on('click', '#json_btn', function(e){
  e.preventDefault();
  item_id = $(this).data('idx');
  m_modal(current_data[item_id]);
});















$("body").on('click', '#uninstall_btn', function(e){ 
  e.preventDefault();
  $("#confirm_title").html("삭제 확인");
  $("#confirm_body").html($(this).data('title') + " 플러그인을 삭제 하시겠습니까?");
  package_name = $(this).data('package_name');
  $('#confirm_button').attr('onclick', "javascript:uninstall(package_name);");
  $("#confirm_modal").modal();
});


function uninstall(package_name) {
  globalSendCommand('uninstall', package_name, null, null, null, function(ret) {

  });
}



$("body").on('click', '#plugin_uninstall_btn', function(e){ 
  e.preventDefault();
  plugin_name = $(this).data('plugin_name')
  $.ajax({
    url: '/' + package_name + '/ajax/plugin_uninstall',
    type: "POST", 
    cache: false,
    data:{plugin_name:plugin_name},
    success: function (data) {
      if (data == 'success') {
        $.notify('<strong>재시작시 적용됩니다.</strong>', {
          type: 'success'
        });
      } else {
        $.notify('<strong>실패하였습니다.</strong>', {
          type: 'warning'
        });
      }
    }
  });  
});




</script>    
{% endblock %}
