$(document).on('change', '#year', function (){
  $("#searchFm").submit();
})

$(document).on('click', '#personnelStatusExcelBtn', function (){
  exportExcel($("#year").val()+"년 외사경찰 정원현원 현황", "psTable");
})

$(document).on('click', '#personnelStatusEditModalBtn', function (){
  $("#personnelStatusEditDiv").empty();
  $("#personnelStatusEditModal").modal('show');
})

$(document).on('click', '#personnelStatusAddBtn', function (){
  $.ajax({
    url: '/police/personnelStatusRow',
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#personnelStatusEditDiv").append(html)
    },
    error:function(){

    }
  });
})

$(document).on('click', '.personnelStatusRemoveBtn', function (){
  $(this).parents(".personnelStatusRow").remove();
})

$(document).on('change', '.ogCd', function (){
  const personnelStatusRow = $(this).parents(".personnelStatusRow");
  if(this.value !== ''){
    personnelStatusRow.find(".nowPersonnelStatusBtn")[0].className = "btn btn-sm btn-success nowPersonnelStatusBtn";
    $.ajax({
      url: '/police/selectPersonnelStatus',
      type: 'GET',
      data: {year: new Date().getFullYear(), ogCd: this.value},
      dataType:"json",
      success: function(data){
        personnelStatusRow.find(".jt007Max").val(data.jt007Max);
        personnelStatusRow.find(".jt007Now").val(data.jt007Now);
        personnelStatusRow.find(".jt006Max").val(data.jt006Max);
        personnelStatusRow.find(".jt006Now").val(data.jt006Now);
        personnelStatusRow.find(".jt005Max").val(data.jt005Max);
        personnelStatusRow.find(".jt005Now").val(data.jt005Now);
        personnelStatusRow.find(".jt004Max").val(data.jt004Max);
        personnelStatusRow.find(".jt004Now").val(data.jt004Now);
        personnelStatusRow.find(".jt003Max").val(data.jt003Max);
        personnelStatusRow.find(".jt003Now").val(data.jt003Now);
        personnelStatusRow.find(".jt002Max").val(data.jt002Max);
        personnelStatusRow.find(".jt002Now").val(data.jt002Now);
        personnelStatusRow.find(".jt001Max").val(data.jt001Max);
        personnelStatusRow.find(".jt001Now").val(data.jt001Now);
      },
      error:function(){

      }
    });
  }else{
    personnelStatusRow.find(".nowPersonnelStatusBtn")[0].className = "btn btn-sm btn-success nowPersonnelStatusBtn disabled";
  }
})

$(document).on('click', '.nowPersonnelStatusBtn', function (){
  const personnelStatusRow = $(this).parents(".personnelStatusRow");
  $.ajax({
    url: '/police/nowPersonnelStatus',
    type: 'GET',
    data: {ogCd: personnelStatusRow.find(".ogCd").val()},
    dataType:"json",
    success: function(data){
      if(data === null){
        alert("결과가 없습니다.")
      }else{
        personnelStatusRow.find(".jt007Now").val(data.jt007Now);
        personnelStatusRow.find(".jt006Now").val(data.jt006Now);
        personnelStatusRow.find(".jt005Now").val(data.jt005Now);
        personnelStatusRow.find(".jt004Now").val(data.jt004Now);
        personnelStatusRow.find(".jt003Now").val(data.jt003Now);
        personnelStatusRow.find(".jt002Now").val(data.jt002Now);
        personnelStatusRow.find(".jt001Now").val(data.jt001Now);
      }
    },
    error:function(){
      alert("조회에 실패하였습니다.")
    }
  });
})

$(document).on('click', '#saveBtn', function (){
  if($(".ogCd").length===0){
    alert("저장할 항목이 없습니다.")
  }else{
    if(personnelStatusOgCdCheck()){
      if(confirm("저장하시겠습니까?")){
        contentFade("in");
        const personnelStatusList = [];
        const year = new Date().getFullYear();
        $.each($(".personnelStatusRow"), function(idx, personnelStatus){
          const row = $(personnelStatus);
          personnelStatusList.push({});
          personnelStatusList[idx].year = year;
          personnelStatusList[idx].ogCd = row.find(".ogCd").val()
          personnelStatusList[idx].jt007Max = row.find(".jt007Max").val()
          personnelStatusList[idx].jt007Now = row.find(".jt007Now").val()
          personnelStatusList[idx].jt006Max = row.find(".jt006Max").val()
          personnelStatusList[idx].jt006Now = row.find(".jt006Now").val()
          personnelStatusList[idx].jt005Max = row.find(".jt005Max").val()
          personnelStatusList[idx].jt005Now = row.find(".jt005Now").val()
          personnelStatusList[idx].jt004Max = row.find(".jt004Max").val()
          personnelStatusList[idx].jt004Now = row.find(".jt004Now").val()
          personnelStatusList[idx].jt003Max = row.find(".jt003Max").val()
          personnelStatusList[idx].jt003Now = row.find(".jt003Now").val()
          personnelStatusList[idx].jt002Max = row.find(".jt002Max").val()
          personnelStatusList[idx].jt002Now = row.find(".jt002Now").val()
          personnelStatusList[idx].jt001Max = row.find(".jt001Max").val()
          personnelStatusList[idx].jt001Now = row.find(".jt001Now").val()
        })
        $.ajax({
          type : 'POST',
          data : JSON.stringify(personnelStatusList),
          url : "/police/savePersonnelStatus",
          contentType: 'application/json',
          beforeSend: function (xhr){
            xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
          },
          success : function() {
            alert("저장되었습니다.");
            contentFade("out");
            location.reload();
          },
          error : function(xhr, status) {
            alert("저장에 실패하였습니다.");
            contentFade("out");
          }
        })
      }
    }
  }
})

$(document).on('click', '.statusTr', function (){
  const statusTr = $(this);
  $.ajax({
    url: '/police/personnelStatusHistory',
    type: 'GET',
    data: {year: statusTr.attr("data-year"), ogCd: statusTr.attr("data-ogcd")},
    dataType:"html",
    success: function(html){
      $("#personnelStatusHistoryModalBody").empty().append(html)
      $("#personnelStatusHistoryModal").modal('show');
    },
    error:function(){

    }
  });
})

function personnelStatusOgCdCheck(){
  let flag = true;
  $.each($(".ogCd"), function(idx, selectBox){
    if($(selectBox).val()===''){

      flag = false;
    }
  })
  if(!flag){
    alert("관서가 지정되지 않은 항목이 있습니다.")
  }
  return flag;
}