#!/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 $isDup=0;
my $uid=0;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    my $json ="";
    while (<STDIN>) {
        $json .= $_;
    }
    my $request = decode_json($json);
    if($request->{"userBc"} && $request->{"userBc"} ne ''){
        ($uid)=$dbh->selectrow_array("select uid from opl_user where userbarcode=?",undef,$request->{"userBc"});
    }
    elsif($request->{"sid"} && $request->{"isid"} ne ''){
        ($uid)=$dbh->selectrow_array("select uid from opl_user where sid=?",undef,$request->{"sid"});
    }
    elsif($request->{"username"} && $request->{"username"} ne ''){
        ($uid)=$dbh->selectrow_array("select uid from opl_user where username=?",undef,$request->{"username"});
    }
    $isDup= 1 if(defined $uid && $uid ne $request->{"uid"});
}

print "Content-type: text/plain\n\n";
print   to_json({isDup=>$isDup});




