#!/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_currentUser
);

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::SchoolYear qw(
    schoolyear_getCurrent
  );


use Textbook::Collection qw(
    collectFromTeacher_woBc
);


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

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 $collectionType= $input->{'collectionType'};
my $pidList = "";    #preparation ID List

    
    if($collectionType eq "teachers"){
        my $teacherId     = $input->{'teacherId'};
        $pidList = saveCollection_teacher($dbh,$teacherId);
        $template->param(teachers => 1,
                         teacherId => $teacherId);
    }
    elsif ($collectionType eq "students"){
        
        $template->param(students => 1);
    }
    elsif ($collectionType eq "courses"){
        $template->param(courses => 1);
    }
    elsif ($collectionType eq "homeroom"){
        $template->param(homeroom => 1);
    }

    $template->param(collectionType => $collectionType,);
    if($pidList ne ""){
        $template->param(pidList   => $pidList,
                         msgStatus => 1);
    }



################################################################################
sub saveCollection_teacher{
    my ($dbh,$teacherId) = @_;
    
    #my $params={pid=>$pid,schoolYearId=>$schoolYearId,teacherId=>$teacherId};
      
    my $pidList="";
    my ($p,$i,$j,$k)=(0,0,0,0);
    my ($pid,$courseId,$courseCode,$section,$isbn);
    my ($nCopies_ok, $nCopies_damaged, $nCopies_lost) =(0,0,0); 
    while($pid=$input->{'pid_' . $p}){
        $i=0;
        while($courseId=$input->{'courseId_' . $p . '_'. $i}){
           $j=0;            
           while($section=$input->{'section_' . $p . '_' . $i . '_' .$j}){
                    $k=0;
                    while($isbn =$input->{'ISBN_'. $p . '_' . $i . '_' . $j . '_' .$k}){
                        my $nCopies_ok      = $input->{'nOk_'. $p . '_' . $i . '_' . $j . '_' .$k};
                        my $nCopies_damaged = $input->{'nDamaged_'. $p . '_' . $i . '_' . $j . '_' .$k};
                        my $nCopies_lost    = $input->{'nLost_'. $p . '_' . $i . '_' . $j . '_' .$k};

                        my $p={ pid=>$pid, courseId =>$courseId, sectionId=>$section,teacherId=>$teacherId,
                                schoolYearId=>$schoolYearId,ISBN   => $isbn, nCopies_ok=>$nCopies_ok,
                                nCopies_damaged=>$nCopies_damaged, nCopies_lost=>$nCopies_lost};                         
                        $retVal = collectFromTeacher_woBc($dbh,$p);
                        $k++;

                    }

                $j++;
            }
            $i++;
       }# while($course=$input->{'course_' . $i})
       $p++; 
        $pidList .=$pid .",";
   }

   return $pidList;
}

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

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

