var wizardData = {};
function wizardDelete(nr){
	var path = '/manager/' + nr;
	$.ajax({
		url: path,
		type: 'DELETE'
	})
	.done(function(){
		viewChargenubersicht();	
	})
	.fail(function(){
		alert("Charge konnte nicht gelöscht werden");
	});

};
function wizardSend(){
	//wizardData -> ajax -> server -> fuckoff
	var path = '/manager/' + JSON.stringify(wizardData);

	$.ajax({
		url: path,
		type: 'RECEIVE'
	})
	.done(function(){
		viewChargenubersicht();		
	})
	.fail(function(){
		alert("die Übertragung der Chargendaten zum Server ist fehlgeschlagen");
	});
};
function wizardLosDataSave(los_nr, next_los_nr){
	wizardData[los_nr]['beschreibung'] = document.getElementById('beschreibung').value;
	wizardData[los_nr]['anzGebinde'] = parseInt(document.getElementById('anzGebinde').value);
	wizardData[los_nr]['auktionDauer'] = parseInt(document.getElementById('auktionDauer').value);
	wizardData[los_nr]['auktionLaufzeit'] = 0;
	wizardData[los_nr]['startPreis'] = parseInt(document.getElementById('startPreis').value);
	wizardData[los_nr]['minPreis'] = parseInt(document.getElementById('minPreis').value);
	wizardData[los_nr]['auktionPreis'] = -1;
	wizardData[los_nr]['vkPreis'] = -1;
	
	if(next_los_nr == 0){
		wizardSend();
	}
	else if(next_los_nr > wizardData['info']['anzAuk']){
		wizardLosData(next_los_nr, 1);
	}
	else{
		wizardLosData(next_los_nr, 0);
	}
};
function wizardLosData(los_nr, neu){
	$('#content').html('<h1>Los ' + los_nr + '</h1>');
	if(neu){
		wizardData['info']['anzAuk']++;
		wizardData[los_nr] = {};
		
		$.get('templates/html/losLeerForm.html', function(markup){
			$('#content').append(markup);
			if(los_nr > 9){
				$('#content').append('<button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr - 1)+') >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button>');			
			}
			else if(los_nr == 1){
				$('#content').append('<button class = "bigbutton" onclick = wizardChargenname(0) >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button><button class = "wizardButtonNeu" onclick = wizardLosDataSave('+los_nr+','+(los_nr + 1)+')>neu</button>');
			}
			else{
				$('#content').append('<button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr - 1)+') >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button><button class = "wizardButtonNeu" onclick = wizardLosDataSave('+los_nr+','+(los_nr + 1)+')>neu</button>');
			}
		})	
	}
	else{
		markup = TPL.engine.execute_px('losFilledForm.tpl',wizardData[los_nr]);
		$('#content').append(markup);
		if(los_nr > 9){
			$('#content').append('<button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr - 1)+') >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button>');
		}
		else if(los_nr == 1){
			$('#content').append('<button class = "bigbutton" onclick = wizardChargenname(0) >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button><button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr + 1)+')>weiter</button>');
		}
		else{
			$('#content').append('<button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr - 1)+') >zurück</button><button class = "wizardButtonFertig" onclick = wizardLosDataSave('+los_nr+',' +0+ ')>Fertig</button><button class = "bigbutton" onclick = wizardLosDataSave('+los_nr+','+(los_nr + 1)+')>weiter</button>');
		}		
	}
};

function wizardChargennameSave(neu){
	wizardData['info']['name'] = document.getElementById('chargenname').value;
	if(neu){
		wizardLosData(1, 1);
	}
	else{
		wizardLosData(1, 0);
	}
};

function wizardChargenname(neu){
	if(neu){
		$('#content').html('<h1>neue Charge</h1>');
		$('#content').append('<p><input type = "text" id = "chargenname"></p>');
		$('#content').append('<p><button class = "bigbutton" onclick = viewChargenubersicht() >zurück</button><button class = "bigbutton" onclick = wizardChargennameSave(1)>speichern und weiter</button></p>');
	}
	else{
		$('#content').html('<h1>Charge bearbeiten</h1>');		
		$('#content').append('<p><input type = "text" id = "chargenname" value = '+wizardData['info']['name']+'></p>');
		$('#content').append('<p><button class = "bigbutton" onclick = viewChargenubersicht()>zurück</button><button class = "bigbutton" onclick = wizardChargennameSave(0)>speichern und weiter</button></p>');
	}
};

function wizardStart(chargenNr = 0){
	if(chargenNr != 0){
		var path = '/manager/' + chargenNr;
		$.ajax({
			dataType: "json",
			url: path,
			type: 'GET'
		})
		.done(function(data){
			wizardData = eval("(" + data + ")");
			wizardChargenname(0);
		})
		.fail(function(){
			alert("Charge konnte nicht geladen werden");
		});
	}	
	else{
		wizardData = {};
		wizardData['info'] = {};
		wizardData['info']['anzAuk'] = 0;
		wizardData['info']['chargenNr'] = -1;

		wizardChargenname(1);
	}
}