<?php
//error_reporting(E_ALL ^ E_DEPRECATED); ini_set('display_errors', 1);
session_start();
include('script/variablen.php');
include('connect.php');

if ( !is_numeric($_GET['marke']) OR !is_numeric($_GET['hauptrubrik']) )
{
	header("Location: http://www.fuxtools.ch/404.php");
	exit;
}

$result_marke = mysqli_query($dbi,"SELECT nummer, marke, rubrik, bildname, bild_w_1, bild_h_1, bildtitel FROM marken WHERE nummer='".intval($_GET['marke'])."' AND aktiv='1'");
$row_marke = $result_marke->fetch_assoc();

// Wenn Marke nicht aktiv (Fehlermeldung und weiterleitung auf index)
if ( mysqli_num_rows($result_marke) == 0 )
{
	header("Location: http://www.fuxtools.ch/404.php");
	exit;
}

// Wenn marke und hauptrubrik leer
if ( empty($_GET['hauptrubrik']) OR empty($_GET['marke']) )
{
	header("Location: http://www.fuxtools.ch/404.php");
	exit;
}

$result_rubrik = mysqli_query($dbi,"SELECT name FROM rubrik WHERE nummer='".$_GET['hauptrubrik']."'");
$row_rubrik = $result_rubrik->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="de-CH">
<head>

<?php include('page_meta.php'); ?>

	<title><? echo $row_rubrik['name'] ?> - <? echo $row_marke['marke'] ?> - Fux Elektrowerkzeuge GmbH, Rümlingen</title>

<?php include('page_css.php'); ?>

<?php include('page_script_top.php'); ?>

</head>

<?php
// WENN Cookie Admin aktiv
if ( !empty($_COOKIE["administrator"]) == 1 )
{
	echo '<body class="admin">'."\n";
}
else
{
	echo '<body oncontextmenu="return false">'."\n";
}

include('page_header.php'); ?>

<div id="content">

<section>

	<div class="titel_flex">
		<h1 class="flex"><? echo $row_rubrik['name'] ?> - <? echo $row_marke['marke'] ?></h1>
		<div class="navi_back">
			<a href="hauptrubrik.php?marke=<?php echo $_GET['marke'] ?>">Zur <?php echo $row_marke['marke'] ?> Auswahl</a>
		</div>
	</div>
	<article>
<?php
$filemarke = 'images/logos/'.$row_marke['bildname'].'.svg';

if (file_exists($filemarke))
{
	echo '			<a href="hauptrubrik.php?marke='.$row_marke['nummer'].'"><img src="images/logos/'.$row_marke['bildname'].'.svg" alt="'.$row_marke['bildtitel'].'" width="'.$row_marke['bild_w_1'].'" height="'.$row_marke['bild_h_1'].'" id="marken_logo_svg"/></a>';
}
else
{	
	$bild_groesse = getimagesize ("images/logos/".$row_marke['bildname'].".png");
	$bild_breite = $bild_groesse[0];
	$bild_hoehe = $bild_groesse[1];
	echo '			<a href="hauptrubrik.php?marke='.$row_marke['nummer'].'"><img src="images/logos/'.$row_marke['bildname'].'.png" alt="'.$row_marke['bildtitel'].'" width="'.$bild_breite.'" height="'.$bild_hoehe.'" id="marken_logo"/></a>';
}

// S Banner  Set
if ( $_GET['hauptrubrik'] == 490 )
{
	echo '<a href="set_metabo_auswahl.php">
	<picture>
		<source media="(max-width:600px)" srcset="images/banner/pick_and_mix_kl.avif"/>
		<source srcset="images/banner/pick_and_mix.avif"/>
		<source media="(max-width:600px)" srcset="images/banner/pick_and_mix_kl.jpg"/>
		<img src="images/banner/pick_and_mix_gr.jpg" width="1313" height="150" alt="Metabo Pick+Mix: Einfach clever kombiniert." class="rund rahmen img_100 zoom"/>
	</picture>
</a>
<br><br>';
}
if ( $_GET['hauptrubrik'] == 6803 )
{
	echo '<a href="mafell-power-set-pure.php">
	<picture>
		<source media="(max-width:600px)" srcset="images/banner/mafell-power-set-aktion-kl.avif"/>
		<source srcset="images/banner/mafell-power-set-aktion.avif"/>
		<source media="(max-width:600px)" srcset="images/banner/mafell-power-set-aktion-kl.jpg"/>
		<img src="images/banner/mafell-power-set-aktion.jpg" width="1311" height="200" alt="Mafell Power-Set Aktion" class="rund rahmen img_100 zoom"/>
	</picture>
</a>
<br><br>';
}
if ( $_GET['hauptrubrik'] == 1 )
{
	echo '<div class="cycle-slideshow" data-cycle-swipe="true" data-cycle-random="true" data-cycle-swipe-fx="scrollHorz" data-cycle-timeout="7000" data-cycle-speed="2000" data-cycle-manual-speed="700" data-cycle-carousel-visible="1" data-cycle-pause-on-hover="true" data-cycle-slides="> a" data-cycle-pager="#pager_banner">
	<a href="set_makita_auswahl.php">
		<picture>
			<source media="(max-width:600px)" srcset="images/banner/makita_systemkit_kl.avif"/>
			<source srcset="images/banner/makita_systemkit.avif"/>
			<source media="(max-width:600px)" srcset="images/banner/makita_systemkit_kl.jpg"/>
			<img src="images/banner/makita_systemkit.jpg" width="1311" height="200" alt="Makita SystemKIT" class="rund rahmen img_100 zoom"/>
		</picture>
	</a>
	<a href="unterrubrik.php?marke=1&hauptrubrik=1">
		<picture>
			<source media="(max-width:600px)" srcset="images/banner/makita_akku_kl.avif"/>
			<source srcset="images/banner/makita_akku.avif"/>
			<source media="(max-width:600px)" srcset="images/banner/makita_akku_kl.jpg"/>
			<img src="images/banner/makita_akku.jpg" width="1311" height="200" alt="Vergünstigter Makita 18 Volt Akku?" class="rund rahmen img_100 zoom"/>
		</picture>
	</a>
	<div class="cycle-pager" id="pager_banner"></div>
</div><br>';
}
if ( $_GET['hauptrubrik'] == 1642 )
{
	echo '<div class="cycle-slideshow" data-cycle-swipe="true" data-cycle-random="true" data-cycle-swipe-fx="scrollHorz" data-cycle-timeout="7000" data-cycle-speed="2000" data-cycle-manual-speed="700" data-cycle-carousel-visible="1" data-cycle-pause-on-hover="true" data-cycle-slides="> a" data-cycle-pager="#pager_banner">
	<a href="set_flex.php">
		<picture>
			<source media="(max-width:600px)" srcset="images/banner/flex_pack_kl.avif"/>
			<source srcset="images/banner/flex_pack.avif"/>
			<source media="(max-width:600px)" srcset="images/banner/flex_pack_kl.jpg"/>
			<img src="images/banner/flex_pack.jpg" width="1313" height="150" alt="Flex Pack" class="rund rahmen img_100 zoom"/>
		</picture>
	</a>
	<a href="#">
		<picture>
			<source media="(max-width:600px)" srcset="images/banner/flex_akku_kl.avif"/>
			<source srcset="images/banner/flex_akku.avif"/>
			<source media="(max-width:600px)" srcset="images/banner/flex_akku_kl.jpg"/>
			<img src="images/banner/flex_akku.jpg" width="1311" height="200" alt="Vergünstigter Flex 18 Volt Akku?" class="rund rahmen img_100 zoom"/>
		</picture>
	</a>
	<div class="cycle-pager" id="pager_banner"></div>
</div><br>';
}
// E Banner Set

$result = mysqli_query($dbi,"SELECT nummer, name, kurz FROM rubrik WHERE marke=".$_GET['marke']." AND rubrik='".$_GET['hauptrubrik']."' AND rubrikart='2' ORDER by name");

while($row = mysqli_fetch_assoc($result)) {

// überprüfen ob in unterrubrik Maschinen eingetragen
$num_maschine = mysqli_fetch_array(mysqli_query($dbi, "SELECT COUNT(nummer) FROM produkte WHERE marke=".$_GET['marke']." AND unterrubrik='".$row['nummer']."'"));

if ( $num_maschine[0] != '0' )
{
	$link = 'produkte.php?marke='.$_GET['marke'].'&amp;hauptrubrik='.$_GET['hauptrubrik'].'&amp;unterrubrik='.$row['nummer'].'';
}

// überprüfen ob weitere Unterrubrik vorhanden
$result_rubrik = mysqli_query($dbi,"SELECT nummer FROM rubrik WHERE rubrik='".$row['nummer']."'");
$num_rubrik = mysqli_num_rows($result_rubrik);

if ( $num_rubrik != '0' )
{
// Wenn nur 1 unterrubrik_1
$result_unterrubrik_1 = mysqli_query($dbi,"SELECT nummer, rubrik FROM rubrik WHERE rubrik='".$row['nummer']."'");
$row_unterrubrik_1 = $result_unterrubrik_1->fetch_assoc();

	// S prüfen ob nur eine unterrubik Produkte hat
	mysqli_data_seek($result_unterrubrik_1, 0);
	$count = 0;
	while($row_unterrubrik_1_count = mysqli_fetch_assoc($result_unterrubrik_1))
	{
		$num_maschine_unterrubrik_1 = mysqli_query($dbi, "SELECT nummer FROM produkte WHERE marke=".$_GET['marke']." AND unterrubrik_1='".$row_unterrubrik_1_count['nummer']."'");

		if ( mysqli_num_rows($num_maschine_unterrubrik_1) != 0 )
		{
			$count++;
			$nummer = $row_unterrubrik_1_count['nummer'];
		}
	}
	// E prüfen ob nur eine unterrubik Produkte hat

	if ( mysqli_num_rows($result_unterrubrik_1) == 1 OR $count == 1 )
	{
		$link = 'produkte.php?marke='.$_GET['marke'].'&amp;hauptrubrik='.$_GET['hauptrubrik'].'&amp;unterrubrik_1='.$nummer.'';
		//produkte.php?marke=1&hauptrubrik=383&unterrubrik_1=6166
	}
	else
	{
		$link = 'unterrubrik_1.php?marke='.$_GET['marke'].'&amp;unterrubrik='.$row['nummer'].'';
	}
}

// Wenn Maschine eingetragen
if ( $num_maschine[0] != '0' )
{

// START Bild	
	// Zufallsbild auswählen Neuheit
	$result_bild = mysqli_query($dbi,"SELECT nummer FROM produkte 
	WHERE marke='".$_GET['marke']."' AND hauptrubrik='".$_GET['hauptrubrik']."' AND unterrubrik='".$row['nummer']."' AND neu='1' AND bild='1'  
	ORDER BY RAND() LIMIT 1");
	$row_bild = $result_bild->fetch_assoc();

	// Zufallsbild auswellen wenn keine Neuheit vorhanden
	if ( mysqli_num_rows($result_bild) == 0 )
	{
		$result_bild = mysqli_query($dbi,"SELECT nummer FROM produkte 
		WHERE marke='".$_GET['marke']."' AND hauptrubrik='".$_GET['hauptrubrik']."' AND unterrubrik='".$row['nummer']."' AND bild='1'   
		ORDER BY RAND() LIMIT 1");
		$row_bild = $result_bild->fetch_assoc();
	}

	// wenn kein Bild vorhanden
	if ( mysqli_num_rows($result_bild) == 0 )
	{
		$bild = 'images/produkte/klein/bild_folgt.jpg';		
	}
	else
	{
		$bild = 'images/produkte/klein/'.$row_bild['nummer'].'.jpg?'.filemtime('images/produkte/klein/'.$row_bild['nummer'].'.jpg').'';
	}
// ENDE Bild

echo '
	<div class="box_marken">
		<a href="'.$link.'" class="rubrik rahmen rund">
		<img src="'.$bild.'" class="unterrubrik_bild" width="110" height="110" alt="'.$row['name'].'"/>
		<div class="unterrubrik_text"><b>'.stripslashes($row['name']).'</b>';

		if ( !empty($row['kurz']) )
		{
			echo '<div class="sehr_klein">'.nl2br(stripslashes($row['kurz'])).'</div>';
		}
		echo '</div>
		</a>
	</div>';

}

}
?>
	</article>

<?php include('page_last.php'); ?>

</section>

</div>

<?php include('page_footer.php'); ?>

<?php include('page_script_bottom.php'); ?>

</body>
</html>