google.load("visualization", "1", {packages:["corechart", 'line']});

function drawChart(dataJson, chartType) {
    var options = {
        bar: {groupWidth: "85%"}
    };
    console.log(dataJson);
   var arr= new Array();
    $.each(dataJson, function( ind, val ) {
    	   if(jQuery.type(val)=="object"){
    		   //console.log( ind + ": " + val );
    		   var array = $.map(val, function(value1, index1) {
    			   //console.log(value1); 
    			   return [value1];
    			});
    		   arr.push(array)
    	  }else{
    		  arr.push(val);
    	  }
    	});
    console.log(arr);
    var data = google.visualization.arrayToDataTable(arr);

    if(chartType == 'columnChart') {
        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    } else {
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    }

    chart.draw(data, options);
}

function btnChart() {
    Loader(true);

    SeachTable();

    setTimeout(function () {
        JSONAjax(data);
    }, 100);
}

function JSONAjax(data) {
    data['chart_column[]'] = $( "[name='chart1']:checked" ).val();
    var chartType = $( "[name='chart-type']:checked" ).val();

   
    if(data.document_sales_type== null){
    	console.debug(data.document_sales_type);
    	data['document_sales_type[]']=['invoice','invoice_correct','recipe','recipe_correct'];
    	
    }
    console.log(data);
    google.setOnLoadCallback(
        $.ajax({
            type: "POST",
            url: 'index.php?module=EcmReports&action=ReportSalesByGroup&to_pdf=1&to_json=1&xd=1',
            dataType: "json",
            data: data,
            async: false,
            success: function(response,a,b){
                console.log(response);
                console.log(a);
                console.log(b);
                drawChart(response, chartType);
                Loader(false);
            },
            error: function(err) {
                Loader(false);
            }
        })
    );
}