<?php
if ("" != $_POST['ID'])
{
	$path = $pfad;
	$ref = "index.php?section=" . $_GET['section'] . "&kfz=" . $_POST['ID'];
	$id = $_POST['ID'];
}
elseif ("" != $_GET['kfz'])
{
	$path = $pfad;
	$ref = "index.php?section=" . $_GET['section'] . "&kfz=" . $_GET['kfz'];
	$id = $_GET['kfz'];
}
else
{
	$path = $pfad;
	$ref = "index.php?section=" . $_GET['section'] . "&kfz=" . $id;
}
while (strlen($id) < $_SESSION['Firma']['Stellen'])
{
	$id = "0" . $id;
}

$sql = "SELECT * FROM " . PREFIX . "_KfZ WHERE ID=\"" . $id . "\";";
$result = $db->query($sql);
$nt = $result->fetch_assoc();

echo "<fieldset>\n";
echo "	<legend>Bilder zum Fahrzeug:</legend>\n";

if ('del' == $_GET['action'])
{
	$delfile = $path . $_GET['kfz'] . "_" . $_GET['pic'] . ".jpg";
	$delthumb = $path . $_GET['kfz'] . "_" . $_GET['pic'] . "_thumb.jpg";
	if (unlink($delfile))
	{
		if (unlink($delthumb))
		{
			echo "<p>Bild gelöscht</p>";
		}
	}
	else
	{
		echo "<p>Fehler beim Löschen</p>";
	}
	echo '<a href="' . $ref . '&action=foto">Fotos bearbeiten</a>';
}
elseif ('add' == $_GET['action'])
{
	$ref .= "&action=speichern&pic=" . $_GET['pic'];
	echo "	<form action=\"" . $ref . "\" method=\"post\" enctype=\"multipart/form-data\">\n";
	echo "		<label>Foto ausw&auml;hlen</label>\n";
	echo "		<input type=\"file\" name=\"Foto\" />\n";
	echo "		<br />\n";
	echo "		<input type=\"submit\" name=\"Speichern\" value=\"Speichern\" />\n";
	echo "	</form>\n";
}
elseif ('speichern' == $_GET['action'])
{
	$uploadfile = $path . $_FILES['Foto']['name'];
	move_uploaded_file($_FILES['Foto']['tmp_name'], $uploadfile);
	$src = imagecreatefromjpeg($uploadfile);
	list($width, $height) = getimagesize($uploadfile);
	if ($height > $width)
	{
		$newheight = $large;
		$thumbheight = $small;
		$newwidth = ($width / $height) * $large;
		$thumbwidth = ($width / $height) * $small;
	}
	else
	{
		$newwidth = $large;
		$thumbwidth = $small;
		$newheight = ($height / $width) * $large;
		$thumbheight = ($height / $width) * $small;
	}
	$tmp = imagecreatetruecolor($newwidth, $newheight);
	$tmp_thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
	imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	imagecopyresampled($tmp_thumb, $src, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
	$filename = $path . $_GET['kfz'] . "_" . $_GET['pic'] . ".jpg";
	$thumbname = $path . $_GET['kfz'] . "_" . $_GET['pic'] . "_thumb.jpg";
	imagejpeg($tmp, $filename, 90);
	imagejpeg($tmp_thumb, $thumbname, 90);
	imagedestroy($src);
	imagedestroy($tmp);
	imagedestroy($tmp_thumb);
	unlink($path . $_FILES['Foto']['name']);
	chmod($filename, 0755);
	echo "<p>Bild hochgeladen</p>";
	echo '<a href="' . $ref . '&action=foto">Fotos bearbeiten</a>';
}
else
{
	echo "	<p>Klicken Sie auf ein vorhandenes Bild um es zu löschen. Zum Hochladen eines neuen Bildes klicken Sie auf einen Platzhalter.</p>\n";
	echo "	<form action=\"index.php?section=" . $_GET['section'] . "\" method=\"post\">\n";
	echo "		<fieldset>\n";
	echo "			<legend class=\"sub\">Bilder für die Homepage</legend>\n";
	echo "			<input type=\"hidden\" name=\"fotos\" value=\"1\" />\n";
	echo "			<table class=\"foto\">\n";
	echo "				<colgroup>\n";
	echo "					<col span=\"2\" width=\"350\" />\n";
	echo "				</colgroup>\n";
	echo "				<tr>\n";
	for ($i = 0; $i < 8; $i++)
	{
		$pic = $path . $id . "_" . $i . "_thumb.jpg";
		if ((2 == $i) OR (4 == $i) OR (6 == $i))
		{
			echo "				</tr>\n";
			echo "				<tr>\n";
		}
		if (file_exists($pic))
		{
			$link = $ref . "&action=del&pic=" . $i;
			echo "					<td>\n";
			echo "						<a href=\"$link\">";
			echo "<img src=\"$pic\" />";
			echo "</a>\n";
			echo "						<br />\n";
			echo "						<input type=\"text\" name=\"FotoB" . $i . "\" value=\"" . $nt['FotoB' . $i] . "\" />\n";
			echo "					</td>\n";
		}
		else
		{
			$link = $ref . "&action=add&pic=" . $i;
			echo "					<td>\n";
			echo "						<a href=\"$link\">";
			echo "<img src=\"img/no.jpg\" />";
			echo "</a>\n";
			echo "					</td>\n";
		}
	}
	echo "				</tr>\n";
	echo "			</table>\n";
	echo "		</fieldset>\n";
	echo "		<fieldset>\n";
	echo "			<legend class=\"sub\">Bilder zur internen Verwendung</legend>\n";
	echo "			<table class=\"foto\">\n";
	echo "				<colgroup>\n";
	echo "					<col span=\"2\" width=\"350\" />\n";
	echo "				</colgroup>\n";
	echo "				<tr>\n";
	for ($i = 8; $i < 10; $i++)
	{
		$pic = $path . $id . "_" . $i . "_thumb.jpg";
		if (file_exists($pic))
		{
			$link = $ref . "&action=del&pic=" . $i;
			echo "					<td>\n";
			echo "						<a href=\"$link\">";
			echo "<img src=\"$pic\" />";
			echo "</a>\n";
			echo "					</td>\n";
		}
		else
		{
			$link = $ref . "&action=add&pic=" . $i;
			echo "					<td>\n";
			echo "						<a href=\"$link\">";
			echo "<img src=\"img/no.jpg\" />";
			echo "</a>\n";
			echo "					</td>\n";
		}
	}
	echo "				</tr>\n";
	echo "			</table>\n";
	echo "		</fieldset>\n";
	echo "		<fieldset>\n";
	echo "			<legend class=\"sub\">Bildbeschreibungen Speichern und Fahrzeug anzeigen</legend>\n";
	echo "			<input type=\"hidden\" name=\"ID\" value=\"" . $id . "\" />\n";
	echo "			<input type=\"hidden\" name=\"foto\" value=\"1\" />\n";
	echo "			<input type=\"submit\" name=\"formaction\" value=\"show\" class=\"button_save\" />\n";
	echo "		</fieldset>\n";
	echo "	</form>\n";
}
echo "</fieldset>\n";