<?php
session_start();
include('../connexion.php');
if (!isset($_SESSION['celest']))
{
	header ('Location: index.php');
}
$active='parametre';
if (isset($_GET['W9mvyDvFWE5']))
{
	$id=addslashes($_GET['id']);
	$retour=addslashes($_GET['retour']);
	$chaiz=addslashes($_GET['W9mvyDvFWE5']);
	$num=addslashes($_GET['num']);
	
	switch($chaiz)
	{
		case 'entreprise_grande':
		$sql= 'SELECT * FROM grande_categorie, '.$chaiz.' WHERE '.$chaiz.'.id_grande_categorie=grande_categorie.id_grande_categorie AND id_entreprise="'.$num.'" AND grande_categorie.id_grande_categorie="'.$id.'"';
		$reponse=$bdd->query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
		$data=$reponse->fetch();
		break;
		case 'entreprise_categorie':
		$sql= 'SELECT * FROM categorie, '.$chaiz.' WHERE '.$chaiz.'.id_categorie=categorie.id_categorie AND id_entreprise="'.$num.'" AND categorie.id_categorie="'.$id.'"';
		$reponse=$bdd->query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
		$data=$reponse->fetch();
		break;
		
		case 'video':
		case 'image':
		case 'fichier':
		case 'photo':
		case 'adresse':
		$sql= 'SELECT * FROM '.$chaiz.' WHERE id_'.$chaiz.'="'.$id.'"';
		$reponse=$bdd->query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
		$data=$reponse->fetch();
		break;
	}
	if($data[0]==NULL)
	{
		//header('Location: administration.php');
	}
?>
	<?php include('tete.php'); ?>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="css/local.css" />
    <link rel="stylesheet" type="text/css" href="style/style_admin.css" />

    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>

</head>
<body>
    <div id="wrapper">
    	<?php include('menu_admin.php'); ?>
        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-3"></div>
                <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12" id="administration">
                    <div id="titro_admin">Suppression <?php echo $chaiz ?></div>
                    <div align="center">
						<?php
						switch($chaiz)
						{
							case 'image':
							case 'fichier':
							case 'video':
							$lien_retour='afficher-table.php?W9mvyDvFWE5='.$retour.'&QYtgoGbhhd='.$num.'#positionimg';
							break;
							case 'photo':
							case 'adresse':
							case 'entreprise_categorie':
							case 'entreprise_grande':
							$lien_retour='afficher-entreprise.php?QYtgoGbhhd='.$num.'#positionimg';
							break;
						}
						if (isset($_POST['valider']))
						{
							if ($chaiz=='image' or $chaiz=='fichier' or $chaiz=='photo')
							{
								unlink ('../upload/'.$data['lib_'.$chaiz.'']);
							}
							
							switch($chaiz)
							{
								case 'entreprise_grande':
								$req='DELETE FROM '.$chaiz.' WHERE id_entreprise='.$num.' AND id_grande_categorie='.$id.'';
								$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
								
								$i=0;
								$sql= 'SELECT * FROM categorie, entreprise_categorie WHERE entreprise_categorie.id_categorie=categorie.id_categorie AND id_entreprise="'.$num.'" AND id_grande_categorie="'.$id.'"';
								$reponse=$bdd->query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
								while ($data=$reponse->fetch())
								{
									$i++;
									$req2='DELETE FROM entreprise_categorie WHERE id_entreprise='.$num.' AND id_categorie='.$data[0].'';
									$reponse2=$bdd-> query($req2) or die('Erreur SQL !<br />'.$req2.'<br />'.mysql_error());
								}
								echo $i;
								break;
								case 'entreprise_categorie':
								$req='DELETE FROM '.$chaiz.' WHERE id_entreprise='.$num.' AND id_categorie='.$id.'';
								$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
								break;
								
								case 'image':
								case 'fichier':
								case 'photo':
								case 'adresse':
								case 'video':
								$req='DELETE FROM '.$chaiz.' WHERE id_'.$chaiz.'='.$id.'';
								$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
								break;
							}
							echo '<div class="alert alert-success">Suppression effectuée avec succès </div>';
							?> 
							<script type="text/javascript">
							window.setTimeout("location=('<?php echo $lien_retour ?>');",2000);
							</script>
                            <?php
						}			
						?>
                        Voulez-vous supprimer cet élément: <?php echo $data[1] ?> ?
                        <form role="form" method="post" action="">
                            <input type="submit" class="btn btn-default" value="Oui" name="valider"> <a href="<?php echo $lien_retour ?>">Non</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<?php 
}
else
{
	header('Location: index.php');
}
?>