<?php 
/*
 * Created on 31.05.2011
 *
 * @copyright (c) 2011 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */
?>

<?php 
function displayClanItems($action, $raumid, $raumname, $staerke, $verteidigung, $glueck, $ausdauer, $geschwindigkeit, $info){
	if($action == ''){
		$raume_qry = mysql_query('SELECT * FROM clan_item order by tp');
		?>
			<table>
				<tr>
					<th align="center" colspan="11">Clanr&auml;ume bearbeiten</th>
				</tr>
				<tr><td>Name</td><td>St&auml;rke</td><td>Verteidigung</td><td>Gl&uuml;ck</td><td>Ausdauer</td><td>Geschwindigkeit</td><td>Kaufpreis</td><td>Miete</td><td>Nutzungsgeb&uuml;hr</td><td>TP</td><td>bearbeiten</td></tr>
		<?php
			while($res = mysql_fetch_assoc($raume_qry)){
				echo '<tr><td>'.$res['name'].'</td><td>'.$res['staerke'].'</td><td>'.$res['verteidigung'].'</td><td>'.$res['glueck'].'</td><td>'.$res['ausdauer'].'</td><td>'.$res['geschwindigkeit'].'</td><td>'.$res['preis'].'</td><td>'.$res['miete'].'</td><td>'.$res['nutzung'].'</td><td>'.$res['tp'].'</td><td><a href="'.$_SERVER['PHP_SELF'].'?choose=clanitems&action=edit&raumid='.$res['id'].'">edit</a> | <a href="'.$_SERVER['PHP_SELF'].'?choose=clanitems&action=delete&raumid='.$res['id'].'">delete</a></td></tr>';
			}
		?>
				<tr>
					<td align="center" colspan="11">
						<a href="<?php echo $_SERVER['PHP_SELF'].'?choose=clanitems&action=create'; ?>">Raum erstellen</a>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="11">
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>">Zum Hauptmenu</a>
					</td>
				</tr>
			</table>
		<?php
	} else if($action == 'edit' || $action == 'create'){
		if($raumname !== NULL){ // Sprich es wurde was geaendert!
			// Berechne den TP Verbrauch
			$b38 = 1.3; // Parameter aus Exceltabelle
			$c38 = 0.3;
			$g3 = ($staerke!=0?1:0) + ($verteidigung!=0?1:0) + ($glueck!=0?1:0) + ($ausdauer!=0?1:0) + ($geschwindigkeit!=0?1:0);
			$summe_werte = ($staerke+$verteidigung+$glueck+$ausdauer+$geschwindigkeit);
			$tp = round($summe_werte * (pow($b38,$g3)-$c38));
			// Berechne Kaufpreis
			$b33 = 1.1; // Parameter aus Exceltabelle
			$c33 = 3000;
			$kaufpreis = round(pow($b33,$summe_werte) * $c33);
			// Berechne Miete
			$b42=0.5; $c42=2; $d42=4; $e42=2; $f42=1; $g42=5; $h42=6; $i42=1.9; $j42=100;
			$gewichtete_summe= $ausdauer*$b42+$staerke*$c42+$verteidigung*$d42+$geschwindigkeit*$e42+$glueck*$f42;
			$miete = floor(pow($i42,log($gewichtete_summe,$h42)*$g42)*$j42/7); // Durch sieben (weil 7 Chars pro User)
			// Berechne Nutzungsgebuehr
			$b46 = 100; $c46 = 0.69; $d46 = -1762;
			$nutz = floor(($miete*7*0.7)/log($gewichtete_summe, $b46)*$c46+$d46);
			
			// Und jetzt poliere die Preise optisch etwas auf
			$stepkp = round(pow(10,floor(max(log10($kaufpreis),2)))/4);
			$stepmp = round(pow(10,floor(max(log10($miete),2)))/4);
			$stepnp = round(pow(10,floor(max(log10($nutz),2)))/4);
			
			$kaufpreis = round($kaufpreis/$stepkp)*$stepkp;
			$miete = ceil($miete/$stepmp)*$stepmp;
			$nutz = ceil($nutz/$stepnp)*$stepnp;
			// So und nun kann weiter gemacht werden!
			if($action == 'edit'){
				logaction('Der Raum '.$raumname.' wurde editiert!');
				mysql_query('Update clan_item set name = \''.$raumname.'\', staerke = '.$staerke.', verteidigung = '.$verteidigung.', ausdauer = '.$ausdauer.', geschwindigkeit = '.$geschwindigkeit.', glueck = '.$glueck.', preis = '.$kaufpreis.', miete = '.$miete.', nutzung = '.$nutz.', tp = '.$tp.', info = \''.$info.'\' where id = '.$raumid.'');
			} else if($action == 'create'){
				logaction('Der Raum '.$raumname.' wurde erstellt!');
				mysql_query('INSERT INTO clan_item(name, staerke, verteidigung, ausdauer, geschwindigkeit, glueck, preis, miete, nutzung, tp, info) values(\''.$raumname.'\', '.$staerke.', '.$verteidigung.', '.$ausdauer.', '.$geschwindigkeit.', '.$glueck.', '.$kaufpreis.', '.$miete.', '.$nutz.', '.$tp.', \''.$info.'\')');
			}
		}

		if($action == 'edit'){
			$raum = mysql_fetch_assoc(mysql_query('SELECT * FROM clan_item WHERE id = '.$raumid));
			$aLabel = 'editieren';
		} else{
			$aLabel = 'erstellen';
		}
		?>
		<form action="" method="POST">
			<input name="choose" value="clanitems" type="hidden"></input>
			<input name="action" value="<?php echo $action; ?>" type="hidden"></input>
			<input name="raumid" value="<?php echo $raumid; ?>" type="hidden"></input>
			<table>
				<tr>
					<th align="center" colspan="2">Clanr&auml;ume <?php echo $aLabel; ?></th>
				</tr>
				<tr>
					<td>Name</td><td><input type="text" class="text" name="raumname" value="<?php echo $raum['name']; ?>" /></td>
				</tr>
				<tr>
					<td>St&auml;rke</td><td><input type="text" class="text" name="staerke" value="<?php echo $raum['staerke']; ?>" /></td>
				</tr>
				<tr>
					<td>Verteidigung</td><td><input type="text" class="text" name="verteidigung" value="<?php echo $raum['verteidigung']; ?>" /></td>
				</tr>
				<tr>
					<td>Gl&uuml;ck</td><td><input type="text" class="text" name="glueck" value="<?php echo $raum['glueck']; ?>" /></td>
				</tr>
				<tr>
					<td>Ausdauer</td><td><input type="text" class="text" name="ausdauer" value="<?php echo $raum['ausdauer']; ?>" /></td>
				</tr>
				<tr>
					<td>Geschwindigkeit</td><td><input type="text" class="text" name="geschwindigkeit" value="<?php echo $raum['geschwindigkeit']; ?>" /></td>
				</tr>
				<tr>
					<td>Kaufpreis</td><td><?php echo $raum['preis']; ?></td>
				</tr>
				<tr>
					<td>Miete</td><td><?php echo $raum['miete']; ?></td>
				</tr>
				<tr>
					<td>Nutzungsgeb&uuml;hr</td><td><?php echo $raum['nutzung']; ?></td>
				</tr>
				<tr>
					<td>Trainingspunkte</td><td><?php echo $raum['tp']; ?></td>
				</tr>
				<tr>
					<td>Info</td><td><textarea name="info" rows="10" cols="50"><?php echo $raum['info']; ?></textarea></td>
				</tr>
				<tr>
					<td align="center" colspan="2"><input type="submit" /></td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<a href="<?php echo $_SERVER['PHP_SELF'].'?choose=clanitems'; ?>">Zur &Uuml;bersicht</a>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>">Zum Hauptmenu</a>
					</td>
				</tr>
			</table>
		</form>
		<?php		
	} else if($action == 'delete'){
		?>
			<table>
		<?php
		if($raumname !== NULL){
			mysql_query('DELETE FROM clan_item WHERE id = '.$raumid);
			mysql_query('DELETE FROM clan_ware where item_id ='.$raumid);
		?>
				<tr>
					<td align="center" colspan="2">
						Der Raum "<?php echo $raumname; ?>" wurde gel&ouml;scht
					</td>
				</tr>
		<?php
		} else{
			$res = mysql_fetch_assoc(mysql_query('SELECT * FROM clan_item WHERE id = '.$raumid));
		?>
				<tr>
					<td align="center" colspan="2">
						Den Raum "<?php echo $res['name']; ?>" wirklich l&ouml;schen? <a href="<?php echo $_SERVER['PHP_SELF'].'?choose=clanitems&action=delete&raumid='.$raumid.'&raumname='.$res['name']; ?>">L&ouml;schen best&auml;tigen!</a>
					</td>
				</tr>
		<?php
		}
		?>
				<tr>
					<td align="center" colspan="2">
						<a href="<?php echo $_SERVER['PHP_SELF'].'?choose=clanitems'; ?>">Zur &Uuml;bersicht</a>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>">Zum Hauptmenu</a>
					</td>
				</tr>
			</table>
		<?php
		
	}
}
?>