<?php
include("../config.php3");
include($BIB."bib_generic.php3");
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Nouvelle Campagne Tracking</div>
<br>

<table border=1>
	<tr class=trTitle>
		<td>Id</td>
		<td>Code</td>
		<td>Libellé</td>		
		<td>Total clicks</td>
		<td>Total Affichés</td>
		<td>Afficher Liens de tracking</td>
		<td>RAZ</td>
	</tr>
<?php
switch($submit)
{
	case "Valider":
	
		if ($Id==0)
		{
			# on insere une nvo
			main_db_query("INSERT INTO Tracking (Id,TypeId, Lib, Total) VALUES ('".$Id."','".$TypeId."', '".$Lib."', '".$Total."')");
		}
		else
		{
			# on update si c'est une modif
			main_db_query("UPDATE Tracking SET TypeId='".$TypeId."' ,Lib='".$Lib."', Total = '".$Total."' WHERE Id='".$Id."'");
		}
		$Id=0;
		break;
		
	case "Supprimer":
		main_db_query("DELETE FROM Tracking WHERE Id='".$Id."'");
		$Id=0;
		$TypeId  = "";
		$Nb  = "";
		$Lib  = "";
		$Total  = "";
		
		break;
	
	case "Infos":
		$info = db_fetch_array(main_db_query("SELECT Tracking.*,Tracking_Type.CodeType FROM Tracking LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id WHERE Tracking.Id='".$Id."'"));
		
		$TypeId = $info[TypeId];
		$Nb = $info[Nb];
		$Lib = $info[Lib];
		$Total = $info[Total];

		break;
	
	case "Link":
		$info = db_fetch_array(main_db_query("SELECT Tracking.*,Tracking_Type.CodeType FROM Tracking LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id WHERE Tracking.Id='".$Id."'"));
		
		$Tag =  "<u>Sur un site : </u><br>$"."SERVER_URL.\"trck_clck.php3?code=".$info['CodeType']."_".$info['Id']."&email=\".$"."member[\"Email\"].\"&url=\"."."$"."url";
		$Tag .=  "<br><u>Sur un mail : </u><br>";
		
		$Tag .=  "OUVERTURE :<br> &#60;img src='".$SERVER_URL."Images/trck_img.php3?code=".$info['CodeType']."_".$info['Id']."&email=__EMAIL__'&#62";
		$Tag .=  "<br>CLIC : <br>".$SERVER_URL."trck_clck.php3?code=".$info['CodeType']."_".$info['Id']."&email=__EMAIL__&url=__URL__";
		$PromoTag = $info[CodeType]." : ".$info[Lib];

		break;
	case "RAZ":
		$info = db_fetch_array(main_db_query("UPDATE Tracking SET Nb=0 WHERE Id='".$Id."'"));
		$info = db_fetch_array(main_db_query("DELETE FROM Tracking_Email WHERE TrackId='".$Id."'"));
		break;
}




$rows = main_db_query("SELECT Tracking.*,Tracking_Type.CodeType FROM Tracking LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id ORDER BY CodeType ASC");

while($row = db_fetch_array($rows))
{
	echo "<tr><td><a href='?Id=".$row['Id']."&submit=Infos'>".$row['Id']."</a></td>";
	echo "<td><a href='?Id=".$row['Id']."&submit=Infos'>".$row['CodeType']."_".$row['Id']."</a></td>";
	echo "<td>".$row['Lib']."</td>";
	echo "<td align=right>".$row['Nb']."</td>";
	echo "<td align=right>".$row['Total']."</td>";
	echo "<td align=center><a href='?Id=".$row['Id']."&submit=Link' style='color:#55FF55'>Voir</a></td>";
	echo "<td align=center><a href='?Id=".$row['Id']."&submit=RAZ' style='color:#55FF55'>RAZ</a></td></tr>";
}

?>
</table>
<?php
if(isset($Tag))
{
	echo "<br><b><u>Voici le lien de la promo ".$PromoTag." :</u></b><br>";
	echo $Tag;
}
?>
<form>
<table>
	<tr>
		<td>
			Id (vide ou egal 0 pour ajout):
		</td>
		<td>
			<input type="text" size=10 name=Id value="<?php echo $Id ?>">
		</td>
	</tr>
	<tr>
		<td>
			CodeType :
		</td>
		<td>
			<select name="TypeId">
			<option value=0></option>
			<?php
			$rows = main_db_query("SELECT * FROM Tracking_Type ORDER BY Id ASC");
			while($row = db_fetch_assoc($rows))
			{
				$SELECTED = "";
				if($TypeId == $row["Id"])
					$SELECTED = "SELECTED";
				echo "<option value=".$row["Id"]." ".$SELECTED.">".$row["CodeType"]."</option>";
			}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td>
			Libellé :
		</td>
		<td>
			<input type="text" size=30 name=Lib value="<?php echo $Lib ?>">
		</td>
	</tr>
	<tr>
		<td>
			Total affichés :
		</td>
		<td>
			<input type="text" size=30 name=Total value="<?php echo $Total ?>">
		</td>
	</tr>
</table>
<br>
    <input type=submit name=submit value="Infos">&nbsp;&nbsp;&nbsp;
    <input type=submit name=submit value="Valider">&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit value="Supprimer"><br>
	</form>
<a class=under href="<?php echo $PHP_SELF ?>">Rafraichir la page</a>