#!/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::Constant;

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


    eq_containerFields_getList
);

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/getItem.tmpl',
        #reqPermission   => 'eq_record_edit',
    }
);
    my $authCtrlFields  = _authCtrlFields_getList($dbh);

    my $rid     = $input->{'rid'};
    my $barcode = $input->{'barcode'};
    my $acList;
    foreach my $ac (@{$authCtrlFields}){
        $acList->{$ac->{'fId'}} = $ac->{'list'};
    }
    
    my $item = getItemByBarcode($dbh,$rid,$barcode);
    my $json = to_json($item,{pretty=>1});
        $template->param (
            json => $json,
    );
tmpl_write($dbh, $cgi, $cookieList, $template);

sub getItemByBarcode {

    my ($dbh,$rid,$barcode) = @_;
    
    my $sth = $dbh->prepare("select iid, barcode,typeId,copyNo from eq_items where rid=? && barcode =?");
    my $item;

    $sth->execute($rid,$barcode);
    #my ($iid,$barcode,$typeId,$copyNo) = $sth->fetchrow_array();
    $item  = $sth->fetchrow_hashref();
    $item->{'rid'}=$rid;
    my $sql = " SELECT  d.*, fdt.dataType, fdt.maxVal, i.id as i_iid, i.rid, i.sfId, i.sfValue 
                FROM    eq_def as d 
                LEFT OUTER JOIN eq_fieldDataType fdt on d.fieldType=fdt.id
                LEFT OUTER JOIN eq_itemFields as i on i.sfId = d.id && i.rid=? && i.iid=? 
                WHERE   d.defType = 'item' order by i.sfId";
    
    if ($item->{'iid'} && $item->{'iid'} > 0){
        $sth = $dbh->prepare($sql);
        $sth->execute($rid,$item->{'iid'});
        my @fields=();
        while ( my $f = $sth->fetchrow_hashref()){
            push @fields, {
                id              => ($f->{'sfId'})? $f->{sfId}:$f->{'id'},
                value           => $f->{'sfValue'}, 
                name            => $f->{'name'},
                url             => ($f->{'fieldType'} eq '2')?1:0,
                fieldType       => $f->{'fieldType'},
                dataType        => lc($f->{'dataType'}) || "text",
                fieldSize       => $f->{'fieldSize'} || 10,
                acList          => $acList->{$f->{'sfId'}} || [],
                updateAll       => '0',
                checkedList     => ($f->{'sfValue'})? [split(/, /,$f->{'sfValue'})] : [],
                textType        => $f->{'textType'} || ""
            }
        }
        $item->{'fields'} = \@fields;
    }
    $sth->finish;
    return $item;
}

sub _authCtrlFields_getList{

    my ($dbh) = @_;
    my $sth = $dbh->prepare(<<_STH_);
select ac.name as name,ac.fCode as fId,ac.fData as fVal,ed.defType,ed.fieldType,fdt.dataType from eq_authCtrl ac inner join eq_def ed on ac.fCode=ed.id left outer join eq_fieldDataType fdt on ed.fieldType=fdt.id  order by defType,fCode,fVal
_STH_
    $sth->execute();
    my $tmp;
    my $retVal = [];
    while (my $rec = $sth->fetchrow_hashref()){
        if (!$tmp->{$rec->{'fId'}}){
            $tmp->{$rec->{'fId'}}->{'fId'} = $rec->{'fId'};
            $tmp->{$rec->{'fId'}}->{'name'} = $rec->{'name'};
            $tmp->{$rec->{'fId'}}->{'type'} = $rec->{'defType'};
            $tmp->{$rec->{'fId'}}->{'fieldType'} = $rec->{'dataType'};
            $tmp->{$rec->{'fId'}}->{'checkboxList'} = ($rec->{'dataType'} eq 'CheckboxList')?1:0;
        }
        push @{$tmp->{$rec->{'fId'}}->{'list'}}, $rec->{'fVal'};
    }

    foreach my $fId (sort keys %{$tmp}) {
        push @$retVal, $tmp->{$fId};
    }
    return $retVal;
}

