#!/usr/bin/perl

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

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);
use Opals::User qw(
    user_getInformationById
    user_getFamilyMember

);

use Time::localtime;


my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);

    my $uid = -1;
    my $errUser = 0;
    $uid=$input->{'uid'};
    my $userInfo = getUser($dbh, $uid) ;
    if(!defined $userInfo->{'country'}|| $userInfo->{'country'} eq ''){
        $userInfo->{'country'}=Opals::Context->preference('country');
    }
    my $userJSON = to_json($userInfo, {pretty => 1})  ;

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



#-------------------------------------------------------------
sub getUser{
    my ($dbh,$uid)=@_;
    my ($user,$guardianList) =user_getInformationById($dbh, $uid);
    my $fmList=user_getFamilyMember($dbh,$uid);
    $user->{'familyMembers'}= $fmList->{'direct'};
    $user->{'familyMembers_byRef'}= $fmList->{'byRef'};
    $user->{'guardianList'} = $guardianList;
    #$user->{'contactList'}  = user_getContactList($dbh,$uid);
    return $user;

}



