#!/usr/bin/perl -w

use CGI qw/:standard :html3/;


###
# Global Variables
###
$ROOT_PATH = "/Users/rcpageiv/Documents";

# Work Week and Year Defaults to current week and year
$workweek = `date "+%W"`;
$year = `date "+%Y"`;

$date_time = `date "+%A, %B %e %Y - %H:%M:%S %Z"`;
@workweeks = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52);
@years = (2004,2005,2006,2007,2008,2009,2010);

###
# User List
#  Create User Array from control file. Start with static array for testing.
###
@USERS = ('Robert','Karen','Mike','Marylou','Empty');
@PROJECTS = ('A','B','C','D','E','F','Empty');
@PAGES = ('Select User','Select Project','Enter Weekly');

@parms = param();
#print "@parms";

# 
# Capture current values
#
$page = param('goto');
$user = param('user');
$project = param('project');
$selectedyear = param('year');
$wkweek = param('wkweek');
$doweekly = param('Submit');

#
# Print out Menus and Input Form Pages
#
#

print_header();  

# Debug print statements
#print "User $user Project $project",p();

print start_form;
mymenu();
print hr;

#
# Print 'Current' Page based on state
#
   
if ($page eq 'Select User') {
   select_user();
   
} elsif ($page eq 'Select Project') {
   select_project();
   
} elsif ($page eq 'Enter Weekly') {
   
   print "Weekly Report for Selected ";
   
   if ($user ne 'Empty' & $user ne '') {
      print "User: $user,  ";
      $usrproj = $user;
   } elsif ($project ne 'Empty' & $project ne '') {
      print "Project: $project,  ";
      $usrproj = $project;
   } else {
      print " Invalid User/Project Selection ";
   }
   
   $report = "$ROOT_PATH/weekly/$usrproj/$selectedyear/WW$wkweek.txt";
   print " Year: $selectedyear  ";
   print " Work Week: $wkweek";
   print br(), "Path: $report";
   print p();
   
   #
   # Subroutine to display 'executive summary' and 'details'
   # testarea forms.
   #
   enter_weekly();
   
} elsif ($doweekly eq 'Email Weekly') {
   email_weekly();	
} elsif ($doweekly eq 'Save Weekly') {
   save_weekly();
} else {
   select_user();
}

print p(),hr;
mymenu();

print end_form, end_html;


###
# *********** FUNCTIONS **********
###

sub print_header {
   print header,
   	
      start_html('Weekly CGI Script'),
      h1('Weekly Report Interface');
       
      print "Work Week: $workweek - $date_time",   
      hr;
        
}

sub mymenu {
   
   print "Navigation Menu: ";
      
   $numpages = @PAGES;

   foreach (0..$numpages-1) {
      print submit(-name=>'goto', -value=>"$PAGES[$_]");
   }
}


sub select_year_week {

   print "Select Year: ",
   
      popup_menu(-name=>'year',
   		 -value=>[@years,$year],
   		 -default=>$year),  	
      p();
   
   print "Select Work Week: ",
   
      popup_menu(-name=>'wkweek',
   		 -value=>[@workweeks,$workweek],
   		 -default=>$workweek),   	
      p();
}

sub select_user {

   print "Select User: ", 
       
      popup_menu(-name=>'user',
   		 -value=>[@USERS],
   		 -default=>'Empty'),
      p();   		 
      select_year_week();   
      param(-name=>'project',-value=>'Empty');	  
}   
   
sub select_project {
	 
   print "Select Project: ",
      
      popup_menu(-name=>'project',
   		 -value=>[@PROJECTS],
   		 -default=>'Empty'),
      p();
      select_year_week();   	  
      param(-name=>'user',-value=>'Empty');	  
}      

sub enter_weekly {

   #
   # Check for Existing Report
   # If one exists, read its contents for editing
   # else create directories and open report file for writing.
   # PATH: $ROOT_PATH/weekly/$usrproj/$selectedyear/WW$wkweek.txt
   #
   if ( -e $report) {
      open (REPORT, "$report") || die "Can not open $report: $!\n";
      
      until (<REPORT> =~ /Details/){
         $summary = $_;
      }
      while (<REPORT>) {
         $details = $_;
      }
      
      param('executive summary', $summary);
      param('details', $detail);
      
      close REPORT;
            
   } else {
      
      if ( !-e "$ROOT_PATH/weekly") {
   	 `mkdir "$ROOT_PATH/weekly"`;
      }
      if ( !-e "$ROOT_PATH/weekly/$usrproj") {
   	 `mkdir "$ROOT_PATH/weekly/$usrproj"`;
      }
      if ( !-e "$ROOT_PATH/weekly/$usrproj/$selectedyear") {
   	 `mkdir "$ROOT_PATH/weekly/$usrproj/$selectedyear"`;
      }
      #if ( !-e "$report") {
         #`touch "$report"`;
   	 #open (REPORT, ">$report") || die "Can not open $report: $!\n";
      #}
      
   }  
  
   #
   #  Display Entry Form
   #
   weekly_form();
      
}

sub weekly_form {
   #
   #  Display Entry Form
   #
   print "Executive Summary",
      p(),
      textarea(-name=>'executive summary',
               -value=>'Executive Summary',
	       -rows=>20,
	       -columns=>80,
	       -wrap=>'physical'),
      p(),
      "Details",
      p(),
      textarea(-name=>'details',
               -value=>'Details',
	       -rows=>60,
	       -columns=>80,
	       -wrap=>'physical'),
      p(),
      "Email Recipients",
      p(),
      textfield(-name=>'recipients',
                -value=>'Robert@home.net',
		-size=>80),
      p(),
      		
      submit(-name=>'Submit',-value=>'Save Weekly'),
      submit(-name=>'Submit',-value=>'Email Weekly');
      
}

sub email_weekly {
    print "SUB: email_weekly";
    $body = $summary ."\n" . $detail;
    print "$body";
    
}

sub save_weekly {
    print "Weekly Saved as:",p();
    
    #
    # Capture Weekly input from textarea, write to file.
    #
    $summary = param('executive summary');
    $detail = param('details');
        
    open (REPORT, ">$report") or warn("Can not open $report: $!\n");
    
    print REPORT "Success!!\n";
    close REPORT;
     
    print "$summary",
    p(),
    "$detail",
    p();
    
}
