<?php

include './core.php';

if( !isset( $user ) || (!$user->can_access( 'rohstoffe' ) && $user->rank < 10)) access_error();

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'charid', 'objekt', 'preis', 'intervall_d', 'letzte_zahlung', 'staffler' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'charid', 'objekt', 'preis', 'intervall_d', 'letzte_zahlung', 'staffler' );
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.miete
	        		(charid, objekt, preis, intervall_d, letzte_zahlung, staffler)
	        	VALUES
	        		(".$_REQUEST['charid'].",
	        		'".$_REQUEST['objekt']."',
	        		".$_REQUEST['preis'].",
	        		".$_REQUEST['intervall_d'].",
	        		".$_REQUEST['letzte_zahlung'].",
	        		".$_REQUEST['staffler'].")");
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'miete_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

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

if( isset( $_REQUEST['edit'] ) )
{
  if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'charid', 'objekt', 'preis', 'intervall_d', 'staffler', 'letzte_zahlung' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'charid', 'objekt', 'preis', 'intervall_d', 'staffler', 'letzte_zahlung' );
	    	
	    	$page['content'] = $Smarty->fetch( 'miete_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.miete
	        	SET
	        		charid='".$_REQUEST['charid']."',
	        		objekt='".$_REQUEST['objekt']."',
	        		intervall_d='".$_REQUEST['intervall_d']."',
					staffler='".$_REQUEST['staffler']."',
					preis='".$_REQUEST['preis']."',
					letzte_zahlung='".$_REQUEST['letzte_zahlung']."'
	        	WHERE 
	        		 id = ".intval( $_REQUEST['edit'] ));
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.miete WHERE id = ".intval( $_REQUEST['edit'] ));
		$formvalues['edit'] = $_REQUEST['edit'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'miete_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$mieter = $MySQL->fetch_complete("SELECT *, date_format(letzte_zahlung, '%d.%m.%y') AS zeit_format FROM server.miete INNER JOIN server.chars ON (miete.charid=chars.charid) ORDER BY `name` ASC");
$Smarty->assign('mieter', $mieter);

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

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

?>