#!/usr/bin/perl

use CGI;
use DBI;
use strict;
unshift(@INC, '.');
use ipac_cfg;

my @pr_types = ('free',	'per byte', '2', '3', '4', '5', '6', '7', '8', '9',
		'10', '11', '12', '13', '14', 'per month');
my @yes_no = ('no', 'yes');
my $sort;
my $sort_by;
my $login_ord;
my $tar_ord;
my $paid_ord;
my $type_ord;

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";             
my $user = $ENV{REMOTE_USER};

if ($ENV{HTTPS} ne "on" || $user ne "admin") {
    print $q->header(-type=>"text/html;", -status=>'403 Forbidden'),
	    $q->start_html('Forbidden'),
	    $q->h1('You dont have permission to access this script'),
	    $q->end_html;
    exit;
}

print  $q->header,
       $q->start_html(-title=>'Browse tariffs');
$sort=$q->param('sort');
$sort_by="login asc";
$login_ord="login_d";
$tar_ord="tariff_a";
$paid_ord="paid_a";
$type_ord="type_a";

print "<STYLE type=\"text/css\"><!-- a.noneline {text-decoration: none;} --></style>";

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"ipac-ng logo\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>ipac-ng browse tariffs</font></center>".
	"</td></tr></table><A HREF=\"http://sf.net/projects/ipac-ng\">".
    	"ipac-ng home page</A>&nbsp;&nbsp;";
print "<A HREF=\"$path/stat\">Go to main page</A>&nbsp;&nbsp;";

print $ipac_cfg::menu;

if ($sort eq "login_d") {
	$sort_by="login desc";
	$login_ord="login_a";
} elsif ($sort eq "login_a") {
	$sort_by="login asc";
	$login_ord="login_d";
} elsif ($sort eq "tariff_d") {
	$sort_by="tariff_name desc";
	$tar_ord="tariff_a";
} elsif ($sort eq "tariff_a") {
	$sort_by="tariff_name asc";
	$tar_ord="tariff_d";
} elsif ($sort eq "paid_d") {
	$sort_by="last_paid desc";
	$paid_ord="paid_a";
} elsif ($sort eq "paid_a") {
	$sort_by="last_paid asc";
	$paid_ord="paid_d";
} elsif ($sort eq "type_a") {
	$sort_by="pr_type asc";
	$type_ord="type_d";
} elsif ($sort eq "type_d") {
	$sort_by="pr_type desc";
	$type_ord="type_a";
}
	
my $dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",                  
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,            
                                     { RaiseError => 1, AutoCommit => 0 });

my $sth = $dbh->prepare("SELECT login,tariff_name,price,pr_type,last_paid, ".
		    "active_nocash,tariffs.cust_id,tariffs.tariff_id,tariffs.detailed ".
		    "from customers,tariffs,tariffs_names where ".
		    "customers.cust_id=tariffs.cust_id and tariffs.tariff_id=".
		    "tariffs_names.tariff_id order by $sort_by");
$sth->execute() or die $sth->errstr;
my @row;
print "<table align=center CELLPADDING=2 CELLSPACING=1 BORDER=1>";
#print "<tr><td>login</td><td>tariff</td><td>price</td>".
#	"<td>type</td><td>last_paid</td><td>allow debt</td>".
#	"<td>detailed logging</td><td colspan=2>&nbsp;</td></tr>";
print $q->td([  "<a href=$cgi/br_tariffs?sort=$login_ord>login</a>",
		"<a href=$cgi/br_tariffs?sort=$tar_ord>tariff</a>",
		'price',
		"<a href=$cgi/br_tariffs?sort=$type_ord>price type</a>",
		"<a href=$cgi/br_tariffs?sort=$paid_ord>last paid</a>",
		'allow debt',
		'detailed logging']);
print "<td colspan=2>&nbsp;</td></tr>";
while (@row = $sth->fetchrow_array) {
	print "<tr>";
	print "<td>&nbsp;$row[0]</td>";
	print "<td>&nbsp;$row[1]</td>";
	print "<td>&nbsp;$row[2]</td>";
	print "<td>&nbsp;$pr_types[$row[3]]</td>";
	print "<td>&nbsp;$row[4]</td>";
	print "<td>&nbsp;$yes_no[$row[5]]</td>";
	print "<td>&nbsp;$yes_no[$row[8]]</td>";
	print "<td><A HREF=\"$cgi/edit_tariff?cust=$row[6]&tar=$row[7]\" onMouseOver=\"window.status='Edit tariff';return true;\" onMouseOut=\"window.status=\'\';return true;\" CLASS=\"noneline\">edit</A></td>";
	print "<td><A HREF=\"$cgi/dele_tariff?cust=$row[6]&tar=$row[7]\" onMouseOver=\"window.status='Delete tariff';return true;\" onMouseOut=\"window.status=\'\';return true;\" CLASS=\"noneline\">delete</A></td>";
	print "</tr>";
}
print "</table>";

$dbh->disconnect  or warn $dbh->errstr;
print $q->end_html;
