#!/usr/bin/perl

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

use Opals::Context;
use Opals::BackgroundJobs qw(
    bgjob_create
    bgjob_execute
    bgjob_getStatus
    bgjob_getOutput
    bgjob_process
);
use Opals::BarcodeMgmt qw(
    bcm_validateBc
);

use Opals::Date qw(date_now);


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

my $validateBc = Opals::Context->preference('validateBarcode');
my $barcodeType = Opals::Context->preference('barcodeType');


my $input=getJobInfo();
my $cgi     = CGI->new;
my $status=1;

my $newLoc              =$input->{'newLoc'};
my $resetLocOnReturn    =$input->{'resetLocOnReturn'};
   $resetLocOnReturn=1 if(!defined $resetLocOnReturn);
my $bcList              =$input->{'bcList'};
my $shelvingType        =$input->{'shelvingType'};



if($shelvingType eq 're-shelf'){
    reshelfItemOnCart($dbh,$bcList);
}

elsif($shelvingType eq 'moveShelfLoc' || $shelvingType eq 'revertShelfLoc' ){
    my $jobInfo = {};
    my $itemList=getJobItemList($dbh,$bcList);
    $jobInfo->{"type"}=$shelvingType;
    if($shelvingType eq 'moveShelfLoc' ){
        $jobInfo->{"newLocation"}=$newLoc;
        createTmpShelf($dbh,$newLoc,$resetLocOnReturn);
    }
    elsif($shelvingType eq 'revertShelfLoc' ){
        $jobInfo->{"reqDate"}=date_now();
    }
    my ($jId,$jItemCount)= bgjob_create($dbh,to_json($jobInfo),$itemList);
    bgjob_process($dbh,$jId);


}

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

my $rs={status=>$status};
print   to_json($rs);

#
#-------------------------------------------------------------------------------
sub reshelfItemOnCart{
    my ($dbh,$bcList)=@_;
    my $sth=$dbh->prepare("update opl_loan set timeOnShelvingCart=0 where barcode=? && timeOnShelvingCart>0");

    foreach my $bc(@$bcList){
        if($validateBc eq '1'){
             $bc=bcm_validateBc($dbh,$bc,$barcodeType);
        }
        $sth->execute($bc);
   }

}

#-------------------------------------------------------------------------------
sub getJobItemList{
    my ($dbh,$bcList)=@_;
    my $itemList=[];
    my $sth =$dbh->prepare("select rid,barcode from opl_item where barcode= ? ");
    foreach my $bc(@$bcList){
        if($validateBc eq '1'){
             $bc=bcm_validateBc($dbh,$bc,$barcodeType);
        }
        $sth->execute($bc);
        if(my ($rid,$barcode)=$sth->fetchrow_array){
            push @$itemList,{rid=>$rid, barcode=>$barcode}
        }
    }
    return $itemList;

}

#-------------------------------------------------------------------------------
sub createTmpShelf{
    my ($dbh,$newLoc,$resetLocOnReturn)=@_;
    my $sth=$dbh->prepare("replace into opl_tmpLocation set locName=? ,resetLocOnReturn=?");
    $sth->execute($newLoc,$resetLocOnReturn);
    $sth->finish;

}

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


