$(function (){
  $("#dateSelectorDiv").datepicker({
    format: "yyyy-mm-dd",
    language: "ko"
  });
})
$(document).on('click', '.categoryTr', function (){
  if(confirm(this.innerText+" 페이지로 이동하시겠습니까?")){
    location.href = "/board/contentList?categorySeq="+$(this).attr("data-categoryseq")
  }
})
$(document).on('click', '.contentTr', function (){
  $(".contentCheckBox").prop('checked', false);
  const target = $(this).find(".contentCheckBox")[0];
  target.checked = true;
  const selectedTab = $(".nav-tabs").find(".active")[0].id;
  if(selectedTab === "contentTab"){
    getBoardContent(target.value);
  }else if(selectedTab === "logTab"){
    getBoardLog(target.value);
  }
})
$(document).on('click', '.page-item', function (){
  if(location.pathname.indexOf("contentList")>0){
    $("#pageIndex").val($(this).attr("data-pageindex"));
    $("#searchBtn").click();
  }else{ // 메인페이지
    location.href = location.pathname+"?pageIndex="+$(this).attr("data-pageindex");
  }
})
$(document).on('change', '#fileCheckAll', function (){
  $(".fileCheckBox").prop("checked", this.checked);
})

$(document).on('click', '#contentTab', function (){
  getBoardContent(getContentSeq())
})
$(document).on('click', '#logTab', function (){
  getBoardLog(getContentSeq())
})

$(document).on('click', '.fileDownLink', function (){
  let url = "/board/fileDownload?"
  url += "contentSeq="+Number($("#detailViewContentSeq").val());
  url += "&fileSeq="+$(this).attr("data-fileseq");
  window.open(encodeURI(url));
})

$(document).on('click', '#zipDownBtn', function (){
  const checkFiles = $(".fileCheckBox:checked");
  if(checkFiles.length>0){
    let url = "/board/fileDownloadToZip?"
    url += "contentSeq="+Number($("#detailViewContentSeq").val());
    checkFiles.each(function (idx, el){
      url += "&fileSeq="+Number($(el).attr("data-fileseq"))
    });
    window.open(encodeURI(url));
  }else{
    alert("선택된 파일이 없습니다.")
  }
})

$(document).on('click', '#deleteBtn', function (){
  if(confirm("이 게시물을 삭제하시겠습니까?\n되돌릴 수 없습니다.")){
    $.ajax({
      type : 'DELETE',
      url : "/board/deleteContent",
      data : {contentSeq: Number($(this).attr("data-contentseq"))},
      beforeSend: function (xhr){
        xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
      },
      success : function(data) {
        alert("저장되었습니다.");
        location.reload();
      },
      error : function(xhr, status) {

      }
    })
  }
})
$(document).on('click', '#modifyBtn', function (){
  location.href = "/board/contentWrite?contentSeq="+Number($(this).attr("data-contentseq"));
})

function getContentSeq(){
  return $(".contentCheckBox:checked").val();
}

function getBoardContent(contentSeq){
  if(contentSeq !== undefined){
    const viewContentSeq = $("#detailViewContentSeq").val();
    if(contentSeq !== viewContentSeq){
      $.ajax({
        url: '/board/selectBoardContent',
        data: {contentSeq: contentSeq},
        type: 'GET',
        dataType:"html",
        success: function(html){
          $("#contentDiv").empty().append(html)
          if($("#contentStatus").val() !== "D"){
            const viewCntTd = $(".contentCheckBox:checked").parents("tr").find(".viewCntTd");
            viewCntTd.text(Number(viewCntTd.text())+1);
          }
        },
        error:function(){

        }
      });
    }
  }
}
function getBoardLog(contentSeq){
  if(contentSeq !== undefined){
    $.ajax({
      url: '/board/selectBoardLog',
      data: {contentSeq: contentSeq},
      type: 'GET',
      dataType:"html",
      success: function(html){
        $("#logDiv").empty().append(html)
      },
      error:function(){

      }
    });
  }
}