#!/usr/bin/perl

#use utf8;
use strict;

use Encode;
use LWP::UserAgent;
use HTTP::Request::Common;

use CGI;
use JSON;

use Opals::Context;

use Opals::Eq_SolrSearch;
use Opals::Log;

use Opals::Template qw(
    tmpl_read
    tmpl_write
);

use Opals::Equipment qw(
 
    eq_record_findByRId
    eq_categoryMapList
);

use Opals::Eq_Circulation qw(
    
    circ_infoRecord
);
use Opals::User qw(
    user_currentUserID
);

use Opals::Eq_SolrIndex;

use Opals::Constant;

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

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'eqmnt/ajax/record/getRecords.tmpl',
        #reqPermission   => 'eq_record_edit',
    }
);

    my $uIdInfo = user_currentUserID($dbh,$cgi);
    my $sessionID = $cgi->cookie('globalSessionID');

    #my $ridList = $input->{'ridList'};
    #my @rids = split(/,/,$ridList);
    my $rids = getRIdListBySessionID($dbh,$sessionID);
    my @list=();
    my $categoryList = eq_categoryMapList($dbh);
    my $rec;
    my @records;
    foreach my $rid (@{$rids}){
        $rec = findRecordById($dbh,{recordId=> $rid});
        push @records, $rec;
    }
    my $recInfo_json = to_json(\@records, {pretty=>1});
    $template->param (
        records => $recInfo_json

    );
tmpl_write($dbh, $cgi, $cookieList, $template);



sub getRIdListBySessionID {
    my ($dbh,$ssid) = @_;
    my @ridList;
    my $sql = "select rid from opl_sessionVar where ssid='$ssid' order by sOrder";
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    while (my $rid = $sth->fetchrow_hashref()){
        push @ridList, $rid->{'rid'};
    }
    return \@ridList;
}

sub findRecordById {

    my ($dbh,$params) = @_;
    return if ($params->{'recordId'} eq '');
    my $rid = $params->{'recordId'};
    my $categoryMapList = eq_categoryMapList($dbh);
    my @recordInfo=();
    my $sql = "SELECT r.rid,r.rname,r.category,r.container,  rf.fId, rf.fValue as fValue
                FROM eq_records as r 
                    left JOIN eq_recordFields as rf ON r.rid = rf.rid 
                    right JOIN eq_def as d ON rf.fId = d.id && r.rid = ?
                WHERE d.defType = 'record'  order by d.fOrder";
    my $sth = $dbh->prepare($sql);
    $sth->execute($params->{'recordId'});
    
    my $record;

    while( my $rec = $sth->fetchrow_hashref){
        
        if (!$record->{'rid'} ) {
            $record->{'rid'} = $rec->{'rid'};
        }
        if (!$record->{'name'} ) {
            $record->{'name'} = $rec->{'rname'};
        }
        if (!$record->{$rec->{'fId'}}){
            $record->{$rec->{'fId'}} = $rec->{'fValue'};
        }
    }
    my @barcodeList = ();
    $sql = "select barcode from eq_items where barcode not regexp '^\_\_\_'  && rid = $rid order by barcode";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    my $i=0;
    while (my $bc = $sth->fetchrow_hashref()){
        push @barcodeList, { 
            barcode => $bc->{'barcode'},
        };
    }
    $record->{'items'} = \@barcodeList;
    
    $sth->finish;
    return $record;
}

