<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'rohstoffe' ) ) 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'])) {
	$herstellungsids = $MySQL->fetch_complete("SELECT her_herstellungsid, name_interface FROM server.herstellung WHERE ressourcen='' ORDER BY `name_interface` ASC");
	$Smarty->assign('herstellungsids', $herstellungsids);
	
	$haeufigkeitsklassen = $MySQL->fetch_complete("SELECT id, name FROM server.haeufigkeitsklassen ORDER BY `name` ASC");
	$Smarty->assign('haeufigkeitsklassen', $haeufigkeitsklassen);
	
	$rohstoffklassen = $MySQL->fetch_complete("SELECT id, name FROM server.rohstoffklassen ORDER BY `name` ASC");
	$Smarty->assign('rohstoffklassen', $rohstoffklassen);
}

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

if( isset( $_REQUEST['del'] ) )
{
    $MySQL->query( "DELETE FROM server.regionen_herstellung WHERE herstellungsid = ".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', 'haeufigkeitsklasse', 'rohstoffklasse' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'herstellungsid', 'region', 'wkeit', 'haeufigkeitsklasse', 'rohstoffklasse', 'edit', 'region' );
	    	
	    	$page['content'] = $Smarty->fetch( 'rohstoffe_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.regionen_herstellung
	        	SET
	        		haeufigkeitsklasse=".$_REQUEST['haeufigkeitsklasse'].",
	        		rohstoffklasse=".$_REQUEST['rohstoffklasse'].",
	        		wkeit='".$_REQUEST['wkeit']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 herstellungsid = ".intval( $_REQUEST['edit'] )." AND region_id=".intval($_REQUEST['region']));
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.regionen_herstellung WHERE herstellungsid = ".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( 'rohstoffe_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$region_id = $regions[0][0];
if(isset($_REQUEST['region_id'])) {
	$region_id = intval($_REQUEST['region_id']);
	$formvalues = array('region_id' => $region_id);
	$Smarty->assign('formvalues', $formvalues);
}

if(!empty($region_id)) {
	$entries = $MySQL->fetch_complete( "
		SELECT 
			server.regionen_herstellung.*, 
			server.haeufigkeitsklassen.name AS hkeitclassname,
			server.herstellung.name_interface AS rohstoff,
			server.rohstoffklassen.name AS rohstoffclassname
		FROM server.regionen_herstellung 
		JOIN server.haeufigkeitsklassen ON(server.regionen_herstellung.haeufigkeitsklasse=server.haeufigkeitsklassen.id)
		JOIN server.herstellung ON(server.regionen_herstellung.herstellungsid=server.herstellung.her_herstellungsid)
		JOIN server.rohstoffklassen ON(server.regionen_herstellung.rohstoffklasse=server.rohstoffklassen.id)
		WHERE region_id = ".$region_id."
		ORDER BY wkeit DESC" );
}

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

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

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

?>