#!/usr/bin/perl
#################################################################
#Демо-версия виртуальниго стола заказов или магазина.
#Впрочем можно использовать как доску обьявлений.
#Если вам нужны более серьезные программы пишите algol@hotmail.ru
#################################################################
use CGI;
$co = new CGI;
$d=$co->param('action');
$ad=$co->param('cat');
$add=$co->param('act');

print $co->header(-type  =>  'text/html; charset=Windows1251');
print $co->start_html(-title=>'CGI example');
PrintWelcome();
#print"<br>action=$d,,,,$add<br>";
if ($d eq "sale"){&sale;}
if ($d eq "show"){&show;}
if ($d eq "new_cat"){&new_cat;}
if ($add eq "new_cat"){&new_cat;}
if ($add eq "new_pro"){&new_pro;}
if ($d eq "adm_cat"){&adm_cat;}
if ($d eq "ok"){&ok;}
if ($d eq "submit"){&submit;}
if ($d eq "delet"){&delet;}

sub submit{
print "<h2>Ваш заказ принят</h2>",
"и тому подобное";
&cler;
}
sub delet{
print "<h2>Ваш заказ аннулирован</h2>";
&cler;
}
sub cler{
if (unlink (<zakaz.txt>)){
print $co->a({-href=>"script.cgi"}, "<b>HOME<b>");
}
print $co->end_html;
exit;
}

print $co->center($co->h1('Welcome to us!'));
print $co->a({-href=>"script.cgi?action=show"}, "<h2>Куплю</h2>"),"- Магазин виртуальных покупок";
print $co->a({-href=>"script.cgi?action=sale"}, "<h2>Продам</h2>"),"-Администрирование";
print $co->end_html;

sub ok{
$pr=$co->param('pro');
$a=$co->param('categ');
open(CAT, "categ.txt");
@categ=<CAT>; close(CAT);
foreach $line (@categ){
@rl=split(/\|/, $line);
if($a eq $rl[0]){$categor=$rl[1]} 
}

$file=$a.".txt";
print $file;
open(NEW,"$file"); @data=<NEW>; close(NEW);
@tovar=$data[$pr];
open(ZAS,">>zakaz.txt");
$p=join(/|/,@tovar);
print ZAS "$categor|$p";
close (ZAS);
print"<h2>Ваш заказ</h2>";
open(ZAS,"zakaz.txt"); @zac=<ZAS>; close(ZAS); $zen=0;
foreach $line (@zac){
@rl=split(/\|/, $line);
print"$rl[1], категория-$rl[0], цена-$rl[2]<br>\n";
$rl[2]=~s/\D//g;
$zen+=$rl[2];
}
print "Ваш заказ на сумму $zen рублей.<br>\n";
print $co->hr, $co->a({-href=>"script.cgi?action=submit"}, "<b>Оформить заказ</b>")," или", $co->a({-href=>"script.cgi?action=delet"}, "<b>Аннулировать</b>");
print $co->end_html;
exit;
}

sub show{
print "<h2>Для заказа жмите ссылку</h2>\n";
open(CAT, "categ.txt");
@categ=<CAT>; close(CAT);
foreach $line(@categ){
@rol=split(/\|/, $line);
$file=$rol[0].".txt";
open(NEW,"$file"); @data=<NEW>; close(NEW); $n=0;
print"<table width=50% border=2><caption><b>$rol[1]</b></caption><tr><td><b>Наименование</b></td><td><b>Цена</b></td></tr>";
foreach $line(@data){
@le=split(/\|/,$line);
print "<tr><td><a href=\"script.cgi?action=ok\&categ=$rol[0]\&pro=$n\" target=\"_new\">$le[0]</a></td><td>$le[1]</td></tr>";
$n++;}
print "</table>";

}
print $co->end_html;
exit;
}



sub sale{
open(CAT, "categ.txt");
@categ=<CAT>; close(CAT);
print "<h2>Категории продукции:</h2>для редактирования перейти по ссылке<br>\n";
foreach $line(@categ){
@rol=split(/\|/, $line);
print"<li><a href=\"script.cgi?action=adm_cat\&cat=$rol[0]\"><b>$rol[1]</b></a>, $rol[2] </li>";
}
print $co->start_form(-method=>POST, -action=>'script.cgi');
print $co->hidden(-name=>'act', -default=>'new_cat');
print $co->center("Новая категория:", $co->textfield(-name=>'category'),$co->p,);
print $co->center("Описание:", $co->textfield(-name=>'cat_desc'),$co->p,);
print $co->center($co->submit, $co->reset);
print $co->end_form;
print $co->end_html;
exit;
}
sub adm_cat{
$cat=$co->param('cat');
$file=$cat.".txt";
open(NEW,"$file"); @data=<NEW>; close(NEW); 
print"<table width=50% border=2><tr><td><b>Наименование</b></td><td><b>Цена</b></td></tr>";
foreach $line(@data){
@le=split(/\|/,$line);
print "<tr><td>$le[0]</td><td>$le[1]</td></tr>";
}
print "</table>";
print $co->hr;
print $co->start_form(-method=>POST, -action=>'script.cgi');
print $co->hidden(-name=>'cat', -default=>"$cat");
print $co->hidden(-name=>'act', -default=>'new_pro');
print $co->center("<b>Введите новый продукт</b>");
print $co->center("Название:", $co->textfield(-name=>'name'),$co->p,);
print $co->center("Цена:", $co->textfield(-name=>'prize'),$co->p,);
print $co->center($co->submit, $co->reset);
print $co->end_form, $co->hr;
print $co->a({-href=>"script.cgi?action=sale"},"<b>Категории</b>");
print $co->end_html;
exit;
}
sub new_pro{
$cat=$co->param('cat');
$file=$cat.".txt";
$name=$co->param('name');
$prize=$co->param('prize');
open(NEW,">>$file");
print NEW "$name|$prize|\n"; 
close(NEW); 
&adm_cat;


}








sub new_cat{
$ss=$co->param('category');
$we=$co->param('cat_desc');
open(CAT, "categ.txt");
@categ=<CAT>; close(CAT);
$n=@categ+1;
$file=$n.".txt";
open(NEW,">$file");close(NEW);  
   
open(CAT, ">>categ.txt");

print "$ss,,,,$we";
print CAT "$n|$ss|$we|\n";
close(CAT);
&sale;
}




sub do_work {
           my($co) = @_;
#           my(@values,$key);
#           print "<H4>Here are the current settings in this form</H4>";
#           foreach $key ($co->param) {
#              print "<STRONG>$key</STRONG> -> ";
#              @values = $co->param($key);
#              print join(", ",@values),"<BR>\n";
#          }
#$d=$co->param('male');
print "Comment <b>$d</b> send to: ", $co->param('emale');
exit;
}

sub PrintWelcome {	
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
	$thisday=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)[$wday];
	#$thismon=(January,February,March,April,May,June,July,August,September,October,November,December)[$mon];
	if ($hour < 12) {
		print "Good morning!  ";
	} elsif ($hour < 17) {
		print  "Good afternoon!  ";
	} else {
		print  "Good evening!  ";
	};
	$ampm = 'AM';
	if ($hour > 12) {
		$hour-=12;
		$ampm = 'PM';
		}
	$time = sprintf '%d:%2.2d:%2.2d',$hour,$min,$sec;
	$year += 1900;
	$datetime = $time.' '.$ampm.', '.$thisday.', '.$mon.' '.$mday.', '.$year;		
	print "$datetime";
}

