let eduList=[];
let selectedIdx=0;

$(document).on('click', '.policeTr', function (event){
	const target = event.target;
	if(!(target.className === "checkBoxTd" ||$(target).parents("td").length>0)){
		const userSeq = (Number($(this).find(".userSeq").val()));
		showModal(userSeq);
	}
});

function showModal(userSeq){
	$.ajax({
    url: '/police/eduEditModal',
     data: {userSeq: userSeq},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#eduEditModalContent").empty().append(html);
      $(".dateSelector").datepicker({
        format: "yyyy-mm-dd",
        language: "ko",
        autoclose: true
      });
      $("#eduEditModal").modal('show');
    },
    error:function(){

    }
  });
}

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

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

$(document).on('click', '#eduAddBtn', function (){
	const userSeq = (Number($(this).data('userseq')));
  $('#insertEdu').append(
		'<div id="eduDiv">'+
	        '<div class="mb-4 row">'+                
		        '<label for="ogCd" class="col-sm-1 col-form-label col-form-label-sm text-center">교육명</label>'+
		        '<div class="col-sm-2">'+
		        '<input type="text" class="form-control form-control-sm tcDt eduInput" name="eduName">'+
		        '</div>'+
		        '<label for="ofcCd" class="col-sm-1 col-form-label col-form-label-sm text-center">교육시작</label>'+
		        '<div class="col-sm-2">'+
		        '<input type="text" class="form-control form-control-sm eduSdate eduInput" name="eduSdate" readonly>'+
		        '</div>'+
		        '<label for="ofcCd" class="col-sm-1 col-form-label col-form-label-sm text-center">교육종료</label>'+
		        '<div class="col-sm-2">'+
		        '<input type="text" class="form-control form-control-sm eduEdate eduInput" name="eduEdate" readonly>'+
		        '</div>'+
		        '<label for="ofcCd" class="col-sm-1 col-form-label col-form-label-sm text-center">담당기관</label>'+
		        '<div class="col-sm-2">'+
		        '<input type="text" class="form-control form-control-sm eduInput" name="eduRa">'+
		        '</div>'+
	        	'</div>'+
	        '<div class="row justify-content-center">'+
	        '<div class="col-sm-1">'+
	        '<button type="button" class="btn btn-sm btn-outline-danger rowDeleteBtn"><i class="bi bi-x"></i></button>'+
			'</div>'+
			'</div>'+
		'</div>'
  )
  $(".eduSdate").datepicker({
                format: "yyyy-mm-dd",
                language: "ko"
  });
  $(".eduEdate").datepicker({
                format: "yyyy-mm-dd",
                language: "ko"
  });
  eduList.push({userSeq:userSeq, eduName:"",eduSdate:"",eduEdate:"",eduRa:""});
})

$(document).on('click', '.rowDeleteBtn', function (){
	selectedIdx = $(this).parents("#eduDiv").index()-1;
	eduList.splice(selectedIdx,1);	
	$(this).parents('#eduDiv').remove();
})

$(document).on('change', '.eduInput', function (){
  selectedIdx = $(this).parents("#eduDiv").index()-1;
  const target = eduList[selectedIdx];
  switch (this.name){
    case "eduName":
      target.eduName = this.value
      break;
    case "eduSdate":
      target.eduSdate = this.value
      break;
    case "eduEdate":
      target.eduEdate = this.value
      break;
    case "eduRa":
      target.eduRa = this.value
      break;
  }
})

$(document).on('click', '#saveBtn', function (){
	if(eduList.length < 1){
		alert("새로 입력된 교육정보가 없습니다.");
		return false;
	}
    if(confirm("저장하시겠습니까?")){
      contentFade("in");
      $.ajax({
        type : 'POST',
        data : JSON.stringify(eduList),
        url : "/police/saveEdu",
       contentType: 'application/json',
	    beforeSend: function (xhr){
	      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
	    },
	    success : function(data) {
		 contentFade("out");
	      alert("교육등록이 완료되었습니다.");
	      showModal(data);
	      eduList.length = 0;
	    },
	    error : function(xhr, status) {
		  contentFade("out");
	      alert("교육등록을 실패하였습니다");
	    }
      })
    }
})

$(document).on('click', '#deleteBtn', function (){
	const eduSeq = (Number($(this).data('eduseq')));
	const userSeq = (Number($(this).data('userseq')));
    if(confirm("삭제하시겠습니까?")){
		$.ajax({
	    type : 'POST',
	    url : "/police/deleteEdu",
	    data : JSON.stringify({eduSeq:eduSeq,
	    		userSeq:userSeq}),
	    contentType: 'application/json',
	    beforeSend: function (xhr){
	      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
	    },
	    success : function(data) {
	      alert("삭제 처리되었습니다.");
	      showModal(data);
	    },
	    error : function(xhr, status) {
	      alert("삭제 처리에 실패하였습니다");
	    }
	  })
   }
})


