#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

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

use JSON;
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $input= getRequest();
my $cgi = CGI->new;
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ajax/user/saveUserNote.tmpl',
        }
);



my $noteType    = $input->{'noteType'};
my $uid         = $input->{'uid'};
my $userNote    = $input->{'notes'};
my $privateNotes= $input->{'privateNotes'};
my @fields=();
my @data=();
if($noteType eq 'notes' ){
    push @fields,"notes=?"; 
    push @data,$userNote; 
}
if($noteType eq 'privateNotes' ){
    push @fields,"privateNotes=?" ;
    push @data,$privateNotes; 
}
push @data,$uid;
$dbh->do("update opl_user set " . join(",",@fields) ." where uid=?",undef,@data);

print "Content-type: text/plain\n\n";
print  to_json({uid=>$uid, notes=>$userNote,privateNotes=>$privateNotes},{pretty=>1});

#-------------------------------------------------------------------------------
sub getRequest{
  my $fineList=[];
  my $in={};
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
        my $json ="";
        while (<STDIN>) {
            $json .= $_;
        }
        $in = decode_json($json);
   }
   return $in;
}

