#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi      = CGI->new;


my ($permission, $cookie, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'ajax/circ/getLibHours.tmpl',
    }
);

    my $openingHours = listOpeningHr($dbh);
    $template->param(
        openingHours => $openingHours,
    );


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

sub listOpeningHr{
    my ($dbh) = @_;
    
    my $sth = $dbh->prepare(<<_STH_);
    select weekday,open,close from  opl_openHours order by weekday,id 
_STH_
    $sth->execute;
 
    my @oHoursList;
    my $preWd=-1;
    my $i=-1;
    while (my ($wd,$open,$close) = $sth->fetchrow_array) {
         if($wd !=$preWd){
             my @h=({'open'=>$open,'close'=>$close});
             push @oHoursList,{weekday=>$wd,hours=>\@h};
             $i++;
             $preWd=$wd;
         }
         else{
             push @{@oHoursList[$i]->{'hours'}},{'open'=>$open,'close'=>$close};
         }
    }
    $sth->finish;
    return \@oHoursList;

}

#------------------------------------------------------------------------------
