$(document).on('click', '#previousTab', function (){
  const userStatus = $(this).data('userstatus');
  location.href = "/police/careerMgt?userStatus="+userStatus;
})

$(document).on('click', '#presentTab', function (){
  const userStatus = $(this).data('userstatus');
  location.href = "/police/careerMgt?userStatus="+userStatus;
})
$(document).on('click', '#notPoliceTab', function (){
  const userStatus = $(this).data('userstatus');
  location.href = "/police/careerMgt?userStatus="+userStatus;
})

$(document).on('click', '.policeTr', function (){
  getCareerModal($(this).find(".userSeq").val(), "CRC001")
});

$(document).on('click', '.careerAddBtn', function (){
  $.ajax({
    url: '/police/careerFormModal',
    data: {
      userSeq: $("#mngModelUserSeq").val(),
      careerCd: $(this).attr("data-careercd")
    },
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#careerFormModalContent").empty().append(html);
      $("#dateSelectorDiv").datepicker({
        format: "yyyy-mm-dd",
        language: "ko",
        autoclose: true
      });
      $("#careerFormModal").modal('show');
    },
    error:function(){

    }
  });
})

$(document).on('click', '.kwmsCareerBtn', function (){
  getKwmsCareerModal($("#mngModelUserSeq").val(), $("#mngModelDicCode").val())
})
$(document).on('click', '#kwmsModalSelectBtn', function (){
  const selectedList = [];
  $.each($(".careerSelector"), function (idx, selector){
    if($(selector).val()!==''){
      selectedList.push(selector);
    }
  })
  if(selectedList.length>0){
    if(confirm("저장하시겠습니까?")){
      const formData = new FormData();
      $.each(selectedList, function (idx, selector){
        const kwmsCareerTr = $(selector).parents(".kwmsCareerTr")
        const userSeq = $("#mngModelUserSeq").val();
        formData.append("careerList["+idx+"].userSeq", userSeq)
        formData.append("careerList["+idx+"].careerCd", selector.value)
        formData.append("careerList["+idx+"].designationCd", kwmsCareerTr.find(".designationCd").val())
        formData.append("careerList["+idx+"].workPositionStr", kwmsCareerTr.find(".workPositionStr").val())
        formData.append("careerList["+idx+"].workTitle", kwmsCareerTr.find(".workTitle").val())
        formData.append("careerList["+idx+"].startDate", kwmsCareerTr.find(".startDate").val())
        formData.append("careerList["+idx+"].endDate", kwmsCareerTr.find(".endDate").val())
        formData.append("careerList["+idx+"].workDay", kwmsCareerTr.find(".workDay").val())
        formData.append("careerList["+idx+"].workMonth", kwmsCareerTr.find(".workMonth").val())
        formData.append("careerList["+idx+"].hordCd", kwmsCareerTr.find(".hordCd").val())
      })
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/police/saveCareerList",
        processData: false,
        contentType: false,
        beforeSend: function (xhr){
          xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
        },
        success : function(result) {
          alert("저장되었습니다.");
          $("#kwmsCareerModal").modal('hide');
          getCareerModal($("#mngModelUserSeq").val(), "CRC001");
          contentFade("out");
        },
        error : function(xhr, status) {
          alert("저장에 실패하였습니다.");
          contentFade("out");
        }
      })
    }
  }else{
    alert("불러올 경력의 구분을 선택해주세요.")
  }
})

$(document).on('change', '#positionCheckBox', function (){
  const selfInputDiv = $(".selfInputDiv");
  const selectInputDiv = $(".selectInputDiv");
  if(this.checked){
    selfInputDiv.show();
    selfInputDiv.find("input").removeAttr("disabled");
    selectInputDiv.hide();
    selectInputDiv.find("select").attr("disabled", "disabled");
  }else{
    selfInputDiv.hide();
    selfInputDiv.find("input").attr("disabled", "disabled");
    selectInputDiv.show();
    selectInputDiv.find("select").removeAttr("disabled");
  }
})
$(document).on('click', '#saveBtn', function (){
  if(checkValue()){
    contentFade("in");
    const formData = new FormData($("#careerForm")[0]);
    $.ajax({
      type : 'POST',
      data : formData,
      url : "/police/saveCareer",
      processData: false,
      contentType: false,
      beforeSend: function (xhr){
        xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
      },
      success : function(result) {
        alert("저장되었습니다.");
        $("#careerFormModal").modal('hide');
        getCareerModal($("#mngModelUserSeq").val(), $("#careerForm").find("#careerCd").val());
        contentFade("out");
      },
      error : function(xhr, status) {
        alert("저장에 실패하였습니다.");
        contentFade("out");
      }
    })
  }
})
$(document).on('click', '.deleteCareerBtn', function (){
  if(confirm("삭제하시겠습니까?")){
    contentFade("in");
    const userSeq = $("#mngModelUserSeq").val()
    const tabCd = $(this).attr("data-tab")
    $.ajax({
      type : 'POST',
      url : "/police/deleteCareer",
      data : {
        userSeq: userSeq,
        careerSeq: $(this).attr("data-careerseq")
      },
      beforeSend: function (xhr){
        xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
      },
      success : function(data) {
        alert("삭제 되었습니다.");
        getCareerModal(userSeq, tabCd);
        contentFade("out");
      },
      error : function(xhr, status) {
        alert("삭제를 실패하였습니다");
        contentFade("out");
      }
    })
  }
})

function getCareerModal(userSeq, careerCd){
  $.ajax({
    url: '/police/careerModal',
    data: {userSeq: userSeq, careerCd: careerCd},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#careerModalContent").empty().append(html);
      $("#careerModal").modal('show');
    },
    error:function(){

    }
  });
}
function getKwmsCareerModal(userSeq, dicCode){
  $.ajax({
    url: '/kwms/kwmsCareerModal',
    data: {userSeq: userSeq, dicCode: dicCode},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#kwmsCareerModalContent").empty().append(html);
      $("#kwmsCareerModal").modal('show');
    },
    error:function(){

    }
  });
}
function checkValue(){
  let flag = true;
  const form = $("#careerForm");
  if(!form.find("#designationCd").val()){
    alert("임용구분을 선택해주세요.")
    flag = false;
  }
  if(!form.find("#startDate").val()){
    alert("근무 시작일을 입력해주세요.")
    flag = false;
  }
  return flag;
}
