$(function (){
  $("#dateSelectorDiv").datepicker({
    format: "yyyy-mm-dd",
    language: "ko"
  });
})

$(document).on('click', '.modifyRequestTr', function (){
  const checkBox = $(this).find(".modifyRequestCheckBox")
  $(".modifyRequestCheckBox").prop("checked", false);
  checkBox.prop("checked", true);
  getRequestDetail(checkBox.val());
})

$(document).on('click', '#deleteBtn', function (){
  if(confirm("삭제하시겠습니까?")){
    modifyRequestStatusChange({
      requestSeq: $(this).attr("data-requestseq"),
      status: 'delete'
    });
  }
})

$(document).on('click', '#dinialBtn', function (){
  if(confirm("요청을 거부 하시겠습니까?")){
    modifyRequestStatusChange({
      requestSeq: $(this).attr("data-requestseq"),
      status: 'dinial'
    })
  }
})

$(document).on('click', '#approvalBtn', function (){
  if(confirm("요청을 승인 하시겠습니까?")){
    const request = $(this)
    let flag = true;
    if(request.attr("data-requesttype")==="etc"){
      flag = confirm("기타 분류의 요청은 직접 요청 내용을 수정해주셔야 합니다.");
    }
    if(flag){
      modifyRequestStatusChange({
        requestSeq: request.attr("data-requestseq"),
        createId: request.attr("data-createid"),
        status: 'approval'
      })
    }
  }
})

function modifyRequestStatusChange(data){
  $.ajax({
    type: "PUT",
    url: '/info/modifyRequestStateChange',
    data: data,
    beforeSend: function (xhr){
      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
    },
    success: function (html) {
      alert("저장 되었습니다.")
      location.reload()
    },
    error: function () {

    }
  });
}

function getRequestDetail(requestSeq){
  $.ajax({
    url: '/info/modifyRequestDetail',
    data: {requestSeq: requestSeq},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#requestDetailDiv").empty().append(html)
    },
    error:function(){

    }
  });
}