#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Mail::Sendmail;
use Fcntl qw(:flock);
use File::Copy;
use File::Basename;
use Date::Calc qw(:all);
use Excel::Writer::XLSX;

&today;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

@ARGV   = split(/\\*\&/, $ENV{'QUERY_STRING'});
$action = $ARGV[0] ;

our ($q)                 = CGI -> new() ;
our $iaction             = $q -> param('iaction')                || $action ;
our $isaved              = $q -> param('isaved')                 || '' ;

# our $debug = 1 ;

#-------- page opts --------------------------------------------------------------------------------------------------------------------------------------------------

&page_opts ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

print "Content-type: text/html\n\n";
	
our $srchscr   = 1 ;

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	if ($iaction eq ''){ &report_screen ; }
	&common_min_action;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

exit;

#------------------------------------------------------------------------------------------

sub report_ifields {
	
&load_search_vars ;
	
# if ($i{camera_id}) {
    # push @report_sql, "(`id`='$i{camera_id}')" ;
    # push @report_results, "camera = $camera_name{$i{camera_id}}" ;
    # $xlsreportname .= '-' . $i{camera_id} ;
	# # our $acc_id_sql_where = "`camera_id`='$i{camera_id}'"
	# }
	
# if ($i{camera_system_id}) {
    # push @report_sql, "(`camera_system_id`='$i{camera_system_id}')" ;
    # push @report_results, "camera_system = $camera_system{$i{camera_system_id}}" ;
    # $xlsreportname .= '-' . $i{camera_system_id} ;
	# }
	
if ($i{camera_system_id} and $i{camera_system_id} ne 'main' and $i{camera_system_id} ne 'other' and $i{camera_system_id} ne 'all') {
    push @report_sql, "(`camera_system_id`='$i{camera_system_id}')" ;
    push @report_results, "camera_system = $camera_system{$i{camera_system_id}}" ;
    $xlsreportname .= '-' . $i{camera_id} ;
	# our $acc_id_sql_where = "`camera_id`='$i{camera_id}'"
} elsif ($i{camera_system_id} eq 'main' or $i{camera_system_id} eq 'other' or $i{camera_system_id} eq 'all') {
	push @report_results, "camera_system = $i{camera_system_id}" ;
}
	
if ($i{date_from} and $i{date_to}) { # check dates
    my $date_from_check = $i{date_from} ;
       $date_from_check =~ s/\-//g ;
    my $date_to_check = $i{date_to} ;
       $date_to_check =~ s/\-//g ;
    if ($date_from_check > $date_to_check) { $error = qq(CAMERAS FROM ($i{date_from}) > CAMERAS TO ($i{date_to})); &report_screen; }
    }
    
if ($i{date_from}) {
    # push @report_sql, "(`date_active` >= '$i{date_from}')" ;
    # push @report_results, "date_active >= $i{date_from}" ;
    push @report_sql, "(`date_time` >= '$i{date_from}')" ;
    push @report_results, "date_time >= $i{date_from}" ;
    }

if ($i{date_to}) {
    # push @report_sql, "(`date_active` <= '$i{date_to}')" ;
    # push @report_results, "date_active <= $i{date_to}" ;
    push @report_sql, "(`date_time` <= '$i{date_to}')" ;
    push @report_results, "date_time <= $i{date_to}" ;
    }
	
if ($i{client_id}) {    
	# push @report_sql, "(`client_id` = '$i{client_id}')" ;
	push @report_results, "Client = $client{$i{client_id}}" ;
    }
	
$report_results_msg = uc join(', ', @report_results) ;

unless ($report_results_msg) { $error = qq(ENTER AT LEAST ONE SEARCH PARAMETER); &report_screen; } else { $isaved = qq(SELECT WHERE $report_results_msg) ; }
 
} #------------------------------------------------------------------------------------------

sub load_search_vars {

	# my $cameras_sql_where = "`id`='$i{camera_id}'" ;
	# &db_min_ro('cameras','id,camera_nr',$cameras_sql_where,'','') ;
	# foreach my $id (keys %{$db{cameras}}) { $camera_name{$id} = $db{cameras}{$id}{camera_nr} ; }

	my $camera_systems_sql_where = "`id`='$i{camera_system_id}'" ;
	&db_min_ro('camera_systems','id,name',$camera_systems_sql_where,'','') ;
	foreach my $id (keys %{$db{camera_systems}}) { $camera_system{$id} = $db{camera_systems}{$id}{name} ; } 

	if ($i{client_id}) {
		my $customers_sql_where = "`id`='$i{client_id}'" ;
		&db_min_ro('customers','*',$customers_sql_where,'','') ;
		foreach my $id (keys %{$db{customers}}) { $client{$id} = $db{customers}{$id}{name} ; }
	}

} #------------------------------------------------------------------------------------------

sub list_screen {

my $srch_where_sql = join(' AND ', @report_sql) ;

my $add_sql_where = '' ;   
    
# our @sql_col_display = ("id","camera_nr","camera_serial_nr","vpu_serial_nr","client_id","ref_nr","ref","date_delivered","date_active") ;
our @sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","vpu_serial_nr","client_id","ref_nr","ref","date_delivered","date_active") ;

&report_xls_export_header("Camera_Active_Report$xlsreportname",'camerareport') ;

&load_list_vars("$srch_where_sql$add_sql_where") ;

foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
	if ($linked_sn{$db{$table}{$id}{serial_nr}}) { $vpu_is_linked{$linked_sn{$db{$table}{$id}{serial_nr}}} = 1 ; }
}

foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
	
	# unless ($quote_active_date{$id}) { next ; }
	unless ($db{$table}{$id}{quote_active_date}) { next ; }
	
	next if (&common_camera_system_filter($table,$id)) ;
	
	if ($i{client_id}) {    
		next unless $client{$i{client_id}} eq $quote_customer{$id} ;
	}
	
	next if $vpu_is_linked{$db{$table}{$id}{serial_nr}} ;
	
	$print_tbody .= qq~<tr>~ ;
	
	$xlscol=0;
	
	foreach (@sql_col_display) { 
		unless ($_) { next ; } # blank for the buttons column
        my $val = $db{$table}{$id}{$_} ;
		my $align = '' ;
		
		my $center = qq~ class="dt-center"~ ;
		
		# if ($_ eq 'date_received')    { $align = $center ; }
		if ($_ eq 'date_delivered')     { $val   = $db{$table}{$id}{quote_delivery_date} ; $align = $center ; }
		if ($_ eq 'date_active')        { $val   = $db{$table}{$id}{quote_active_date} ; $align = $center ; }
		if ($_ eq 'client_id')          { $val   = $quote_customer{$id} ; }
		if ($_ eq 'ref_nr')             { $val   = $db{$table}{$id}{quote_nr} ; $align = $center ; }
		# if ($_ eq 'ref')              { $val   = $db{$table}{$id}{quote_ref} ? "<b><i>$db{$table}{$id}{quote_ref}</i></b>" : $quote_ref{$id} ; }
		if ($_ eq 'ref')                { $val   = $db{$table}{$id}{quote_ref} ? $db{$table}{$id}{quote_ref} : $quote_ref{$id} ; }
		if ($_ eq 'camera_serial_nr')   { $val   = $db{$table}{$id}{serial_nr}  ; }        		
		if ($_ eq 'camera_system_id')   { $val   = $camera_system{$val}         ; }
		# if ($_ eq 'vpu_serial_nr')    { $val   = &common_check_for_linked_vpu($db{$table}{$id}{camera_nr},$db{$table}{$id}{quote_nr},$id,'SN') ; }
		if ($_ eq 'vpu_serial_nr')      { $val   = $linked_sn{$db{$table}{$id}{serial_nr}} ; } # $val .= ' [' . $linked_cn{$db{$table}{$id}{serial_nr}} . ']' if $linked_cn{$db{$table}{$id}{serial_nr}} ;
		
        &report_xls_export_process_cell('','',$val) ; # ------------------------------------------------------------------------- HTML AFTER
        $xlscol++;

		# $val = &common_camera_links($table,$id,$val) if $_ eq 'camera_nr' or $_ eq 'ref_nr' ;		
		$val = &common_camera_links($table,$id,$val) if ($_ eq 'camera_nr' or $_ eq 'ref_nr') and $pixellot_limit{$username}!=1 and $events_limit{$username}!=1 ;
		
		$print_tbody .= qq~<td $align>$val</td>~ ;  
        }
		
	$print_tbody .= qq~</tr>~ ;
    $xlsrow++ ;
	}
	
&report_xls_export_footer('L',15,'camerareport') ;

$fnsortcol   = 8 ; 
$fnsortorder = 'asc' ;

$print_more_boxes .= qq~<br><button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button><br><br>~ ;

&common_min_extra_crumb("$lcpage\s-report","Search Screen") ;
	
} #------------------------------------------------------------------------------------------

sub set_tick {
	
my ($color,$tooltip) = @_ ;
	
$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;

return ($tick{$color}) ;
		
} #------------------------------------------------------------------------------------------

sub load_list_vars {
	
my ($where) = @_ ;
	
&db_min_ro('camera_systems','id,name','','','') ;
foreach my $id (keys %{$db{camera_systems}}) { $camera_system{$id} = $db{camera_systems}{$id}{name} ; } 
	
&db_min_ro('customers','id,name','','','') ;
foreach my $id (keys %{$db{customers}}) { $customer{$id} = $db{customers}{$id}{name} ; } 

# &common_load_quote_vars('>0');
&common_load_quote_vars("cameras.quote_nr>0 AND cameras.quote_nr=quotes.quote_nr");
	
&db_min_ro($table,'*',$where,'','') ;

# foreach my $id (keys %{$db{$table}}) {
	# $cam_nr{$id}    = $db{$table}{$id}{camera_nr} ;
	# $serial_nr{$id} = $db{$table}{$id}{serial_nr} ;
# }

} #------------------------------------------------------------------------------------------

sub thead {

&common_min_thead ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

our $glyphicon = 'list' ;
our $lcpage    = 'camera' ;

&common_page_name ;
   
our $table     = 'cameras' ;
$page_title    = 'Camera' ;

# &common_min_add_extras ;

} #-------------------------------------------------------------------------------

sub report_screen {

our $lcol = 3 ;
our $fcol = 5 ;

$print_box_content_rows .= &common_min_forms_start('report') ;

# &common_min_select_opts('camera_id','cameras','camera_nr',$i{camera_id},'') ;

&common_camera_opts ;
&common_min_select_opts('camera_system_id','camera_systems','code',$i{camera_system_id},'') ;
&common_min_select_opts('client_id','customers','name',$i{client_id},'') ;

my @datefrom = &common_add_delta_ymd(-2,0,0) ;

# $print_box_content_rows  .= &common_min_form_datepicker('date_from',"$now_year-01-01") ;  
$print_box_content_rows  .= &common_min_form_datepicker('date_from',"$datefrom[0]-01-01") ;  
$print_box_content_rows  .= &common_min_form_datepicker('date_to',"$now_year-$now_mm-$now_dd") ; 
#$print_box_content_rows .= &common_min_form_select('camera_id',$i{camera_id}) ; 
$print_box_content_rows  .= &common_min_form_select('camera_system_id',$i{camera_system_id}) ; 
$print_box_content_rows  .= &common_min_form_select('client_id',$i{client_id}) ; 

# my ($set_year,$set_month,$set_day) = Add_Delta_Days($now_year,$now_mm,$now_dd,-365); # 6 weeks back
# $set_month  = sprintf("%02s", $set_month) ;
# $set_day    = sprintf("%02s", $set_day) ;
# my $set_ccyy_dd_mm = $set_year . '-' . $set_month . '-' . $set_day ;
#$print_box_content_rows .= &common_min_form_datepicker('date_received_from',$set_ccyy_dd_mm) ;
#$print_box_content_rows .= &common_min_form_datepicker('date_received_to',$now_ccyy_mm_dd) ;
 
$print_box_content_rows .= &common_min_forms_end('','','report') ;

&common_min_search_screen ;

} #-------------------------------------------------------------------------------

use common ;
use report ;
use xlsxcreator ;
use today ;

1;