#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Date::Calendar::Year;
use Date::Calendar::Profiles qw( $Profiles );
use Encode;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Date qw(
    date_parse
    date_text
    date_profileList
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);


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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $op = $input->{'op'};
my ($permission, $cookieList, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/libHours.tmpl',
            reqPermission   => 'pref_edit',
#            op              => $op,
        }
);

$template->param(hlpUrl     => Opals::Constant->getHlpUrl('libHours') );

# See User.pm for the list of permissions
if ($permission && $permission->{'pref_edit'}) {

   
#############################################       
if($op eq 'save'){
       my @weekday  = ($cgi->param('weekday'));
       my @open_hr  = ($cgi->param('open_hr'));
       my @open_min = ($cgi->param('open_min'));
       my @close_hr = ($cgi->param('close_hr'));
       my @close_min= ($cgi->param('close_min'));
      $dbh->do(<<_QRY_);
truncate table opl_openHours
_QRY_

my $sth = $dbh->prepare(<<_STH_);
insert into     opl_openHours
set               
                weekday = ?,
                open    = ?,
                close   = ?
_STH_

        my $open    = "";
        my $close   = "";
        my $preWd   = -1;
        for(my $i =0; $i < scalar(@weekday);$i++) {
            $open   = $open_hr[$i];
            $close  = $close_hr[$i];
            next if( $open eq '' && $close eq '' && $i<scalar(@weekday)-1  && $weekday[$i] eq $weekday[$i+1]);
            $open_min[$i] = '00' if($open_min[$i] eq '');
            $open .=  ":" . $open_min[$i] if($open =~ m/[0-9]{1,2}/g);
            
            $close_min[$i] = '00' if($close_min[$i] eq '') ;
            $close .=  ":" . $close_min[$i] if($close =~ m/[0-9]{1,2}/g);

            if($preWd != $weekday[$i] || (length($open)>3 || length($close)>3)){  
                $sth->execute($weekday[$i],$open,$close);
                $preWd = $weekday[$i];
            }
        }
}

###############################################        
    my $pref = tmpl_preference($dbh);
 
    my $openingHours = listOpeningHr($dbh);
    $template->param(
        openingHours => $openingHours,
    );
}
#Thu, Jan 07, 2010 @ 13:50:35 EST
my $msgValMap ={};
my $stdMsgMap            =loc_getMsgFile('util/lHours.msg',$msgValMap);
loc_write($template,$stdMsgMap);

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


#################################################
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;


 

}

