#!/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_user
    search_student
    search_teacher
    search_course
    search_homeroom

    search_prep_student
    search_prep_teacher
    search_prep_course
    search_prep_homeroom    
    
    search_prep_reciveByStudent
    search_prep_reciveByTeacher
    
    search_coll_student
    search_coll_teacher
    search_coll_course
    search_coll_homeroom

);

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

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

my $whatStep=$input->{'step'};
if($whatStep eq "") {$whatStep = "p"};

my ($param,$params);
my ($resultList,$resultCount);
$resultCount=0;
    my $preparationType= $input->{'preparationType'};
    if($preparationType eq ""){ $preparationType ='teachers';}
    my $searchVal= $input->{'searchVal'};

    my ($offset,$pagesize) = (0,10);
    my $pNum = $input->{'pNum'};
    $pNum = 1 if ( !$pNum );
    $offset= ($pNum -1)*$pagesize ;
    

    if($whatStep eq "d"){
        ($resultList,$resultCount) = prepSearch($dbh,$searchVal,$offset,$pagesize ,$preparationType);
    }
    elsif($whatStep eq "c"){
        ($resultList,$resultCount) = collSearch($dbh,$searchVal,$offset,$pagesize ,$preparationType);
    }
    else{
        ($resultList,$resultCount) = generalSearch($dbh,$searchVal,$offset,$pagesize ,$preparationType);
    }

    ($input->{'pNum'} && $input->{'pNum'} > 0) || ($input->{'pNum'} = 1);
    my @rangedPageList = tmpl_rangedPageList($resultCount, $input->{'pNum'}, $pagesize, 10);
    
    if($resultCount>0){
        $template->param(
         resultList=>$resultList,
         resultCount => $resultCount,
        );
        if($resultCount>scalar(@$resultList)){
            $template->param(
                rangedPageList  => \@rangedPageList,
                pNum    => $pNum
             );
        }
    }   
    
    if($searchVal ne ""){
        $template->param(searchVal => $searchVal );
    }

    if($whatStep eq "p"){
        $template->param(preparation  => 1,
                         stepLabel    => "Prepare for",
                         stepHeader   => "Preparation",
                         actionName   => "prepSearchResult");
    }
    elsif($whatStep eq "d"){
        $template->param(distribution  => 1,
                         stepLabel     => "Distribute to ",
                         stepHeader    => "Distribution",
                         actionName    => "distSearchResult");
    }
    elsif($whatStep eq "c"){
        $template->param(collection    => 1,
                         stepLabel     => "Collect from ",
                         stepHeader    => "Collection",
                         actionName    => "collSearchResult");
    }


$template->param( preparationType => $preparationType,
                  textbookPrepare => 1,
                  whatStep        => $whatStep);


############################################################
sub generalSearch{
     my ($dbh,$searchVal,$offset,$pagesize ,$preparationType) = @_;
     my ($resultList,$resultCount);
     if($preparationType eq "teachers"){
        $param={teacherId=>$searchVal, name=>$searchVal,offset=>$offset,count=>$pagesize };
        my $result =search_teacher($dbh,$param);
        $resultList  = $result->{'teacherList'}    ;
        $resultCount = $result->{'found'};
        $template->param(teachers => 1);
    }
    elsif ($preparationType eq "students"){
        $param={studentId=>$searchVal,name=>$searchVal,offset=>$offset,count=>$pagesize };
        my $result =search_student($dbh,$param);
        $resultList  = $result->{'studentList'}    ;
        $resultCount = $result->{'found'};
        $template->param(students => 1);
    }
    elsif ($preparationType eq "courses"){
        $param={courseName=>$searchVal,courseCode=>$searchVal,offset=>$offset,count=>$pagesize};
        my $result = search_course($dbh,$param);
        $resultList  = $result->{'courseList'}    ;
        $resultCount = $result->{'found'};
        $template->param(courses => 1);
    }
    elsif ($preparationType eq "homeroom"){
        $param={homeroom=>$searchVal};
        my $result = search_homeroom($dbh,$param);
        $resultList  = $result->{'homeroomList'}    ;
        $resultCount = $result->{'found'};
        $template->param(homeroom => 1);
    }

    return ($resultList,$resultCount); 
}


############################################################
sub prepSearch{
     my ($dbh,$searchVal,$offset,$pagesize ,$preparationType) = @_;
     my ($resultList,$resultCount);
     if($preparationType eq "teachers"){
        $param={teacherId=>$searchVal, name=>$searchVal,offset=>$offset,count=>$pagesize };
        #my $result =search_prep_teacher($dbh,$param);
        my $result =search_prep_reciveByTeacher($dbh,$param);
        $resultList  = $result->{'teacherList'}    ;
        $resultCount = $result->{'found'};
        $template->param(teachers => 1);
    }
    elsif ($preparationType eq "students"){
        $param={studentId=>$searchVal,name=>$searchVal,offset=>$offset,count=>$pagesize };
        #my $result =search_prep_student($dbh,$param);
        my $result =search_prep_reciveByStudent($dbh,$param);
        $resultList  = $result->{'studentList'}    ;
        $resultCount = $result->{'found'};
        $template->param(students => 1);
    }
    elsif ($preparationType eq "courses"){
        $param={courseName=>$searchVal,courseCode=>$searchVal,offset=>$offset,count=>$pagesize};
        my $result = search_prep_course($dbh,$param);
        $resultList  = $result->{'courseList'}    ;
        $resultCount = $result->{'found'};
        $template->param(courses => 1);
    }
    elsif ($preparationType eq "homeroom"){
        $param={homeroom=>$searchVal};
        my $result = search_prep_homeroom($dbh,$param);
        $resultList  = $result->{'homeroomList'}    ;
        $resultCount = $result->{'found'};
        $template->param(homeroom => 1);
    }
    elsif ($preparationType eq "quick"){
        $param={name=>$searchVal,userId=>$searchVal,offset=>$offset,count=>$pagesize};
        my $result = search_user($dbh,$param);
        $resultList  = $result->{'userList'}    ;
        $resultCount = $result->{'found'};

        $template->param(quick => 1);
    }

    return ($resultList,$resultCount);    
}


############################################################
sub collSearch{
     my ($dbh,$searchVal,$offset,$pagesize ,$preparationType) = @_;
     my ($resultList,$resultCount);
     if($preparationType eq "teachers"){
        $param={teacherId=>$searchVal, name=>$searchVal,offset=>$offset,count=>$pagesize };
        my $result =search_coll_teacher($dbh,$param);
        $resultList  = $result->{'teacherList'}    ;
        $resultCount = $result->{'found'};
        $template->param(teachers => 1);
    }
    elsif ($preparationType eq "students"){
        $param={studentId=>$searchVal,name=>$searchVal,offset=>$offset,count=>$pagesize };
        my $result =search_coll_student($dbh,$param);
        $resultList  = $result->{'studentList'}    ;
        $resultCount = $result->{'found'};
        $template->param(students => 1);
    }
    elsif ($preparationType eq "courses"){
        $param={courseName=>$searchVal,courseCode=>$searchVal,offset=>$offset,count=>$pagesize};
        my $result = search_coll_course($dbh,$param);
        $resultList  = $result->{'courseList'}    ;
        $resultCount = $result->{'found'};
        $template->param(courses => 1);
    }
    elsif ($preparationType eq "homeroom"){
        $param={homeroom=>$searchVal};
        my $result = search_coll_homeroom($dbh,$param);
        $resultList  = $result->{'homeroomList'}    ;
        $resultCount = $result->{'found'};
        $template->param(homeroom => 1);
    }

    return ($resultList,$resultCount);    
}

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

