####################################################################
#  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")}});

        $cont .= <<ENDSTR;
      
      <form name="searchForm" method="GET" action="$MyURL">
	$title
	$subtitle
	<table border="0">
	 <tr>
	  <td>
	    Share:
	  </td>
	  <td>
	    <select name="search_host">
	      $hosts_html
            </select>
	  </td>
	 </tr>
	 <tr>
	  <td>
	    All file(s) between:
	  </td>
	  <td>
	    <table>
	      <tr>
	        <td>
	          Day
	        </td>
	        <td>
	          <input type="text" name="search_day_from">
	        </td>
	        <td>
	          Month
	        </td>
	        <td>
	          <input type="text" name="search_month_from">
	        </td>
	        <td>
	          Year
	        </td>
	        <td>
	          <input type="text" name="search_year_from">
	        </td>
	      </tr>
	      <tr>
	        <td>
	          Day
	        </td>
	        <td>
	          <input type="text" name="search_day_to">
	        </td>
	        <td>
	          Month
	        </td>
	        <td>
	          <input type="text" name="search_month_to">
	        </td>
	        <td>
	          Year
	        </td>
	        <td>
	          <input type="text" name="search_year_to">
	        </td>
	      </tr>	
	  
	    </table>	  
	  </td>
	 </tr>
	 <tr>
	   <td>
	      All backups between:	
	   </td>
	   <td>
	     <table>
	      <tr>
	       <td>Day</td>
	       <td>Month</td>
	       <td>Year</td>
	      </tr>
	      <tr>
	       <td>
	        <input type="text" name="search_backup_day_from">
	       </td>
	       <td>
	        <input type="text" name="search_backup_month_from">
	       </td>
	       <td>
	        <input type="text" name="search_backup_year_from">
	       </td>	 
	      </tr>
	      <tr>
	       <td>
	        <input type="text" name="search_backup_day_to">
	       </td>
	       <td>
	        <input type="text" name="search_backup_month_to">
	       </td>
	       <td>
	        <input type="text" name="search_backup_year_to">
	       </td>	 
              </tr>
	     </table>
	   </td>
	 </tr>
	 <tr>
	  <td>
	    Filename:
	  </td>
	  <td>
	    <input type="text" name="search_filename">
	  </td>
	 </tr>
	</table>
	<input type="submit" value="Search">
	<input type="hidden" value="search" name="action">
	<input type="hidden" value="results" name="search_results">
      </form>      
ENDSTR
    }
    else {
	$cont = eval(q{ ${h1("Search")}});
	$cont .= eval(q{ ${h2("Search results")}});
	my $result;
	my $rows = "";

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

1;
