#!/usr/bin/perl

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

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;

&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";

&common_min_action;

exit;

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

sub validate {

	my $exists   = '' ;

	# $i{name} = lc $i{name} ;
	$i{name} =~ s/ /\_/g ;

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

	foreach my $id (keys %{$db{$table}}) { 
		if (lc $db{$table}{$id}{name} eq lc $i{name} && (!$i{id} || ($i{id} && $i{id} ne $id))) { 
			$exists = 1 ; 
		} 
	}

	if ($exists) {
		$alert = &common_min_alert('warning',"'$i{name}' AS A SYSTEM 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" ;
	
	$i{id} = &db_min_get_max('event_systems','id') ;
	
	&db_min_insert($table) ;

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

sub update {

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

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

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

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

sub get_linked_systems {
	
	my $add_sql_where = ($i{id}) ? " AND event_system_id_multiple LIKE '%$i{id}%'" : '' ;
	
	&db_min_ro('event_quotes','id,ref,event_system_id_multiple',"event_system_id_multiple <> ''$add_sql_where",'','') ;
	foreach my $id (sort keys %{$db{event_quotes}}) { 
		foreach my $event_system_id (split(";",$db{event_quotes}{$id}{event_system_id_multiple})) {
			# $linked_cameras{$event_system_id} .= qq~<span class="label label-success" title="$db{event_quotes}{$id}{ref}" data-toggle="tooltip">$id</span>&nbsp;~ ;
			$linked_events{$event_system_id} .= qq~<span title="$db{event_quotes}{$id}{ref}" data-toggle="tooltip">$id</span> ~ ;
		}
	}
	
	$add_sql_where = ($i{id}) ? "AND event_system_id = '$i{id}'" : '' ;

	&db_min_ro('cameras','id,camera_nr,event_system_id,event_linked_vpu,serial_nr,linked_vpu_id',"event_system_id > 0 $add_sql_where",'','') ;
	foreach my $id (sort keys %{$db{cameras}}) { 
	
		# if ($db{cameras}{$id}{event_system_id} || $event_linked_vpu{$db{cameras}{$id}{serial_nr}}) { 
			# # &common_debug("1. event_system_id=$db{cameras}{$id}{event_system_id} [serial_nr=$db{cameras}{$id}{serial_nr}] [event_linked_vpu=$event_linked_vpu{$db{cameras}{$id}{serial_nr}}]") ;
			# $linked_cameras{$db{cameras}{$id}{event_system_id}} .= qq~<span class="label label-success" title="$db{cameras}{$id}{serial_nr}" data-toggle="tooltip">$db{cameras}{$id}{camera_nr}</span>&nbsp;~ unless $done_cam{$db{cameras}{$id}{camera_nr}};
			# $event_linked_vpu{$db{cameras}{$id}{event_linked_vpu}} = $db{cameras}{$id}{event_system_id} ; 	
			# $done_cam{$db{cameras}{$id}{camera_nr}} = 1 ;
		# }
		
		# if ($event_linked_vpu{$db{cameras}{$id}{serial_nr}}) { 
			# # &common_debug("1. event_system_id=$db{cameras}{$id}{event_system_id} [serial_nr=$db{cameras}{$id}{serial_nr}] [event_linked_vpu=$event_linked_vpu{$db{cameras}{$id}{serial_nr}}]") ;
			# $linked_cameras{$event_linked_vpu{$db{cameras}{$id}{serial_nr}}} .= qq~<span class="label label-success" title="$db{cameras}{$id}{serial_nr}" data-toggle="tooltip">$db{cameras}{$id}{camera_nr}</span>&nbsp;~ unless $done_cam{$db{cameras}{$id}{camera_nr}};
			# $done_cam{$db{cameras}{$id}{camera_nr}} = 1 ;
		# }
		
		# if ($db{cameras}{$id}{event_linked_vpu}) { 
			# &common_debug("2. $db{cameras}{$id}{event_linked_vpu}") ;
			
		# }		
		
		unless ($done_cam{$db{cameras}{$id}{camera_nr}}) {
			$linked_cameras{$db{cameras}{$id}{event_system_id}} .= qq~<span class="label label-success" title="$db{cameras}{$id}{serial_nr}" data-toggle="tooltip">$db{cameras}{$id}{camera_nr}</span>&nbsp;~ ;
			$linked_cameras{$db{cameras}{$id}{event_system_id}} .= qq~<span class="label label-success" title="$linked_vpu_camera_nr{$_}" data-toggle="tooltip">$linked_vpu_camera_nr{$_}</span>~ if $linked_vpu_camera_nr{$_} ;
		}		
		$done_cam{$db{cameras}{$id}{camera_nr}} = 1 ;
	}
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	&db_open_ro;
		$db_ignore_open_close=1;
	
		&db_min_ro('cameras','id,camera_nr',"event_system_id > 0 AND camera_nr LIKE 'VPU%'",'','') ;
		local %linked_vpu_camera_nr = () ; 
		foreach (%{$db{cameras}}) {
			$linked_vpu_camera_nr{$_} = $db{cameras}{$_}{camera_nr} ;
		}
		
		&db_min_ro('logistics_locations','id,location','','','') ;
		
		&get_linked_systems;

		&db_min_ro('event_cost_items','id,name',"excl_from_expenses = '0'",'','') ;
		
		&db_min_ro($table,'*','','name','') ;
		
		$db_ignore_open_close=0 ;
	&db_close_conn ;

	foreach my $id (keys %{$db{$table}}) {
		
		my $edit_butt = '' ; my $del_butt = '' ;
		
		# if ($useropts{super}{$username}) { 
		if ($glod_user_level >= 5) { 
			$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}) { 
		if ($glod_user_level >= 5) { 
			$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>~ ; 
		}
			
		# &common_debug("$id : $db{cameras}{$id}{event_system_id}") ;
			
		# if ($db{cameras}{$id}{event_system_id}) { 
		if ($linked_cameras{$id} || $linked_events{$id}) { 
			# $edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="System in use!" data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
			$del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="System in use!" data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;  
		}
		
		$print_tbody .= qq~
			<tr id="$id">
				<td>$id</td>
				<td>$db{$table}{$id}{name}</td>
				<td>$db{$table}{$id}{description}</td>
				<td>$db{$table}{$id}{system_type}</td>
				<td>$db{logistics_locations}{$db{$table}{$id}{logistics_location_id}}{location}</td>
				<td>$db{event_cost_items}{$db{$table}{$id}{event_cost_item_id}}{name}</td>
				<td>$linked_cameras{$id}</td>
				<td>$linked_events{$id}</td>
				<td nowrap>
					$edit_butt
					$del_butt
				</td>
			</tr>
		~ ;
	}

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

sub add_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

	# $ignore{id} = 1 ;

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

sub edit_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

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

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

sub hidden_db_fields {
	
	$ignore{iaction}        = 1 ;

	# $readonly{name}       = 'READONLY' ;
	$required{name}         = 1 ;
	$required{description}  = 1 ;
	$required{logistics_location_id} = 1 ;
	$preferred_title{logistics_location_id} = "Location" ;
	$select{logistics_location_id} = 1 ;
	$select{event_cost_item_id} = 1 ;
	$required{event_cost_item_id} = 1 ;
	
} #------------------------------------------------------------------------------------------

sub sort_fields {
	
	%sort_field = () ;

	$sort_field{1}  = 'name' ;
	$sort_field{2}  = 'description' ;
	$sort_field{3}  = 'system_type' ;
	$sort_field{4}  = 'logistics_location_id' ;
	$sort_field{5}  = 'event_cost_item_id' ;

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

sub add_screen {

	# called from common_add_screen

	&select_opts('') ;

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

sub edit_screen {
	
	&get_linked_systems;
	
	$readonly{name} = 'READONLY' if $linked_cameras{$i{id}} || $linked_events{$i{id}} ;

	&select_opts($i{id}) ;

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

sub select_opts {
	
	my ($id) = @_ ; 
	
	$selected{system_type}{$db{$table}{$id}{system_type}} = 'SELECTED' ;
	$required{system_type} = 1 ;
	$select{system_type} = 1 ;
	$opts{system_type} = qq~
		<option value="fixed" $selected{system_type}{'fixed'}>Fixed</option>
		<option value="mobile" $selected{system_type}{'mobile'}>Mobile</option> 
		<option value="solo" $selected{system_type}{'solo'}>Solo</option>
	~ ;
	&common_min_select_opts('logistics_location_id','logistics_locations','location',$db{$table}{$id}{logistics_location_id},'','');
	
	&common_min_select_opts('event_cost_item_id','event_cost_items','name',$db{$table}{$id}{event_cost_item_id},'','',"(name LIKE 'Mobile%' OR name LIKE '% Mobile%' OR name LIKE 'Fixed%' OR name LIKE 'Cricket%' OR name LIKE '% Cricket%') AND excl_from_expenses = '0'");
 
	$trigger_jquery_raw .= qq~
		// \$("#selectEvent_cost_item_id").chosen({allow_single_deselect:true}) ;
	~ ; 
	
	if ($i{name}) {
		$trigger_jquery_raw .= qq~
			\$("input[name='name']").val("$i{name}") ;
		~ ;
	}
	if ($i{description}) {
		$trigger_jquery_raw .= qq~
			\$("input[name='description']").val("$i{description}") ;
		~ ;
	}
	if ($i{system_type}) {
		$trigger_jquery_raw .= qq~
			\$("#selectSystem_type").val("$i{system_type}").trigger("chosen:updated") ;
		~ ;
	}
	
	if ($i{system_type}) {
		$trigger_jquery_raw .= qq~
			\$("#selectLogistics_location_id").val("$i{logistics_location_id}").trigger("chosen:updated") ;
		~ ;
	}
	
	if ($i{event_cost_item_id}) {
		$trigger_jquery_raw .= qq~
			\$("#selectEvent_cost_item_id").val("$i{event_cost_item_id}").trigger("chosen:updated") ;
		~ ;
	}
	
	# $selected{logistics_location_id}{$db{$table}{$id}{logistics_location_id}} = 'SELECTED' ;

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

sub thead {

	$print_thead = qq(
		<THEAD>
			<TR>
				<TH>ID</TH>
				<TH>NAME</TH>
				<TH>DESCRIPTION</TH>
				<TH>TYPE</TH>
				<TH>LOGISTICS_LOCATION</TH>
				<TH>EVENT_COST_ITEM</TH>
				<TH>LINKED_CAMERAS</TH>
				<TH>LINKED_EVENTS</TH>
				<TH>&nbsp;</TH>
			</TR>
		</THEAD>
	) ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'facetime-video' ;
	our $lcpage    = 'event-system' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
	our $table     = 'event_systems' ;
	our $page_title = 'Event Systems' ;
	
	&common_min_add_extras ;

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

use common_min ;
use common ;
use today ;

1;