$(function (){
  $("#description").summernote({
    lang:'ko-KR',
    height: 120,
    disableDragAndDrop: true,
    toolbar: [
      ['style', ['style']],
      ['font', ['bold', 'underline', 'clear']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['table', ['table']]
    ]
  })
  if(pageType === "modify"){
    $("#modifyInfoDiv").empty().append($("#"+$("#categorySelector").val()+"Div").children().clone());
  }
})
$(document).on('change', '#categorySelector', function (){
  $("#modifyInfoDiv").empty().append($("#"+this.value+"Div").children().clone());
})
$(document).on('click', '#saveBtn', function (){
  if(contentCheck()){
    if(confirm("저장하시겠습니까?")){
      saveModifyRequest();
    }
  }
})

$(document).on('change', '.parentSeq', function (){
  childCategoryStatusChange(this.value, this.checked);
})

function saveModifyRequest(){
  const formData = new FormData($("#requestForm")[0]);
  $.ajax({
    type : 'POST',
    data : formData,
    url : "/info/saveModifyRequest",
    processData: false,
    contentType: false,
    success : function(result) {
      if(result>0){
        alert("저장되었습니다.");
        location.href = "/info/modifyRequestList";
      }
    },
    error : function(xhr, status) {

    }
  })
}

function childCategoryStatusChange(parentSeq, flag){
  const target = $("[data-parentseq='"+parentSeq+"']");
  if(target.length>0){
    target.prop("checked", flag);
    target.each(function(idx, el){
      childCategoryStatusChange(el.value, flag);
    })
  }
}

function contentCheck(){
  let flag = true;
  if(!$("#categorySelector").val()){
    alert("분류 선택이 되지 않았습니다.")
    flag = false;
  }
  if(!$("#title").val()){
    alert("제목을 입력해주세요.")
    flag = false;
  }
  return flag;
}
