using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;

/* =====================================================================

  File:      OrderDetails.aspx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Displays a summary of the selected order and the line items which are part of the order.
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer {

    public partial class OrderDetailsPage : System.Web.UI.Page
    {


//         public OrderDetailsPage() {
//             Page.Init += new System.EventHandler(Page_Init);
//         }

        //*******************************************************
        //
        // The Page_Load event on this page is used to obtain
        // order information from a database and then update
        // UI elements with them.
        //
        //*******************************************************

        private void Page_Load(object sender, System.EventArgs e) {

            // Obtain Order ID from QueryString
            int OrderID = Int32.Parse(Request.Params["OrderID"], CultureInfo.InvariantCulture);

            // Get the customer ID too
            string CustomerId = User.Identity.Name;

            // Obtain Order Details from Database
            OrdersDB orderHistory = new OrdersDB();
            OrderDetails myOrderDetails = orderHistory.GetOrderDetails(OrderID, CustomerId);

            // if order was found, display it
			if (myOrderDetails != null)
			{

				// Bind Items to GridControl
				GridControl1.DataSource = myOrderDetails.OrderItems;
				GridControl1.DataBind();

				// Update labels with summary details
				totalLabel.Text = myOrderDetails.OrderTotal;
				orderNumberLabel.Text = OrderID.ToString(CultureInfo.InvariantCulture);
				orderDateLabel.Text = myOrderDetails.OrderDate.ToShortDateString();
				shipDateLabel.Text = myOrderDetails.ShipDate.ToShortDateString();

				AddressLabelControls billToLabels = new AddressLabelControls(
					billToAddressLine1, billToAddressLine2, billToAddressCity,
					billToAddressStateProvince, billToAddressPostalCode);
				AddressLabelControls shipToLabels = new AddressLabelControls(
					shipToAddressLine1, shipToAddressLine2, shipToAddressCity,
					shipToAddressStateProvince, shipToAddressPostalCode);
				CustomerAddresses addresses = orderHistory.
											  GetSalesOrderAddresses(OrderID);

				billToLabels.FillControls(addresses.BillingAddress);
				shipToLabels.FillControls(addresses.ShippingAddress);
			}
				// otherwise display an error message
				else
			{
				MyError.Text = "Order not found!";
				detailsTable.Visible = false;
			}
        }

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

        #region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {

        }
        #endregion

    }

	/// <summary>
	///	Collects together a set of address label controls for simpler display code.	
	/// Similar to AddressControls.  Perhaps use generics to combine at some point.
	/// </summary>
 
	public class AddressLabelControls
	{
		private Label line1;

		private Label line2;

		private Label city;

		private Label stateProvince;

		private Label postalCode;

		public AddressLabelControls(Label line1, Label line2, Label city,
									Label stateProvince, Label postalCode)
		{
			this.line1 = line1;
			this.line2 = line2;
			this.city = city;
			this.stateProvince = stateProvince;
			this.postalCode = postalCode;
		}

		public void FillControls(CustomerAddress ca)
		{
			if (ca.AddressID > -1)
			{
				line1.Text = ca.Line1;
				line2.Text = ca.Line2;
				city.Text = ca.City;
				stateProvince.Text = ca.StateProvinceName;
				postalCode.Text = ca.PostalCode;
			}
		}
	}
}