#!/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 Opals::MarcXml qw(
    mxml_delete
);
use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_f005
);
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_rangedPageList
);


use Textbook::Search qw(
    search_student
    search_teacher
    search_course
    search_homeroom
);

use Textbook::Books qw(
    bookList_forCourse
    bookList_forTeacher
    bookList_forStudent
    bookList_forHomeroom
    book_TotalAvailable
);
use Textbook::Students qw(
    student_findById
);

use Textbook::Teachers qw(
    teacher_findById
);

use Textbook::Course qw(
    course_findById

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

my $preparationType= $input->{'preparationType'};
my $prepList;
my $dateToday = date_f005();
   $dateToday =~ s/([\d]{4})([\d]{2})([\d]{2})[\d]+\.(0|1)/$1-$2-$3/;
  
  
    if($preparationType eq "teachers"){
        my $teacherInfo = teacher_findById($dbh,$input->{'teacherId'});
        $prepList=getPreparationInfo_teacher();
        $template->param(
            teachers => 1,
            teacherId=>$teacherInfo->{'teacherId'},
            firstName=>$teacherInfo->{'firstName'},
            lastName=>$teacherInfo->{'lastName'},
            preparationDate=>$dateToday,
            expectedDate=>$input->{'expectedDate'},
        );
    }
    elsif ($preparationType eq "students"){
        my $studentInfo = student_findById($dbh, $input->{'studentId'});
        $prepList = getPreparationInfo_student();
        $template->param(
            students => 1,
            studentId=>$studentInfo->{'studentId'},
            firstName=>$studentInfo->{'firstName'},
            lastName=>$studentInfo->{'lastName'},
            grade=>$studentInfo->{'grade'},
            preparationDate=>$dateToday,
            expectedDate=>$input->{'expectedDate'},
        );

    }
    elsif ($preparationType eq "courses"){
        my $courseInfo  = course_findById($dbh, $input->{'courseId'});
        $prepList= getPreparationInfo_course();

        $template->param(
            courses => 1,
            courseId=>$input->{'courseId'},
            courseCode=>$courseInfo->{'courseCode'},
            courseName=>$courseInfo->{'courseName'},
            preparationDate=>$dateToday,
            expectedDate=>$input->{'expectedDate'},
            
        );
    }
    elsif ($preparationType eq "homeroom"){

        $prepList = getPreparationInfo_homeroom();

        $template->param(
            homeroom => 1,
            homeroomVal => $input->{'homeroomVal'},
            preparationDate=>$dateToday,
            expectedDate=>$input->{'expectedDate'},
        );
    }

$template->param(
        preparationType => $preparationType,
        prepList=>$prepList,
        
         );

################################################################################
sub getPreparationInfo_teacher{
    my @prepList;
    my ($i,$j,$jj,$k)=(0,0,0,0);
    my ($courseCode,$courseId,$section,$isbn, $title,$nStudents,$aType,$qty,$nCBooks,$recipient);
    my $aTypeDisplay;

    while($section=$input->{'section_' . $i}){
            
        if($input->{'course_' . $i}){
            $courseCode =$input->{'course_' . $i };
            $courseId =$input->{'courseId_' . $i };
            $nStudents  =$input->{'nStudents_' . $i};
            $j=0;
            
            push @prepList,{courseCode => $courseCode,
                            courseId   => $courseId,
                            section    => $section,
                            nStudents  => $nStudents,
                            order      => $k};
            $j=0;
            $jj=0;
            while($isbn =$input->{'ISBN_' . $i . '_' .$j}){
                    $title       = $input->{'title_' . $i . '_' . $j };
                    $aType       = $input->{'aType_' . $i . '_' .$j};
                    $qty         = $input->{'qty_' . $i . '_' .$j};
                    #$recipient   = $input->{'recipient_' . $i . '_' .$j};
                    $recipient   = $input->{'recipient_' . $i . '_0'};
                    $nCBooks     = $input->{'nCBooks_' . $i . '_' .$j};
                    
                    if($aType eq "cps"){
                        $aTypeDisplay ="Copies per student";
                    }
                    else{
                        $aTypeDisplay ="Copies per class";
                    }
                    if ($qty > 0) {
                    push @{$prepList[$k]->{'detail'}},{
                             ISBN           => $isbn,
                             title          => $title,
                             aType          => $aType,
                             aTypeDisplay   => $aTypeDisplay,
                             qty            => $qty,
                             nCBooks        => $nCBooks,
                             recipient      => $recipient,
                             order          => $k .'_' . $jj};
                    $jj++;
                    }
                $j++;
            }
            $k++;
        }
        $i++;
    }
   return  \@prepList;
}
################################################################################
sub getPreparationInfo_student{

    my @prepList;
    my ($i,$j,$jj,$k) = (0,0,0,0);
    my ($courseCode,$courseId,$courseName,$sectionId,$isbn,$title,$qty,$nBooks);
    my $course;
    while($courseCode = $input->{'courseCode_' . $i }){
        if($course = $input->{'course_' . $i}){
            $j= 0;
            $jj=0; 
            my @detail=();
            while($isbn = $input->{'ISBN_' . $i . '_' . $j}){
                $qty        = $input->{'qty_' . $i . '_' . $j };
                if ($qty > 0){
                    $isbn       = $input->{'ISBN_' . $i . '_' . $j };
                    $title      = $input->{'title_' . $i .'_' . $j };
                    $qty        = $input->{'qty_' . $i . '_' . $j };
                    push @detail, {
                        ISBN    => $isbn,
                        title   => $title,
                        qty     => $qty,
                        order   => $k . '_' . $jj};
                        $jj++;
                }
            $j++;
            }#while($qty
            if(scalar(@detail)>0){
                $courseCode = $input->{'courseCode_' . $i };
                $courseName = $input->{'courseName_' . $i };
                $courseId = $input->{'courseId_' . $i };
                $sectionId  = $input->{'sectionId_' . $i };
                push @prepList, {
                    courseCode  => $courseCode,
                    courseName  => $courseName,
                    courseId    => $courseId,
                    sectionId   => $sectionId,
                    order       => $k,
                    detail      => \@detail };
                    $k++;
            }#if
        }#if
        $i++;
    }
    return \@prepList;
}
################################################################################I
sub getPreparationInfo_course{

    my @prepList;
    my ($i,$j,$jj,$k )=(0,0,0,0);
    my ($courseCode,$courseId,$section,$isbn,$nStudents,$aType,$qty,$nSBooks,$recipient,$title);
    my ($aTypeDisplay,$rowspan);

    while ($nStudents = $input->{'nStudents_' . $i}){
        if ($section = $input->{'section_' . $i}) {
            $j=0;
            while ($qty = $input->{'qty_' .$i . '_' . $j} ){
                if ($qty > 0 ){$j++;}
            }
            
            $rowspan= ($j > 1)?$j:1;
            push @prepList, {
            section     =>  $section,
            nStudents   =>  $nStudents,
            rowspan     =>  $rowspan,
            order       =>  $k,
            };
            $j=0;
            $jj=0;

            while($isbn=$input->{'ISBN_' . $i . '_' .$j} ){
                $aType      = $input->{'aType_' .$i . '_' .$j};
                #$recipient  = $input->{'recipient_' .$i . '_' . $j};
                $recipient  = $input->{'recipient_' .$i . '_0' };
                $nSBooks    = $input->{'nSBooks_' .$i . '_' . $j};
                $title      = $input->{'title_' .$i . '_' . $j};
                $qty        = $input->{'qty_' .$i . '_' . $j}; 
                
                if ($aType eq "cps"){
                    $aTypeDisplay="Copies per student";
                }   
                else{
                    $aTypeDisplay="Copies per section";
                }

                if ($qty > 0) {
                    push @{$prepList[$k]->{'bookList'}},{
                        ISBN            => $isbn,
                        title           => $title,
                        aType           => $aType,
                        aTypeDisplay    => $aTypeDisplay,
                        qty             => $qty,
                        recipient       => $recipient,
                        nSBooks         => $nSBooks,
                        order           => $k . '_' . $jj,

                    };
                    $jj++;
                }#if (qty
                $j++;
            }
            $k++;
        }      
        $i++;
    }
    return \@prepList;
}

sub getPreparationInfo_homeroom{
    my @prepList;
    my ($i,$j,$jj,$k)=(0,0,0,0);
    my ($courseCode,$courseId,$section,$isbn, $title,$nStudents,$aType,$qty,$nCBooks,$recipient);
    my $aTypeDisplay;

    while($section=$input->{'section_' . $i}){
            
        if($input->{'course_' . $i}){
            $courseCode =$input->{'course_' . $i };
            $courseId =$input->{'courseId_' . $i };
            $nStudents  =$input->{'nStudents_' . $i};

            push @prepList,{courseCode => $courseCode,
                            courseId   => $courseId,
                            section    => $section,
                            nStudents  => $nStudents,
                            order      => $k};
            $j=0;
            $jj=0;
            while($isbn =$input->{'ISBN_' . $i . '_' .$j}){
                    $title       = $input->{'title_' . $i . '_' . $j };
                    $aType       = $input->{'aType_' . $i . '_' .$j};
                    $qty         = $input->{'qty_' . $i . '_' .$j};
                    #$recipient   = $input->{'recipient_' . $i . '_' .$j};
                    $recipient   = $input->{'recipient_' . $i . '_0' };
                    $nCBooks     = $input->{'nCBooks_' . $i . '_' .$j};

                    if($aType eq "cps"){
                        $aTypeDisplay ="Copies per student";
                    }
                    else{
                        $aTypeDisplay ="Copies per class";
                    }
                    if ($qty > 0){
                        push @{$prepList[$k]->{'detail'}},{
                             ISBN           => $isbn,
                             title          => $title,
                             aType          => $aType,
                             aTypeDisplay   => $aTypeDisplay,
                             qty            => $qty,
                             nCBooks        => $nCBooks,
                             recipient      => $recipient,
                             order          => $k .'_' . $jj};
                        $jj++;
                    }
                 $j++;
            }
            $k++;
        }
        $i++;
    }
   return  \@prepList;

}

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


