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

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

if(isset($_REQUEST['new']) || isset($_REQUEST['edit'])) {
	$kiids = $MySQL->fetch_complete("SELECT id, name FROM server.kis ORDER BY `name` ASC");
	$Smarty->assign('kiids', $kiids);
	
	$npcs = $MySQL->fetch_complete("SELECT id, name_interface FROM server.npc_type ORDER BY `name_interface` ASC");
	$Smarty->assign('npcs', $npcs);
}

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();

	    if( !check_fields( 'npc_type_id', 'kis_override_id', 'name', 'titel', 'profil', 'startx', 'starty', 'startz', 'startmap', 'restock_typ', 'verkaufszeiten', 'max_distanz_zu_start') )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'npc_type_id', 'kis_override_id', 'name', 'titel', 'profil', 'startx', 'starty', 'startz', 'startmap', 'restock_typ', 'verkaufszeiten', 'max_distanz_zu_start', 'notiz' );
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.vendoren
	        		(kis_override_id,
	        		npc_type_id,
	        		serial,
	        		name,
	        		titel,
	        		profil,
	        		startx,
	        		starty,
	        		startz,
	        		startmap,
	        		restock_typ,
	        		verkaufszeiten,
	        		notizen,
	        		created,
	        		createdby,        		
					lastchangedby,
					max_distanz_zu_start
				) VALUES
	        		(".$_REQUEST['kis_override_id'].",
	        		".$_REQUEST['npc_type_id'].",
	        		-1,
	        		'".$_REQUEST['name']."',
	        		'".$_REQUEST['titel']."',
	        		'".$_REQUEST['profil']."',
	        		".$_REQUEST['startx'].",
	        		".$_REQUEST['starty'].",
	        		".$_REQUEST['startz'].",
	        		".$_REQUEST['startmap'].",
	        		'".$_REQUEST['restock_typ']."',
	        		'".$_REQUEST['verkaufszeiten']."',
	        		'".$_REQUEST['notizen']."',
	        		NOW(),
	        		".$user->ID.",
					".$user->ID.",
					".$_REQUEST['max_distanz_zu_start']."
				)");
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'vendoren_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

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

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

if( isset( $_REQUEST['delqlink'] ) )
{
    $MySQL->query( "DELETE FROM server.vendoren_questbausteine WHERE questbausteine_id = ".intval( $_REQUEST['delqlink'] )." AND vendoren_id=".intval($_REQUEST['edit']) );
}

if( isset( $_REQUEST['edit'] ) )
{
	if( isset($_REQUEST['newlink']))
	{
		if( isset( $_POST['send_form'] ) )
		{
		    $form_errors = array();
		
		    if( !check_fields( 'sellbuy_lists_id', 'min_preis_multiplikator', 'max_preis_multiplikator', 'min_mengen_multiplikator', 'max_mengen_multiplikator' ) )
		        $form_errors[] = form_error( 'uncomplete' );
			else
			{
				$res = $MySQL->query_first("SELECT COUNT(*) FROM server.vendoren_sellbuy_lists WHERE vendoren_id=".$_REQUEST['edit']." AND sellbuy_lists_id=".$_REQUEST['sellbuy_lists_id']);
		    	if($res['COUNT(*)'] > 0) {
		    		$form_errors[] = form_error( 'duplicate_vendoren_liste' );
		    	}
			}
		    
		        
		    if( count( $form_errors ) > 0 )
		    {
		        $Smarty->assign( 'form_errors', $form_errors );
	
		        assign_formvalues( 'edit', 'sellbuy_lists_id', 'min_preis_multiplikator', 'max_preis_multiplikator', 'min_mengen_multiplikator', 'max_mengen_multiplikator', 'ist_verkaufsliste' );
		    }
		    else
		    {
		    	$MySQL->query("INSERT INTO server.vendoren_sellbuy_lists
	        		(
	        		vendoren_id,
	        		sellbuy_lists_id,
	        		min_preis_multiplikator,
	        		max_preis_multiplikator,
	        		min_mengen_multiplikator,
	        		max_mengen_multiplikator,
	        		ist_verkaufsliste,
	        		lastchangedby
				) VALUES
	        		(".$_REQUEST['edit'].",
	        		".$_REQUEST['sellbuy_lists_id'].",
	        		'".$_REQUEST['min_preis_multiplikator']."',
	        		'".$_REQUEST['max_preis_multiplikator']."',
	        		'".$_REQUEST['min_mengen_multiplikator']."',
	        		'".$_REQUEST['max_mengen_multiplikator']."',
	        		".$_REQUEST['ist_verkaufsliste'].",
					".$user->ID."
				)");
		    }
		}
		
		$lists = $MySQL->fetch_complete("SELECT id, name FROM server.sellbuy_lists ORDER BY `name` ASC");
		$Smarty->assign('lists', $lists);
		
		$formvalues = array();
		$formvalues['edit'] = $_REQUEST['edit'];
		$Smarty->assign( 'formvalues', $formvalues );		
		
		$page['content'] = $Smarty->fetch( 'vendoren_newlink.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
	else if( isset($_REQUEST['editlink']))
	{
		if( isset( $_POST['send_form'] ) )
		{
		    $form_errors = array();
		
		    if( !check_fields( 'sellbuy_lists_id', 'min_preis_multiplikator', 'max_preis_multiplikator', 'min_mengen_multiplikator', 'max_mengen_multiplikator' ) )
		        $form_errors[] = form_error( 'uncomplete' );
		        
		    if( count( $form_errors ) > 0 )
		    {
		        $Smarty->assign( 'form_errors', $form_errors );
	
		        assign_formvalues( 'edit', 'editlink', 'sellbuy_lists_id', 'min_preis_multiplikator', 'max_preis_multiplikator', 'min_mengen_multiplikator', 'max_mengen_multiplikator', 'ist_verkaufsliste' );
		    }
		    else
		    {
		    	$MySQL->query("UPDATE server.vendoren_sellbuy_lists
	        	SET
	        		vendoren_id=".$_REQUEST['edit'].",
	        		sellbuy_lists_id=".$_REQUEST['sellbuy_lists_id'].",
	        		min_preis_multiplikator='".$_REQUEST['min_preis_multiplikator']."',
	        		max_preis_multiplikator='".$_REQUEST['max_preis_multiplikator']."',
	        		min_mengen_multiplikator='".$_REQUEST['min_mengen_multiplikator']."',
	        		max_mengen_multiplikator='".$_REQUEST['max_mengen_multiplikator']."',
	        		ist_verkaufsliste=".$_REQUEST['ist_verkaufsliste'].",
	        		lastchangedby=".$user->ID."
				WHERE sellbuy_lists_id=".$_REQUEST['editlink']." AND vendoren_id=".$_REQUEST['edit'] );
		    	
		    	set_location('vendoren.php?edit='.$_REQUEST['edit'].SID);
		    }
		} else {
			$formvalues = $MySQL->query_first("SELECT * FROM server.vendoren_sellbuy_lists  WHERE sellbuy_lists_id=".$_REQUEST['editlink']." AND vendoren_id=".$_REQUEST['edit']);
			$formvalues['edit'] = $_REQUEST['edit'];
			$formvalues['editlink'] = $_REQUEST['editlink'];
			$Smarty->assign( 'formvalues', $formvalues );
		}
 
		$lists = $MySQL->fetch_complete("SELECT id, name FROM server.sellbuy_lists ORDER BY `name` ASC");
		$Smarty->assign('lists', $lists);
		
		$page['content'] = $Smarty->fetch( 'vendoren_editlink.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
	else if( isset($_REQUEST['newqlink']))
	{
		if( isset( $_POST['send_form'] ) )
		{
		    $form_errors = array();
		
		    if( !check_fields( 'questbausteine_id' ) )
		        $form_errors[] = form_error( 'uncomplete' );
			else
			{
				$res = $MySQL->query_first("SELECT COUNT(*) FROM server.vendoren_questbausteine WHERE vendoren_id=".$_REQUEST['edit']." AND questbausteine_id=".$_REQUEST['questbausteine_id']);
		    	if($res['COUNT(*)'] > 0) {
		    		$form_errors[] = form_error( 'duplicate_vendoren_qbaustein' );
		    	}
			}
		    
		        
		    if( count( $form_errors ) > 0 )
		    {
		        $Smarty->assign( 'form_errors', $form_errors );
	
		        assign_formvalues( 'edit', 'questbausteine_id' );
		    }
		    else
		    {
		    	$MySQL->query("INSERT INTO server.vendoren_questbausteine
	        		(
	        		vendoren_id,
	        		questbausteine_id
				) VALUES
	        		(".$_REQUEST['edit'].",
	        		".$_REQUEST['questbausteine_id']."
				)");
		    }
		}
		
		$lists = $MySQL->fetch_complete("SELECT id, name FROM server.questbausteine ORDER BY `name` ASC");
		$Smarty->assign('lists', $lists);
		
		$formvalues = array();
		$formvalues['edit'] = $_REQUEST['edit'];
		$Smarty->assign( 'formvalues', $formvalues );		
		
		$page['content'] = $Smarty->fetch( 'vendoren_newqlink.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
	else if( isset($_REQUEST['editqlink']))
	{
		if( isset( $_POST['send_form'] ) )
		{
		    $form_errors = array();
		
		    if( !check_fields( 'questbausteine_id' ) )
		        $form_errors[] = form_error( 'uncomplete' );
		        
		    if( count( $form_errors ) > 0 )
		    {
		        $Smarty->assign( 'form_errors', $form_errors );
	
		        assign_formvalues( 'edit', 'editqlink', 'questbausteine_id' );
		    }
		    else
		    {
		    	$MySQL->query("UPDATE server.vendoren_questbausteine
	        	SET
	        		vendoren_id=".$_REQUEST['edit'].",
	        		questbausteine_id=".$_REQUEST['questbausteine_id']."
				WHERE questbausteine_id=".$_REQUEST['editqlink']." AND vendoren_id=".$_REQUEST['edit'] );
		    	
		    	set_location('vendoren.php?edit='.$_REQUEST['edit'].SID);
		    }
		} else {
			$formvalues = $MySQL->query_first("SELECT * FROM server.vendoren_questbausteine  WHERE questbausteine_id=".$_REQUEST['editqlink']);
			$formvalues['edit'] = $_REQUEST['edit'];
			$formvalues['editqlink'] = $_REQUEST['editqlink'];
			$Smarty->assign( 'formvalues', $formvalues );
		}

		$lists = $MySQL->fetch_complete("SELECT id, name FROM server.questbausteine ORDER BY `name` ASC");
		$Smarty->assign('lists', $lists);
		
		$page['content'] = $Smarty->fetch( 'vendoren_editqlink.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
	else if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'npc_type_id', 'kis_override_id', 'name', 'titel', 'profil', 'startx', 'starty', 'startz', 'startmap', 'restock_typ', 'verkaufszeiten', 'max_distanz_zu_start' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );

	        assign_formvalues( 'npc_type_id', 'kis_override_id', 'name', 'titel', 'profil', 'startx', 'starty', 'startz', 'startmap', 'restock_typ', 'verkaufszeiten', 'max_distanz_zu_start', 'notizen', 'edit' );
	    	
	    	$page['content'] = $Smarty->fetch( 'vendoren_newlink.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	    	if(!empty($_REQUEST['notizen']))
	    		$notizen = "'".$_REQUEST['notizen']."'";
	    	else
	    		$notizen = "NULL";
	    	
	        $MySQL->query("UPDATE server.vendoren
	        	SET
					kis_override_id=".$_REQUEST['kis_override_id'].",
					npc_type_id=".$_REQUEST['npc_type_id'].",
					name='".$_REQUEST['name']."',
					titel='".$_REQUEST['titel']."',
					profil='".$_REQUEST['profil']."',
					startx=".$_REQUEST['startx'].",
					starty=".$_REQUEST['starty'].",
					startz=".$_REQUEST['startz'].",
					startmap=".$_REQUEST['startmap'].",
					restock_typ='".$_REQUEST['restock_typ']."',
					verkaufszeiten='".$_REQUEST['verkaufszeiten']."',
					notizen='".$_REQUEST['notizen']."',
					max_distanz_zu_start=".$_REQUEST['max_distanz_zu_start'].",
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 id = ".intval( $_REQUEST['edit'] ));
	    }
	}
	else
	{
		$links = $MySQL->fetch_complete("SELECT server.vendoren_sellbuy_lists.*, server.sellbuy_lists.name AS name FROM server.vendoren_sellbuy_lists JOIN server.sellbuy_lists ON(server.vendoren_sellbuy_lists.sellbuy_lists_id=server.sellbuy_lists.id) WHERE vendoren_id=".$_REQUEST['edit']." ORDER BY name ASC");
		$Smarty->assign('links', $links);
		
		$qlinks = $MySQL->fetch_complete("SELECT server.vendoren_questbausteine.*, server.questbausteine.name AS name FROM server.vendoren_questbausteine JOIN server.questbausteine ON(server.vendoren_questbausteine.questbausteine_id=server.questbausteine.id) WHERE vendoren_id=".$_REQUEST['edit']." ORDER BY name ASC");
		$Smarty->assign('qlinks', $qlinks);
		
		$formvalues = $MySQL->query_first( "SELECT * FROM server.vendoren WHERE id = ".intval( $_REQUEST['edit'] ) );
		$formvalues['edit'] = $_REQUEST['edit'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'vendoren_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$entries = $MySQL->fetch_complete( "
	SELECT 
		server.vendoren.*, 
		server.kis.name AS ki_name
	FROM server.vendoren
	JOIN server.kis ON(server.vendoren.kis_override_id=server.kis.id)
	ORDER BY name ASC" );

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

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

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

?>
