'use strict';

var produkt_id = null;

if (document.getElementById('produkte')) {
	request({
		type:"GET",
		url: "/sven/api/article"
	}, function (data) {
			data.data = data.data.DATA;
		/*
		*Füllt das Produkteingabefeld mit den vorhandenen Produkten
		*/
		var str_2 = "";
		data.data.forEach(function(elem) {
			str_2+='<option value='+elem.ID+'>'+elem.Name+'</option>';
		});
		$('#produkte').html(str_2);    //JQuerry
	}, function(data) {});
	$('#produkte').change(function(){
		produkt_id = $(this).val();
	});
};

var gesMengen_Daten;
if (document.getElementById('chart1')) {
	request({
		url: "/sven/api/salesFigures"
	}, function (data) {
		  data.data = data.data.DATA;

		  var series_Menge = [];
		  var labels_Menge = [];
		  var Wochen = {};
		  data.data.forEach(function(item) {
			    item.Woche = parseInt(item.Woche);
			    if(!Wochen[item.Woche]){
            Wochen[item.Woche] = parseInt(item.Menge);
          }
			    else{
            Wochen[item.Woche] += parseInt(item.Menge);
          }
		});

		Object.keys(Wochen).forEach(function(item) {
			series_Menge.push(Wochen[item]);
		});
		labels_Menge =Object.keys(Wochen);

		gesMengen_Daten = { labels: labels_Menge, series: [series_Menge]};
		new Chartist.Bar('#chart1', gesMengen_Daten, {
		    chartPadding: {
					top: 0,
					right: 0,
					bottom: 40,
					left: 40
				},
				plugins: [
					Chartist.plugins.ctAxisTitle({
						axisX: {
							axisTitle: 'Woche',
							axisClass: 'ct-axis-title',
							offset: {
								x: 0,
								y: 40
							},
							textAnchor: 'middle'
						},
						axisY: {
							axisTitle: 'Menge',
							axisClass: 'ct-axis-title',
							offset: {
								x: 0,
								y: -20
							},
							textAnchor: 'middle',
							flipTitle: false
						}
					})
				]
			});

	   }, function (data) {
		     console.log(data);
		     console.log('Error Produktgraphen');
	      });

	new Chartist.Bar('#chart1', daten_Menge);

};



var daten_Menge;
function selectProduct(){
	if(produkt_id === null) return;

	request({
		url: "/sven/api/salesFigures"
	}, function (data) {
		data.data = data.data.DATA;
		var series_2 = [];
		var labels_2 = [];
		data.data.forEach(function(item) {
			if(item.f_Artikel_ID === produkt_id) {
				labels_2.push(item.Woche);
				series_2.push({x: item.Woche, y: item.Menge});
			}
		});

		daten_Menge = {labels: labels_2, series: [series_2]};

		if (document.getElementById('chart2')) {
			new Chartist.Bar('#chart2', daten_Menge, {
				chartPadding: {
					top: 0,
					right: 0,
					bottom: 40,
					left: 40
				},
				plugins: [
					Chartist.plugins.ctAxisTitle({
						axisX: {
							axisTitle: 'Woche',
							axisClass: 'ct-axis-title',
							offset: {
								x: 0,
								y: 40
							},
							textAnchor: 'middle'
						},
						axisY: {
							axisTitle: 'Menge',
							axisClass: 'ct-axis-title',
							offset: {
								x: 0,
								y: -20
							},
							textAnchor: 'middle',
							flipTitle: false
						}
					})
				]
			});
		};

	}, function (data) {
		  console.log(data);
		  console.log('Error Produktgraphen');
	   });
};

if (document.getElementById('chart2')) {
	new Chartist.Bar('#chart2', daten_Menge);
};

function gewinnGraph() {
	var gewinn = 0;
	if(!produkt_id) return;
	request({
		type:"GET",
		url:"/sven/api/article"
	}, function(data){
			data.data = data.data.DATA;
			console.log(data.data);
			var series_2 = [];
			var labels_2 = [];

			gewinn = data.data[produkt_id-1].Rohgewinn / data.data[produkt_id-1].Anzahl_pro_Palette;
			console.log(produkt_id, data.data[produkt_id-1].Rohgewinn, data.data[produkt_id-1].Anzahl_pro_Palette);

			request({
				type:"GET",
				url:"/sven/api/salesFigures"
			}, function(data){
				  data.data = data.data.DATA;
				  data.data.forEach(function(item) {
				    if(item.f_Artikel_ID === produkt_id) {
						  console.log(item);
						  labels_2.push(item.Woche);
						  series_2.push(gewinn * item.Menge);
            		}
      			  });
          		  daten_Menge = {labels: labels_2, series: [series_2]};

					if (document.getElementById('chart2')) {
							new Chartist.Line('#chart2', daten_Menge, {showArea:true}, {
								chartPadding: {
									top: 0,
									right: 0,
									bottom: 40,
									left: 40
								},
								plugins: [
									Chartist.plugins.ctAxisTitle({
										axisX: {
											axisTitle: 'Woche',
											axisClass: 'ct-axis-title',
											offset: {
												x: 0,
												y: 40
											},
											textAnchor: 'middle'
										},
										axisY: {
											axisTitle: 'Preis in €',
											axisClass: 'ct-axis-title',
											offset: {
												x: 0,
												y: -20
											},
											textAnchor: 'middle',
											flipTitle: false
										}
									})
								]
							});
						}
	}, function (data) {
		  console.log(data);
		  console.log('Error 12');
	  });
  })};
