####################################################################
#  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::SearchLib;
use Data::Dumper;
use vars qw($Cgi %In $MyURL $User %Conf $TopDir $BinDir $bpc);

sub action() {
    my $cont = "";
    my $host;
    my @hosts;
    my $hosts_html = "";

    
    BackupPC::CGI::Lib::NewRequest();

    @hosts = BackupPC::SearchLib::getUnits();
    if ( !defined($In{search_results}) ) {
        foreach $host (@hosts) {
            $hosts_html .=
                "<option value="
              . $host->{'ID'} . "> "
              . $host->{'name'}
              . " </option>\n";
        }
	my $title = eval(q{ ${h1("Search")}});
	my $subtitle = eval(q{ ${h2("Search criteria")}});

	sub date_from_to_html {
		my $name = shift || return;
		return qq{
<table>
<tr>
	<td colspan="3" align="center">from</a>
	<td>&nbsp;</td>
	<td colspan="3" align="center">to</a>
</tr>
<tr>
	<td align="center">dd</td>
	<td align="center">mm</td>
	<td align="center">yyyy</td>
	<td align="center">&nbsp;</td>
	<td align="center">dd</td>
	<td align="center">mm</td>
	<td align="center">yyyy</td>
</tr>
<tr>
	<td><input type="text" name="${name}_day_from" size="2"></td>
	<td><input type="text" name="${name}_month_from" size="2"></td>
	<td><input type="text" name="${name}_year_from" size="4"></td>
	<td>&nbsp;-&nbsp;</td>
	<td><input type="text" name="${name}_day_to" size="2"></td>
	<td><input type="text" name="${name}_month_to" size="2"></td>
	<td><input type="text" name="${name}_year_to" size="4"></td>
</tr>	
</table>
		};
	}

        $cont .= qq{
<form name="searchForm" method="GET" action="$MyURL">
$title
$subtitle
<table border="0" width="100%">
<tr>
	<td>Share:</td>
	<td><select name="search_host">$hosts_html</select></td>
</tr>
<tr class="fviewborder">
	<td>All file(s) between:</td>
	<td>} . date_from_to_html('search') . qq{</td>
</tr>
<tr>
	<td>All backups between:</td>
	<td>} . date_from_to_html('search_backup') . qq{</td>
</tr>
<tr class="fviewborder">
	<td>Filename:</td>
	<td><input type="text" name="search_filename" size="25"></td>
</tr>
</table>

<input type="submit" value="Search">
<input type="hidden" value="search" name="action">
<input type="hidden" value="results" name="search_results">
</form>
	};
    }
    else {
	$cont = eval(q{ ${h1("Search")}});
	$cont .= eval(q{ ${h2("Search results")}});
	my $result;
	my $rows = "";

	my ($where, $from_f, $to_f, $from_b, $to_b) = BackupPC::SearchLib::getWhere(\%In);

	my $q = $In{'search_filename'};

	$cont .= "<small>";
	$cont .= "Filename filter: $q</br>" if ($q);
	$cont .= "Files date limit: $from_f - $to_f</br>" if ($from_f && $to_f);
	$cont .= "Backup date limit: $from_b - $to_b</br>" if ($from_b && $to_b);
	$cont .= "</small>";

	my $grid = BackupPC::SearchLib::displayGrid( $where, 1, $In{'offset'}, $q);
	$cont .= $grid;
    }
    Header( eval("qq{$Lang->{Search_archive}}"), "", 1, "", $cont );
    Trailer();
}

1;
