#!/usr/bin/perl

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

use Opals::Context;
use Date::Calc::Object qw(
    :all
);
use Time::localtime;

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

use Opals::TeacherCourse qw(
    
    teacherCourse_getList
    teacher_getCatCode
    
);
use Opals::Course qw(
    
    course_getSchoolYearList
    course_getSchoolYearInfo

);
use Opals::Tb_Fines qw(
    fine_getFineRate
);

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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'txtbk/circ/loan1.tmpl',
            reqPermission   => 'tb_circ_loan',
        }
);
my $syspref          = tmpl_preference($dbh);
my $circulationSound = $syspref->{'circulationSound'};
my $loginuid         = $template->param('curUserId');
#$ENV{'curUserId'}= $template->param('curUserId');
open debug, ">/tmp/debugKK";
print debug "curUserId", $template->param('curUserId'), "\n";
print debug "ENV curUserId", $ENV{'curUserId'},  "\n";

close debug;
my @cookieList = (@$cookie);
my $schoolYearList      = course_getSchoolYearList($dbh);
my $curSchoolYearId = 0;
my $fineRate =fine_getFineRate($dbh);

foreach my $y (@{$schoolYearList}){
    if ($y->{'curSchoolYear'} == 1){
        $curSchoolYearId = $y->{'id'};
        last;
    }
}
my $catCode_teacher = teacher_getCatCode($dbh);
    my $teacherCourseList;
    $teacherCourseList = getTeacherCourseList($dbh,$curSchoolYearId , $catCode_teacher);
    my $tcList;

    foreach my $t (@{$teacherCourseList}){
        if (!$tcList->{$t->{'tId'}}){
            $tcList->{$t->{'tId'}}->{'tId'} = $t->{'tId'};
            $tcList->{$t->{'tId'}}->{'firstname'} = $t->{'firstname'};
            $tcList->{$t->{'tId'}}->{'lastname'} = $t->{'lastname'};
        }
        push @{$tcList->{$t->{'tId'}}->{'courseList'}} , {
            tsId        => $t->{'tsId'},
            courseId    => $t->{'courseId'},  
            courseCode  => $t->{'courseCode'},
            courseName  => $t->{'courseName'},
        }
    }
    my @courstListByTeacher;
    foreach my $t (sort keys %{$tcList}){
        push @courstListByTeacher, $tcList->{$t};
    }
    @courstListByTeacher = sort { lc($a->{"lastname"}) cmp lc($b->{"lastname"})} @courstListByTeacher;


if ($permission && $permission->{'tb_circ_loan'}) {   
    $template->param(
        teacherCourseList => to_json(\@courstListByTeacher),
        chargeOverdue =>  $syspref->{'textbookChargeOverdue'},
        chargeDamage  =>  $syspref->{'textbookChargeDamage'},
        chargeLost    =>  $syspref->{'textbookChargeLost'},
        fineRateTbl   =>  to_json($fineRate)
    );
}#if ($permission && $permission->{'tb_circ_loan'})


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

################################################################################
sub getTeacherCourseList {
    my ($dbh,$curSchoolYearId ,$catCode_teacher) = @_;
        
    my $sql = "select u.uId as tId,u.firstname,u.lastname,ts.id as tsId,cl.courseId,cl.courseCode,cl.courseName 
	    from opl_user u 
        left outer join tb_teacherSchedule ts on u.uId = ts.teacherId 
        left outer join tb_courseList cl on cl.courseId = ts.courseId && cl.schoolYear = ?
        where u.categorycode = ? && cl.courseId is not NULL
        order by u.lastname, u.firstname ";
    my  @val =($curSchoolYearId, $catCode_teacher);
    my $ret = $dbh->selectall_arrayref($sql, {Slice => {}}, @val);
    return $ret;

}
