
$(document).ready(function(){
	$(".table_id").each(function(){
		var rows = $(".table_id:contains('"+$(this).text()+"')");
		if(rows.length > 1){
			rows.eq(0).attr("rowspan", rows.length);
			rows.not(":eq(0)").remove();
		}
	})
	
});

$(document).on('click', '#addVuln', function (){
	const vulnKey =null;
    vulnEditModal(vulnKey);
})

function vulnEditModal(vulnKey){
	$.ajax({
    url: '/target/vulnEditModal',
    type: 'GET',
    data: {vulnKey:vulnKey},
    dataType:"html",
    success: function(html){
      $("#vulnEditModalContent").empty().append(html);
      $("#vulnEditModal").modal('show');
      setUploadDiv();
    },
    error:function(){

    }
  });
}

$(document).on('click', '#saveVuln', function() {
	if (Validation()) {
		if (confirm("저장하시겠습니까?")) {
			document.getElementById("mgtOrgan").disabled = false;
			contentFade("in");
			const formData = new FormData($("#saveVulnoFm")[0]);
			for (const file of files) {
				if (!file.isDelete)
					formData.append('uploadFiles', file, file.name);
			}
			$.ajax({
				type: 'POST',
				data: formData,
				url: "/target/saveVulnerable",
				processData: false,
				contentType: false,
				success: function(result) {
					alert("저장되었습니다.");
					contentFade("out");
					location.reload();
				},
				error: function(xhr, status) {
					alert("저장에 실패하였습니다.")
					contentFade("out");
				}
			})
		}
	}
})

$(document).on('click', '#infoModal', function (){   
   const mgtOrgan = $(this).attr("data-mgtOrgan");
   
   $.ajax({
    url: '/target/vulnInfoModal',
    type: 'GET',
    data: {mgtOrgan:mgtOrgan},
    dataType:"html",
    success: function(html){
      $("#vulnEditModalContent").empty().append(html);
      $("#vulnEditModal").modal('show');
    },
    error:function(){

    }
  });
})

$(document).on('click', '#viewModal', function (){   
   const vulnKey = $(this).attr("data-vulnKey");
   
   $.ajax({
    url: '/target/vulnViewModal',
    type: 'GET',
    data: {vulnKey:vulnKey},
    dataType:"html",
    success: function(html){
      $("#vulnEditModalContent").empty().append(html);
      $("#vulnEditModal").modal('show');
    },
    error:function(){

    }
  });
})

$(document).on('click', '#fileDown', function (){
  const target = $(this)
  let url = "/target/vulnFileDownload?"
  url += "&fileSeq="+target.attr("data-fileSeq");
  url += "&vulnKey="+target.attr("data-vulnKey");
  window.open(encodeURI(url));
})

$(document).on('click', '#deleteVuln', function (){
  const vulnKey = $('input[name=vulnKey]').val();
   if(confirm("삭제하시겠습니까?")){
      contentFade("in");
      $.ajax({
	    type : 'POST',
	    url : "/target/deleteVulnerable",
	    data : JSON.stringify({vulnKey:vulnKey}),
	    contentType: 'application/json',
	    beforeSend: function (xhr){
	      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
	    },
	    success : function(data) {
	      alert("삭제 처리되었습니다.");
	      location.reload();
	    },
	    error : function(xhr, status) {
	      alert("삭제 처리에 실패하였습니다");
	    }
	  })

	}

})

$(document).on('click', '#goEdit', function (){   
   const vulnKey = $(this).attr("data-vulnKey");
   vulnEditModal(vulnKey);
})

$(document).on('click', '#updateVuln', function (){
	if(Validation()){
	   if(confirm("수정하시겠습니까?")){
	      contentFade("in");
	      const formData = new FormData($("#saveVulnoFm")[0]);
	      document.getElementById("mgtOrgan").disabled = false;
	      for(const file of files) {
	        if(!file.isDelete)
	          formData.append('uploadFiles', file, file.name);
	      }
	      $(".text-decoration-line-through").each(function (idx, el){
	        formData.append('fileSeq', $(el).attr("data-fileseq"));
	      })
	      $.ajax({
	        type : 'POST',
	        data : formData,
	        url : "/target/saveVulnerable",
	        processData: false,
	        contentType: false,
	        success : function(data) {
	          alert("수정되었습니다.");
	          contentFade("out");
	          vulnEditModal(data)
	        },
	        error : function(xhr, status) {
	          alert("수정에 실패하였습니다.")
	          contentFade("out");
	        }
	      })
	   }
    }
})

function Validation(){
	let flag = true;
	if($('#mgtOrgan').val() == ""){
		alert("경찰서를 선택해주세요.");
		$('#mgtOrgan').focus();
		flag = false;
	}
	if($('#vulnNm').val() == ""){
		alert("취약지명을 입력해주세요.");
		$('#vulnNm').focus();
		flag = false;
	}
	if($('#vulnType').val() == ""){
		alert("취약등급을 선택해 주세요.");
		$('#vulnType').focus();
		flag = false;	
	}
  return flag;
}

$(document).on('click', '#btn-close', function (){
	location.reload();
})



