package BackupPC::CGI::BurnMedia;

use strict;
use BackupPC::CGI::Lib qw(:all);
use BackupPC::Search;
use Data::Dumper;
use vars qw($Cgi %In $MyURL $User $TopDir $BinDir $bpc);

sub action() {
	my $cont = "";
	my $title;
	my $subtitle;
	my @files;

	$bpc->ConfigRead('_search_archive');
	my %Conf = $bpc->Conf;

	my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 } );

	BackupPC::CGI::Lib::NewRequest();
#	$cont = Dumper(\%In);
	if (!defined($In{submitBurner})) {
		$title = eval(q{ ${h1($Lang->{Burn_media})}});
		$cont = Dumper(%In);
		$subtitle = eval(q{ ${h2($Lang->{Burn_media_select})}});
		$cont = <<EOF;

	$title
	$subtitle
	
EOF
	      
		$cont .= "Backups that have not been archived:<br>";
		$cont .= BackupPC::Search::displayBackupsGrid( \%In );
	    
	} else {

		my @selected_backup_parts;

		foreach my $key(keys(%In)) {
			next unless $key =~ m/^fcb(\d+)/;
			push @selected_backup_parts, $1;
		}


		my ($total_size) = $dbh->selectrow_array( q{
			SELECT sum(size) FROM backup_parts
			WHERE id IN ( }.join(',', @selected_backup_parts).q{ )
		});


		if ($total_size > ($Conf{ArchiveMediaSize})) {
			$cont .= eval(' ${h2(Error)} ');
			$cont .= join(' '
				, "Selected backups size", unit($total_size)
				, "exceed medium size ", unit($Conf{ArchiveMediaSize})
			);
		} elsif ($total_size == 0) {
			$cont .= eval( q{ ${h2(Error)}});
			$cont .= "No backups selected.";
		} else {

			# create new archive
			my $sth = $dbh->prepare(q{
				INSERT INTO archive (
					id,
					dvd_nr,
					note,
					username,
					date,
					total_size
				) VALUES (
					nextVal('archive_id_seq'),
					nextVal('dvd_nr'),
					?,
					?,
					NOW(),
					?
				)
			});

			$sth->execute($In{'note'}, $User, $total_size);

			my $sth = $dbh->prepare(q{
				INSERT INTO archive_parts (
					archive_id, backup_part_id
				) VALUES (
					(SELECT last_value FROM archive_id_seq), ?
				)
			});
			$sth->execute($_) foreach @selected_backup_parts;

			my ($dvd_nr) = $dbh->selectrow_array(qq{
				select last_value from dvd_nr
			});

			die "no dvd_nr" unless $dvd_nr;

			$dbh->commit();

			$cont .= q{
				Archived following backups parts:
			} . join(", ", @selected_backup_parts);

			my $sth = $dbh->prepare(qq{
				select
					filename,
					part_nr,
					TIMESTAMP 'epoch' + date * INTERVAL '1 second' as date,
					size
				from archive_backup_parts
				where dvd_nr = ?
				order by backup_part_id
			});
			$sth->execute( $dvd_nr );
			$cont .= '<table border=1>';
			while( my $row = $sth->fetchrow_arrayref ) {
				$cont .= '<tr><td>' . join('</td><td>', @$row) . '</tr><tr>'; 
			}
			$cont .= '</table>';
	
			$cont .= q{
				<br/>with total size of
			<b>} . unit($total_size)
			. q{</b>
				to media <b>} . $dvd_nr . q{</b>
				with following message:
				<div style="background-color: #e0e0e0; display: inline; padding: 2px;">
			} . $In{'note'} . q{
				</div>
			};


		}
	}

	$cont .= join(' '
		, "ArchiveMediaSize:", unit($Conf{ArchiveMediaSize})
		, "ArchiveChunkSize:", unit($Conf{ArchiveChunkSize})
	);

	Header($Lang->{Burn_media}, "", 1, "", $cont);

	Trailer();
	$dbh->disconnect();
}

1;
