function contentFade(action){
  if(action === "in"){
    $("#fadeDiv").show()
  }else{
    $("#fadeDiv").hide()
  }
}

function sessionReload(){
  $.ajax({
    url: '/refreshSession',
    type: 'GET',
    success: function(){location.reload();},
    error:function(){}
  });
}

$(document).on('click', '.allChk', function (){
  $(this).parents('table').find('[type="checkbox"]').prop("checked", this.checked);
})
$(document).on('click', '.page-item', function (){
  searchFormSubmit($(this).attr("data-pageindex"))
})
$(document).on('change', '#rowCnt', function (){
  searchFormSubmit(1)
})
function searchFormSubmit(pageIndex){
  $("#pageIndex").val(pageIndex);
  $("#searchBtn").click();
}


$(document).on('mouseenter', '.firstMenuLink', function (event){
  $(".secondMenu").hide();
  const targetMenu = $(this).parent().find(".secondMenu");
  targetMenu.show()
  targetMenu.find("ul").css("padding-top",(event.clientY-20)+"px")
})
$(document).on('mouseenter', '.secondMenuLink', function (event){
  $(".thirdMenu").hide();
  const targetMenu = $(this).parent().find(".thirdMenu");
  targetMenu.show()
  targetMenu.find("ul").css("padding-top",(event.clientY-20)+"px")
})
$(document).on('mouseleave', '.menuDiv', function (){
  $(".secondMenu").hide();
  $(".thirdMenu").hide();
})

$(document).on('change', '#fileInputer', function (){
  for(const file of this.files){
    setFileDiv(file, files.push(file));
  }
  this.value = null;
})

function byteCalculation(size) {
  const bytes = parseInt(size);
  const s = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
  const e = Math.floor(Math.log(bytes)/Math.log(1024));

  if(e === "-Infinity") return "0 "+s[0];
  else return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];

}

$(document).on('click', '.fileDelete', function (){
  const target = $(this);
  files[Number(target.attr("data-fileidx"))].isDelete = true;
  target.parent().remove();
  const uploadDiv = $("#uploadDiv");
  if(uploadDiv.children().length === 0){
    uploadDiv.append("<br>파일을 업로드 해주세요.");
  }
})
$(document).on('click', '.uploadedFileDelete', function (){
  const target = $(this).parent().find("span")[0];
  if(target.className===""){
    target.className = "text-decoration-line-through";
  }else{
    target.className = "";
  }
})

$(document).on('click', '.fileDownLink', function (){
  const target = $(this)
  let url = "/file/fileDownload?"
  url += "board="+target.attr("data-board");
  url += "&parentKey="+target.attr("data-parentkey");
  url += "&fileSeq="+target.attr("data-fileseq");
  window.open(encodeURI(url));
})


function setUploadDiv(){
  files = [];
  $("#uploadDiv").on("dragenter", function(e) {
    // $(this).addClass('drag-over');
  }).on("dragleave", function(e) {
    // $(this).removeClass('drag-over');
  }).on("dragover", function(e) {
    e.stopPropagation();
    e.preventDefault();
  }).on('drop', function(e) {
    e.preventDefault();
    // $(this).removeClass('drag-over');
    for(const file of e.originalEvent.dataTransfer.files){
      setFileDiv(file, files.push(file));
    }
  }).on('click', function (e){
    if(e.target.className.indexOf("ileDelete")<0){
		if( e.target.className.indexOf("artInfo")<0){
	      $("#fileInputer").click();
	    }
    }
  });
}

function setFileDiv(file, idx){
  const uploadDiv = $("#uploadDiv");
  if($(".uploadedFileDelete").length===0 && $(".fileDelete").length === 0){
    uploadDiv.empty();
  }
  let fileInfo = "<div class='row-col-6'>";
  fileInfo += file.name+" "+byteCalculation(file.size)+" ";
  fileInfo += "<a href='#' class='fileDelete text-danger text-decoration-none' data-fileidx='"+(idx-1)+"'>삭제</a>";
  fileInfo += "</div>";
  uploadDiv.append(fileInfo);
}

function fileCheck(flag, files){
  let totalSize = 0;
  for(const file of files) {
    if(!file.isDelete){
      totalSize+=file.size;
      if(file.size>209715200){
        alert("파일당 사이즈는 200MB을 넘길 수 없습니다.")
        flag = false;
      }
    }
  }
  if(totalSize>524288000){
    alert("첨부파일의 용량 합은 500MB를 넘길 수 없습니다.")
    flag = false;
  }
  return flag;
}