#!/usr/bin/perl -w

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

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";
my $cust_id = "0";
my $login;
my $ip;
my $name;
my $sname;
my $phone;
my $email;
my $address;
my $comment;
my $pause;
my $cash;
my $kredit;
my $dbh;
my $sth;
my $options = "0";
my @row;
my %yes_no = ('f'=>'no', 't'=>'yes');
my %yes_no1 = ('0'=>'no', '1'=>'yes');
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 $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;
}

if ($q->param('log')) {
	$login = $q->param('log');
	$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",
	                      $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,
	                                   { RaiseError => 1, AutoCommit => 0 });
	$sth = $dbh->prepare("SELECT cust_id from customers where login = ?");
	$sth->execute($login) or die $sth->errstr;
	@row = $sth->fetchrow_array;
	$sth->finish;
	$dbh->disconnect  or warn $dbh->errstr;
##	warn "blyat $row[0]";
	$cust_id = $row[0];
} else {
	if ($q->param('cust')) {
		$cust_id = $q->param('cust');
	} else {
		exit;
	}
}

if (defined ($q->param('login'))) {
	$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,
	                             { RaiseError => 1, AutoCommit => 0 });
	$login = $q->param('login');
	$ip = $q->param('ip');
	$name =  $q->param('name');
	$sname = $q->param('sname');
	$phone = $q->param('phone');
	$email = $q->param('email');
	$address = $q->param('address');
	$comment = $q->param('comment');
	$pause = $q->param('pause');
	$cash = $q->param('cash');
	$kredit = $q->param('kredit');

	$dbh->do(q{update customers set login=?, ip_addr=?, name=?, sname=?,
		phone=?, e_mail=?, address=?, comment=?, pause=?
		where cust_id=?}, undef,
		$login, $ip, $name, $sname, $phone, $email, $address, 
		$comment, $pause, $cust_id)
						    or die $dbh->errstr;
						    
	$dbh->do(q{update cash set cash=?, kredit=? where cust_id=?}, undef, 
					$cash, $kredit, $cust_id)
						    or die $dbh->errstr;

	$dbh->commit();
	$dbh->disconnect  or warn $dbh->errstr;
	reconfig();
	print "Location: $cgi/br_customers\n\n";
}

print  $q->header,
       $q->start_html(-title=>'Edit customer');

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 edit customer</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 "<A HREF=\"$cgi/new_tariff?cust=$cust_id\">Add new tariff</A>";
print $ipac_cfg::menu;

$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,
	                             { RaiseError => 1, AutoCommit => 0 });

$sth = $dbh->prepare("SELECT * from customers where cust_id = ?");
$sth->execute($cust_id) or die $sth->errstr;
@row = $sth->fetchrow_array;
$sth->finish;

print "<table align=center CELLPADDING=2 CELLSPACING=1 BORDER=1>";
print "<tr>", $q->td(['login', 'ip', 'name', 'surname', 'phone', 
		    'email', 'address', 'comment', 'pause', 'cash', 'max loan']);
print "</tr>";
print $q->start_form;
    
print "<tr>";
print $q->td([$q->textfield('login', $row[1], 8, 8, -override=>1),
	    $q->textfield('ip', $row[2], 15, 30, -override=>1),
	    $q->textfield('name', $row[3], 10, 20, -override=>1),
	    $q->textfield('sname', $row[4], 15, 25, -override=>1),
	    $q->textfield('phone', $row[5], 6, 16, -override=>1),
	    $q->textfield('email', $row[6], 15, 40, -override=>1),
	    $q->textfield('address', $row[7], 15, 60, -override=>1),
	    $q->textfield('comment', $row[8], 15, 60, -override=>1),
	    $q->popup_menu('pause', ['1', '0'], $row[9], \%yes_no1)]);

$sth = $dbh->prepare("select cash,kredit from cash where cust_id = ?");
$sth->execute($cust_id) or die $sth->strerr;
@row = $sth->fetchrow_array;
$sth->finish;
print $q->td($q->textfield('cash', $row[0], 8, 12, -override=>1));
print $q->td($q->textfield('kredit', $row[1], 8, 12, -override=>1));
print $q->hidden('cust', $cust_id);
print "</tr></table>";
print "<center>", $q->submit(), "</center>";
print $q->endform();
$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 and customers.cust_id=?");
$sth->execute($cust_id) or die $sth->errstr;                                            
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>";
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>";     
$sth->finish;
$dbh->disconnect  or warn $dbh->errstr;
print $q->end_html;
