#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use JSON;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
);
;



my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $cgi = CGI->new;
my $input = $cgi->Vars();

my $nbList          = getNeiboughtborList($dbh) ;
my $residencyList   = getResidencyList($dbh);
my $nbListJSON = to_json({nbList=>$nbList,residencyList =>$residencyList}, {pretty => 1})  ;

print "Content-type: text/plain\n\n";
print   $nbListJSON;


#-------------------------------------------------------------------------
sub getNeiboughtborList{
    my ($dbh)=@_;
    my $sth =$dbh->prepare("select distinct neighborhood from opl_user 
    where neighborhood is not null order by neighborhood");
    $sth->execute();
    my $ret;
    while( my ($n) =$sth->fetchrow_array){
        push @$ret,{value=>$n,label=>$n};
    }
    
    return $ret;
}

#---------------------------------------------------------------------------
sub getResidencyList{
    my ($dbh)=@_;
    my $sth =$dbh->prepare("select distinct residency from opl_user 
    where residency is not null order by residency");
    $sth->execute();
    my $ret;
    while( my ($n) =$sth->fetchrow_array){
        push @$ret,{value=>$n,label=>$n};
    }
    
    return $ret;
}
#----------------------------------------------------------------------------


