/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/sc/sc.html>
 * November 22, 1995.
 *
 * See the URL above for more information.
 *
 * Caveats: don't have a item name in the form that starts with uppercase 'X'
 */

#include "cgi_common.h"
#include "cgi_display.h"
#include "cgi_error_handlers.h"
#include "cgi_defines.h"
#include "sc_defines.h"
#include "items.h"

int content_type_displayed = FALSE;

/******************************************************************/
/* Function declarations */

int set_order_flags(int number_of_entries, 
			    int *test_order, int *international_order, entry entries[]);
int mail_order(int number_of_entries, int international_order, entry entries[], item items[]);
int display_order_results_intro(int test_order);
int display_order(int number_of_entries, int international_order, entry entries[], item items[]);
int display_order_signature(int test_order);
	
/******************************************************************/

int main()
{
  entry entries[MAX_ENTRIES];
  item items[MAX_ITEMS];  
  int number_of_entries, test_order, international_order;
  char buf[200];
  
  check_method("POST", "main");
  check_content_type("main");
  sprintf(buf, "Results of %s order", ORGANISATION);
  display_content_type(buf);  
  read_items(DEFAULT_ITEMS_FILE, items);
  
  number_of_entries = read_entries(entries);
  set_order_flags(number_of_entries, &test_order, &international_order, entries);

  display_order_results_intro(test_order);
  display_order(number_of_entries, international_order, entries, items);
  display_order_signature(test_order);
  
  if (!test_order)
    mail_order(number_of_entries, international_order, entries, items);

  return TRUE;
}

/******************************************************************/

int set_order_flags(int number_of_entries, 
		     int *test_order, int *international_order, entry entries[])
{
  int i;
  
  *test_order = TRUE;
  *international_order = FALSE;

  for(i = 0; i <= number_of_entries; i++)
    {
      if (strcmp(entries[i].name, "E-mail") == 0)
	if ((entries[i].val[0] == '\0') || (strstr(entries[i].val, "@") == NULL))
	  display_error("Invalid e-mail address specified; you must specify a valid e-mail address!");
      
      if (entries[i].name[0] == 'X') /* see what the action or the country is */
	{
	  if (strcmp(entries[i].val, "submit") == 0)
	    *test_order = FALSE;
	  if (strstr(entries[i].val, "outside_US") != NULL) 
	    *international_order = TRUE;
	}
    }
  return TRUE;
}

/******************************************************************/

int display_order(int number_of_entries, int international_order, entry entries[], item items[])
{
  char desc[ITEM_DESCRIPTION_STRING_LENGTH];
  int i;
  double price = 0.0, total_price = 0.0, shipping_price = 0.0, total_shipping_price = 0.0;
  
  printf("<p>\n");
  for(i = 0; i <= number_of_entries; i++)
    if (entries[i].val[0] != '\0')
      {
	if (get_item_desc_price(entries[i].name, atoi(entries[i].val), &price,  &shipping_price, desc, items))
	  {
	    printf("<tr align=center> <td> %s </td> <td> %d </td> <td> $%5.2f + shipping ($%5.2f) </td> </tr>\n",
		   desc, atoi(entries[i].val), price, shipping_price);
	    total_price += price;
	    total_shipping_price += shipping_price;
	  }
	else
	  {
	    if (entries[i].name[0] != 'X')
	      printf("%s: %s<br>\n",entries[i].name, entries[i].val);
	    else
	      {
		if (strcmp(entries[i].val, "start_table") == 0)
		  {
		    printf("</p>\n<table border>\n");
		    printf("<tr> <th> Item </th> <th> Quantity </th> <th> Price </th> </tr>\n");
		  }
		if (strcmp(entries[i].val, "end_table") == 0)
		  printf("</table>\n<p>\n");
	      }
	  }
      }
  if (international_order) 
    total_shipping_price += INTERNATIONAL_SHIPPING_ADDON;
  printf("</p>\n");
  printf("<p> The cost of your order is $%5.2f, with\n", total_price);
  printf("a shipping and handling charge of $%5.2f.\n</p>\n",  total_shipping_price);
  printf("<p><b>Total: $%5.2f</b></p>\n", total_price + total_shipping_price);
  return TRUE;
}

/******************************************************************/

int mail_order(int number_of_entries, int international_order, entry entries[], item items[])
{
  char buf[1000], file_name[1000], desc[ITEM_DESCRIPTION_STRING_LENGTH];
  FILE *output_fp;
  int i;
  double price = 0.0, total_price = 0.0, shipping_price = 0.0, total_shipping_price = 0.0;
  
  sprintf(buf, ".%d", (int) getpid());
  strcpy(file_name, TMP_FILENAME_STRING);
  strcat(file_name, buf);

  open_file(&output_fp, file_name, "w", "mail_order");
  fprintf(output_fp, "Subject: %s order\n\n", ORGANISATION);

  for(i = 0; i <= number_of_entries; i++)
    if (entries[i].val[0] != '\0')
      {
	if (get_item_desc_price(entries[i].name, atoi(entries[i].val), &price, &shipping_price, desc, items))
	  {
	    fprintf(output_fp, "$%5.2f + shipping ($%5.2f) for %2d %s\n", price,
		    shipping_price, atoi(entries[i].val), desc);
	    total_price += price;
	    total_shipping_price += shipping_price;
	  }
	else
	  {
	    if (entries[i].name[0] != 'X')
	      fprintf(output_fp, "%s: %s\n",entries[i].name, entries[i].val);
	    else
	      {
		if (strcmp(entries[i].val, "start_table") == 0)
		  fprintf(output_fp, "\n");
		if (strcmp(entries[i].val, "end_table") == 0)
		  fprintf(output_fp, "\n");
	      }
	  }	
      }
  if (international_order) total_shipping_price += INTERNATIONAL_SHIPPING_ADDON;
  fprintf(output_fp, "\n\nThe cost of the order is $%5.2f, with a shipping price of $%5.2f.\n", 
	  total_price, total_shipping_price);
  fprintf(output_fp, "\n\n-------------------------------------------------\n");
  fprintf(output_fp, "TOTAL PAYMENT DUE (please pay promptly): $%5.2f\n", 
	  total_price + total_shipping_price);
  fprintf(output_fp, "-------------------------------------------------\n");
  fprintf(output_fp, "\n\nPlease send payment to:\n");
  fprintf(output_fp, "Ram Samudrala\n10418 Marine View Drive\nMukilteo WA 98275\n");
  close_file(&output_fp, file_name, "mail_order");
  sprintf(buf, "%s %s <%s", SENDMAIL_LOCATION, CARETAKER, file_name);
  system(buf);
/*  remove(file_name); */
  return TRUE;
}

/******************************************************************/

int display_order_results_intro(int test_order)
{
  if (test_order)
    {     
      printf("<h1> Thank you for your order! </h1>");
      printf("<p> The items you ordered are listed below along with the cost.\n");
      printf("If are certain of your order and  wish it to be mailed to %s,\n", ORGANISATION);
      printf("please use the \"back\" button in your browser and place the order.\n");
      printf("You can also change your order in this manner.</p>\n");
      printf("<hr>\n");
    }
  else
    {
      printf("<h1> Your order as listed below has been mailed to %s. </h1>\n", ORGANISATION);
      printf("<p> You will receive an e-mail confirming your order within 48 hours. Thank you!</p>\n");
      printf("<hr>\n");
    }
  return TRUE;
}

/******************************************************************/

int display_order_signature(int test_order)
{
  if (!test_order)
    {
      printf("<p>Please send payment to:</p>\n");
      printf("<pre>\nRam Samudrala\n");
      printf("10418 Marine View Drive\n");
      printf("Mukilteo, WA 98275\n</pre>\n");
    }
  display_signature(SIGNATURE_FILE);
  return TRUE;
}

/******************************************************************/

