$(document).on('click', '.menuTr', function (){
  const checkBox = $(this).find(".menuCheckBox")[0]
  checkBox.checked = !checkBox.checked;
  if(checkBox.checked){
    selectedList.push({
      menuKey: Number(checkBox.value),
      orderNum: selectedList.length+1,
      cat1Cd: $(this).find(".cat1Cd").val(),
      cat2Cd: $(this).find(".cat2Cd").val(),
      cat3Cd: $(this).find(".cat3Cd").val(),
      menuUrl: $(this).find(".menuUrl").val()
    });
  }else{
    const tempList = [];
    $.each(selectedList, function (idx, menu){
      if(menu.menuKey !== Number(checkBox.value)){
        menu.orderNum = idx+1;
        tempList.push(menu);
      }
    })
    selectedList = tempList;
  }
})

$(document).on('click', '#getMenuBtn', function (){
  $.ajax({
    type : 'POST',
    url : "/myInfo/selectedMenuTable",
    data : JSON.stringify(selectedList),
    contentType: 'application/json',
    dataType:"html",
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success : function(html) {
      $("#dashboardConfigTbody").empty().append($(html)[1].children[0].children);
      $("#menuModal").modal("hide");
    },
    error : function(xhr, status) {

    }
  })
})

function setSelectedChkBox(){
  $.each(selectedList, function (idx, item){
    $("[value="+item.menuKey+"]").prop("checked", true);
  })
}