<?php
include './core.php';

if( !isset( $user ) || !$user->can_access( 'npcs' ) ) access_error();

/* # bitte erstmal drinlassen *g*
$array = array('reittier_id', 'kleidungsset', 'emote_set', 'xp_klassen', 'kis_id', 'name', 'beschreibung', 'name_interface', 'geschlecht', 'bodyid', 'farbe', 'farbe_speech', 'farbe_emote', 'basis_sound_id', 'freigeschalten', 'status', 'kommentar',
	    		'kl_min', 'kl_max', 'in_min', 'in_max', 'ge_min', 'ge_max', 'ff_min', 'ff_max', 'kk_min', 'kk_max', 'ko_min', 'ko_max',
	    		'skill_0_id', 'skill_0_min', 'skill_0_max',
				'skill_1_id', 'skill_1_min', 'skill_1_max',
				'skill_2_id', 'skill_2_min', 'skill_2_max',
				'skill_3_id', 'skill_3_min', 'skill_3_max',
				'skill_4_id', 'skill_4_min', 'skill_4_max',
				'skill_5_id', 'skill_5_min', 'skill_5_max',
				'skill_6_id', 'skill_6_min', 'skill_6_max',
				'skill_7_id', 'skill_7_min', 'skill_7_max',
				'skill_8_id', 'skill_8_min', 'skill_8_max',
				'skill_9_id', 'skill_9_min', 'skill_9_max', 'skill_rest',
				'kann_schwimmen', 'lebensenergie_min', 'lebensenergie_max', 'ausdauer_min', 'ausdauer_max', 'geschwindigkeit_normal', 'geschwindigkeit_max',
				'regeneration_trefferpunkte', 'regeneration_ausdauer',
				'zaehmen_tierkunde', 'zaehmen_zaehmen', 'zaehmen_rezept',
				'sichtweite', 'hoerweite', 'virtueller_ruestungswert', 'animation_hoehe', 'animation_breite', 'schaden_min', 'schaden_max',
				'reitbar', 'reit_itemid', 'delay_gehen', 'delay_rennen' );
				
for($i=0; $i < count($array); $i++) {
	#echo '$'.$array[$i].' = empty($_REQUEST[\''.$array[$i].'\']) ? \'NULL\' : "\'".$_REQUEST[\''.$array[$i].'\']."\'";'."\n";
	#echo $array[$i].'=".$'.$array[$i].'.", '."\n";
} */

if(isset($_REQUEST['new']) || isset($_REQUEST['edit'])) {
	$npcids = $MySQL->fetch_complete("SELECT id, name_interface FROM server.npc_type ORDER BY `name_interface` ASC");
	$Smarty->assign('npcids', $npcids);
	
	$kleidungssetids = $MySQL->fetch_complete("SELECT id, name FROM server.kleidungsset ORDER BY `name` ASC");
	$Smarty->assign('kleidungssetids', $kleidungssetids);
	
	$futterlistenids = $MySQL->fetch_complete("SELECT id, name FROM server.futterliste ORDER BY `name` ASC");
	$Smarty->assign('futterlistenids', $futterlistenids);
	
	$emotesetids = $MySQL->fetch_complete("SELECT id, name FROM server.emote_set ORDER BY `name` ASC");
	$Smarty->assign('emotesetids', $emotesetids);
	
	$xpklassenids = $MySQL->fetch_complete("SELECT id, bezeichnung FROM server.xp_klassen ORDER BY `bezeichnung` ASC");
	$Smarty->assign('xpklassenids', $xpklassenids);
	
	$kiids = $MySQL->fetch_complete("SELECT id, name FROM server.kis ORDER BY `name` ASC");
	$Smarty->assign('kiids', $kiids);
	
	$skillids = $MySQL->fetch_complete("SELECT id, bezeichnung FROM server.skills ORDER BY `bezeichnung` ASC");
	$Smarty->assign('skillids', $skillids);
	
	$lootlistenids = $MySQL->fetch_complete("SELECT id, name FROM server.lootlisten ORDER BY `name` ASC");
	$Smarty->assign('lootlistenids', $lootlistenids);
}

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	    
	    if( !check_fields( 'reittier_id', 'kleidungsset', 'futterliste', 'emote_set', 'xp_klassen', 'kis_id', 'name', 'beschreibung', 'name_interface', 'geschlecht', 'bodyid', 'freigeschalten',
	    		'skill_rest',
				'kann_schwimmen', 'lebensenergie_min', 'lebensenergie_max', 'ausdauer_min', 'ausdauer_max', 'geschwindigkeit_normal', 'geschwindigkeit_max',
				'virtueller_ruestungswert', 'animation_hoehe', 'animation_breite', 'schaden_min', 'schaden_max' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	        
	    if( $_REQUEST['reitbar'] == '1' && !check_fields('reit_itemid', 'delay_gehen', 'delay_rennen') )
	    	$form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'reittier_id', 'kleidungsset', 'futterliste', 'emote_set', 'xp_klassen', 'kis_id', 'name', 'beschreibung', 'name_interface', 'geschlecht', 'bodyid', 'farbe', 'farbe_speech', 'farbe_emote', 'basis_sound_id', 'freigeschalten', 'status', 'kommentar',
	    		'kl_min', 'kl_max', 'in_min', 'in_max', 'ge_min', 'ge_max', 'ff_min', 'ff_max', 'kk_min', 'kk_max', 'ko_min', 'ko_max',
	    		'skill_0_id', 'skill_0_min', 'skill_0_max',
				'skill_1_id', 'skill_1_min', 'skill_1_max',
				'skill_2_id', 'skill_2_min', 'skill_2_max',
				'skill_3_id', 'skill_3_min', 'skill_3_max',
				'skill_4_id', 'skill_4_min', 'skill_4_max',
				'skill_5_id', 'skill_5_min', 'skill_5_max',
				'skill_6_id', 'skill_6_min', 'skill_6_max',
				'skill_7_id', 'skill_7_min', 'skill_7_max',
				'skill_8_id', 'skill_8_min', 'skill_8_max',
				'skill_9_id', 'skill_9_min', 'skill_9_max', 'skill_rest',
				'kann_schwimmen', 'lebensenergie_min', 'lebensenergie_max', 'ausdauer_min', 'ausdauer_max', 'geschwindigkeit_normal', 'geschwindigkeit_max',
				'regeneration_trefferpunkte', 'regeneration_ausdauer',
				'zaehmen_tierkunde', 'zaehmen_zaehmen', 'zaehmen_rezept',
				'sichtweite', 'hoerweite', 'virtueller_ruestungswert', 'animation_hoehe', 'animation_breite', 'schaden_min', 'schaden_max',
				'reitbar', 'reit_itemid', 'delay_gehen', 'delay_rennen' );
	    }
	    else
	    {
	        $farbe = empty($_REQUEST['farbe']) ? '0' : $_REQUEST['farbe'];
		    $farbe_speech = empty($_REQUEST['farbe_speech']) ? '0' : $_REQUEST['farbe_speech'];
		    $farbe_emote = empty($_REQUEST['farbe_emote']) ? '0' : $_REQUEST['farbe_emote'];
		    $basis_sound_id = empty($_REQUEST['basis_sound_id']) ? '0' : $_REQUEST['basis_sound_id'];
		    
		    $kommentar = empty($_REQUEST['kommentar']) ? 'NULL' : "'".$_REQUEST['kommentar']."'";
		    
		    $kl_min = empty($_REQUEST['kl_min']) ? 'NULL' : "'".$_REQUEST['kl_min']."'";
			$kl_max = empty($_REQUEST['kl_max']) ? 'NULL' : "'".$_REQUEST['kl_max']."'";
			$in_min = empty($_REQUEST['in_min']) ? 'NULL' : "'".$_REQUEST['in_min']."'";
			$in_max = empty($_REQUEST['in_max']) ? 'NULL' : "'".$_REQUEST['in_max']."'";
			$ge_min = empty($_REQUEST['ge_min']) ? 'NULL' : "'".$_REQUEST['ge_min']."'";
			$ge_max = empty($_REQUEST['ge_max']) ? 'NULL' : "'".$_REQUEST['ge_max']."'";
			$ff_min = empty($_REQUEST['ff_min']) ? 'NULL' : "'".$_REQUEST['ff_min']."'";
			$ff_max = empty($_REQUEST['ff_max']) ? 'NULL' : "'".$_REQUEST['ff_max']."'";
			$kk_min = empty($_REQUEST['kk_min']) ? 'NULL' : "'".$_REQUEST['kk_min']."'";
			$kk_max = empty($_REQUEST['kk_max']) ? 'NULL' : "'".$_REQUEST['kk_max']."'";
			$ko_min = empty($_REQUEST['ko_min']) ? 'NULL' : "'".$_REQUEST['ko_min']."'";
			$ko_max = empty($_REQUEST['ko_max']) ? 'NULL' : "'".$_REQUEST['ko_max']."'";
			$skill_0_id = empty($_REQUEST['skill_0_id']) ? 'NULL' : "'".$_REQUEST['skill_0_id']."'";
			$skill_0_min = empty($_REQUEST['skill_0_min']) ? 'NULL' : "'".$_REQUEST['skill_0_min']."'";
			$skill_0_max = empty($_REQUEST['skill_0_max']) ? 'NULL' : "'".$_REQUEST['skill_0_max']."'";
			$skill_1_id = empty($_REQUEST['skill_1_id']) ? 'NULL' : "'".$_REQUEST['skill_1_id']."'";
			$skill_1_min = empty($_REQUEST['skill_1_min']) ? 'NULL' : "'".$_REQUEST['skill_1_min']."'";
			$skill_1_max = empty($_REQUEST['skill_1_max']) ? 'NULL' : "'".$_REQUEST['skill_1_max']."'";
			$skill_2_id = empty($_REQUEST['skill_2_id']) ? 'NULL' : "'".$_REQUEST['skill_2_id']."'";
			$skill_2_min = empty($_REQUEST['skill_2_min']) ? 'NULL' : "'".$_REQUEST['skill_2_min']."'";
			$skill_2_max = empty($_REQUEST['skill_2_max']) ? 'NULL' : "'".$_REQUEST['skill_2_max']."'";
			$skill_3_id = empty($_REQUEST['skill_3_id']) ? 'NULL' : "'".$_REQUEST['skill_3_id']."'";
			$skill_3_min = empty($_REQUEST['skill_3_min']) ? 'NULL' : "'".$_REQUEST['skill_3_min']."'";
			$skill_3_max = empty($_REQUEST['skill_3_max']) ? 'NULL' : "'".$_REQUEST['skill_3_max']."'";
			$skill_4_id = empty($_REQUEST['skill_4_id']) ? 'NULL' : "'".$_REQUEST['skill_4_id']."'";
			$skill_4_min = empty($_REQUEST['skill_4_min']) ? 'NULL' : "'".$_REQUEST['skill_4_min']."'";
			$skill_4_max = empty($_REQUEST['skill_4_max']) ? 'NULL' : "'".$_REQUEST['skill_4_max']."'";
			$skill_5_id = empty($_REQUEST['skill_5_id']) ? 'NULL' : "'".$_REQUEST['skill_5_id']."'";
			$skill_5_min = empty($_REQUEST['skill_5_min']) ? 'NULL' : "'".$_REQUEST['skill_5_min']."'";
			$skill_5_max = empty($_REQUEST['skill_5_max']) ? 'NULL' : "'".$_REQUEST['skill_5_max']."'";
			$skill_6_id = empty($_REQUEST['skill_6_id']) ? 'NULL' : "'".$_REQUEST['skill_6_id']."'";
			$skill_6_min = empty($_REQUEST['skill_6_min']) ? 'NULL' : "'".$_REQUEST['skill_6_min']."'";
			$skill_6_max = empty($_REQUEST['skill_6_max']) ? 'NULL' : "'".$_REQUEST['skill_6_max']."'";
			$skill_7_id = empty($_REQUEST['skill_7_id']) ? 'NULL' : "'".$_REQUEST['skill_7_id']."'";
			$skill_7_min = empty($_REQUEST['skill_7_min']) ? 'NULL' : "'".$_REQUEST['skill_7_min']."'";
			$skill_7_max = empty($_REQUEST['skill_7_max']) ? 'NULL' : "'".$_REQUEST['skill_7_max']."'";
			$skill_8_id = empty($_REQUEST['skill_8_id']) ? 'NULL' : "'".$_REQUEST['skill_8_id']."'";
			$skill_8_min = empty($_REQUEST['skill_8_min']) ? 'NULL' : "'".$_REQUEST['skill_8_min']."'";
			$skill_8_max = empty($_REQUEST['skill_8_max']) ? 'NULL' : "'".$_REQUEST['skill_8_max']."'";
			$skill_9_id = empty($_REQUEST['skill_9_id']) ? 'NULL' : "'".$_REQUEST['skill_9_id']."'";
			$skill_9_min = empty($_REQUEST['skill_9_min']) ? 'NULL' : "'".$_REQUEST['skill_9_min']."'";
			$skill_9_max = empty($_REQUEST['skill_9_max']) ? 'NULL' : "'".$_REQUEST['skill_9_max']."'";
			
			$regeneration_trefferpunkte = empty($_REQUEST['regeneration_trefferpunkte']) ? 'NULL' : "'".$_REQUEST['regeneration_trefferpunkte']."'";
			$regeneration_ausdauer = empty($_REQUEST['regeneration_ausdauer']) ? 'NULL' : "'".$_REQUEST['regeneration_ausdauer']."'";
			$zaehmen_tierkunde = empty($_REQUEST['zaehmen_tierkunde']) ? 'NULL' : "'".$_REQUEST['zaehmen_tierkunde']."'";
			$zaehmen_zaehmen = empty($_REQUEST['zaehmen_zaehmen']) ? 'NULL' : "'".$_REQUEST['zaehmen_zaehmen']."'";
			$zaehmen_rezept = empty($_REQUEST['zaehmen_rezept']) ? 'NULL' : "'".$_REQUEST['zaehmen_rezept']."'";
			$sichtweite = empty($_REQUEST['sichtweite']) ? 'NULL' : "'".$_REQUEST['sichtweite']."'";
			$hoerweite = empty($_REQUEST['hoerweite']) ? 'NULL' : "'".$_REQUEST['hoerweite']."'";
			$schaden_min = empty($_REQUEST['schaden_min']) ? 'NULL' : "'".$_REQUEST['schaden_min']."'";
			$schaden_max = empty($_REQUEST['schaden_max']) ? 'NULL' : "'".$_REQUEST['schaden_max']."'";
			
			$status = isset($_REQUEST['status']) ? implode(',',$_REQUEST['status']) : '';
			
			$reit_itemid = $_REQUEST['reitbar'] == '1' ? "'".$_REQUEST['reit_itemid']."'" : 'NULL';
			
	    	$MySQL->query("INSERT INTO server.npc_type
	        		(reittier_id,kleidungsset,futterliste,emote_set,xp_klassen,
					kis_id,name,beschreibung,name_interface,
					geschlecht,bodyid,farbe,farbe_speech,
					farbe_emote,basis_sound_id,freigeschalten,status,
					kommentar,kl_min,kl_max,in_min,
					in_max,ge_min,ge_max,ff_min,
					ff_max,kk_min,kk_max,ko_min,
					ko_max,skill_0_id,skill_0_min,skill_0_max,
					skill_1_id,skill_1_min,skill_1_max,skill_2_id,
					skill_2_min,skill_2_max,skill_3_id,skill_3_min,
					skill_3_max,skill_4_id,skill_4_min,skill_4_max,
					skill_5_id,skill_5_min,skill_5_max,skill_6_id,
					skill_6_min,skill_6_max,skill_7_id,skill_7_min,
					skill_7_max,skill_8_id,skill_8_min,skill_8_max,
					skill_9_id,skill_9_min,skill_9_max,skill_rest,
					kann_schwimmen,lebensenergie_min,lebensenergie_max,ausdauer_min,
					ausdauer_max,geschwindigkeit_normal,geschwindigkeit_max,regeneration_trefferpunkte,
					regeneration_ausdauer,zaehmen_tierkunde,zaehmen_zaehmen,zaehmen_rezept,
					sichtweite,hoerweite,virtueller_ruestungswert,
					animation_hoehe, animation_breite, schaden_min, schaden_max,
					reitbar, reit_itemid, delay_gehen, delay_rennen,
					lastchangedby
				) VALUES
	        		(".$_REQUEST['reittier_id'].",".$_REQUEST['kleidungsset'].",".$_REQUEST['futterliste'].",".$_REQUEST['emote_set'].",".$_REQUEST['xp_klassen'].",
					'".$_REQUEST['kis_id']."','".$_REQUEST['name']."','".$_REQUEST['beschreibung']."','".$_REQUEST['name_interface']."',
					'".$_REQUEST['geschlecht']."','".$_REQUEST['bodyid']."','".$farbe."','".$farbe_speech."',
					'".$farbe_emote."','".$basis_sound_id."','".$_REQUEST['freigeschalten']."','".$status."',
					".$kommentar.",".$kl_min.", ".$kl_max.", ".$in_min.", ".$in_max.", ".$ge_min.", ".$ge_max.", ".$ff_min.", ".$ff_max.", ".$kk_min.", ".$kk_max.", ".$ko_min.", ".$ko_max.", ".$skill_0_id.", ".$skill_0_min.", ".$skill_0_max.", ".$skill_1_id.", ".$skill_1_min.", ".$skill_1_max.", ".$skill_2_id.", ".$skill_2_min.", ".$skill_2_max.", ".$skill_3_id.", ".$skill_3_min.", ".$skill_3_max.", ".$skill_4_id.", ".$skill_4_min.", ".$skill_4_max.", ".$skill_5_id.", ".$skill_5_min.", ".$skill_5_max.", ".$skill_6_id.", ".$skill_6_min.", ".$skill_6_max.", ".$skill_7_id.", ".$skill_7_min.", ".$skill_7_max.", ".$skill_8_id.", ".$skill_8_min.", ".$skill_8_max.", ".$skill_9_id.", ".$skill_9_min.", ".$skill_9_max.",'".$_REQUEST['skill_rest']."',
					'".$_REQUEST['kann_schwimmen']."','".$_REQUEST['lebensenergie_min']."','".$_REQUEST['lebensenergie_max']."','".$_REQUEST['ausdauer_min']."',
					'".$_REQUEST['ausdauer_max']."','".$_REQUEST['geschwindigkeit_normal']."','".$_REQUEST['geschwindigkeit_max']."',
					".$regeneration_trefferpunkte.", ".$regeneration_ausdauer.", ".$zaehmen_tierkunde.", ".$zaehmen_zaehmen.", ".$zaehmen_rezept.", ".$sichtweite.", ".$hoerweite.",'".$_REQUEST['virtueller_ruestungswert']."',
					".$_REQUEST['animation_hoehe'].", ".$_REQUEST['animation_breite'].", ".$_REQUEST['schaden_min'].", ".$_REQUEST['schaden_max'].",
	 				'".$_REQUEST['reitbar']."', ".$reit_itemid.", '".$_REQUEST['delay_gehen']."', '".$_REQUEST['delay_rennen']."',
					'".$user->ID."'
				)");
				
	    	$MySQL->query("INSERT INTO server.spawn_npc
	    			(id,kis_override_id,npc_type,lastchangedby,profil)
	    		VALUES
	    			( (SELECT id FROM server.npc_type WHERE name_interface='".$_REQUEST['name_interface']."'),'".$_REQUEST['kis_id']."',
	    			  (SELECT id FROM server.npc_type WHERE name_interface='".$_REQUEST['name_interface']."'),'".$user->ID."','".$_REQUEST['beschreibung']."'
	    		)" );		  	    			 
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'npcs_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

if( isset( $_REQUEST['del'] ) )
{
    $MySQL->query( "DELETE FROM server.npc_type WHERE id = ".intval( $_REQUEST['del'] ) );
}

if( isset( $_REQUEST['dellink'] ) )
{
    $MySQL->query( "DELETE FROM server.spawn_npc_lootlisten WHERE spawn_npc_id=".intval( $_REQUEST['edit'] )." AND lootlisten=".intval( $_REQUEST['dellink'] ) );
}

if( isset( $_REQUEST['edit'] ) )
{
	$npcids = $MySQL->fetch_complete("SELECT id, name_interface FROM server.npc_type WHERE id != '".$_REQUEST['edit']."' ORDER BY `name_interface` ASC");
	$Smarty->assign('npcids', $npcids);

	if( isset($_REQUEST['newlink']))
	{
		if( isset( $_POST['send_form'] ) )
		{
		    $form_errors = array();
		
		    if( !check_fields( 'lootlisten', 'wkeit' ) )
		        $form_errors[] = form_error( 'uncomplete' );
			else
			{
				$res = $MySQL->query_first("SELECT COUNT(*) FROM server.spawn_npc_lootlisten WHERE spawn_npc_id=".$_REQUEST['edit']." AND lootlisten=".$_REQUEST['lootlisten']);
		    	if($res['COUNT(*)'] > 0) {
		    		$form_errors[] = form_error( 'duplicate_vendoren_liste' );
		    	}
			}
		    
		        
		    if( count( $form_errors ) > 0 )
		    {
		        $Smarty->assign( 'form_errors', $form_errors );
	
		        assign_formvalues( 'lootlisten', 'wkeit' );
		    }
		    else
		    {
		    	$MySQL->query("INSERT INTO server.spawn_npc_lootlisten
	        		(
	        		spawn_npc_id,
					wkeit,
	        		lootlisten,
	        		lastchangedby
				) VALUES
	        		(".$_REQUEST['edit'].",
					".$_REQUEST['wkeit'].",
	        		".$_REQUEST['lootlisten'].",
					".$user->ID."
				)");
		    }
		}

		$lists = $MySQL->fetch_complete("SELECT id, name FROM server.lootlisten ORDER BY `name` ASC");
		$Smarty->assign('lists', $lists);
		
		$formvalues = array();
		$formvalues['edit'] = $_REQUEST['edit'];
		$formvalues['region'] = $_REQUEST['region'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'npcs_newlink.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
	else if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'reittier_id', 'kleidungsset', 'futterliste', 'emote_set', 'xp_klassen', 'kis_id', 'name', 'beschreibung', 'name_interface', 'geschlecht', 'bodyid', 'freigeschalten',
	    		'skill_rest',
				'kann_schwimmen', 'lebensenergie_min', 'lebensenergie_max', 'ausdauer_min', 'ausdauer_max', 'geschwindigkeit_normal', 'geschwindigkeit_max',
				'virtueller_ruestungswert', 'animation_hoehe', 'animation_breite', 'schaden_min', 'schaden_max' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	        
	    if( $_REQUEST['reitbar'] == '1' && !check_fields('reit_itemid', 'delay_gehen', 'delay_rennen') )
	    	$form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );

	        assign_formvalues( 'reittier_id', 'kleidungsset', 'futterliste', 'emote_set', 'xp_klassen', 'kis_id', 'name', 'beschreibung', 'name_interface', 'geschlecht', 'bodyid', 'farbe', 'farbe_speech', 'farbe_emote', 'basis_sound_id', 'freigeschalten',
	    		'kl_min', 'kl_max', 'in_min', 'in_max', 'ge_min', 'ge_max', 'ff_min', 'ff_max', 'kk_min', 'kk_max', 'ko_min', 'ko_max',
	    		'skill_0_id', 'skill_0_min', 'skill_0_max',
				'skill_1_id', 'skill_1_min', 'skill_1_max',
				'skill_2_id', 'skill_2_min', 'skill_2_max',
				'skill_3_id', 'skill_3_min', 'skill_3_max',
				'skill_4_id', 'skill_4_min', 'skill_4_max',
				'skill_5_id', 'skill_5_min', 'skill_5_max',
				'skill_6_id', 'skill_6_min', 'skill_6_max',
				'skill_7_id', 'skill_7_min', 'skill_7_max',
				'skill_8_id', 'skill_8_min', 'skill_8_max',
				'skill_9_id', 'skill_9_min', 'skill_9_max', 'skill_rest',
				'kann_schwimmen', 'lebensenergie_min', 'lebensenergie_max', 'ausdauer_min', 'ausdauer_max', 'geschwindigkeit_normal', 'geschwindigkeit_max',
				'regeneration_trefferpunkte', 'regeneration_ausdauer',
				'zaehmen_tierkunde', 'zaehmen_zaehmen', 'zaehmen_rezept',
				'sichtweite', 'hoerweite', 'virtueller_ruestungswert', 'animation_hoehe', 'animation_breite', 'schaden_min', 'schaden_max', 'edit',
				'reitbar', 'reit_itemid', 'delay_gehen', 'delay_rennen' );
	    	
	    	$page['content'] = $Smarty->fetch( 'npcs_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $farbe = empty($_REQUEST['farbe']) ? '0' : $_REQUEST['farbe'];
		    $farbe_speech = empty($_REQUEST['farbe_speech']) ? '0' : $_REQUEST['farbe_speech'];
		    $farbe_emote = empty($_REQUEST['farbe_emote']) ? '0' : $_REQUEST['farbe_emote'];
		    $basis_sound_id = empty($_REQUEST['basis_sound_id']) ? '0' : $_REQUEST['basis_sound_id'];
		    
		    $kommentar = empty($_REQUEST['kommentar']) ? 'NULL' : "'".$_REQUEST['kommentar']."'";
		    
		    $kl_min = empty($_REQUEST['kl_min']) ? 'NULL' : "'".$_REQUEST['kl_min']."'";
			$kl_max = empty($_REQUEST['kl_max']) ? 'NULL' : "'".$_REQUEST['kl_max']."'";
			$in_min = empty($_REQUEST['in_min']) ? 'NULL' : "'".$_REQUEST['in_min']."'";
			$in_max = empty($_REQUEST['in_max']) ? 'NULL' : "'".$_REQUEST['in_max']."'";
			$ge_min = empty($_REQUEST['ge_min']) ? 'NULL' : "'".$_REQUEST['ge_min']."'";
			$ge_max = empty($_REQUEST['ge_max']) ? 'NULL' : "'".$_REQUEST['ge_max']."'";
			$ff_min = empty($_REQUEST['ff_min']) ? 'NULL' : "'".$_REQUEST['ff_min']."'";
			$ff_max = empty($_REQUEST['ff_max']) ? 'NULL' : "'".$_REQUEST['ff_max']."'";
			$kk_min = empty($_REQUEST['kk_min']) ? 'NULL' : "'".$_REQUEST['kk_min']."'";
			$kk_max = empty($_REQUEST['kk_max']) ? 'NULL' : "'".$_REQUEST['kk_max']."'";
			$ko_min = empty($_REQUEST['ko_min']) ? 'NULL' : "'".$_REQUEST['ko_min']."'";
			$ko_max = empty($_REQUEST['ko_max']) ? 'NULL' : "'".$_REQUEST['ko_max']."'";
			$skill_0_id = empty($_REQUEST['skill_0_id']) ? 'NULL' : "'".$_REQUEST['skill_0_id']."'";
			$skill_0_min = empty($_REQUEST['skill_0_min']) ? 'NULL' : "'".$_REQUEST['skill_0_min']."'";
			$skill_0_max = empty($_REQUEST['skill_0_max']) ? 'NULL' : "'".$_REQUEST['skill_0_max']."'";
			$skill_1_id = empty($_REQUEST['skill_1_id']) ? 'NULL' : "'".$_REQUEST['skill_1_id']."'";
			$skill_1_min = empty($_REQUEST['skill_1_min']) ? 'NULL' : "'".$_REQUEST['skill_1_min']."'";
			$skill_1_max = empty($_REQUEST['skill_1_max']) ? 'NULL' : "'".$_REQUEST['skill_1_max']."'";
			$skill_2_id = empty($_REQUEST['skill_2_id']) ? 'NULL' : "'".$_REQUEST['skill_2_id']."'";
			$skill_2_min = empty($_REQUEST['skill_2_min']) ? 'NULL' : "'".$_REQUEST['skill_2_min']."'";
			$skill_2_max = empty($_REQUEST['skill_2_max']) ? 'NULL' : "'".$_REQUEST['skill_2_max']."'";
			$skill_3_id = empty($_REQUEST['skill_3_id']) ? 'NULL' : "'".$_REQUEST['skill_3_id']."'";
			$skill_3_min = empty($_REQUEST['skill_3_min']) ? 'NULL' : "'".$_REQUEST['skill_3_min']."'";
			$skill_3_max = empty($_REQUEST['skill_3_max']) ? 'NULL' : "'".$_REQUEST['skill_3_max']."'";
			$skill_4_id = empty($_REQUEST['skill_4_id']) ? 'NULL' : "'".$_REQUEST['skill_4_id']."'";
			$skill_4_min = empty($_REQUEST['skill_4_min']) ? 'NULL' : "'".$_REQUEST['skill_4_min']."'";
			$skill_4_max = empty($_REQUEST['skill_4_max']) ? 'NULL' : "'".$_REQUEST['skill_4_max']."'";
			$skill_5_id = empty($_REQUEST['skill_5_id']) ? 'NULL' : "'".$_REQUEST['skill_5_id']."'";
			$skill_5_min = empty($_REQUEST['skill_5_min']) ? 'NULL' : "'".$_REQUEST['skill_5_min']."'";
			$skill_5_max = empty($_REQUEST['skill_5_max']) ? 'NULL' : "'".$_REQUEST['skill_5_max']."'";
			$skill_6_id = empty($_REQUEST['skill_6_id']) ? 'NULL' : "'".$_REQUEST['skill_6_id']."'";
			$skill_6_min = empty($_REQUEST['skill_6_min']) ? 'NULL' : "'".$_REQUEST['skill_6_min']."'";
			$skill_6_max = empty($_REQUEST['skill_6_max']) ? 'NULL' : "'".$_REQUEST['skill_6_max']."'";
			$skill_7_id = empty($_REQUEST['skill_7_id']) ? 'NULL' : "'".$_REQUEST['skill_7_id']."'";
			$skill_7_min = empty($_REQUEST['skill_7_min']) ? 'NULL' : "'".$_REQUEST['skill_7_min']."'";
			$skill_7_max = empty($_REQUEST['skill_7_max']) ? 'NULL' : "'".$_REQUEST['skill_7_max']."'";
			$skill_8_id = empty($_REQUEST['skill_8_id']) ? 'NULL' : "'".$_REQUEST['skill_8_id']."'";
			$skill_8_min = empty($_REQUEST['skill_8_min']) ? 'NULL' : "'".$_REQUEST['skill_8_min']."'";
			$skill_8_max = empty($_REQUEST['skill_8_max']) ? 'NULL' : "'".$_REQUEST['skill_8_max']."'";
			$skill_9_id = empty($_REQUEST['skill_9_id']) ? 'NULL' : "'".$_REQUEST['skill_9_id']."'";
			$skill_9_min = empty($_REQUEST['skill_9_min']) ? 'NULL' : "'".$_REQUEST['skill_9_min']."'";
			$skill_9_max = empty($_REQUEST['skill_9_max']) ? 'NULL' : "'".$_REQUEST['skill_9_max']."'";
			
			$regeneration_trefferpunkte = empty($_REQUEST['regeneration_trefferpunkte']) ? 'NULL' : "'".$_REQUEST['regeneration_trefferpunkte']."'";
			$regeneration_ausdauer = empty($_REQUEST['regeneration_ausdauer']) ? 'NULL' : "'".$_REQUEST['regeneration_ausdauer']."'";
			$zaehmen_tierkunde = empty($_REQUEST['zaehmen_tierkunde']) ? 'NULL' : "'".$_REQUEST['zaehmen_tierkunde']."'";
			$zaehmen_zaehmen = empty($_REQUEST['zaehmen_zaehmen']) ? 'NULL' : "'".$_REQUEST['zaehmen_zaehmen']."'";
			$zaehmen_rezept = empty($_REQUEST['zaehmen_rezept']) ? 'NULL' : "'".$_REQUEST['zaehmen_rezept']."'";
			$sichtweite = empty($_REQUEST['sichtweite']) ? 'NULL' : "'".$_REQUEST['sichtweite']."'";
			$hoerweite = empty($_REQUEST['hoerweite']) ? 'NULL' : "'".$_REQUEST['hoerweite']."'";
			$schaden_min = empty($_REQUEST['schaden_min']) ? 'NULL' : "'".$_REQUEST['schaden_min']."'";
			$schaden_max = empty($_REQUEST['schaden_max']) ? 'NULL' : "'".$_REQUEST['schaden_max']."'";
			
			$status = isset($_REQUEST['status']) ? implode(',',$_REQUEST['status']) : '';
			
			$reit_itemid = $_REQUEST['reitbar'] == '1' ? "'".$_REQUEST['reit_itemid']."'" : 'NULL';
	    	
	    	$MySQL->query("UPDATE server.npc_type
	        	SET
	        		reittier_id=".$_REQUEST['reittier_id'].",
					kleidungsset=".$_REQUEST['kleidungsset'].",
					futterliste=".$_REQUEST['futterliste'].",
					emote_set=".$_REQUEST['emote_set'].",
					xp_klassen=".$_REQUEST['xp_klassen'].",
					kis_id=".$_REQUEST['kis_id'].",
					name='".$_REQUEST['name']."',
					beschreibung='".$_REQUEST['beschreibung']."',
					name_interface='".$_REQUEST['name_interface']."',
					geschlecht='".$_REQUEST['geschlecht']."',
					bodyid='".$_REQUEST['bodyid']."',
					farbe='".$farbe."',
					farbe_speech='".$farbe_speech."',
					farbe_emote='".$farbe_emote."',
					basis_sound_id='".$basis_sound_id."',
					freigeschalten='".$_REQUEST['freigeschalten']."',
					status='".$status."',
					kommentar=".$kommentar.",
					kl_min=".$kl_min.", 
					kl_max=".$kl_max.", 
					in_min=".$in_min.", 
					in_max=".$in_max.", 
					ge_min=".$ge_min.", 
					ge_max=".$ge_max.", 
					ff_min=".$ff_min.", 
					ff_max=".$ff_max.", 
					kk_min=".$kk_min.", 
					kk_max=".$kk_max.", 
					ko_min=".$ko_min.", 
					ko_max=".$ko_max.", 
					skill_0_id=".$skill_0_id.", 
					skill_0_min=".$skill_0_min.", 
					skill_0_max=".$skill_0_max.", 
					skill_1_id=".$skill_1_id.", 
					skill_1_min=".$skill_1_min.", 
					skill_1_max=".$skill_1_max.", 
					skill_2_id=".$skill_2_id.", 
					skill_2_min=".$skill_2_min.", 
					skill_2_max=".$skill_2_max.", 
					skill_3_id=".$skill_3_id.", 
					skill_3_min=".$skill_3_min.", 
					skill_3_max=".$skill_3_max.", 
					skill_4_id=".$skill_4_id.", 
					skill_4_min=".$skill_4_min.", 
					skill_4_max=".$skill_4_max.", 
					skill_5_id=".$skill_5_id.", 
					skill_5_min=".$skill_5_min.", 
					skill_5_max=".$skill_5_max.", 
					skill_6_id=".$skill_6_id.", 
					skill_6_min=".$skill_6_min.", 
					skill_6_max=".$skill_6_max.", 
					skill_7_id=".$skill_7_id.", 
					skill_7_min=".$skill_7_min.", 
					skill_7_max=".$skill_7_max.", 
					skill_8_id=".$skill_8_id.", 
					skill_8_min=".$skill_8_min.", 
					skill_8_max=".$skill_8_max.", 
					skill_9_id=".$skill_9_id.", 
					skill_9_min=".$skill_9_min.", 
					skill_9_max=".$skill_9_max.", 
					skill_rest='".$_REQUEST['skill_rest']."',
					kann_schwimmen='".$_REQUEST['kann_schwimmen']."',
					lebensenergie_min='".$_REQUEST['lebensenergie_min']."',
					lebensenergie_max='".$_REQUEST['lebensenergie_max']."',
					ausdauer_min='".$_REQUEST['ausdauer_min']."',
					ausdauer_max='".$_REQUEST['ausdauer_max']."',
					geschwindigkeit_normal='".$_REQUEST['geschwindigkeit_normal']."',
					geschwindigkeit_max='".$_REQUEST['geschwindigkeit_max']."',
					regeneration_trefferpunkte=".$regeneration_trefferpunkte.", 
					regeneration_ausdauer=".$regeneration_ausdauer.", 
					zaehmen_tierkunde=".$zaehmen_tierkunde.", 
					zaehmen_zaehmen=".$zaehmen_zaehmen.", 
					zaehmen_rezept=".$zaehmen_rezept.", 
					sichtweite=".$sichtweite.", 
					hoerweite=".$hoerweite.", 
					virtueller_ruestungswert='".$_REQUEST['virtueller_ruestungswert']."',
					animation_hoehe=".$_REQUEST['animation_hoehe'].",
					animation_breite=".$_REQUEST['animation_breite'].",
	    			schaden_min=".$_REQUEST['schaden_min'].",
	    			schaden_max=".$_REQUEST['schaden_max'].",
	    			reitbar='".$_REQUEST['reitbar']."',
	    			reit_itemid=".$reit_itemid.",
	    			delay_gehen='".$_REQUEST['delay_gehen']."',
	    			delay_rennen='".$_REQUEST['delay_rennen']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 id = ".intval( $_REQUEST['edit'] ));
	    	
	    	$MySQL->query("UPDATE server.spawn_npc 
	        	SET 
	    			 kis_override_id=".$_REQUEST['kis_id'].",
	    			 profil='".$_REQUEST['beschreibung']."',
	    			 lastchangedby='".$user->ID."'	
	        	WHERE 
	        		 id = ".intval( $_REQUEST['edit'] ));	    	
	    }
	}
	else
	{	
		$links = $MySQL->fetch_complete("SELECT server.spawn_npc_lootlisten.*, server.lootlisten.name AS name FROM server.spawn_npc_lootlisten JOIN server.lootlisten ON(server.spawn_npc_lootlisten.lootlisten=server.lootlisten.id) WHERE spawn_npc_id=".$_REQUEST['edit']." ORDER BY name ASC");
		$Smarty->assign('links', $links);
	
		$formvalues = $MySQL->query_first( "SELECT * FROM server.npc_type WHERE id = ".intval( $_REQUEST['edit'] ) );
		$formvalues['edit'] = $_REQUEST['edit'];
		$formvalues['status'] = explode(',', $formvalues['status']);
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'npcs_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$entries = $MySQL->fetch_complete( "
	SELECT 
		server.npc_type.*, 
		server.kis.name AS ki_name,
		server.xp_klassen.bezeichnung AS xp_classname,
		(SELECT Count(*) FROM server.spawn_npc_lootlisten WHERE spawn_npc_id=server.npc_type.id ) AS loot
	FROM server.npc_type
	JOIN server.kis ON(server.npc_type.kis_id=server.kis.id)
	JOIN server.xp_klassen ON(server.npc_type.xp_klassen=server.xp_klassen.id)
	ORDER BY name_interface ASC" );

$Smarty->assign('entries', $entries);

$page['content'] = $Smarty->fetch( 'npcs.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>
