{% 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', '일반', true) }}
      {{ macros.m_tab_head2('web', '웹', false) }}
      {{ macros.m_tab_head2('menu', '메뉴', false) }}
      {{ macros.m_tab_head2('link', '링크', false) }}
      {{ macros.m_tab_head2('download', '다운로드', false) }}
    {{ macros.m_tab_head_end() }}
  </nav>  

  <div class="tab-content" id="nav-tabContent">
    {{ macros.m_tab_content_start('normal', true) }}
      <form id='setting' name='setting'>
        {{ macros.setting_input_int('port', 'Port', value=arg['port'], min='1', placeholder='Port', desc=['포트 번호입니다.', '네이티브 설치 혹은 도커  네트워크 타입이 호스트일 경우 반영됩니다.', '도커 브릿지 모드인 경우는 docker run -p 옵션에서 변경하시기 바랍니다.', '경고 : -p 브릿지 모드로 사용중 일 경우 9999번을 절대 변경하지 마세요.']) }}
        {{ macros.setting_input_text_and_buttons('ddns', 'DDNS', [['ddns_test_btn', '테스트']], value=arg['ddns'], desc=['외부에서 접근시 사용할 DDNS. http:// 나 https:// 로 시작해야합니다.', 'RSS, Plex Callback, KLive 등에서 URL생성시 사용합니다.', '테스트 버튼 클릭 후 버전을 확인 할 수 있어야 합니다.']) }}
        {{ macros.setting_input_text('auto_restart_hour', '자동 재시작 시간', value=arg['auto_restart_hour'], col='3', desc=['자동 재시작 간격(시간단위)이나 Cron 설정을 입력합니다.', '0이면 재시작 안함.']) }}
        {{ macros.setting_select('log_level', '로그 레벨', [['10', 'DEBUG'],['20', 'INFO'],['30', 'WARNING'],['40', 'ERROR'], ['50', 'CRITICAL'] ], value=arg['log_level'], col='3') }}
        {{ macros.setting_button([['setting_save', '저장']]) }}
      </form>
    </form>
      {{ macros.m_hr() }}
      {{ macros.setting_input_text_and_buttons('command_text', 'Command', [['command_run_btn', 'Run']], value='', desc='') }}
    {{ macros.m_tab_content_end() }}   
    

    {{ macros.m_tab_content_start('web', false) }}
      <form id='setting2' name='setting2'>
        {{ macros.setting_select('theme', '테마 선택', [['Default','Default'], ['Cerulean','Cerulean'], ['Cosmo','Cosmo'], ['Cyborg','Cyborg'], ['Darkly','Darkly'], ['Flatly','Flatly'], ['Journal','Journal'], ['Litera','Litera'], ['Lumen','Lumen'], ['Lux','Lux'], ['Materia','Materia'], ['Minty','Minty'], ['Morph','Morph'],['Pulse','Pulse'], ['Quartz','Quartz'], ['Sandstone','Sandstone'], ['Simplex','Simplex'], ['Sketchy','Sketchy'], ['Slate','Slate'], ['Solar','Solar'], ['Spacelab','Spacelab'], ['Superhero','Superhero'], ['United','United'], ['Vapor','Vapor'], ['Yeti','Yeti'], ['Zephyr','Zephyr']], value=arg['theme'], desc=['https://bootswatch.com'], col='6') }}
        {{ macros.setting_input_text('web_title', '웹 타이틀', value=arg['web_title']) }}
        {{ macros.setting_button([['setting_save2', '저장']]) }}
      </form>
    {{ macros.m_tab_content_end() }} 


    {{ macros.m_tab_content_start('menu', false) }}
      <form id='setting3' name='setting3'>
      {% if arg['use_category_vod'] == 'True' %}
        {{ macros.m_hr() }}
        {{ macros.setting_button_with_info([['menu_toggle_btn', 'Toggle', [{'key':'category', 'value':'vod'}]]], left='VOD', desc=None) }}
          <div id="menu_vod_div" class="collapse">
            {{ macros.setting_checkbox('use_plugin_ffmpeg', 'FFMPEG', value=arg['use_plugin_ffmpeg']) }}
          </div>
      {% endif %}
      {% if arg['use_category_file_process'] == 'True' %}
        {{ macros.m_hr() }}
        {{ macros.setting_button_with_info([['menu_toggle_btn', 'Toggle', [{'key':'category', 'value':'file_process'}]]], left='파일처리', desc=None) }}
          <div id="menu_file_process_div" class="collapse">
            {{ macros.setting_checkbox('use_plugin_ktv', '국내TV', value=arg['use_plugin_ktv']) }}
            {{ macros.setting_checkbox('use_plugin_fileprocess_movie', '영화', value=arg['use_plugin_fileprocess_movie']) }}
          </div>
      {% endif %}
      {% if arg['use_category_plex'] == 'True' %}
        {{ macros.m_hr() }}
        {{ macros.setting_button_with_info([['menu_toggle_btn', 'Toggle', [{'key':'category', 'value':'plex'}]]], left='PLEX', desc=None) }}
          <div id="menu_plex_div" class="collapse">
            {{ macros.setting_checkbox('use_plugin_plex', 'PLEX', value=arg['use_plugin_plex']) }}
            {{ macros.setting_checkbox('use_plugin_gdrive_scan', 'GDrive 스캔', value=arg['use_plugin_gdrive_scan']) }}
          </div>
      {% endif %}
      {% if arg['use_category_tool'] == 'True' %}
        {{ macros.m_hr() }}
        {{ macros.setting_button_with_info([['menu_toggle_btn', 'Toggle', [{'key':'category', 'value':'tool'}]]], left='툴', desc=None) }}
          <div id="menu_tool_div" class="collapse">
            {{ macros.setting_checkbox('use_plugin_rclone', 'RClone', value=arg['use_plugin_rclone']) }}
            {{ macros.setting_checkbox('use_plugin_daum_tv', 'Daum TV', value=arg['use_plugin_daum_tv']) }}
          </div>
      {% endif %}
        {{ macros.setting_button([['setting_save3', '저장']]) }}
      </form>
    {{ macros.m_tab_content_end() }} 


    {{ macros.m_tab_content_start('link', false) }}
      {{ macros.m_button_group([['link_add_btn', '추가'], ['link_add_divider_btn', 'Divider Line 추가'], ['link_save_btn', '저장'], ['link_reset_btn', '초기화']])}}
      {{ macros.m_row_start('0') }}
      {{ macros.m_row_end() }}

      {{ macros.m_hr_head_top() }}
      {{ macros.m_row_start('0') }}
      {{ macros.m_col(1,  macros.m_strong('Idx')) }}
      {{ macros.m_col(4,  macros.m_strong('Title')) }}
      {{ macros.m_col(4,  macros.m_strong('URL')) }}
      {{ macros.m_col(3,  macros.m_strong('Action')) }}
      {{ macros.m_row_end() }}
      {{ macros.m_hr_head_bottom() }}
      <form id="link_form" name="link_form">
        <div id="link_list_div"></div>
      </form>
    {{ macros.m_tab_content_end() }} 

    {{ macros.m_tab_content_start('download', false) }}
      {{ macros.setting_button_with_info([['global_link_btn', '다운로드', [{'key':'url', 'value':'https://github.com/soju6jan/soju6jan.github.io/blob/master/etc/hdhomerun_scan_191214.zip'}]], ['global_link_btn', '매뉴얼', [{'key':'url', 'value':'.'}]]], left='HDHomerun Scan Tool', desc=['HDHomerun 스캔하여 TVH용 프리셋 파일을 만들어주는 Windows용 프로그램', '8VSB 지원 케이블용']) }}
      <!--
      {{ macros.setting_button_with_info([['global_link_btn', '다운로드', [{'key':'url', 'value':'https://github.com/soju6jan/soju6jan.github.io/raw/master/etc/sjva_lc_0.1.1.apk'}]], ['global_link_btn', '매뉴얼', [{'key':'url', 'value':'.'}]]], left='SJVA for Live Channels', desc=['Android TV Live Channels 앱에 채널 소스를 제공하는 앱.', 'Klive, Plex 지원']) }}
      {{ macros.setting_button_with_info([['global_link_btn', '티빙 애드온', [{'key':'url', 'value':'https://github.com/soju6jan/soju6jan.github.io/blob/master/kodi_plugin/plugin.video.tving.zip'}]]], left='KODI', desc=None) }}
      -->
    {{ macros.m_tab_content_end() }} 

  </div><!--tab-content-->
</div> <!--전체-->

<!-- 링크 모달 -->
{{ macros.m_modal_start('link_edit_modal', '링크', 'modal-lg') }}
  <form id="link_form">
    <input type="hidden" id="link_edit_index" name="link_edit_index"/>
    {{ macros.setting_input_text('link_edit_title', '제목') }}
    {{ macros.setting_input_text('link_edit_url', 'URL') }}
    {{ macros.setting_button([['link_edit_confirm_btn', '확인'], ['link_edit_cancel_btn', '취소']]) }}
  </form>
{{ macros.m_modal_end() }}


<script type="text/javascript">
var package_name = 'system';
var current_data;
var link_data;

$(document).ready(function(){
  $(function() {
  });

  $.ajax({
    url: '/' + package_name + '/ajax/get_link_list',
    type: "POST", 
    cache: false,
    data: {},
    dataType: "json",
    success: function (data) {
      link_data = data
      make_link_data();
    }
  });
});

function setting_save_func(formData, noti) {
  $.ajax({
    url: '/' + package_name + '/ajax/setting_save_system',
    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'
        });
      }
    }
  });
}

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

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

$("#setting_save4").click(function(e) {
  e.preventDefault();
  var formData = get_formdata('#setting4');
  setting_save_func(formData, true)
});

$("#setting_save3").click(function(e) {
  e.preventDefault();
  var formData = get_formdata('#setting3');
  setting_save_func(formData, true)
  $.notify('<strong>재시작해야 적용됩니다.</strong>', {
    type: 'success'
  });
});


$("body").on('click', '#ddns_test_btn', function(e){ 
  e.preventDefault();
  ddns = document.getElementById('ddns').value;
  $.ajax({
    url: '/' + package_name + '/ajax/ddns_test',
    type: "POST", 
    cache: false,
    data:{ddns:ddns},
    dataType: "json",
    success: function (data) {
      console.log(data)
      if (data == 'fail') {
        $.notify('<strong>접속에 실패하였습니다.</strong>', {
          type: 'warning'
        });
      } else {
        $.notify('<strong>Version:'+ data+'</strong>', {
          type: 'success'
        });
      }
    }
  });
});


 

$("body").on('click', '#menu_toggle_btn', function(e){ 
  e.preventDefault();
  category = $(this).data('category')
  var div_name = '#menu_'+category+'_div'
  $(div_name).collapse('toggle')
});

$("body").on('click', '#command_run_btn', function(e){ 
  e.preventDefault();
  command_text = document.getElementById('command_text').value;
  $.ajax({
    url: '/' + package_name + '/ajax/command_run',
    type: "POST", 
    cache: false,
    data:{command_text:command_text},
    dataType: "json",
    success: function (data) {
      if (data.ret == 'success') {
        $.notify('<strong>성공 : '+ data.log +'</strong>', {
          type: 'success'
        });
      } else {
        $.notify('<strong>실패 : ' + data.log+'</strong>', {
          type: 'warning'
        });
      }
    }
  });
});

//////////////////////////////////////////////////////////////////////////////
// 링크
//////////////////////////////////////////////////////////////////////////////

// 화면 상단 버튼 START
$("body").on('click', '#link_add_btn', function(e){ 
  e.preventDefault();
  document.getElementById("link_edit_index").value = -1;
  document.getElementById('link_edit_title').value = '';
  document.getElementById('link_edit_url').value = '';
  $('#link_edit_modal').modal();
});

$("body").on('click', '#link_add_divider_btn', function(e){ 
  e.preventDefault();
  tmp = {}
  tmp['type'] = 'divider'
  link_data.splice(link_data.length, 0, tmp);
  make_link_data()
});

$("body").on('click', '#link_save_btn', function(e){ 
  e.preventDefault();
  $.ajax({
    url: '/' + package_name + '/ajax/link_save',
    type: "POST", 
    cache: false,
    data:{link_data:JSON.stringify(link_data)},
    dataType: "json",
    success: function (data) {
      if (data) {
        $.notify('<strong>저장 후 적용하였습니다.</strong>', {
          type: 'success'
        });
      } else {
        $.notify('<strong>실패하였습니다.</strong>', {
          type: 'warning'
        });
      }
    }
  });
});

$("body").on('click', '#link_reset_btn', function(e){ 
  e.preventDefault();
  link_data = []
  make_link_data()
});
// 화면 상단 버튼 END

// 리스트 각 항목 별 버튼 START
$("body").on('click', '#link_item_up_btn', function(e){
  e.preventDefault();
  target_id = $(this).data('index')
  target = link_data[target_id]
  if (target_id != 0) {
    link_data.splice(target_id, 1);
    link_data.splice(target_id-1, 0, target);
  }
  make_link_data()
});

$("body").on('click', '#link_item_down_btn', function(e){
  e.preventDefault();
  target_id = $(this).data('index')
  target = link_data[target_id]
  if (link_data.length -1 != target_id) {
    link_data.splice(target_id, 1);
    link_data.splice(target_id+1, 0, target);
  }
  make_link_data()
});

$("body").on('click', '#link_item_delete_btn', function(e){
  e.preventDefault();
  target_id = $(this).data('index')
  target = link_data[target_id]
  link_data.splice(target_id, 1);
  make_link_data()
});

$("body").on('click', '#link_item_edit_btn', function(e){
  e.preventDefault();
  target_id = $(this).data('index')
  target = link_data[target_id]
  document.getElementById('link_edit_index').value = target_id
  document.getElementById('link_edit_title').value = target.title
  document.getElementById('link_edit_url').value = target.url
  $('#link_edit_modal').modal();
});
// 리스트 각 항목 별 버튼 END

// START 모달 버튼
$("body").on('click', '#link_edit_confirm_btn', function(e){
  e.preventDefault();
  edit_index = parseInt(document.getElementById('link_edit_index').value)
  tmp = {}
  tmp['type'] = 'link'
  tmp['title'] = document.getElementById('link_edit_title').value
  tmp['url'] = document.getElementById('link_edit_url').value
  if (edit_index == -1) {
    link_data.splice(link_data.length, 0, tmp);
  } else {
    link_data.splice(target_id, 1);
    link_data.splice(target_id, 0, tmp);
  }
  make_link_data()
  $('#link_edit_modal').modal('hide');  
});

$("body").on('click', '#link_edit_cancel_btn', function(e){
  e.preventDefault();
  $('#link_edit_modal').modal('hide');  
});
// END 모달 버튼



function make_link_data() {
  str = ''
  for (i in link_data) {
    //console.log(link_data[i])
    str += m_row_start_hover();
    str += m_col(1, parseInt(i)+1);

    if (link_data[i].type == 'link') {
      str += m_col(4, link_data[i].title)
      str += m_col(4, link_data[i].url)
    } else {
      str += m_col(8, '---Divider Line---')        
    }
    tmp = ''
    tmp += m_button('link_item_up_btn', 'UP', [{'key':'index', 'value':i}]);
    tmp += m_button('link_item_down_btn', 'DOWN', [{'key':'index', 'value':i}]);
    tmp += m_button('link_item_delete_btn', '삭제', [{'key':'index', 'value':i}]);
    if (link_data[i].type == 'link') {
      tmp += m_button('link_item_edit_btn', '편집', [{'key':'index', 'value':i}]);
      tmp += m_button('global_link_btn', 'Go', [{'key':'url', 'value':link_data[i].url}]);
    }
    tmp = m_button_group(tmp)
    str += m_col(3, tmp)
    str += m_row_end();
    if (i != link_data.length -1) str += m_hr(0);
  }
  document.getElementById("link_list_div").innerHTML = str;

    
    

}


$("body").on('click', '#go_filebrowser_btn', function(e){
  e.preventDefault();
  url = document.getElementById('url_filebrowser').value
  window.open(url, "_blank");
});



</script>    
{% endblock %}
