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

$(document).on('click', '#fieldDownExcel', function (){
  exportExcel('견문통계_분야별', 'tableData');
})

$(document).on('click', '#ratingDownExcel', function (){
  exportExcel('견문통계_평가별', 'tableData');
})

$(document).on('click', '#fieldSearch', function (){
	goFieldStatistics();
})

$(document).on('click', '#fieldTab', function (){
	goFieldStatistics();
})

function goFieldStatistics(){
	searchFade("in");
	const formData = new FormData($("#searchFm")[0]);
  $.ajax({
    url: '/affair/fieldStatistics',
    data: formData,
    type: 'POST',
    dataType:"html",
    contentType: false,
    processData: false,
    success: function(html){
      $("#statisticsBody").empty().append(html)
      $("#statisticsBody").show();
      $(".statisticsMenu").hide();
      searchFade("out");
    },
    error:function(){
	searchFade("out");

    }
  });
}

$(document).on('click', '#ratingSearch', function (){
	if($('input:checkbox[name=rating]:checked').length < 1){
		alert("평가항목을 선택해주세요");
		return false;
	}
	goRatingStatistics();
})
$(document).on('click', '#ratingTab', function (){
	if($('input:checkbox[name=rating]:checked').length < 1){
		alert("평가항목을 선택해주세요");
		return false;
	}
	goRatingStatistics();
})

function goRatingStatistics(){
	searchFade("in");
	const formData = new FormData($("#searchFm")[0]);
  $.ajax({
    url: '/affair/ratingStatistics',
    data: formData,
    type: 'POST',
    dataType:"html",
    contentType: false,
    processData: false,
    success: function(html){
      $("#statisticsBody").empty().append(html)
      $("#statisticsBody").show();
      $(".statisticsMenu").hide();
      searchFade("out");
    },
    error:function(){
	 searchFade("out");
    }
  });
}

$(document).on('click', '#showMenu', function (){
	$(".statisticsMenu").show();
})

$(document).ready(function() {
	$("#organAll").click(function() {
		if($("#organAll").is(":checked")){
			$("input[name=organList]").prop("checked", true);
		} else{
	        $("input[name=organList]").prop("checked", false);	
		}
	});
	$("#ratingAll").click(function() {
		if($("#ratingAll").is(":checked")){
			$("input[name=rating]").prop("checked", true);
		} else{
	        $("input[name=rating]").prop("checked", false);	
		}
	});
	$("#category1All").click(function() {
		if($("#category1All").is(":checked")){
			$("input[name=category1]").prop("checked", true);
		} else{
	        $("input[name=category1]").prop("checked", false);	
		}
	});
	$("#category2All").click(function() {
		if($("#category2All").is(":checked")){
			$("input[name=category2]").prop("checked", true);
		} else{
	        $("input[name=category2]").prop("checked", false);	
		}
	});
	$("#category3All").click(function() {
		if($("#category3All").is(":checked")){
			$("input[name=category3]").prop("checked", true);
		} else{
	        $("input[name=category3]").prop("checked", false);	
		}
	});
	$("#category4All").click(function() {
		if($("#category4All").is(":checked")){
			$("input[name=category4]").prop("checked", true);
		} else{
	        $("input[name=category4]").prop("checked", false);	
		}
	});

});

