#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use POSIX qw(
    ceil
);

use Opals::Context;
use Date::Calc qw(Day_of_Week Week_Number Day_of_Year);

use Opals::User qw(
    user_getInformation
);

use Time::localtime;

use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_f005
);
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_rangedPageList
);


use Textbook::Books qw(
       book_getByIsbn
);
use Textbook::Students qw(
    student_getListByCourse
);

use Textbook::Distribution qw(
    getDistribution_byId
);

use Textbook::Teachers qw(
    teacher_findById
);


use Textbook::Course qw(
    course_findById
    course_find
);
use Textbook::SchoolYear qw(
    schoolyear_getCurrent
);

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        => 'textbook/distributionPrt.tmpl',
            reqPermission   => 'report', #textbook
        }
);

my $distype      = $input->{'distributionType'}; 
my $pidList      = $input->{'pidList'};         # preparation ID
my $teacherId    = $input->{'teacherId'};       # teacher ID

my $sListOpt     = $input->{'sListOpt'};  # print student list if $sListOpt =1
my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
my $schoolYearId;
my $schoolYear=schoolyear_getCurrent($dbh);
if ($schoolYear){
    $schoolYearId=$schoolYear->{'id'};
}
my ($recipient,$teacher,$student);
my ($params,$courseId,$courseCode,$sectionId,$nStudents);  
my @courseList;
my $hBList;

my @pidArr = split(',', $pidList);
my $distributions;
my @retval;
  
     if($distype eq "teachers"){
        $teacher = teacher_findById($dbh, $teacherId);
 
        foreach my $pid (@pidArr) {
            my $params={pid=>$pid,schoolYearId=>$schoolYearId,
                        recipient=>"teacher"};       
            my $dInfo = getDistribution_byId($dbh,$params);
            foreach my $d (@{$dInfo}){
                if(!$distributions->{$pid}){
                    $distributions->{$pid}->{'pid'}=$d->{'pid'};
                }

                $recipient = $d->{"recipient"};
                #$teacher = teacher_findById($dbh, $d->{'teacherId'});
                my $course=course_findById($dbh,$d->{'courseId'});
                $d->{"courseName"} =  $course->{"courseName"};
                my $bookInfo = book_getByIsbn($dbh,$d->{'ISBN'});
                $d->{"title"} =  $bookInfo->{"title"};
                push  @{$distributions->{$pid}->{'dInfo'}},$d;

            }
            

            $template->param(
                teacherId        => $teacher->{'teacherId'}, 
                lastName         => $teacher->{'lastName'}, 
                firstName        => $teacher->{'firstName'},
                teachers         => 1,
            );
        }
        foreach my $pid(sort keys %{$distributions}){
            push @retval,$distributions->{$pid};
        }

        $template->param(distributions    => \@retval   );
    }
    

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