using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Collections;

using System.Globalization;

using System.Data;

 

using Utilities;

using PizzaLibrary;

 

 

namespace PaymentProcessor

{

    public partial class PizzaOrder : System.Web.UI.Page

    {

        ArrayList PizzaOrderList = new ArrayList();

        DBConnect objDB = new DBConnect();

        DataSet myDS;

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                myDS = objDB.GetDataSet("SELECT * FROM Pizza");

                gvPizzas.DataSource = myDS;

                gvPizzas.DataBind();

            }

        }

 

        protected void btnOrder_Click(object sender, EventArgs e)

        {

            String strOrderConfirmation = "";

            Pizza objPizza;

            PizzaFunctions objPizzaFunctions = new PizzaFunctions(objDB);

 

 

            for (int currentRow = 0; currentRow < gvPizzas.Rows.Count; currentRow++)

            {

                CheckBox CBox;

                DropDownList DDLBox;

                TextBox TBOX;

 

                CBox = (CheckBox)gvPizzas.Rows[currentRow].FindControl("chkSelect");

 

                if (CBox.Checked == true)

                {

                    DDLBox = (DropDownList)gvPizzas.Rows[currentRow].FindControl("ddlSize");

                    TBOX = (TextBox)gvPizzas.Rows[currentRow].FindControl("txtQuantity");

 

                    objPizza = new Pizza();

                    objPizza.PizzaType = gvPizzas.Rows[currentRow].Cells[1].Text;

                    objPizza.Size = DDLBox.SelectedValue;

 

                    int quantity;

                    if (int.TryParse(TBOX.Text, out quantity) != true)

                    {

                        PizzaOrderList.Clear();

                        strOrderConfirmation = "You must enter a valid quantity for each selected pizza.";

                        lblDisplay.Text = strOrderConfirmation;

                        return;

                    }

 

                    objPizza.Quantity = quantity;

 

                    objPizza.Price = objPizzaFunctions.ComputePrice(objPizza.PizzaType, objPizza.Size);

                    objPizza.TotalCost = objPizza.Price * objPizza.Quantity;

                    objPizzaFunctions.UpdateTotalSales(objPizza.PizzaType, objPizza.TotalCost);

                    PizzaOrderList.Add(objPizza);

                }

            }

 

 

            if (PizzaOrderList.Count == 0)

            {

                lblDisplay.Text = "You must select at least 1 pizza before placing your order!";

                return;

            }

 

            strOrderConfirmation = "Thanks for ordering from Pascucci's Pizza Palace. Here is a summary of your order: <br />";

            strOrderConfirmation += "Name: " + txtName.Text + "<br />";

            strOrderConfirmation += "Address: " + txtAddress.Text + "<br />";

            strOrderConfirmation += "Phone: " + txtPhone.Text + "<br />";

            strOrderConfirmation += "Method: " + rblOrderMethod.SelectedValue + "<br />";

            lblDisplay.Text = strOrderConfirmation;

 

            gvOrder.DataSource = PizzaOrderList;

            gvOrder.DataBind();

 

            Decimal total = 0;

            int totalQty = 0;

 

            for (int i = 0; i < gvOrder.Rows.Count; i++)

            {

                totalQty = totalQty + int.Parse(gvOrder.Rows[i].Cells[2].Text);

                total = total + Decimal.Parse(gvOrder.Rows[i].Cells[4].Text, NumberStyles.Currency);

            }

 

            gvOrder.Columns[0].FooterText = "Totals:";

            gvOrder.Columns[2].FooterText = totalQty.ToString("#,###.##");

            gvOrder.Columns[4].FooterText = total.ToString("C2");

            gvOrder.DataBind();

 

            // Store order information in the Session object for the payment processing page

            Session["OrderTotal"] = total;

            Session["Name"] = txtName.Text;

            Session["Address"] = txtAddress.Text;

            Session["Phone"] = txtPhone.Text;

            Session["Method"] = rblOrderMethod.SelectedValue;

 

            gvPizzas.Visible = false;

            btnOrder.Visible = false;

            gvOrder.Visible = true;

            btnPay.Visible = true;

 

        }

 

        protected void btnPay_Click(object sender, EventArgs e)

        {

            Response.Redirect("PaymentDemo.aspx");

        }

    }

}