
$(document).on('click', '.userInfoTr', function (){
	if($('#userStatus').val() == 'USC003'){
  $.ajax({
    url: '/userMgt/userEditModal',
    data: {userSeq: Number($(this).find(".userSeq").val())},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#configInfo").empty().append(html)
      $("#userEditModal").modal('show');
    },
    error:function(){

    }
  });
  }
})

$(document).on('click', '#logTab', function (){
	location.href='/userMgt/userMgtPage?userStatus=USC002'; 
})

$(document).on('click', '#contentTab', function (){
	location.href='/userMgt/userMgtPage?userStatus=USC003';
})

$(document).ready( function() {
  $('#chk-all').click( function() {
    $('.userInfoCheckBox').prop('checked',this.checked);
  });
});

$(document).on('click', '#approvalBtn', function (){
	if(confirm("선택한 대상을 승인처리 하시겠습니까?")){
	 const checkArr = [];
		$('input:checkbox[name=userChk]:checked').each(function (idx, el){
	      checkArr.push({});
	      const target = $(el);
	      checkArr[idx].userSeq = Number(target.parents('tr').find('.userSeq').val());
	      checkArr[idx].userStatus = "USC003"
    })
		userApproval(checkArr);
	}	
})

function userApproval(checkArr){
	$.ajax({
    type : 'POST',
    url : "/userMgt/userApproval",
    data : JSON.stringify(checkArr),
    contentType: 'application/json',
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success : function(data) {
      alert(data+"건이 승인 처리되었습니다.");
      location.reload();
    },
    error : function(xhr, status) {
      alert("승인처리에 실패하였습니다");
    }
  })	
}

$(document).on('click', '#companionBtn', function (){
	if(confirm("선택한 대상을 반려처리 하시겠습니까?")){
	 const checkArr = [];
		$('input:checkbox[name=userChk]:checked').each(function (idx, el){
	      checkArr.push({});
	      const target = $(el);
	      checkArr[idx].userSeq = Number(target.parents('tr').find('.userSeq').val());
	      checkArr[idx].userStatus = "USC005"
    })
		userCompanion(checkArr);
	}	
})

function userCompanion(checkArr){
	$.ajax({
    type : 'POST',
    url : "/userMgt/userCompanion",
    data : JSON.stringify(checkArr),
    contentType: 'application/json',
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success : function(data) {
      alert(data+"건이 반려 처리되었습니다.");
      location.reload();
    },
    error : function(xhr, status) {
      alert("반려처리에 실패하였습니다");
    }
  })	
}

$(document).on('click', '#updateBtn', function (){
    if(confirm("저장하시겠습니까?")){
      contentFade("in");
      const formData = new FormData($("#userInfoUpdate")[0]);
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/userMgt/updateUserInfo",
        processData: false,
        contentType: false,
        success : function() {
          alert("저장되었습니다.");
          contentFade("out");
        },
        error : function(xhr, status) {
          alert("저장에 실패하였습니다.");
          contentFade("out");
        }
      })    
  }
})

$(document).on('click', '#deleteBtn', function (){
  if(confirm("삭제 하시겠습니까?")){
	console.log($("#userSeq").val());
    deleteUser([{userSeq: Number($("#userSeq").val())}])
  }
})

function deleteUser(userList){
	$.ajax({
    type : 'POST',
    url : "/userMgt/userDelete",
    data : JSON.stringify(userList),
    contentType: 'application/json',
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success : function() {
      alert("삭제 처리되었습니다.");
      location.reload();
    },
    error : function(xhr, status) {
      alert("삭제 처리에 실패하였습니다");
    }
  })	
}

	