#!/usr/bin/perl

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

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
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 ;

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

&page_opts ;

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

print "Content-type: text/html\n\n";

our @all_select_ids = () ; our $minify_jquery = 1 ; 

&db_open_ro ;
	$db_ignore_open_close = 1 ;
		&common_min_action ;	
	$db_ignore_open_close = 0 ;
&db_close_conn ;

exit;

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

sub report_screen {
		
	# # &filter_screen if $ENV{QUERY_STRING} eq 'filter' ;

	# our $lcol = 2 ;
	# our $fcol = 5 ;

	# # my ($set_year,$set_month,$set_day) = Add_Delta_Days($now_year,$now_mm,$now_dd,-42) ; # 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 ;
	
	# &select_opts ;
	
	$print_box_content_rows .= &common_min_forms_start('report') ;
	
	# # &common_min_select_opts('customer_name','customers','name','','','','') ;
	# $print_box_content_rows .= &common_min_form_select('camera_system','') ;
	# $print_box_content_rows .= &common_min_form_input('serial_number','') ;
	# $print_box_content_rows .= &common_min_form_input('part_number','') ;
	# # $print_box_content_rows .= &common_min_form_datepicker('quotes_from','') ; # $set_ccyy_dd_mm
	# # $print_box_content_rows .= &common_min_form_datepicker('quotes_to','') ; # $now_ccyy_mm_dd
	# # # $print_box_content_rows .= &common_min_form_input('po_nr','') ;
	# # $print_box_content_rows .= &common_min_form_input('invoice_nr','') ;
	# # &common_min_select_opts('country','countries','name','','','','') ;
	# # $print_box_content_rows .= &common_min_form_select('country','') ;
	# # $print_box_content_rows .= &common_min_form_checkbox('accepted','','') ; # Display all event_quotes that is Accepted
	# # $print_box_content_rows .= &common_min_form_checkbox('closed','','') ; # Display all event_quotes that is Cancelled
	
	# # &common_min_select_opts('type','event_types','name','','','','') ;
	# # $print_box_content_rows .= &common_min_form_select('type','') ;
	
	# # &common_min_select_opts('region_id','regions','name','','','','');
	
	# # &common_min_select_opts('city_id','cities','city','','','','');
	# # &common_min_select_opts('organisation_ids','organisations','name','','','','') ;
	# # &common_min_select_opts('sport_type_ids','sport_types','name','','','','') ;
	
	# # $preferred_title{region_id} = "Region" ;
	# # $preferred_title{city_id} = "City" ;
	# # $preferred_title{organisation_ids} = "Venue" ;
	# # $preferred_title{sport_type_ids} = "Sport Type" ;
	
	# # $print_box_content_rows .= &common_min_form_select('region_id','') ;
	# # $print_box_content_rows .= &common_min_form_select('city_id','') ;
	# # $print_box_content_rows .= &common_min_form_select('organisation_ids','') ;
	# # $print_box_content_rows .= &common_min_form_select('sport_type_ids','') ;
	
	$print_box_content_rows .= &common_min_forms_end('','','report') ;

	&common_min_search_screen ;

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

sub report_ifields {
	
} #-------------------------------------------------------------------------------

sub validate {

# my $exists   = '' ;

# $i{name} = lc $i{name} ;
# $i{name} =~ s/ //gi ;

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

# foreach my $id (keys %{$db{$table}}) { if ($db{$table}{$id}{name} eq $i{name}) { $exists = 1 ; } }

# if ($exists) {
    # $alert = &common_min_alert('warning',"NAME ALREADY EXISTS!",'ok') ;
	# %col_name = ();
    # &common_min_add_screen;
    # &common_min_screen1;
    # }

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

sub insert {

&add_db_fields ;

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

&process_table ;

&defualt_values ;

foreach (keys %i) {
	$ignore{$_} = 1 unless $i{$_} ;
}

&db_min_insert($table) ;

# for (keys %i) {
	# print "\n $_ : $i{$_}" ;
# }

# exit ;

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

sub update {

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

&db_min_ro('rma',"*","id='$i{id}'","","") ; 

&edit_db_fields ;

# unless ($i{block}) { $i{block} = '0' ; }	
# $i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

&process_table ;

&defualt_values ;

foreach (keys %{$db{rma}{$i{id}}}) {
	$ignore{$_} = 1 if $db{rma}{$i{id}}{$_} eq $i{$_}
}

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

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

sub defualt_values {
	
	$i{rma_status_id} 		= 0 unless $i{rma_status_id} ; 
	$i{customer_id} 		= 0 unless $i{customer_id} ; 
	$i{camera_system_id}	= 0 unless $i{camera_system_id} ;
	
} #------------------------------------------------------------------------------------------

sub process_table {
	
	for (1 .. 5) {
		$ignore{"support_fault_$_"} = 1 ;  
		$ignore{"date_$_"} = 1 ;
		$i{support_faults} .= qq~$i{"support_fault_$_"};~ ; 
		$i{support_fault_dates} .= qq~$i{"date_$_"};~ ; 
	}
	chop $i{support_faults}	if $i{support_faults} ;
	chop $i{support_fault_dates} if $i{support_fault_dates} ;
	
} #------------------------------------------------------------------------------------------

sub list_screen {

	# my %seen_camera  = () ; 

	# &db_min_ro('event_quotes','id, camera_system_id','','','') ; 

	# foreach my $id (keys %{$db{'event_quotes'}}) {
		# next if $seen_camera{$db{'event_quotes'}{$id}{camera_system_id}} ; 
		# $seen_camera{$db{'event_quotes'}{$id}{camera_system_id}} = 1 ; 
	# }	

	# &db_min_ro('quotes','id, camera_system_id','','','') ; 

	# foreach my $id (keys %{$db{'quotes'}}) {
		# next if $seen_camera{$db{'quotes'}{$id}{camera_system_id}} ; 
		# $seen_camera{$db{'quotes'}{$id}{camera_system_id}} = 1 ; 
	# }

	&load_data ;

	our @sql_col_display = ("date_received","client","system","serial_nr","fault_reported","RMA_date","status","date_supplier_returned","date_returned_client","replacement_date","replacement_serial") ;

	our $xlsx_title_heading = "RMA Report" ;
	our $xlsxdir            = "rma_report" ; 
	our $xlsxreportname     = "RMA_Report" ; 

	&report_xlsx_export_header("$xlsxreportname",$xlsxdir,'',$xlsx_title_heading) ;
	$xlsxrow-- ;
	
	foreach my $id (keys %{$db{$table}}) {
		my $disp_blocked = qq(<span class="label label-success" title="Active!" data-toggle="tooltip"><i class="glyphicon glyphicon-ok"></i></span>); if ($db{$table}{$id}{block}) { $disp_blocked = qq(<span class="label label-danger" title="Blocked!" data-toggle="tooltip"><i class="glyphicon glyphicon-ok"></i></span>); }
		
		# $opts{$lcpage} .= qq~<option value="$db{$table}{$id}{username}">$db{$table}{$id}{name}</option>~ ;
		
		my $edit_butt = '' ; my $del_butt = '' ;
		
		if ($useropts{super}{$username}) { 
			$edit_butt = qq~&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$db{$table}{$id}{id}');"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ; 
		}	
		
		if ($useropts{boss}{$username} or $useropts{super}{$username}) { 
			$del_butt  = qq~&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{name}','$db{$table}{$id}{id}')"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ if not $seen_camera{$id} ; 
			$del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title data-toggle="tooltip" data-original-title="In system use!"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ if $seen_camera{$id} ; 
		}
		
		$print_tbody .= qq~<tr id="$id">~ ;
		
		$xlsxcol = 0 ; 
		
		foreach (@sql_col_display) {
			
			my $val = $db{$table}{$id}{$_} ;
			my $val_min = '' ; 
			my $formatting = $format84 ;
			
			if ($_ eq 'client') {
				$val = $db{customers}{$db{$table}{$id}{customer_id}}{name} ;
			} elsif ($_ eq 'system') {
				$val = $db{camera_systems}{$db{$table}{$id}{camera_system_id}}{name} ;
				$val .= " [" . $db{camera_systems}{$db{$table}{$id}{camera_system_id}}{description} . "]" if $db{camera_systems}{$db{$table}{$id}{camera_system_id}}{description} ;
			} elsif ($_ eq 'fault_reported') {
				# if ($db{$table}{$id}{replacement}) {
					# $val = &set_tick("green") ; 
				# } else {
					# $val = &set_tick("red") ; 
				# }
				
			} elsif ($_ eq 'RMA_date') {
			
			} elsif ($_ eq 'status') {
				$val = $db{rma_status}{$db{$table}{$id}{rma_status_id}}{status}
			} elsif ($_ eq 'date_supplier_returned') {
				
			} elsif ($_ eq 'date_returned_client') {
			
			} elsif ($_ eq 'replacement_date') {
				
			} elsif ($_ eq 'replacement_serial') {
			
			}
			
			if (!$val_min) {
				$val_min = $val ; 
			}
			
			&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val_min,$formatting) ;
			$xlsxcol++ ;
			
			$print_tbody .= qq~<td class="dt-center">$val</td>~ ;
			
		}
		
		$print_tbody .= qq~<td nowrap>$edit_butt$del_butt</td></tr>~ ;
		
		$xlsxrow++ ;
		
		# <td>$id</td>
		# <td>$db{$table}{$id}{name}</td>
		# <td>$db{$table}{$id}{description}</td>
		# <td nowrap>
			# $edit_butt
			# $del_butt
		# </td>
	# </tr>~ ;
	}
	
	&report_xlsx_export_footer('K',15,$xlsxdir) ;

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

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-remove"></i></span>~ ;

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

sub load_data {
	
	&db_min_ro('camera_systems',"*",'','','') ;
	&db_min_ro('customers',"id,name",'','','') ;
	&db_min_ro('rma_status',"*",'','','') ;
	# &db_min_ro('camera_systems',"*",'','','') ;
	# &db_min_ro('camera_systems',"*",'','','') ;
	# &db_min_ro('camera_systems',"*",'','','') ;
	&db_min_ro($table,'*','','','') ;
	
} #------------------------------------------------------------------------------------------

sub add_db_fields {
	
&hidden_db_fields;

$ignore{id} = 1 ;

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

sub edit_db_fields {
	
&hidden_db_fields;

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

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

sub hidden_db_fields {
	
$ignore{iaction}        = 1 ;
$ignore{date_received}	= 1 ;

# $readonly{name}       = 'READONLY' ;
# $required{name}         = 1 ;
# $required{description}  = 1 ;

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

sub add_screen {
	
our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;
# called from common_add_screen

&select_opts ;

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

&build_boxes_edit ;

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

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

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) = @_ ;

$allow_deselect{"camera_system_id"} = 1 ;
$allow_deselect{"part_number"} = 1 ;
$allow_deselect{"customer_id"} = 1 ;
$allow_deselect{"rma_status_id"} = 1 ; 

$print_box_content_rows .= &common_min_forms_start($table) ;
$print_box_content_rows .= qq~<div class='row'><div class='col-md-6'>~ ;
	&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  .= ($id) ? &common_min_forms_end($id,$table,'update',$skip) : &common_min_forms_end($id,$table,'save',$skip) ;

my $all_select_ids_str = join(",",@all_select_ids) ;

$trigger_jquery_raw .= qq~\$("$all_select_ids_str").chosen({allow_single_deselect:true});~ ; 

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

sub build_boxes_left {

	my ($id) = @_ ;

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

# Camera System (dropdown pull from database)
# Serial Number (enter nr and pull from database)
# Part Number (pull from database)
# System Name (pull from database)
# Warranty period -  From Date to Date   (Show date from ‘Date Received’ database from camera page – plus add 15 months from this date which then gets displayed)  -  this is the automatic warranty period for all system
	
	$preferred_title{serial_nr} = "Serial Number" ;
	
	$preferred_title{warranty_start} = "Warranty Period" ;
	
	
	$add_form_fields .= &common_min_form_select('camera_system_id') ; 
	
	if ($db{$table}{$id}{camera_system_id}) {	
		$trigger_jquery_raw .= qq~
			\$("#selectCamera_system_id").val($db{$table}{$id}{camera_system_id}) ;
			\$("#selectCamera_system_id").trigger("chosen:updated") ;
		~ ;	
	}
	
	$add_form_fields .= &common_min_form_input('serial_nr',$db{$table}{$id}{serial_nr}) ; 
	$add_form_fields .= &common_min_form_select('part_number') ; 
	
	if ($db{$table}{$id}{part_number}) {	
		$trigger_jquery_raw .= qq~
			\$("#selectPart_number").val($db{$table}{$id}{part_number}) ;
			\$("#selectPart_number").trigger("chosen:updated") ;
		~ ;	
	}
	
	$add_form_fields .= &common_min_form_input('system_name',$db{$table}{$id}{system_name}) ; 
	$readonly{warranty_start} = "READONLY" ;
	
	# my $a = &common_min_form_input('warranty_start','','','',1) ; 
	$readonly{warranty_end} = "READONLY" ;
	my $default_date = "$now_ccyy_mm_dd" ;
	my $warranty_start = $db{$table}{$id}{warranty_start} ;
	$warranty_start = $default_date unless $warranty_start ;
	
	my $warranty_end = $db{$table}{$id}{warranty_end} ;
	$warranty_end = $default_date unless $warranty_end ;
	
	$add_form_fields .= &common_min_form_input('warranty_start',$warranty_start,'','',1) ; 
	
	$add_form_fields .= qq~
		<div class='fieldname col-md-1'>
			<label class='control-label' for='inputPart_number'>To</label>
		</div>~ ;
	
	$add_form_fields .= &common_min_form_datepicker_col('warranty_end',$warranty_end) ;
	$add_form_fields .= qq~</div>~ ;
	
	# $add_form_fields .= qq~</div>~ ;
	# $add_form_fields .= qq~</div>~ ;
	$print_box_content_rows .= &common_min_box_top('camera','&nbsp;Camera&nbsp;Details',$box_right_bg); #film
	$print_box_content_rows .= $add_form_fields ;                
	$print_box_content_rows .= &common_min_box_foot;

	$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=4 ;  

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

# Client  (select from Dropdown)
# Date Received  (capture date it was received by ITV warehouse)
# Client Fault Note –a note box where they can note what client says is faulty
# Support Fault Report:  -  Note box they can enter what support has found and a date field  (to show progress the support team
# ADD button to add more Note box and date fields if there is more work to be done and keep track of dates of each note added
# Remarks
	
	$preferred_title{customer_id} = "Client" ;
	$add_form_fields .= &common_min_form_select('customer_id') ; 
	
	if ($db{$table}{$id}{customer_id}) {	
		$trigger_jquery_raw .= qq~
			\$("#selectCustomer_id").val($db{$table}{$id}{customer_id}) ;
			\$("#selectCustomer_id").trigger("chosen:updated") ;
		~ ;	
	}
	
	$add_form_fields .= &common_min_form_textarea('client_fault_note',$db{$table}{$id}{client_fault_note}) ; 
	$readonly{date_received} = "READONLY" ;
	# $default_date = "$now_ccyy_mm_dd 00:00:00" ;
	my $date_received = $db{$table}{$id}{date_received} ;
	$date_received = $default_date unless $date_received ;
	$add_form_fields .= &common_min_form_datepicker('date_received',$date_received) ; 
	
	my %disp_sn_style = () ;  

	$lcol=2 ;
	
	my @a = split(/;/,$db{$table}{$id}{support_faults}) ; 
	my @b = split(/;/,$db{$table}{$id}{support_fault_dates}) ; 
	
	for (2 .. 5) {
		$disp_sn_style{$_} = qq~style="display:none;"~ unless $a[$_-1] ;
		
	}
	
	$default_date = "$now_ccyy_mm_dd" ;
	
	for (1 .. 5) {
		$add_form_fields .= qq~
			<div class='row' id="sn_row_$_" $disp_sn_style{$_}>
				<div class='col-md-12'>~ ; 
					my $col1 = "support_fault_$_" ;
					# $required{$col} = 1 ;
					&common_min_forms_required ; 
					$add_form_fields .= &common_min_form_input($col1,$a[$_-1],'','',1) ; 
					$add_form_fields .= qq~
						<div class='fieldname col-md-1'>
							<label class='control-label' for='inputPart_number'>Date $_</label>
						</div>
			~ ;
			
		
		my $date = $b[$_-1] ;
		$date = $default_date unless $b[$_-1] ;
		
		my $col2 = "date_$_" ;
		$readonly{$col2} = "READONLY" ;
		$add_form_fields .= &common_min_form_datepicker_col($col2,$date) ;   		
		# $fcol=3 ;
		$add_form_fields .= qq~
					</div>~;
		
		$add_form_fields .= qq~<a id="btn_serial_nr_$_" href="#" style="padding-left:16px;clear:both;"><i class="glyphicon glyphicon-plus blue"></i></a>~ if $_ < 5 ;	
		
		$trigger_jquery_raw .= qq~\$("#btn_serial_nr_$_").css("display","none");~ if ($a[1] and $_ == 1) or ($a[$_] and $a[$_-1]) ;

		$trigger_jquery_raw .= qq~ 
			\$("#btn_serial_nr_$_").click(function () {
				let v = $_ + 1 ;			
				\$("#btn_serial_nr_$_").hide() ;	
				\$("#btn_serial_nr_"+v).show() ;
				\$("#sn_row_"+v).show() ;	
				let v2 = $_ + 2 ;	
				if (\$("#sn_row_"+v2).css("display") !== "none") { \$("#btn_serial_nr_"+v).hide() ; }		
			}) ;
		~ ;
		
		$add_form_fields .= qq~		
				</div>
			</div>
		~ ;	
		
	}
	
	# $default_date = "$now_ccyy_mm_dd 00:00:00" ;
	
	$lcol = 2 ;
	# $add_form_fields .= qq~
	# <div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
	$add_form_fields .= qq~
	<div class='row'>
		<div class='col-md-12'>~ ; 
		$preferred_title{remarks_1} = "Remarks" ;
		$add_form_fields .= &common_min_form_textarea('remarks_1',$db{$table}{$id}{remarks_1}) ; 
		$add_form_fields .= qq~
		</div>
	</div>~ ;
	
	$print_box_content_rows .= &common_min_box_top('user','&nbsp;Client&nbsp;Details',$box_right_bg); #film
	$print_box_content_rows .= $add_form_fields ;                
	$print_box_content_rows .= &common_min_box_foot;

	$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 --------------------------------------
	
&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'");

$lcol = 3 ; $fcol = 4 ;
$readonly{rma_date_requested} = "READONLY" ;$readonly{rma_accepted} = "READONLY" ; $readonly{date_returned} = "READONLY" ; $readonly{date_to_supplier} = "READONLY" ;
$preferred_title{rma_status_id} = "Status" ; $preferred_title{remarks_2} = "Remarks" ;	

my $rma_date_requested = $db{$table}{$id}{rma_date_requested} ;
$rma_date_requested = $default_date unless $rma_date_requested ;

my $rma_accepted = $db{$table}{$id}{rma_accepted} ;
$rma_accepted = $default_date unless $rma_accepted ;

my $date_to_supplier = $db{$table}{$id}{date_to_supplier} ;
$date_to_supplier = $default_date unless $date_to_supplier ;

my $date_returned = $db{$table}{$id}{date_returned} ;
$date_returned = $default_date unless $date_returned ;

$print_box_content_rows .= &common_min_box_top('list-alt','&nbsp;RMA&nbsp;Details',$box_right_bg,$box_icon,'') ; #qrcode

	$print_box_content_rows .= &common_min_form_datepicker('rma_date_requested',$rma_date_requested) ;
	$print_box_content_rows .= &common_min_form_input('ticket_nr',$db{$table}{$id}{ticket_nr}) ;
	$print_box_content_rows .= &common_min_form_datepicker('rma_accepted',$rma_accepted) ; 
	$print_box_content_rows .= &common_min_form_textarea('rma_rejected',$db{$table}{$id}{rma_rejected}) ; 
	$print_box_content_rows .= &common_min_form_datepicker('date_to_supplier',$date_to_supplier) ;	
	$print_box_content_rows .= &common_min_form_select('rma_status_id') ;
	$print_box_content_rows .= &common_min_form_textarea('remarks_2',$db{$table}{$id}{remarks_2}) ;
	$print_box_content_rows .= &common_min_form_datepicker('date_returned',$date_returned) ; 

$print_box_content_rows .= &common_min_box_foot ;

if ($db{$table}{$id}{rma_status_id}) {	
	$trigger_jquery_raw .= qq~
		\$("#selectRma_status_id").val($db{$table}{$id}{rma_status_id}) ;
		\$("#selectRma_status_id").trigger("chosen:updated") ;
	~ ;	
}	

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

$readonly{replacement_date} = "READONLY" ; $fcol=4;

my $replacement_date = $db{$table}{$id}{replacement_date} ;
$replacement_date = $default_date unless $replacement_date ;

$print_box_content_rows .= &common_min_box_top('wrench','&nbsp;Replacement',$box_right_bg,$box_icon,'camera-replacement-box') ;

	$print_box_content_rows .= &common_min_form_checkbox('replacement',$db{$table}{$id}{replacement},'',1) ;
	$print_box_content_rows .= &common_min_form_datepicker('replacement_date',$replacement_date) ;
	$print_box_content_rows .= &common_min_form_input('replacement_camera_nr',$db{$table}{$id}{replacement_camera_nr}) ;	
	
	$print_box_content_rows .= qq~
		<input name="replacement" id = "inputReplacement" value="1" style="display:none;">
	~ ;
	
	# $trigger_jquery_raw .= qq~
		# \$("#checkboxReplacement").on("change",function () { 
			# console.log("checkboxReplacement") ;  

		# }) ;
	# ~ ;
	
$print_box_content_rows .= &common_min_box_foot ;

$trigger_jquery .= qq~
	\$('.iPhoneCheckLabelOff').css("top","-5px") ;
	\$('.iPhoneCheckLabelOn').css("top","-5px") ;
~ ; 

if ($db{$table}{$id}{replacement}) {
	$trigger_jquery_raw .= qq~
		\$("#checkboxReplacement").prop('checked',true) ;
	~ ;			
} 

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

sub select_opts {

my ($id) = @_ ;

&db_min_ro($table,'*',"`id`='$id'",'','') if $id ;

$select{camera_system_id} 	= 1	;
$select{customer_id} 		= 1	;

&common_min_select_opts('camera_system_id','camera_systems','name','','','description','','','','','name') ;
&common_min_select_opts('customer_id','customers','name','','','','','','',"id,name",'name') ;
&common_min_select_opts('rma_status_id','rma_status','status','','','','','','','','status') ;

$trigger_jquery_raw .= qq~	
	\$("#savebutt").click(function() {
		if (\$("#checkboxReplacement").is(":checked")) {
			\$("#inputReplacement").val("1") ;
		} else {
			\$("#inputReplacement").val("0") ;
		}
		\$('#rma-form').submit() ;
	}); 
~ ;

# $required{based_in} = 1 ;
# $select{based_in} = 1 ;
# $opts{based_in} = qq~<option value="JNB" $selected{based_in}{'JNB'}>Johannesburg</option><option value="CPT" $selected{based_in}{'CPT'}>Cape Town</option>~ ;
	
} #------------------------------------------------------------------------------------------

sub thead {

# $print_thead = qq~<THEAD><TR><TH>ID</TH><TH>NAME</TH><TH>DESCRIPTION</TH><TH>&nbsp;</TH></TR></THEAD>~ ;
    
&common_min_thead ;
	
} #-------------------------------------------------------------------------------

sub page_opts {

our $glyphicon = 'list' ;
our $lcpage    = 'rma' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
our $table     = 'rma' ;

$page_title  = 'RMA' ;

$s{no} = 1 ; ### ????????????????????????????????????????????????????????????
&common_min_add_extras ;

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

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

1;