####################################################################
#  SearchArchives.pm - module for searching through archived files #
#                      via a sqlite database                       #
####################################################################
package BackupPC::CGI::SearchArchives;


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

sub action() {
	my $hosts_html = "";

    
	BackupPC::CGI::Lib::NewRequest();
	my @hosts = BackupPC::Search::getUnits();

	foreach my $host (@hosts) {
		$hosts_html .= "<option value=" . $host->{'id'} . "> " . $host->{'share'} . " </option>\n";
        }

	sub date_from_to_html {
		my $name = shift || return;
		return qq{
			<input type="text" name="${name}_year_from" size="4">
			<input type="text" name="${name}_month_from" size="2">
			<input type="text" name="${name}_day_from" size="2">
			-
			<input type="text" name="${name}_year_to" size="4">
			<input type="text" name="${name}_month_to" size="2">
			<input type="text" name="${name}_day_to" size="2">
		};
	}

        my $form .= qq{
<form name="searchForm" method="GET" action="$MyURL">
<p>
	Filename:
	<input type="text" name="search_filename" size="50">
	&nbsp;
	on share
	&nbsp;
	<select name="search_share">$hosts_html</select>
	&nbsp;show&nbsp;
	<select name="burned">
	<option value="">all</a>
	<option value="burned">burned</a>
	<option value="pending">pending</a>
	</select>
	files
	&nbsp;&nbsp;&nbsp;
	<input type="submit" value="Search">
</p>

<p style="color:gray;">
	file(s) between:
	} . date_from_to_html('search') . qq{
	&nbsp;&nbsp;
	backups between:
	} . date_from_to_html('search_backup') . qq{
	(yyyy-mm-dd)
</p>

<p style="color: gray;">
	<input type="checkbox" name="use_hest"> use faster full-text search engine
</p>

<input type="hidden" value="search" name="action">
<input type="hidden" value="results" name="search_results">
</form>
	};

	my $html = eval(q{ ${h1("Search")}});

	if ( !defined($In{search_results}) ) {
		$html .= eval(q{ ${h2("Search criteria")}});

		$html .= $form;
	} else {
		$html .= eval(q{ ${h2("Search results")}});

		my $fif = new HTML::FillInForm;
		$html .= $fif->fill(scalarref => \$form, fdat => \%In);

		$html .= BackupPC::Search::displayGrid( \%In );
	}

	Header( eval("qq{$Lang->{Search_archive}}"), "", 1, "", $html );
	Trailer();
}

1;
