#!/usr/bin/perl

# rechercher.pl

require "cgi-lib.pl";

if ( ! -e "commandes.txt" || -z "commandes.txt" )
{
    print "Content-type: text/html\r\n\r\n";
    print "<html><head><title>Aucun resultat</title></head";
    print "<body><h1 style='color: yellow'>";
    print "Aucune commande actuellement en traitement";
    print "</h1></body></html>";
    exit;
}

&ReadParse;

$identifiant = "$in{nom}/$in{prenom}/$in{email}/";
@commandes = ();

open FICHIER,"commandes.txt";
while (<FICHIER>)
{
    $commande = $_;
    s/^\d+\///;     # Eliminer le numero de commande en debut de ligne
    push @commandes,$commande if (index($_,$identifiant) == 0);
}
close FICHIER;

chomp @commandes;

if (@commandes == 0)
{
    print "Content-type: text/html\r\n\r\n";
    print "<html><head><title>Aucune commande</title></head";
    print "<body><h1 style='color: red'>";
    print "Vous n'avez actuellement aucune commande";
    print "</h1></body></html>";
    exit;
}

# Si la personne a des commandes en traitement

print "Content-type: text/html\r\n\r\n";
print "<html><head><title>Resultats</title></head";
print "<body><p>";
print "<table border='1'>";
print "<tr><th>Num&eacute;ro commande</th>";
print "<th>Produit</th><th>Prix</th></tr>";
foreach (@commandes)
{
    print "<tr>";
    ($numero,$nom,$prenom,$email,$produit) = split /\//;
    print "<td>$numero</td><td>$produit</td>";
    $prix = 10 if ($produit eq "Livre");
    $prix = 15 if ($produit eq "CD");
    $prix = 22 if ($produit eq "DVD");
    print "<td>$prix euros</td>";
    print "</tr>";
}
print "</table>";
print "</p></body></html>";
