#!/usr/bin/perl

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

&today ;

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

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

foreach $ARG (@ARGV) { ($par,$val) = split(/\=/,$ARG) ; $param{$par} = $val ; }
	
# https://itvadmin.co.za/cgi-bin/scripts/get/get_db_linked_vpu.pl

our $debug = 1 if $param{debug} == 1 ;
	
my $table = 'cameras' ;

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

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

exit unless $param{camera_system_id} ;

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

&get_db_linked_vpu ;

print $json ;

exit ;

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

sub get_db_linked_vpu {
	
	local ($cam_id,$cam_name) = split(/\:/,$param{camera_system_id}) ;
	
	$json = '[' ;
	
	local $linked_vpu_serial_nr = qq~~ ; 
	if ($param{linked_vpu_id}) {
		&db_min_ro('cameras','1,quote_linked_vpu',"id='$param{linked_vpu_id}'",'','') ;
		$linked_vpu_serial_nr = $db{cameras}{1}{quote_linked_vpu} ;
	}
	
	# print " linked_vpu_serial_nr : $linked_vpu_serial_nr" ;
	# &db_min_ro('cameras','id,camera_nr,serial_nr,linked_vpu_id',"(camera_nr LIKE 'vpu%' OR camera_nr LIKE 'S1%' OR camera_nr LIKE 'S2%') AND (quote_linked_vpu IS NULL OR serial_nr = '$linked_vpu_serial_nr') AND event_linked_vpu IS NULL",'','') ; 

	local $sql_where = qq~~ ; local $sql_select = qq~~ ; 
	if ($cam_name eq 'S1') {
		$sql_where = qq~(camera_nr LIKE 'vpuS1%')~ ; 
	}
	elsif ($cam_name eq 'S2') {
		$sql_where = qq~(camera_nr LIKE 'vpuS2%')~ ; 
	}
	else {
		 $sql_where = qq~(camera_nr LIKE 'vpuS1%' or camera_nr LIKE 'vpuS2%')~ ; 
	}
	if ($param{linked_vpu_id}) {
		$sql_select = qq~id,camera_nr,serial_nr,linked_vpu_id,if(id=$param{linked_vpu_id},1,0) as 'selected'~ ;
		$sql_where .= qq~ AND (quote_linked_vpu IS NULL OR id = $param{linked_vpu_id}) AND (event_linked_vpu IS NULL or id = $param{linked_vpu_id})~ ; 
	} else {
		$sql_select = qq~id,camera_nr,serial_nr,linked_vpu_id,0 as 'selected'~ ;
		$sql_where .= qq~ AND quote_linked_vpu IS NULL AND event_linked_vpu IS NULL~ ; 
	}
	
	&db_min_ro('cameras',"$sql_select","$sql_where",'','') ;

	&common_debug("cam_id=$cam_id,cam_name=$cam_name") ;
	
	# foreach (sort keys %{$db{cameras}}) {
		# $is_linked{$db{cameras}{$_}{linked_vpu_id}} = $db{cameras}{$_}{serial_nr} if $db{cameras}{$_}{linked_vpu_id} ;
		# print "\n linked_vpu_id $db{cameras}{$_}{linked_vpu_id} ,serial_nr $db{cameras}{$_}{serial_nr}" ; 
		# &common_debug("camera_nr=$db{cameras}{$_}{linked_vpu_id},linked_vpu_id=$db{cameras}{$_}{linked_vpu_id}") if $db{cameras}{$_}{linked_vpu_id} ;
	# }
	# print "\n $param{linked_vpu_id} , $param{camera_system_id} " ;
	foreach (sort keys %{$db{cameras}}) {
		
		local ($cam_part_1,$cam_part_2) = split(/\-/,$db{cameras}{$_}{camera_nr}) ;
		
		
		# print "\n camera_nr : $db{cameras}{$_}{camera_nr}, $db{cameras}{$_}{selected}" ; 
		
		# &common_debug("camera_nr=$db{cameras}{$_}{camera_nr},linked_vpu_id=$db{cameras}{$_}{linked_vpu_id}") if $db{cameras}{$_}{linked_vpu_id} ;
		# next if substr($db{cameras}{$_}{camera_nr},0,3) ne 'VPU' and $linked_vpu_serial_nr ne $db{cameras}{$_}{camera_nr} ;
		# next if substr($db{cameras}{$_}{camera_nr},3,2) ne 'S1' and substr($db{cameras}{$_}{camera_nr},3,2) ne 'S2' ;
		# next if $cam_name and $cam_name ne substr($db{cameras}{$_}{camera_nr},3,2) ;
		# print "\n camera_nr $db{cameras}{$_}{camera_nr}, is_linked : $is_linked{$_} " ; 
		
		
		# next if $is_linked{$_} and $linked_vpu_serial_nr ne $db{cameras}{$_}{camera_nr} ; 
		# next if substr($cam_part_1,-2,2) ne $cam_name and $cam_name ;
	

		# $sel = 'SELECTED' if $db{$dbtable}{$i{id}}{linked_vpu_id} == $_ or $db{$dbtable}{$_}{quote_linked_vpu} eq $db{cameras}{$id}{serial_nr} ; 
	
		# if (substr($cam_part_1,-2,2) ne 'S1' and substr($cam_part_1,-2,2) ne 'S2')
		# print "\n id:$_,camera_nr:$db{cameras}{$_}{camera_nr},serial_nr:$db{cameras}{$_}{serial_nr}" ;
		# $opts{linked_vpu_id} .= qq~<option value="$_">$db{cameras}{$_}{camera_nr} [$db{cameras}{$_}{serial_nr}]</option>~ ;
		$json .= qq~{"id":"$_","camera_nr":"$db{cameras}{$_}{camera_nr}","serial_nr":"$db{cameras}{$_}{serial_nr}","sel":" $db{cameras}{$_}{selected}"},~ ;
	}
	
	chop $json if $json ;
	$json .= ']' ;
  
} #------------------------------------------------------------------------------------------

use db ;
use today ;
use common ;

1;