#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "ClearSilver.h"
#include "sqlite3.h"
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "grade.h"		/* for date FORMAT strings */
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_time.h"
#include "manhat-lib/shared_grade_book_menu.h"
#include "manhat-lib/shared_grade_book_db.h"
#include "manhat-lib/shared_grade_book_util.h"

void
read_parameters ( char *course, char *key, int *item_id)
{
    char temp[MAX_KEY +1];
     cs_get_required_parameter("crs", course, MAX_PATH);
     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("item_id", temp, MAX_KEY);
     *item_id = atoi(temp);



}





void
update_item_date(CONFIG_STRUCT *conf, int item_id)
{
    time_t new_date;
    char path[MAX_PATH +1];
    int ret = 0;
    sqlite3 *db;
    char *stmt;
    
    new_date = get_time("item_due_date");

    snprintf(path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_DB);
    ret = sqlite3_open(path, &db);
    if(ret)
      cs_critical_error(ERR_GRADES_DB, "update item date function");

    stmt = sqlite3_mprintf("update Items set dueDate=%d where itemId=%d", new_date, item_id);
    if(stmt)
    {
        ret = sqlite3_exec(db, stmt, 0, 0, 0);
        if(ret)
	{
	    sqlite3_free(stmt);
            sqlite3_close(db);
	    cs_critical_error(ERR_GRADES_DB, "update item date error");
	}
        sqlite3_free(stmt);
    }
    sqlite3_close(db);
  
 
}

    
void do_action(CONFIG_STRUCT *conf, const char *course, const char *key, int item_id)
{
      struct tm *due_time;
      char temp[MAX_PATH +1];
      char *oper;


      oper = hdf_get_value(global_cgi->hdf, "Query.oper", "");
      if(strlen(oper))
      {
         update_item_date(conf, item_id);
	 printf("Location: %s?crs=%s&id=%s&item_id=%d&save=ok\n\n", "grade_book_due_date",course, key, item_id);

      }
      else
      {
          get_one_item(conf, item_id, GRADE_BOOK_DB);         /* shared_grade_book_db.h */
      
          if(!global_item_list || !global_item_list->head)
	    cs_critical_error(ERR_GRADE_BOOK_NOT_FOUND, "grade book due date");

          due_time = localtime(&(global_item_list->head->due_date));
          snprintf(temp, MAX_PATH +1, "%s%d/%s%d/%d",
              (due_time->tm_mon + 1) < 10 ? "0" :"",
              due_time->tm_mon +1,
              due_time->tm_mday  < 10 ? "0" :"",
              due_time->tm_mday,
              due_time->tm_year +1900
              );
          cs_set_value("date_date", temp);
     
          set_hour_minute_ampm(due_time, "date"); /* shared_time.c */
     
          cs_load_lang("grade_book_items");
          cs_cgi_display("grade_book_due_date", 0);      
       }

}


int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  SESSION user;
  CONFIG_STRUCT conf;
  int item_id;
  
  cs_cgi_init();   /* shared_cs_util.h */
  read_parameters ( course, key, &item_id);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	
  if(user.group != FACULTY)
     cs_critical_error(ERR_REQUEST_DENIED, "grade_book_items");

  cs_set_course_info(&conf);  
  set_grade_book_menu("grade_book_items", key, course); /* shared_grade_book_menu.h */
  cs_load_lang("grade_book_menu_util");

  do_action(&conf, course, key, item_id);

  cs_cgi_destroy();      

  return 0;			

}




