#!/usr/bin/perl

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

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

use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_validateWorkday
    date_deltaWorkDay
    date_addDeltaWorkday
);

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

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

);
use Textbook::Teachers qw(
    teacher_findById
);

use Textbook::Preparation qw(
    getPreparation_teacher
);
use Textbook::Course qw(
    course_findById
);

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

my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);

my $collectionType = $input->{'collectionType'};
my $collList;


my $schoolYearId;
my $schoolYear=schoolyear_getCurrent($dbh);
if ($schoolYear){
    $schoolYearId=$schoolYear->{'id'};
}
my ($param,$noISBNPerCourse,$noCoursePerPrep);
my (@resultList,$tmpRs);
my ($curCourseId,$curSectionId,$pid); 

   if($collectionType eq "teachers"|| $collectionType eq "students"){
        $collList = getCollectionInfo();
        
       # my $numOfDist = scalar($collList);
       # $if($numOfDist>0){
       #     $template->param(numOfDist    => $numOfDist); 
       # }
       if($collectionType eq "teachers"){
            my $teacherInfo = teacher_findById($dbh,$input->{'teacherId'});
            $template->param( teachers      => 1,
                              teacherId     => $teacherInfo->{'teacherId'},
                              firstName     => $teacherInfo->{'firstName'},
                              lastName      => $teacherInfo->{'lastName'} );
        }
        else{
            my $studentInfo = student_findById($dbh,$input->{'studentId'});
            $template->param( students      => 1,
                              studentId     => $studentInfo->{'studentId'},
                              firstName     => $studentInfo->{'firstName'},
                              lastName      => $studentInfo->{'lastName'} );
        }
        
   }# if($collectionType eq "teachers" OR "students")
  
   elsif($collectionType eq "courses"){
   }
   elsif($collectionType eq "homeroom"){
   }

$template->param(
            collList         => $collList,
            collectionDate   => $todayStr ,
            collectionType   => $collectionType,
        );
################################################################################
sub getCollectionInfo{
    my @collList;
    my ($p,$i,$j,$k)=(0,0,0,0);
    my ($courseCode,$courseName,$courseId,$section,$isbn,$title,$nDistributed,
        $nCollected,$nOk, $nDamaged, $nLost);
    my $iSel =0 ;
     while(my $pid=$input->{'pid_' . $p}){
         $i=0;
        while($courseId=$input->{'courseId_' . $p . '_' . $i}){
           $j=0;            
           if($input->{'distSel_'  . $p . '_' . $i }){
               push @collList,{pid     => $pid,
                               p_order => $iSel};

                        $courseCode =$input->{'courseCode_'. $p . '_' . $i };
                        $courseName =$input->{'courseName_'. $p . '_' . $i };
                push @{$collList[$iSel]->{'courseList'}},{
                                            courseCode   => $courseCode,
                                            courseId     => int ($courseId),
                                            courseName   => $courseName,
                                            c_order=>$i
                                         };
               my $c = @{$collList[$iSel]->{'courseList'}}[$i];
               while($section=$input->{'section_'. $p . '_' . $i . '_' .$j}){
                    push @{$c->{'sectionList'}},{ section    => $section,nISBNs=>0,s_order=>$j };
              
                    $k=0;
                    my $s = @{$c->{'sectionList'}}[$j];
                    while($isbn =$input->{'ISBN_'. $p . '_' . $i . '_' . $j . '_' .$k}){
                        $title        = $input->{'title_'. $p . '_' . $i . '_' . $j . '_' .$k};
                        $nDistributed = $input->{'nDistributed_' . $p . '_'. $i . '_' . $j . '_' .$k};
                        $nCollected   = $input->{'nCollected_' . $p . '_'. $i . '_' . $j . '_' .$k};
                        $nOk          = $input->{'ok_' . $p . '_'. $i . '_' . $j . '_' .$k};
                        $nDamaged     = $input->{'damaged_' . $p . '_'. $i . '_' . $j . '_' .$k};
                        $nLost        = $input->{'lost_' . $p . '_'. $i . '_' . $j . '_' .$k};
                        push @{$s->{'bookList'}}, {  
                                 ISBN           => $isbn,
                                 title          => $title,
                                 nDistributed   => $nDistributed,
                                 nCollected     => $nCollected,
                                 nOk            => $nOk,
                                 nDamaged       => $nDamaged,
                                 nLost          => $nLost,
                                 isbn_order     =>$k++};
                    
                              
                        $s->{'nISBNs'} +=1;
                    }

                    $j++;
                }#while($section
               $iSel++;
                
           } #if
           
            $i++;
        }# while($course=$input->{'course_' . $i})

        $p++;
     } #while pid
   return  \@collList;
        
}

   
################################################################################
    
tmpl_write($dbh, $cgi, $cookie, $template);
#$dbh->disconnect();

################################################################################

