{% extends "base.html" %}
{% block content %}
<style type="text/css">
  .my_hover:hover{
    background-color: #ffff00;
    transition: all 0.01s ease-in-out;
  }
</style>

<div>
  <nav>  
    {{ macros.m_tab_head_start() }}
      {{ macros.m_tab_head2('normal', '일반', false) }}
      {{ macros.m_tab_head2('list', '플러그인 목록', true) }}
    {{ macros.m_tab_head_end() }}
  </nav>  

  <div class="tab-content" id="nav-tabContent">
    
    {{ macros.m_tab_content_start('normal', false) }}
      <form id='setting' name='setting'>
        {{ macros.setting_input_text('plugin_dev_path', '개발용 플러그인 경로', value=arg['plugin_dev_path'], desc=['플러그인을 개발할 때 사용하는 경로'], col='9') }}
        {{ macros.setting_button([['setting_save', '저장']]) }}

      </form>
    </form>
    {{ macros.m_tab_content_end() }}   
 
    {{ macros.m_tab_content_start('list', true) }}
      {{ macros.setting_input_text_and_buttons('plugin_git', '플러그인 수동 설치', [['plugin_install_btn', '설치']], value='https://github.com/', desc=['SJVA.ME 플러그인 게시판에 있는 링크 주소를 입력하세요.']) }}

      {{ macros.m_hr_head_top() }}
      {{ macros.m_row_start('0') }}
      {{ macros.m_col(3,  macros.m_strong('Name')) }}
      {{ macros.m_col(1,  macros.m_strong('Dev.')) }}
      {{ macros.m_col(1,  macros.m_strong('Category')) }}
      {{ macros.m_col(1,  macros.m_strong('Version')) }}
      {{ macros.m_col(6,  macros.m_strong('Description')) }}
      {{ macros.m_row_end() }}
      {{ macros.m_hr_head_bottom() }}
      <div id="plugin_list_div"></div>
    {{ macros.m_tab_content_end() }} 
  </div><!--tab-content-->
</div> <!--전체-->


<script type="text/javascript">
var package_name = 'system';
var current_data;
var install = "{{arg['install']}}";

$(document).ready(function(){
  $.ajax({
    url: '/' + package_name + '/ajax/plugin_list',
    type: "POST", 
    cache: false,
    data: {},
    dataType: "json",
    success: function (data) {
      current_data = data
      make_plugin_list();
      if (install != '') {
        $('#plugin_git').val(install);
        //notify('플러그인이 설치되어 있지 않습니다.', 'danger');
      }
    }
  });
});

//설정 저장
$("#setting_save").click(function(e) {
  e.preventDefault();
  var formData = get_formdata('#setting');
  setting_save_func(formData, true)
  //
});


function setting_save_func(formData, noti) {
  $.ajax({
    url: '/' + package_name + '/ajax/setting_save',
    type: "POST", 
    cache: false,
    data: formData,
    dataType: "json",
    success: function (ret) {
      if (ret) {
        if (noti) {
          $.notify('<strong>설정을 저장하였습니다.</strong>', {
            type: 'success'
          });
        } else {
          window.location.href = "/"
        }
      } else {
        $.notify('<strong>설정 저장에 실패하였습니다.</strong>', {
          type: 'warning'
        });
      }
    }
  });
}

function make_plugin_list() {
  str = ''
  console.log(current_data)
  for (i in current_data) {
    console.log(i)
    console.log(current_data[i])
    str += m_row_start();
    str += m_col(3, i)
    
    if (current_data[i].info != null) {
      str += m_col(1, current_data[i].info.developer);
      str += m_col(1, current_data[i].info.category);
      str += m_col(1, current_data[i].info.version);
      tmp = ''
      tmp += m_button('plugin_uninstall_btn', '삭제', [{'key':'plugin_name', 'value':current_data[i].info.name}]);
      if (current_data[i].info.local_info != null) {
      tmp += m_button('global_link_btn', 'GIT', [{'key':'url', 'value':current_data[i].info.local_info.home}]);
      if (current_data[i].info.local_info.home != current_data[i].info.local_info.more && current_data[i].info.local_info.more.startsWith('http'))
        tmp += m_button('global_link_btn', 'MORE', [{'key':'url', 'value':current_data[i].info.local_info.more}]);
      }

      tmp = m_button_group(tmp)
      str += m_col(6, current_data[i].info.description +'<br><br>'+ tmp)
    }
    str += m_row_end();
    if (i != current_data.length -1) str += m_hr(0);
  }
  document.getElementById("plugin_list_div").innerHTML = str;
}



$("body").on('click', '#plugin_install_btn', function(e){ 
  e.preventDefault();
  plugin_git = document.getElementById("plugin_git").value

  $.ajax({
    url: '/' + package_name + '/ajax/plugin_install',
    type: "POST", 
    cache: false,
    data:{plugin_git:plugin_git},
    dataType: "json",
    success: function (data) {
      t = (data.ret == 'success') ? 'success' : 'warning'
      $.notify('<strong>'+data.log+'</strong>', {
        type: t
      });
    }
  });  
});


$("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 %}
