#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

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

use Opals::Date qw(
    date_f005
);
use Opals::Circulation qw(
    circ_getRecInfo
);
use Opals::Rating qw(
    cmntRating_getInfoByRid
    cmntRating_getComntsByRid
);

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

my $cgi          = CGI->new;
my $input        = $cgi->Vars();
my $rid= $input->{'rid'};
my $pageNum= $input->{'pageNum'};
my $pSize= $input->{'pSize'};
my $filter=$input->{'filter'};

($pageNum  && $pageNum >=1)||($pageNum=1); 
($pSize && $pSize >=1)||($pSize=10);

my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ajax/rating/cmntList.tmpl',
        }
    );

    my $recInfo =circ_getRecInfo($dbh,$rid);
    if($recInfo->{'rid'} >0){
        my $ratingInfo =cmntRating_getInfoByRid($dbh,$rid);
        my $offset= ($pageNum -1) * $pSize ;
        my $cmntList = cmntRating_getComntsByRid($dbh,$rid,$filter,$offset,$pSize);

        $template->param(
            title=>$recInfo->{'title'},
            author=>$recInfo->{'author'},
            pubName=>$recInfo->{'pubName'},
            pubPlace=>$recInfo->{'pubPlace'},
            pubDate=>$recInfo->{'pubDate'},
        );

        $template->param(
            totalRating=>$ratingInfo->{'totalRating'},
            totalAccepted=>$ratingInfo->{'totalAccepted'},
            totalRejected=>$ratingInfo->{'totalRejected'},
            totalNotReview=>$ratingInfo->{'totalNotReview'},
            totalReviewed=>$ratingInfo->{'totalReviewed'},
            avgRating=>$ratingInfo->{'avgRating'},
            cmntList=>$cmntList                    
            );

    }

tmpl_write($dbh, $cgi, $cookie, $template);
#------------------------------------------------------------------------------
