#!/usr/bin/perl
    use lib '/opt/beta/embmon-lib';
    use strict;
    use warnings;
    use DBI;
    use EmbMon::DAO;
    use EmbMon::Config;
    use CGI qw/:standard/;
    use CGI::Carp qw/fatalsToBrowser/;
    use Template;

    # Code to pull the factory Abbreviation from the DB
    my $cgi = new CGI;
    my $dbh = DBI->connect(config('data_source'), config('username'), config('password'));
    my $dao = EmbMon::DAO->new($dbh);
    my $sql = qq/SELECT factory_id, abbreviation from factory/;
    my $sth = $dbh->prepare($sql);
    $sth->execute();

    
    my @rows_factory;
    while ( my @factory_data = $sth->fetchrow_array() )
    {
    push @rows_factory, {'id' => $factory_data[0], 'abbr' => $factory_data[1]};
    } 
 #   $sth->finish();
    # End of Factory codes routine

    # Get Shift data (names,days of week, times) for use in the pulling of report data for the shifts

    my $sql1 = qq/SELECT shift_name from shifts/;
    my $sth1 = $dbh->prepare($sql1);
    $sth1->execute();
    

    my $row_shift;
    my @rows_shift;
    while ($row_shift = $sth1->fetchrow_array() )
      {
      push @rows_shift, $row_shift;
      }
    $sth1->finish();

    # End Get shift data routine
    



########## Rendering code for the page ################

    my $file = 'templates/shift_rpt.html';
    my $vars = {
	'cgi' =>CGI->new(),
	'factories' => \@rows_factory,  # This moves the data from the array into a var that gets carried into the template
	'shifts' => \@rows_shift # This moves the custom shifts names into the dropdown box for the reports
    };
    
    print "Content-type: text/html\n\n";


    my $template = Template->new({
    # where to find template files
    INCLUDE_PATH => ['/home/markh/work/epic.git/code/embmon-web/src', '/home/markh/work/epic.git/code/embmon-web/lib'],
    # pre-process lib/config to define any extra values
    PRE_PROCESS  => ['config']
    });
    
    $template->process($file, $vars)
        || die "Template process failed: ", $template->error(), "\n";

  #  print $cgi->end_html;
