#!/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 ;
our $double_box_layout = 1 ; # box on the right layout included

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

# our $allow_add_override = 0 ; if ($user_type eq 'ops_users') { $allow_add_override = 1 ; }

&page_opts ;

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

print "Content-type: text/html\n\n";
	
our $srchscr   = 0 ;
our $fnsortcol = 0 ;
our $redirtype = 'list' ; # $redirtype = 'search' ;

&db_open_upd ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	
	if ($iaction eq 'edit') { &load_table_vars("`camera_nr` LIKE 'VPU%' OR `camera_nr` LIKE 'S1%' OR `camera_nr` LIKE 'Prime%' OR `camera_nr` LIKE 'Coach%'"); }
	
	&common_min_action;
	# if ( $iaction ~~ ['stock','active','purchased','demo','all','main','other','vpu','rental'] ) {
		# &list_screen ;    
		# &common_min_screen3;
    # } 
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

exit;

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

sub validate {

# called from common_min_action

my $exists   = '' ;

&db_min_ro($table,'*',"camera_nr='$i{camera_nr}'",'','') ;

foreach my $id (keys %{$db{$table}}) {
    if ($db{$table}{$id}{camera_nr} eq $i{camera_nr}) {
		$exists = 1 ;
		}
	}
	
if ($exists) {
    $alert = &common_min_alert('warning',"CAMERA_NR ALREADY EXISTS!",'ok') ;
	%col_name = ();
	&common_min_add_screen;
    &common_min_screen1;
    }

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

sub insert {

&add_db_fields ;

$i{last_edited_by} = $userid ; 
$i{user_id} = $userid ;

$i{id} = &db_min_get_max($table,'id') ;

&val_check ;

$i{date_time}    = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
$i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

&common_upload_files("cameras/$i{id}") ;
&process_upload_ifields;
&process_sport_type_ids;

&set_fields ;

if ($i{linked_vpu_id}) {
	local $store_serial_nr      = $i{serial_nr} ; 
	local $store_linked_vpu_id  = $i{linked_vpu_id} ; 
}

&db_min_insert($table) ;

if ($store_linked_vpu_id) {
	%i = () ; 
	$i{quote_linked_vpu} = $store_serial_nr ;
	&db_min_upd($table,"id='$store_linked_vpu_id'") ;
}

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

sub update {

unless ($i{id}) { $error = qq(NO ID) ; return ; }

&edit_db_fields ;

if ($userid) { $i{last_edited_by} = $userid ; }

&val_check ;

$i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

&common_upload_files("cameras/$i{id}") ;
&process_upload_ifields;
&process_sport_type_ids;

&set_fields ;

&db_min_upd($table,"id='$i{id}'") ;

my $store_linked_vpu_id = $i{linked_vpu_id} ;
my $store_serial_nr     = $i{serial_nr} ;
my $store_id            = $i{id} ;

if ($i{event_linked_vpu} and $i{event_system_id}) { # update the linked VPU too
	my $store_event_system_id  = $i{event_system_id} ;
	my $store_event_linked_vpu = $i{event_linked_vpu} ;
	%i = () ;
	$i{event_system_id} = $store_event_system_id ;
	&db_min_upd($table,"serial_nr='$store_event_linked_vpu'") ;
}

if ($store_linked_vpu_id) {
	%i = () ;
	$i{quote_linked_vpu} = $store_serial_nr ;
	&db_min_upd($table,"id='$store_linked_vpu_id'") ; # updating the VPU camera
} else {
	%i = () ;
	$i{quote_linked_vpu} = '' ;
	&db_min_upd($table,"quote_linked_vpu='$store_serial_nr'") ; # updating the VPU camera
	%i = () ;
	$i{event_linked_vpu} = '' ;
	&db_min_upd($table,"event_linked_vpu='$store_serial_nr'") ; # updating the VPU camera
}

$i{id} = $store_id ;
	
# exit if $username eq 'rory' ;

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

sub process_sport_type_ids {

local @sporttypeids   = split(/\,/,$i{sporttypeids}) ;

my $sporttypeids      = join(",",@sporttypeids) ;

$sporttypeids         =~ s/\s//g; # remove white space

$ignore{sporttypeids} = 1 ;
$i{sport_type_ids}    = $sporttypeids ;
    
} #------------------------------------------------------------------------------------------

sub set_fields {

$ignore{serial_nr}        = '' ;
$ignore{serial_nr_2}      = '' ;
$ignore{serial_nr_3}      = '' ;
$ignore{serial_nr_desc}   = '' ;
$ignore{serial_nr_2_desc} = '' ;
$ignore{serial_nr_3_desc} = '' ;

$i{usd_amount}            = '0.00' unless $i{usd_amount} ;
$i{cur_amount}            = '0.00' unless $i{cur_amount} ;
$i{linked_vpu_id}         = '0'    unless $i{linked_vpu_id} ;
$i{sport_type_ids}        = 'NULL' unless $i{sport_type_ids} ;
$i{system_name}           = 'NULL' unless $i{system_name} ;
$i{replaced_vpu_ids}      = 'NULL' unless $i{replaced_vpu_ids} ;

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

sub duplicate {

unless ($i{id}) { $error = qq~NO ID~ ; return ; }

local $copy_id = $i{id} ;

&db_min_copy($table,$copy_id) ;

&common_min_copy_files("$htmlpath/uploads/cameras/$copy_id","$htmlpath/uploads/cameras/$i{id}") ;

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

sub val_check {
	
# unless ($i{quote_id})  { $i{quote_id} = '0' ; }

# unless ($i{replacement_date})  { $ignore{replacement_date} = 1 ; }
unless ($i{replacement_date})  { $i{replacement_date} = NULL ; }
unless ($i{date_received})     { $i{date_received}    = NULL ; }
unless ($i{event_linked_vpu})  { $i{event_linked_vpu} = NULL ; }
unless ($i{event_system_id})   { $i{event_system_id}  = 0    ; }

if ($i{camera_system_id}) { 
	my @valarr = split(/\:/,$i{camera_system_id}) ;
	$i{camera_system_id} = $valarr[0] ; # set the value to only be the id
	$camera_nr_prefix    = $valarr[1] ; # set the value to only be the id
}

# &db_min_ro($table,'id,camera_nr',"`camera_system_id`='$i{camera_system_id}'",'','') ;

# &set_max_cam_nr ;
# # $camera_nr_suffix++ ; 
	
} #------------------------------------------------------------------------------------------

# sub set_max_cam_nr {
	
# foreach my $id (keys %{$db{$table}}) { 
	# # $camera_nr_suffix++ ; 
	# my ($cam_type,$cam_cnt) = split(/\-/,$db{$table}{$id}{camera_nr});
	
	# $max_cam_cnt{$cam_type} = $cam_cnt unless $max_cam_cnt{$cam_type} > $cam_cnt;
# }

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

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}) {
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{stock}) {
    push @report_results, "stock = $i{stock}" ;
    $xlsreportname .= '-' . $i{stock} ;
	}
	
if ($i{client_id}) {    
	# push @report_sql, "(`client_id` = '$i{client_id}')" ;
	push @report_results, "Client = $client{$i{client_id}}" ;
    }
	
if ($i{serial_nr}) {
    push @report_sql, "(`serial_nr` LIKE '%$i{serial_nr}%')" ;
    push @report_results, "serial_nr LIKE `$i{serial_nr}`" ;
    }
	
if ($i{quote_nr}) {
    push @report_sql, "(`quote_nr`='$i{quote_nr}')" ;
    push @report_results, "quote_nr = `$i{quote_nr}`" ;
    }
	
if ($i{date_received_from} and $i{date_received_to}) { # check dates
    my $date_received_from_check = $i{date_received_from} ;
       $date_received_from_check =~ s/\-//g ;
    my $date_received_to_check = $i{date_received_to} ;
       $date_received_to_check =~ s/\-//g ;
    if ($date_received_from_check > $date_received_to_check) { $error = qq(CAMERAS FROM ($i{date_received_from}) > CAMERAS TO ($i{date_received_to})); &report_screen; }
    }
    
if ($i{date_received_from}) {
    push @report_sql, "(`date_received` >= '$i{date_received_from}')" ;
    push @report_results, "date_received >= $i{date_received_from}" ;
    }

if ($i{date_received_to}) {
    push @report_sql, "(`date_received` <= '$i{date_received_to}')" ;
    push @report_results, "date_received <= $i{date_received_to}" ;
    }
	
$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 {

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

	&db_min_ro('camera_systems','id,name',"`id`='$i{camera_system_id}'",'','') ;
	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 process_upload_ifields {

foreach (keys %uploads_file) { $i{$_} = $uploads_file{$_} ; $ignore{$_} = '' ; }

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

sub list_screen {

our $open_new_tab = qq~target="_blank"~ ; 

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

my $add_sql_where = '' ;   
    
our @sql_col_display = ("camera_nr","in_stock","camera_system_id","serial_nr","part_number","system_name","date_received","client_id","ref_nr","date_delivered","date_active","quote_ref","replacement_date") ;
	
push @sql_col_display, "" ;

&report_xls_export_header("Cameras$xlsreportname",'cameras') ;

&load_list_vars("$srch_where_sql$add_sql_where") ;

foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
	
	next if (&common_camera_system_filter($table,$id)) ;
	
	if ($i{client_id}) { next unless $client{$i{client_id}} eq $quote_customer{$id} ; }
	# if ($i{stock})     { next unless $i{stock} eq lc $quote_type{$id} ; }
	
	# if ($i{stock})     { &common_debug("next unless $i{stock} eq $lc_stock"); }
	
	$camera_system_count{$camera_system{$db{$table}{$id}{camera_system_id}}}++;
	
	$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   = $quote_delivery_date{$id} ; $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 'camera_system_id')   { $val   = $camera_system{$val} ; }
		if ($_ eq 'client_id')          { $val   = $quote_customer{$id} ; }
		# if ($_ eq 'quote_type')       { $val   = $quote_type{$id} ; }
		if ($_ eq 'ref_nr')             { $val   = $db{$table}{$id}{quote_nr} ; $align = $center ; }
		if ($_ eq 'quote_ref')          { $val   = $db{$table}{$id}{quote_ref} ? $db{$table}{$id}{quote_ref} : $quote_ref{$id} ; }
		# if ($_ eq 'purchase/rental')  { $val   = $quote_type{$id} ; $align = $center ; }
		if ($_ eq 'demo')               { $val   = $db{$table}{$id}{demo_recipient} ; }
		
		if ($_ eq 'extended_warranty')  { 
			$val   = $db{$table}{$id}{quote_ew_date_from} if $db{$table}{$id}{quote_ew_date_from} ;
			$val  .= "/$db{$table}{$id}{quote_ew_date_to}" if $db{$table}{$id}{quote_ew_date_to} ;
			$align = $center ;
		}
		
		if ($_ eq 'in_stock')           { if ($db{$table}{$id}{quote_nr} or $db{$table}{$id}{demo_recipient} or $db{$table}{$id}{replacement_date} or $db{$table}{$id}{event_system_id}) { $val = 0 ; } else { $val = 1 ; } }
				
        &report_xls_export_process_cell('','',$val) ; # ------------------------------------------------------------------------- HTML AFTER
        $xlscol++; 
		
		# if ($_ eq 'in_stock') { if ($db{$table}{$id}{quote_nr}) { $val = &set_tick('red', '') ; } else { $val = &set_tick('green', '') ; } $align = $center ; }
		if ($_ eq 'in_stock') { if ($val==1) { $val = &set_tick('green', '') ; } else { $val = &set_tick('red', '') ; } $align = $center ; }
		# if ($_ eq 'quote_nr') { $val = qq~<a href="javascript:editQuote('$quote_id{$val}');">$val</a>~ ; }
		
		if ($_ eq 'replacement_date') { 
			$val = qq~<span class="label" style="background-color:#000000;border:1px solid #000000;">$val</span>~ ; 
		}
		
		$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 ;
		
		if ($_ eq 'camera_nr' && $camera_ticket{$id} && $pixellot_limit{$username}!=1 && $events_limit{$username}!=1) {
			$val .= $camera_ticket{$id} ;
		}
		
		$print_tbody .= qq~<td $align>$val</td>~ ;  
        }
	
	my $delete_butt = '' ; my $copy_butt = '' ;	 my $edit_butt = '' ;	
	
	if (($useropts{boss}{$username} or $useropts{super}{$username}) and $pixellot_limit{$username}!=1 and $events_limit{$username}!=1) {
		$edit_butt   = qq~<a class="btn btn-info btn-xs" href="javascript:editMinItem('$id');"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;
		$copy_butt   = qq~<a class="btn btn-success btn-xs" href="javascript:copyMinItem('$id');" title="Copy" data-toggle="tooltip" data-placement="top"><i class="glyphicon glyphicon-retweet icon-white"></i></a>~;
		$delete_butt = qq~<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{camera_nr}','$id')"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;
		}

	# if ($camera_exists_on_quote{$id}) { $delete_butt = '' ; }
	if ($db{$table}{$id}{quote_nr}) { $delete_butt = '' ; }
		
	$print_tbody .= qq(<td nowrap>&nbsp;$edit_butt&nbsp;$copy_butt&nbsp;$delete_butt</td></tr>) ;
    $xlsrow++ ;
	}
	
&report_xls_export_footer('L',15,'cameras') ;
	
our $fnsortcol = 0 ; our $fnsortorder = 'asc' ;

$print_more_boxes .= qq(<br><button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button>) ;
	
&common_min_extra_crumb("search-$lcpage\s","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} ; } 

	my $surl = qq~javascript:dlgMdl('$useropts{scripts}/dialog/read_ticket.pl~;
	&db_min_ro('tickets','*',"camera_ids IS NOT NULL",'','') ;
	foreach my $id (keys %{$db{tickets}}) { 
		my $cam_ids     = $db{tickets}{$id}{camera_ids} ;		
		my $uid         = $db{tickets}{$id}{unique_id} ;
		my $sent_from   = $db{tickets}{$id}{sent_from} ;
		my $date_time   = $db{tickets}{$id}{date_time} ;
		my $s_uid       = $db{tickets}{$id}{suid} ;
		my $subject     = $db{tickets}{$id}{subject} ;
		
		my @camera_ids = split(/\,/,$cam_ids) ;

		foreach my $cam_id (@camera_ids) { 
			$camera_ticket{$cam_id} .= '<a href="' . $surl . qq~?uniqueid=$uid&from=$sent_from&suid=$s_uid&responded=1&subject=$subject','$subject [$uid]','','medium-dialog');" data-toggle="tooltip" data-placement="right" data-title="Read ticket [$date_time]" style="margin:2px 2px 0 0;"><i class="glyphicon glyphicon-envelope red" style="margin-left:2px;top:3px;"></i></a>~
		}
	} 

	# &common_load_quote_vars('>0');

	&common_load_quote_vars("cameras.quote_nr>0 AND cameras.quote_nr=quotes.quote_nr");

	&load_table_vars($where);
		
	# &db_min_ro($table,'*',$where,'','') ;

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

sub load_table_vars {
	
my ($where) = @_ ;
	
&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 {
	
	my $cam_qtys = '' ;
	
	foreach (keys %camera_system_count) {
		$cam_qtys .= qq~$_ : $camera_system_count{$_}<br>~ ; 
	}
		
	$replace_blank_table_top_right = qq~&nbsp;<a href="#" data-toggle="popover" data-placement="left" data-content="$cam_qtys" title="Camera Qty Count" data-html="true" data-trigger="focus" class="btn btn-default btn-xs"><i class="glyphicons glyphicons-hash" style="margin-top:3px;"></i></a>~ if $cam_qtys ;

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

sub hidden_db_fields {

$ignore{iaction} = 1 ;
# $ignore{logo} = 1 ;
$ignore{serial_nr} = 1 ;
$ignore{serial_nr_2} = 1 ;
$ignore{serial_nr_3} = 1 ;
$ignore{serial_nr_desc} = 1 ;
$ignore{serial_nr_2_desc} = 1 ;
$ignore{serial_nr_3_desc} = 1 ;

$ignore{quote_nr}            = 1 ; 
$ignore{quote_customer}      = 1 ; 
# $ignore{quote_ref}         = 1 ; 
$ignore{quote_main_ref}      = 1 ; 
$ignore{quote_delivery_date} = 1 ; 
$ignore{quote_active_date}   = 1 ; 
$ignore{quote_type}          = 1 ; 
$ignore{quote_date_from}     = 1 ; 
$ignore{quote_date_to}       = 1 ; 
$ignore{quote_linked_vpu_sn} = 1 ; 
$ignore{quote_linked_vpu_cn} = 1 ; 

$hidden{quote_linked_vpu} = 2 ; 
# $ignore{active} = 1 ; 
$hidden{replaced_vpu_ids} = 2 ;


# $checkbox{active} = 1 ;
$hidden{last_updated} = 2 ;

$hidden{last_edited_by} = 2 ;
$hidden{date_time} = 2 ;
$hidden{user_id} = 2 ;

$required{currency} = 1 ;
$readonly{currency} = 'READONLY' ;

$readonly{cur_amount} = 'READONLY' ;

# $required{serial_nr} = 1 ; # alphanumeric
$textarea{notes} = 1 ;

$datepicker{replacement_date} = 1 ;
$datepicker{date_received} = 1 ;
# $datepicker{date_delivered} = 1 ;
# $datepicker{date_active} = 1 ;

$info_glyph_tooltip{demo_recipient} = qq~<i style="margin-top:-3px" data-toggle="tooltip" data-placement="right" data-title="Set to staff member name if demo stock taken home." class="glyphicons glyphicons-info-sign blue"></i>~ ; 

for (1 .. 3) {
	$ignore{"camera_$_"} = 1 ; 
	$ignore{"iattachcamera_$_"} = 1 ;
	}

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

sub add_db_fields {
	
&sort_fields;

&hidden_db_fields ;

$hidden{id} = 2 ;
$required{camera_nr} = 1 ;
# $hidden{camera_nr} = 2 ;

&common_exchange_rates;

$db{$table}{''}{currency} = 'ZAR' ;
$db{$table}{''}{roe} = $roe{ZAR} ;

if ($iaction eq 'add') { &build_boxes_add ; } # can't be in add_screen, only works if here

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

sub edit_db_fields {
	
&sort_fields;

&hidden_db_fields ;

$readonly{camera_nr}      = 'READONLY' ;
$hidden{camera_system_id} = 2 ;

$ignore{date_time}        = 1 ;
$ignore{user_id}          = 1 ;
	
unless ($db{$table}{$i{id}}{currency}) {	
	&common_exchange_rates;
	$db{$table}{$i{id}}{currency} = 'ZAR' ;
	$db{$table}{$i{id}}{roe} = $roe{ZAR} ;
}

$hidden{id} = 1 ; # 1 = bypass for db update

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

sub sort_fields {
	
%sort_field = () ;

$sort_field{1}     = 'camera_system_id' ; $fcol{camera_system_id} = 8 ;
$sort_field{2}     = 'camera_nr' ; $fcol{camera_nr} = 8 ;
$sort_field{3}     = 'date_received' ; $fcol{date_received} = 4 ;
$sort_field{4}     = 'usd_amount' ; $dont_end_row{usd_amount} = 1 ; $fcol{usd_amount} = 2 ; $preferred_title{usd_amount} = 'Amount' ;
$sort_field{5}     = 'currency' ; $add_col{currency} = 1 ; $fcol{currency} = 2 ;
$sort_field{6}     = 'roe' ; $add_col{roe} = 1 ; $fcol{roe} = 2 ;
$sort_field{7}     = 'cur_amount' ; $add_col{cur_amount} = 1 ; $fcol{cur_amount} = 2 ;
$sort_field{8}     = 'demo_recipient' ; $add_finish_div{demo_recipient} = 1 ; $fcol{demo_recipient} = 8 ;
$sort_field{9}     = 'linked_vpu_id' ; $fcol{linked_vpu_id} = 4 ; $dont_end_row{linked_vpu_id} = 1 ; $select{linked_vpu_id} = 1 ; $preferred_title{linked_vpu_id} = "Linked VPU" ; 
$sort_field{10}    = 'sport_type_ids' ; $select{sport_type_ids} = 1 ; $preferred_title{sport_type_ids} = "Sport Types" ; $dlg{sport_type_ids} = qq~ <div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_sport_type.pl?add&selectSport_type_ids','Add Sport Type','height:450px','medium-dialog');" class="btn btn-primary btn-xs" style="padding:1px 2px 1px 3px;margin-top:0px;" title='Add Sport Type' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;"></i></a></div>~ ;
$sort_field{11}    = 'quote_ref' ;
$sort_field{12}    = 'notes' ;

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

sub select_opts {

my ($id) = @_ ;

$multiple{sport_type_ids} = 1; 

my @sport_type_ids = split(/\,/,$db{$table}{$id}{sport_type_ids});

foreach (@sport_type_ids) { 
	$selected_multi{$_} = 'SELECTED' ; 
	&common_debug("SELECTED sport_type_ids [$_] : [selected=$selected_multi{$_}]") ;
}

&common_min_select_opts('sport_type_ids','sport_types','name',$db{$table}{$id}{sport_type_ids},'') ;

$extra_form_fields .= qq~<input type="hidden" name="sporttypeids" id="sport-type-ids" value="">~;

$trigger_jquery_raw .= qq~
\$("#savebutt").click(function() {
	let st_val = \$("#selectSport_type_ids").chosen().val() ;
	\$('#sport-type-ids').val(st_val);
	\$('#cameras-form').submit();   
});~;

if ($iaction eq 'add') {
	&db_min_ro('camera_systems','*',$where,'','') ;
	$select{camera_system_id} = 1 ;
	$required{camera_system_id} = 1 ;
	local $sel_camera_system_id = 0 ;
	local %camera_system_code = () ; 
	foreach my $id (keys %{$db{camera_systems}}) {	
		my $saved_val = $db{$table}{$i{id}}{camera_system_id} ;
		if ($saved_val) { my @valarr = split(/\:/,$saved_val) ; $saved_val = $valarr[0] ; }
		if ($saved_val eq $id) { $selected = 'SELECTED' ; $sel_camera_system_id = $id } else { $selected = '' ; }
		$opts{camera_system_id} .= qq~<option $selected value="$id:$db{camera_systems}{$id}{code}">$db{camera_systems}{$id}{name}</option>~ ; 
	}
}

local $sel = 'SELECTED' ;
$opts{linked_vpu_id} = qq~<option value="" $sel>- NONE -</option>~ ;
$sel = '' ;

if ($db{$table}{$id}{camera_system_id} == 1 or $db{$table}{$id}{camera_system_id} == 2 or $db{$table}{$id}{camera_system_id} == 14) {
	my $dbtable = "itv_admin_db.$table" ;
	&db_min_ro($dbtable,'id,camera_nr,serial_nr,linked_vpu_id,quote_linked_vpu,camera_system_id',"(camera_nr LIKE 'vpu%' OR camera_nr LIKE 'S1%' OR camera_nr LIKE 'S2%') AND (quote_linked_vpu IS NULL OR quote_linked_vpu = '' OR quote_linked_vpu = '$db{cameras}{$id}{serial_nr}') AND (event_linked_vpu IS NULL OR event_linked_vpu = '')",'','') ; 

	local %is_linked = () ; 
	foreach (sort keys %{$db{$dbtable}}) {
		next if $_ == $i{id} ; 
		$is_linked{$db{$dbtable}{$_}{linked_vpu_id}} = $db{$dbtable}{$_}{serial_nr} if $db{$dbtable}{$_}{linked_vpu_id} ;
	}
	
	$opts{linked_vpu_id} = qq~~ ;
	foreach (sort keys %{$db{$dbtable}}) {	
		local ($cam_part_1,$cam_part_2) = split(/\-/,$db{$dbtable}{$_}{camera_nr}) ;	
		
		next if substr($db{$dbtable}{$_}{camera_nr},0,3) ne 'VPU' ;
		next if ($is_linked{$_} and $db{$dbtable}{$_}{quote_linked_vpu} ne $db{cameras}{$id}{serial_nr}) ;
		next if substr($db{$dbtable}{$_}{camera_nr},3,2) ne 'S1' and substr($db{$dbtable}{$_}{camera_nr},3,2) ne 'S2' ;
		next if $db{camera_systems}{$db{$table}{$i{id}}{camera_system_id}}{code} and $db{camera_systems}{$db{$table}{$i{id}}{camera_system_id}}{code} ne substr($db{$dbtable}{$_}{camera_nr},3,2) ;
		
		$sel = 'SELECTED' if $db{$dbtable}{$i{id}}{linked_vpu_id} == $_ or $db{$dbtable}{$_}{quote_linked_vpu} eq $db{cameras}{$id}{serial_nr} ; 
		
		$opts{linked_vpu_id} .= qq~<option value="$_" $sel>$db{$dbtable}{$_}{camera_nr} [$db{$dbtable}{$_}{serial_nr}]</option>~ ;
		$sel = '' ; 
	}
}

$trigger_jquery_raw .= qq~
\$("#selectCamera_system_id").change(function() {
    var cam_nr     = \$("#inputCamera_nr") ;
    var sel_cam_id = \$("#selectCamera_system_id").val() ;
    var url = "$useropts{scripts}/get/get_db_latest_cam_nr.pl?camera_system_id=" + \$("#selectCamera_system_id").val() ;
    \$.get(url, function(json) {
      \$.each(json, function(key, data) {
        cam_nr.val(data.camera_nr);
      });
    },
    'json');
	
	// console.log("sel_cam_id :"+sel_cam_id) ;
	
	var vpu_id = \$("#selectLinked_vpu_id") ;
	vpu_id.empty(); 
	vpu_id.trigger("chosen:updated");
	
	var linked_vpu_id = '$db{cameras}{$id}{linked_vpu_id}' ;	
	// console.log("1 . vpu_id : "+vpu_id.val()) ; 
	if (sel_cam_id == '1:S1' || sel_cam_id == '2:S2' || sel_cam_id == '14:') {
		vpu_id.prepend("<option value='0'>- NONE -</option>") ;	
		// var active = \$("#checkboxActive") ; 
		var url2 = "$useropts{scripts}/get/get_db_linked_vpu.pl?camera_system_id=" + \$("#selectCamera_system_id").val() + "&linked_vpu_id="+linked_vpu_id ;
		// console.log("url2 : "+url2+" linked_vpu_id : "+linked_vpu_id) ; 
		vpu_id.empty(); 
		\$.get(url2, function(json) {
			\$.each(json, function(key, data) {
				// console.log("vpu_id :"+data.id+" - "+data.camera_nr+" - "+data.serial_nr+"id data.sel : "+data.sel) ;
		
				if (data.sel == 0) {
					vpu_id.append("<option value='"+data.id+"'>"+data.camera_nr+" ["+data.serial_nr+"]</option>") ;
				}
				else {
					vpu_id.append("<option value='"+data.id+"' SELECTED>"+data.camera_nr+" ["+data.serial_nr+"]</option>") ;
				}
			});
			vpu_id.trigger("chosen:updated");
		},
		'json');

	}
	// console.log("2 . vpu_id : "+vpu_id.val()) ; 
	if (vpu_id.val() == null) {
		vpu_id.append("<option value='0'>- NONE -</option>") ;	
		vpu_id.trigger("chosen:updated");
	}
});


\$("#inputUsd_amount,#inputRoe").change(function() {
	var roe  = \$("#inputRoe").val() ;
	var curr = \$("#inputCurrency").val() ; // \$("#selectCurrency").val() ;
	var uval = \$("#inputUsd_amount").val() ;
	
	var usdAmnt  = uval * 1 ;
	var currAmnt = usdAmnt * roe ;
	
	\$("#inputCur_amount").val(currAmnt.toFixed(2)) ;
});
~;

$trigger_jquery .= qq~
	\$("#camera-uploads-box").trigger('click');
	\$("#camera-replacement-box").trigger('click');
~;

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

sub add_screen {

# called from common_add_screen

&select_opts('') ;

our $savjqy = 1 ; # incorporate jquery into the save event to accommodate the multiple chosen options

# &build_boxes_add ; # can't do this here

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

sub build_boxes_add {

$print_box_content_rows .= &common_min_forms_start($table) ;
$print_box_content_rows .= qq~<div class='row'><div class='col-md-6'>~ ;
$print_box_content_rows .= &common_min_box_top('camera',"&nbsp;&nbsp;Camera",$box_main_bg) ;   
$print_box_content_rows .= &common_min_edit_form($id,$table,'',3,9,1,1) ;
$print_box_content_rows .= &common_min_box_foot ;

&build_boxes_left('');

$print_box_content_rows .= qq~</div><div class='col-md-6'>~ ; 

&build_boxes_right('');

$print_box_content_rows .= qq~</div></div>~ ;

$print_box_content_rows .= &common_min_forms_end('',$table,'save') ;

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

sub edit_screen {

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

&select_opts($i{id}) ;

$addcpb = 1 ; # add copy button to the edit screen

our $savjqy = 1 ; # incorporate jquery into the save event to accommodate the multiple chosen options

&build_boxes_edit($i{id}) ;

&common_min_extra_crumb("search-$lcpage\s","Search Screen") ;

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

sub build_boxes_edit {

my ($id) = @_ ;

$print_box_content_rows .= &common_min_forms_start($table) ;
$print_box_content_rows .= qq~<div class='row'><div class='col-md-6'>~ ;
$print_box_content_rows .= &common_min_box_top('camera',"&nbsp;&nbsp;Camera : $db{$table}{$id}{id}",$box_main_bg) ;   
$print_box_content_rows .= &common_min_edit_form($id,$table,'',3,9,1,1) ;
$print_box_content_rows .= &common_min_box_foot ;
	&build_boxes_left($id);
$print_box_content_rows .= qq~</div><div class='col-md-6'>~ ;  
	&build_boxes_right($id);
$print_box_content_rows  .= qq~</div></div>~ ;
$print_box_content_rows  .= &common_min_forms_end($id,$table,'update',$skip) ;

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

sub build_boxes_left {

	my ($id) = @_ ;

	# our ($lcol,$fcol) = &common_min_columns(3,9) ;

	$print_box_content_rows .= qq~
	<div class='row'>
		<div class='col-md-12'>
		</div>
	</div>~ ; # fixes row highlighting issue

	# #----------------- NEW BOX LEFT ---------------------------------------------------------------------

	$add_form_fields = '' ; $fcol=3 ;  

	# if ($_>$cntcamloop) { $cam_display_butt = qq~display:none;~ ;  } else { $cam_display_butt = '' ; }

	$add_form_fields .= qq~
	<div class='row' id="sn_row">
		<div class='col-md-12'>~ ; 
		$col = 'serial_nr' ;
		$required{$col} = 1 ;
		&common_min_forms_required ;
		$add_form_fields .= &common_min_form_input('serial_nr',$db{$table}{$id}{serial_nr},'','',1) ; 
		$add_form_fields .= qq~
			<div class='fieldname col-md-2'>
				<label class='control-label' for='inputPart_number'>Part Number</label>
			</div>
		~ ;
		$add_form_fields .= &common_min_form_input_col('part_number',$db{$table}{$id}{part_number}) ;   		
		$fcol=3 ; $lcol=3 ;
		$add_form_fields .= qq~
		</div>
	</div>~ ;
		
		$add_form_fields .= qq~<br><br>~ ;  
		
		unless ($db{$table}{$id}{serial_nr_2}) {
			$disp_sn_2_style = qq~style="display:none;"~ ;
			$add_form_fields .= qq~<a id="btn_serial_nr" href="#" style="padding-left:16px;clear:both;"><i class="glyphicon glyphicon-plus blue"></i></a>~ ;
			$trigger_jquery_raw .= qq~ \$("#btn_serial_nr").click(function () {
				\$("#sn_2_row").toggle();
				\$("#btn_serial_nr").hide();
				\$("#btn_serial_nr_2").show();
			});~ ; 
		}
		
		$add_form_fields .= qq~
	</div>~ ;
	

	$add_form_fields .= qq~<div class='row' id="sn_2_row" $disp_sn_2_style><div class='col-md-12'>~ ; 					 
		$add_form_fields .= &common_min_form_input('serial_nr_2',$db{$table}{$id}{serial_nr_2},'','',1) ; $fcol=5 ;    
		$add_form_fields .= &common_min_form_input_col('serial_nr_2_desc',$db{$table}{$id}{serial_nr_2_desc}) ; $fcol=3 ;  
		$add_form_fields .= qq~<br><br>~ ;  
		
		unless ($db{$table}{$id}{serial_nr_3}) {
			$disp_sn_3_style = qq~style="display:none;"~ ;
			$add_form_fields .= qq~<a id="btn_serial_nr_2" href="#" style="padding-left:16px;clear:both;"><i class="glyphicon glyphicon-plus blue"></i></a>~ ;
			$trigger_jquery_raw .= qq~ \$("#btn_serial_nr_2").click(function () {
				\$("#sn_3_row").toggle();
				\$("#btn_serial_nr_2").hide();
				\$("#btn_serial_nr_3").show();
			});~ ; 
		}
		
		$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~</div></div>~ ;

	$add_form_fields .= qq~<div class='row' id="sn_3_row" $disp_sn_3_style><div class='col-md-12'>~ ; 					 
		$add_form_fields .= &common_min_form_input('serial_nr_3',$db{$table}{$id}{serial_nr_3},'','',1) ; $fcol=5 ;    
		$add_form_fields .= &common_min_form_input_col('serial_nr_3_desc',$db{$table}{$id}{serial_nr_3_desc}) ; $fcol=3 ;  
		$add_form_fields .= qq~<br><br>~ ;  
		
		unless ($db{$table}{$id}{serial_nr_4}) {
			$disp_sn_4_style = qq~style="display:none;"~ ;
			$add_form_fields .= qq~<a id="btn_serial_nr_3" href="#" style="padding-left:16px;clear:both;"><i class="glyphicon glyphicon-plus blue"></i></a>~ ;
			$trigger_jquery_raw .= qq~ \$("#btn_serial_nr_3").click(function () {
				\$("#sn_4_row").toggle();
				\$("#btn_serial_nr_3").hide();
				\$("#btn_serial_nr_4").show();
			});~ ; 
		}
		
		$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~</div></div>~ ;

	$add_form_fields .= qq~<div class='row' id="sn_4_row" $disp_sn_4_style><div class='col-md-12'>~ ;   
		$add_form_fields .= &common_min_form_input('serial_nr_4',$db{$table}{$id}{serial_nr_4},'','',1) ; $fcol=5 ;    
		$add_form_fields .= &common_min_form_input_col('serial_nr_4_desc',$db{$table}{$id}{serial_nr_4_desc}) ; $fcol=3 ; 
		$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~</div></div>~ ;
	
	$add_form_fields .= qq~
	<div class='row'><div class='col-md-12'>&nbsp;</div></div>
	<div class='row'>
		<div class='col-md-12'>~ ; 
		$add_form_fields .= &common_min_form_input('system_name',$db{$table}{$id}{system_name},'','','') ; 
		$add_form_fields .= qq~
		</div>
	</div>~ ;

	$print_box_content_rows .= &common_min_box_top('film','Serial Nr.',$box_right_bg);
	$print_box_content_rows .= $add_form_fields ;                
	$print_box_content_rows .= &common_min_box_foot;
	
	my $cam_nr_prefix = substr($db{$table}{$id}{camera_nr},0,2) ;

	if (($cam_nr_prefix eq 'S1' || $cam_nr_prefix eq 'S2' || $cam_nr_prefix eq 'VP') and $db{$table}{$id}{quote_nr} == 0) {

		$add_form_fields = '' ;

		$print_box_content_rows .= qq~<div class='row'><div class='col-md-12'></div></div>~ ; # fixes row highlighting issue

		%done_col_val = () ;

		&common_min_select_opts('event_linked_vpu','itv_admin_db.cameras','camera_nr',$db{$table}{$id}{event_linked_vpu},'','',"camera_nr LIKE 'VPU%' AND (quote_linked_vpu = '' OR quote_linked_vpu IS NULL)",'serial_nr') unless $cam_nr_prefix eq 'VP' ;

		$print_box_content_rows .= &common_min_box_top('star','Events',$box_right_bg); $preferred_title{event_linked_vpu} = 'Event System' ; 
		$print_box_content_rows .= qq~<div class='row'><div class='col-md-12'>~ ;   
			$print_box_content_rows .= &common_min_form_select('event_linked_vpu',$db{$table}{$id}{event_linked_vpu},1) ; $fcol=5 ;
			&common_min_select_opts('event_system_id','itv_admin_db.event_systems','description',$db{$table}{$id}{event_system_id},'') ;
			$allow_deselect{event_system_id} = 1 ;
			$print_box_content_rows .= &common_min_form_select_col('event_system_id',$db{$table}{$id}{event_system_id}) ; 
			$print_box_content_rows .= qq~</div>~ ;               
		$print_box_content_rows .= qq~</div></div>~ ;               
		$print_box_content_rows .= &common_min_box_foot;
		
	}

	# #----------------- END BOX LEFT ---------------------------------------------------------------------

	$print_box_content_rows .= qq~<div class='row'><div class='col-md-12'></div></div>~ ; # fixes row highlighting issue

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

sub build_boxes_right {

my ($id) = @_ ;

my $box_icon = '' ;

#----------------------------------------------------------------- New box --------------------------------------

if ($iaction eq 'edit') {
	
	&db_min_ro('customers','id,name','','','') ;
	foreach my $cust_id (keys %{$db{customers}}) { $customer{$cust_id} = $db{customers}{$cust_id}{name} ; } 
	
	my $q_nr = $db{$table}{$id}{quote_nr} ;
	my $c_nr = $db{$table}{$id}{camera_nr} ;
	
	# &common_load_quote_vars("='$id'");
	&common_load_quote_vars("cameras.quote_nr='$q_nr' AND quotes.quote_nr='$q_nr'");
	
	if ($q_nr) {
		
		# my $ref = $db{$table}{$id}{quote_ref} ? $db{$table}{$id}{quote_ref} : $quote_ref{$id} ;
	
		$print_box_content_rows .= &common_min_box_top('qrcode','Details',$box_right_bg,$box_icon,'') ;
			$lcol=3;
			$fcol=4;
		  	my $field = 'quote_nr'             ; $preferred_title{quote_nr}            = 'Ref Nr'              ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$q_nr)                                         ; $ignore{$field} = 1 ;
		  	my $field = 'quote_customer'       ; $preferred_title{quote_customer}      = 'Customer'            ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$quote_customer{$id})                          ; $ignore{$field} = 1 ;
		  	my $field = 'quote_main_ref'       ; $preferred_title{quote_main_ref}      = 'Ref'                 ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$quote_ref{$id})                               ; $ignore{$field} = 1 ;
		  	my $field = 'quote_delivery_date'  ; $preferred_title{quote_delivery_date} = 'Delivery Date'       ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$db{$table}{$id}{quote_delivery_date})         ; $ignore{$field} = 1 ;
		  	my $field = 'quote_active_date'    ; $preferred_title{quote_active_date}   = 'Active Date'         ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$db{$table}{$id}{quote_active_date})           ; $ignore{$field} = 1 ;
			my $field = 'quote_type'           ; $preferred_title{quote_type}          = 'Type'                ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$quote_type{$id})                              ; $ignore{$field} = 1 ;
			my $field = 'quote_date_from'      ; $preferred_title{quote_date_from}     = 'EW From'             ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$db{$table}{$id}{quote_ew_date_from})          ; $ignore{$field} = 1 ;
			my $field = 'quote_date_to'        ; $preferred_title{quote_date_to}       = 'EW To'               ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$db{$table}{$id}{quote_ew_date_to})            ; $ignore{$field} = 1 ;
			my $field = 'quote_linked_vpu_sn'  ; $preferred_title{quote_linked_vpu_sn} = 'Linked VPU SerialNr' ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$linked_sn{$db{$table}{$id}{serial_nr}})       ; $ignore{$field} = 1 ; # &common_debug("SN - $val [$db{$table}{$id}{camera_nr},$quote_nr{$id},$id]");
			my $field = 'quote_linked_vpu_cn'  ; $preferred_title{quote_linked_vpu_cn} = 'Linked VPU CameraNr' ; $readonly{$field} = 'READONLY' ; $print_box_content_rows .= &common_min_form_input($field,$linked_cn{$db{$table}{$id}{serial_nr}})       ; $ignore{$field} = 1 ; # &common_debug("CN - $val [$db{$table}{$id}{camera_nr},$quote_nr{$id},$id]");				
		$print_box_content_rows .= &common_min_box_foot ;
	}
}

$print_box_content_rows .= &common_min_box_top('picture','Uploads',$box_right_bg,$box_icon,'camera-uploads-box') ;
	$print_box_content_rows .= qq~<div class="row"><div class="col-md-12">&nbsp;</div></div><div class="row">~;

	for (1 .. 3) {
		my $uploads = &common_get_field_uploads($id,"camera_$_",'id') ;

		if ($uploads) {
			$print_box_content_rows .= qq~<div class="col-md-6" id="logos">$uploads</div>~; 
		} else {
			$print_box_content_rows .= qq~<div class="col-md-6"><div class="form-group">
					<label for="imageInputFile">Attach Upload $_</label>
					<input type="file" id="imageInputFile" name='iattachcamera_$_' tabindex='$tindex'>
					<p class="help-block">Upload file from PC.</p>
				</div></div>~;
		}
	}
			
	$print_box_content_rows .= qq~</div>~;
$print_box_content_rows .= &common_min_box_foot ;

#----------------------------------------------------------------- New box --------------------------------------

$print_box_content_rows .= &common_min_box_top('wrench','Replacement',$box_right_bg,$box_icon,'camera-replacement-box') ;
	# $print_box_content_rows .= qq~<div class="row"><div class="col-md-12">&nbsp;</div></div><div class="row">~;
	$fcol=4;
	$print_box_content_rows .= &common_min_form_datepicker('replacement_date',$db{$table}{$id}{replacement_date}) ;
	$print_box_content_rows .= &common_min_form_input('replacement_camera_nr',$db{$table}{$id}{replacement_camera_nr}) ;	
	# $print_box_content_rows .= qq~</div>~;
$print_box_content_rows .= &common_min_box_foot ;

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

sub get_uploads {

my ($id,$type) = @_ ;

unless ($id and $type) { return() ; }

my $gallery = '' ; my %gallery = () ;

my $field = $type ;
if ($db{$table}{$id}{$field}){
    $tooltip = qq(data-toggle="tooltip" data-placement="top" title="$type" data-original-title="$type") ;
    $gallery{$type} .= qq(<li id="$type-$db{$table}{$id}{$field}" class="thumbnail" $tooltip>
            <a style="background:url(/mediastorage/trackusers/$id/$db{$table}{$id}{case_nr}/$db{$table}{$id}{$field})" title="$type" href="/mediastorage/trackusers/$id/$db{$table}{$id}{case_nr}/$db{$table}{$id}{$field}"><img class="grayscale" src="/mediastorage/trackusers/$id/$db{$table}{$id}{case_nr}/$db{$table}{$id}{$field}" alt="$type" style="position:relative;max-height:100%;"></a>
        </li>) ;
    }

if ($gallery{$type}) { $gallery = '<ul class="thumbnails gallery">' . $gallery{$type} . '</ul>' ; }

return ($gallery) ;

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

sub page_opts {

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

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

$alert = qq~<div id="alertbar"></div>~ ;

&common_min_add_extras unless $pixellot_limit{$username} or $events_limit{$username} ;

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

sub report_screen {

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

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

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

&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},'') ;
$print_box_content_rows .= &common_min_form_select('client_id',$i{client_id}) ; 
$print_box_content_rows .= &common_min_form_input('serial_nr','') ;
$print_box_content_rows .= &common_min_form_select('camera_system_id',$i{camera_system_id}) ; 
$print_box_content_rows .= &common_min_form_datepicker('date_received_from','') ;
$print_box_content_rows .= &common_min_form_datepicker('date_received_to','') ;
$print_box_content_rows .= &common_min_form_select('stock','') ;

&common_min_select_opts('quote_nr','cameras','quote_nr','','','','quote_nr>0','','','DISTINCT(quote_nr)') ;

$print_box_content_rows .= &common_min_forms_end('','','report') ;

&common_min_search_screen ;

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

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

1;