<?php

include './core.php';

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

$regions = $MySQL->fetch_complete("SELECT id, name_intern FROM server.regionen ORDER BY `name_intern` ASC");
$Smarty->assign('regions', $regions);

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

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'region_id', 'wkeit', 'npc_type', 'max_anzahl', 'ki_override', 'spieler_in_reichweite' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    else {
	    	$res = $MySQL->query_first("SELECT COUNT(*) FROM server.spawn_npc_region WHERE region_id=".$_REQUEST['region_id']." AND spawn_npc_id=".$_REQUEST['npc_type']);
	    	if($res['COUNT(*)'] > 0) {
	    		$form_errors[] = form_error( 'duplicate_region_spawns' );
	    	}
	    }
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'region_id', 'wkeit', 'npc_type', 'max_anzahl', 'ki_override', 'spieler_in_reichweite' );
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.spawn_npc_region
	        		(region_id, spawn_npc_id, wkeit, max_anzahl, ki_override, spieler_in_reichweite, lastchangedby)
	        	VALUES
	        		(".$_REQUEST['region_id'].",
	        		".$_REQUEST['npc_type'].",
	        		'".$_REQUEST['wkeit']."',
	        		".$_REQUEST['max_anzahl'].",
	        		".$_REQUEST['ki_override'].",
	        		".$_REQUEST['spieler_in_reichweite'].",
	        		".$user->ID.")");
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'spawns_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

if( isset( $_REQUEST['del'] ) )
{
    $MySQL->query( "DELETE FROM server.spawn_npc_region WHERE spawn_npc_id = ".intval( $_REQUEST['del'] )." AND region_id=".intval($_REQUEST['region']) );
}

if( isset( $_REQUEST['edit'] ) )
{
  if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'region', 'wkeit', 'max_anzahl', 'ki_override', 'spieler_in_reichweite' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'spawn_npc_id', 'region', 'wkeit', 'max_anzahl', 'ki_override', 'spieler_in_reichweite', 'edit', 'region' );
	    	
	    	$page['content'] = $Smarty->fetch( 'spawns_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.spawn_npc_region
	        	SET
	        		wkeit='".$_REQUEST['wkeit']."',
	        		max_anzahl='".$_REQUEST['max_anzahl']."',
	        		ki_override='".$_REQUEST['ki_override']."',
	        		spieler_in_reichweite='".$_REQUEST['spieler_in_reichweite']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 spawn_npc_id = ".intval( $_REQUEST['edit'] )." AND region_id=".intval($_REQUEST['region']));
	    }
	}
	else
	{
		$spawn_type = $MySQL->query_first("SELECT server.npc_type.name_interface AS name FROM server.npc_type JOIN server.spawn_npc ON(server.npc_type.id=server.spawn_npc.npc_type) WHERE server.spawn_npc.id=".intval( $_REQUEST['edit'] ) );
		$Smarty->assign('spawn_type', $spawn_type);				
	
		$formvalues = $MySQL->query_first( "SELECT * FROM server.spawn_npc_region WHERE spawn_npc_id = ".intval( $_REQUEST['edit'] )." AND region_id=".intval($_REQUEST['region']) );
		$formvalues['edit'] = $_REQUEST['edit'];
		$formvalues['region'] = $formvalues['region_id'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'spawns_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

if( !(isset($_REQUEST['new']) || isset($_REQUEST['edit'])) ) {
	if( isset( $_POST['send_aendern'] ) ) {
		$form_errors = array();
		
		if( !check_fields( 'parent_region_override_spawn' ) ) echo "b";
	
	    if( !check_fields( 'region_id', 'spawn_anzahl', 'spawn_min_regeneration', 'spawn_max_regeneration', 'parent_region_override_spawn' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    
	    if( count( $form_errors ) == 0 )	    
	    {
	        $MySQL->query("UPDATE server.regionen
	        	SET
	        		spawn_anzahl='".$_REQUEST['spawn_anzahl']."',
	        		spawn_min_regeneration='".$_REQUEST['spawn_min_regeneration']."',
	        		spawn_max_regeneration='".$_REQUEST['spawn_max_regeneration']."',
	        		parent_region_override_spawn='".$_REQUEST['parent_region_override_spawn']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 id=".$_REQUEST['region_id']);
	    }
	}
}

$region_id = $regions[0][0];
if(isset($_REQUEST['region_id'])) {
	$region_id = intval($_REQUEST['region_id']);
}
	
$formvalues = $MySQL->query_first("SELECT id AS region_id, name_intern, spawn_anzahl, spawn_min_regeneration, spawn_max_regeneration, parent_region_override_spawn FROM server.regionen WHERE id=".$region_id);	
$Smarty->assign('formvalues', $formvalues);


if(!empty($region_id)) {
	$entries = $MySQL->fetch_complete( "
		SELECT 
			server.spawn_npc_region.*, 
			server.npc_type.name_interface AS npc_type
		FROM server.spawn_npc_region
		JOIN server.spawn_npc ON(server.spawn_npc_region.spawn_npc_id=server.spawn_npc.id)
		JOIN server.npc_type ON(server.spawn_npc.npc_type=server.npc_type.id)
		WHERE region_id = ".$region_id."
		ORDER BY wkeit DESC" );
		
		$sum = $MySQL->fetch_complete( "
			SELECT
				SUM(server.spawn_npc_region.max_anzahl) AS summe
				FROM server.spawn_npc_region
				WHERE region_id= ".$region_id
		);
}

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

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

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

?>