
$(document).on('click', '#addMenuBtn', function (){
  $("#menuEditModalLabel").text("메뉴 추가")
  getModalForm();
})
$(document).on('click', '.menuTr', function (event){
  if(event.target.classList.value!=="menuCheckBox"){
    if(event.target.cellIndex !== 0){
      $("#menuEditModalLabel").text("메뉴 수정")
      const row = $(this)
      getModalForm({
        menuKey: row.find(".menuKey").val(),
        cat1Cd: row.find(".cat1Cd").val(),
        cat2Cd: row.find(".cat2Cd").val(),
        cat3Cd: row.find(".cat3Cd").val(),
        menuUrl: row.find(".menuUrl").val(),
        approvalChk: row.find(".approvalChk").val(),
        useChk: row.find(".useChk").val()
      })
    }
  }
})
$(document).on('click', '#saveMenuBtn', function (){
  let valueChk = true;
  if(!$("#cat1Cd").val()){
    alert("대분류를 선택해주세요.")
    valueChk = false;
  }
  if(!$("#cat2Cd").val()){
    alert("중분류를 선택해주세요.")
    valueChk = false;
  }
  if(!$("#menuUrl").val()){
    alert("URL을 입력해주세요.")
    valueChk = false;
  }
  if(valueChk && confirm("저장하시겠습니까?")){
    contentFade("in");
    const formData = new FormData($("#menuEditForm")[0]);
    $.ajax({
      type : 'POST',
      data : formData,
      url : "/menuMgt/saveMenuMgt",
      processData: false,
      contentType: false,
      beforeSend: function (xhr){
        xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
      },
      success : function(result) {
        if(result!==""){
          alert("url: "+result+"로 저장되어있습니다.")
        }else{
          // location.reload();
          alert("저장되었습니다.")
        }
        contentFade("out");
      },
      error : function(xhr, status) {
        alert("저장에 실패하였습니다.")
        contentFade("out");
      }
    })
  }
})
$(document).on('click', '#deleteMenuBtn', function (){
  if(confirm("삭제 하시겠습니까?")){
    deleteMenu([{menuKey: Number($("#menuKey").val())}])
  }
})
$(document).on('click', '#deleteCheckedBtn', function (){
  if(confirm("선택된 대상을 모두 삭제 하시겠습니까?")){
    const menuList = [];
    $(".menuCheckBox:checked").each(function (idx, el){
      menuList.push({});
      const target = $(el);
      menuList[idx].menuKey = Number(target.parents('tr').find('.menuKey').val());
    })
    deleteMenu(menuList)
  }
})
function getModalForm(menuMgt){
  $.ajax({
    url: '/menuMgt/menuEditModal',
    data: menuMgt,
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#menuEditModalContent").empty().append(html)
      $("#menuEditModal").modal('show')
    },
    error:function(){

    }
  });
}

function deleteMenu(menuList){
  contentFade("in");
  $.ajax({
    type : 'POST',
    url : "/menuMgt/deleteMenuMgt",
    data : JSON.stringify(menuList),
    contentType: 'application/json',
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success : function(data) {
      alert("삭제되었습니다.");
      contentFade("out");
      location.reload();
    },
    error : function(xhr, status) {
      alert("삭제를 실패하였습니다.")
      contentFade("out");
    }
  })
}