#! /usr/bin/perl -w
# **** calander_calc.pl ******
# **** program to give calander and to return date picked *****

use CGI qw/:all/;
use Date::Calc qw(:all);
use DBD::Pg;
use DBI;

#$month = 2;
#$year = 2001;
#$days = Days_in_Month($year, $month);
print "days in month: $days \n";
$j = 1;

$today = localtime(time);
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$year = (1900 + $year);
$year_this = $year;
$month_this = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec) [$mon];
$month = ($mon + 1);

$query_params = new CGI;
if ($query_params->param('month')) { $month = $query_params->param('month') }
if ($query_params->param('year')) { $year = $query_params->param('year') }
if ($query_params->param('day')) { $day = $query_params->param('day') }
$days = Days_in_Month($year, $month);

# ****** Web Interface ******
print header, start_html(-title=>"Date::Calc Calander - Maintenance", -BGCOLOR=>'white');
print $query_params->Dump;
if (($query_params->param('performaction')) eq "Add Record") { add_row() };
if (($query_params->param('performaction')) eq "Edit Record") { edit_row() };
if (($query_params->param('performaction')) eq "Remove Records")
     { remove_row() };
if (($query_params->param('performaction')) eq "Save_Edit") { edit_row_db() };


print ("<table border=10 align=center bgcolor=yellow><tr><td>");
#print system("date");
$this_month = Month_to_Text($month);
  $day_name = Day_of_Week_to_Text($wday);

#print ("<table border=10 align=center bgcolor=yellow><tr><td>");
print ("<table border=5 bgcolor=aqua col=7 align=left>
        <tr>
          <th colspan=7 align=center bgcolor=white >
             today is $day_name - $month_this / $mday / $year_this</th></tr>
        <tr>
         <th colspan=7 align=center bgcolor=aqua>
            calander for $this_month $year </th>
        </tr><tr bgcolor=aqua>
         <th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th>
         <th>Thr</th><th>Fri</th><th>Sat</th>
        </tr><tr>");

for ($i = 1 ; $i <= $days; $i++) {
    $weekday = Day_of_Week($year,$month,$i);
   if ($weekday ne 7){
    if ($weekday >= 1 and $i eq 1) {
	for ($j = 1; $j lt $weekday + 1; $j++) {
	    print ("<td> &nbsp </td>");
	}
     }
   }
  #$day_name = Day_of_Week_to_Text($weekday);
    print ("<form method=post><td align=center>
             <input type=hidden name=year value=$year>
             <input type=hidden name=month value=$month>
             <input type=hidden name=day value=$i>
             <input type=submit value=$i>
             </td></form> ");
    if ($weekday eq '6') {
	print ("</tr><tr>");
    }
}
print ("</table>");

print ("<form method=post>
        <table border align=center bgcolor=skyblue>
         <tr><th align=center colspan=2>Select Calander</th></tr>
         <tr>
           <td align=center>Month</td><td align=center>Year</td></tr>
          <tr>
            <td><select name=month size=1>");
for ($i = 1; $i < 13; $i++) {
    print ("<option>$i");
}
print ("</select></td><td><select name=year size=1>
         <option>2000<option>2001<option>2002<option>2003<option>2004
        </select></td>
        </tr>
        <tr><td colspan=2 align=center>
             <input type=submit value='Get Calander'></td></tr>
        </table></form>");

print ("<form method=post action='./calander_calc.pl'>
        <table border=3 bgcolor=skyblue>
         <tr>
          <td>Calander Functions</td>
         </tr>
         <tr>
           <td align=center><input type=submit value='View Calander'</td>
         </tr></table></form>");
print ("</tr></table><hr noshade>");

if ($query_params->param('day')) {
#    $selected_day = $query_params->param('day');
    specified_date();
} else {
    current_date();
}
        
print end_html;

# ****** Subroutines below ******************

sub specified_date() {
    $date_wanted = $month."/".$day."/".$year;
print ("<form method=post>
        <table align=center border=5 cols=3 width='80%'>
        <tr align=center bgcolor=white>
         <td colspan=3>The selected date is: $date_wanted</td>
        </tr>
        <tr align=center bgcolor=aqua>
         <td colspan=3>
             <input type=hidden name=year value=$year>
             <input type=hidden name=month value=$month>
             <input type=hidden name=day value=$day>
             <input type=submit name='performaction' value='Edit Record'>
               &nbsp
             <input type=submit name='performaction' value='Remove Records'>
               &nbsp
             <input type=submit name='performaction' value='Add Record'>
               &nbsp
             <input type=reset value='UnCheck'></td>
        </tr>");
open_info_db();
print ("</table></form>");
}

sub current_date {
    $date_wanted = $month."/".$mday."/".$year;
print ("<table align=center border=5 cols=3>
        <tr align=center>
         <td colspan=3 bgcolor=white>The listings for : $date_wanted</td>
        </tr>
        <tr align=center>
         <td bgcolor=yellow colspan=3>Below are the things on the calender for this date</td>
        </tr>");
open_info_db();
print ("</table>");
}

sub day_planner_input {


}

# ********** DataBase Routines **************

sub open_info_db {
    $dbname = "info_manager";
    $dbtable = "day_planner";
#    $date_wanted = "9/16/2000";
    $select_used = "select oid,* from $dbtable where planner_date = '$date_wanted' order by planner_catagory";
    $dbh = DBI->connect("DBI:Pg:dbname = $dbname", "postgres");
    $sth = $dbh->prepare("$select_used");
    $sth->execute || die print ("Failure in execute");
    $sth->bind_columns(undef, \$planner_oid, \$planner_date,
		       \$planner_catagory, \$planner_message);

    while ($sth->fetch) {
	chomp($planner_message);
	print ("<tr bgcolor=yellow>
         <th align=left><nobr>
          <input type=radio name=record_id value='$planner_oid'>
            &nbsp $planner_date &nbsp &nbsp:: $planner_catagory </nobr>
                </th></tr>
	 <tr><th colspan=3>
          <textarea cols=50 rows=3 wrap='physical'>$planner_message</textarea>
         </th></tr>");
    }
}


sub add_row_db {
    $dbname = "info_manager";
    $dbtable = "day_planner";
    $catagory_wanted = $query_params->param("catagory");
    $message = $query_params->param("message");
    chomp($message);
    $date_wanted = $month."/".$day."/".$year;
    $select_used = "insert into $dbtable values ('$date_wanted', '$catagory_wanted', '$message')";
    $dbh = DBI->connect("DBI:Pg:dbname = $dbname", "postgres");
    $sth = $dbh->prepare("$select_used");
    $sth->execute || die print ("DB Insert Failure to execute");
}

sub edit_row_db {
    $dbname = "info_manager";
    $dbtable = "day_planner";
    $message = $query_params->param("message");
    chomp($message);
    $planner_iod = $query_params->param("record_id");
  $select_used = "update $dbtable set planner_message='$message' where oid ='$planner_iod'";
    $dbh = DBI->connect("DBI:Pg:dbname = $dbname", "postgres");
    $sth = $dbh->prepare("$select_used");
    $sth->execute || die print ("DB row Update failure");

}

sub delete_row_db {

    $dbname = "info_manager";
    $dbtable = "day_planner";
    $planner_oid = $query_params->param("record_id");
    $dbh = DBI->connect("DBI:Pg:dbname = $dbname", "postgres");
    $sth = $dbh->prepare("delete from $dbtable where oid='$planner_oid'");
    $sth->execute || die print ("DELETE FAILURE");

}

sub get_row_db {
    $dbname = "info_manager";
    $dbtable = "day_planner";
    $planner_oid = $query_params->param('record_id');
    $dbh = DBI->connect("DBI:Pg:dbname = $dbname", "postgres");
    $sth = $dbh->prepare("select * from $dbtable where oid = '$planner_oid'");
    $sth->execute ||die print ("Selecting record for update failure");
    $sth->bind_columns(undef, \$planner_date, \$planner_catagory, \$planner_message);
    $sth->fetch;
}

# ********** PreformAction routines **************

sub add_row {
    $row_input_title = "Add NEW Row to Day Planner";
    $row_input_performaction = "Add Record";
    $row_input_submit = "Add Record";
    if (($query_params->param("message")) &&
	($query_params->param("catagory"))) {
    add_row_db();
    } else { print ("<center><font color=red>You did not enter enough data
                  </font></center>");
    }
    row_input_form();


}

sub edit_row {
    print ("Edit Row routine");
    $row_input_title = "Edit THIS Row";
    $row_input_performaction = "Save_Edit";
    $row_input_submit = "Save EDITS";
 if ($query_params->param('record_id')) {
    get_row_db();
    row_input_form();
 } else {
    print ("<center><font color=red>ACTION NOT PERFORMED
            <br>You need to check the item you want to edit</font></center>");
 }
}

sub remove_row {
    print ("Remove Row");
    delete_row_db();
    print ("Row Removed");

}

# ******* Misc Routines  ***************
# ******* can be called by any routine ************

sub row_input_form {

print ("<form method=post><table border=5 bgcolor=aqua align=center>
         <tr><th bgcolor=white align=center>
              $row_input_title</th></tr>
         <tr><td>");

print ("select Month-Day-Year : <select name=month>
         <option value=$month>$month");
     for ($i=1; $i <= 12; $i++) {
	 print ("<option>$i");
     }
print ("</select>");

print ("<select name=day>
          <option value=$day>$day");
     for ($i=1; $i<= $days; $i++) {
	 print ("<option>$i");
     }
print ("</select>");

print ("<select name=year>
         <option value=$year>$year");
     for ($i=$year; $i <= 2020 ; $i++) {
       print ("<option>$i");
     }
print ("</select>");

 print ("</td></tr><tr>
           <td>Catagory: 
            <input type=text name=catagory value='$planner_catagory'>
           </td></tr>
         <tr>
           <td>Enter Message:<br>
             <textarea name=message cols=50 rows=3 wrap='physical'>$planner_message</textarea>
           </td></tr>
         <tr>
           <td align=center bgcolor=white>
            <input type=hidden name=record_id value=$planner_oid>
            <input type=hidden name=performaction
                    value='$row_input_performaction'>
            <input type=submit value='$row_input_submit'></td></tr>
         </table></form>");

}

