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 Microsoft.RFIDLab.EPCISDemo.Common;
namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Summary description for Order Details Webform
	/// </summary>
	public class OrderDetails : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtOrderNumber;
		protected System.Web.UI.WebControls.Label lblOrderNumbers;
		protected System.Web.UI.WebControls.Label lblStatus;
		protected System.Web.UI.WebControls.Label lblOrderNumber;
		protected System.Web.UI.WebControls.DataGrid dgOrderDetails;
		protected System.Web.UI.WebControls.Button btnCreateCaseEPC;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.Button btnGetOrderDetails;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//Check if you have been redirected to this page because the session has expired
			if (Request.QueryString["session_expired"] != null)
			{
				if (Request.QueryString["session_expired"] == "true")
				{
					lblStatus.Text = "<B>You have been redirected to this page because your session expired.</B>";
					lblStatus.ForeColor = System.Drawing.Color.Red;
				}
			}
			else
			{
				lblStatus.Text = "";
			}
			
			lblOrderNumbers.Text = "For the demo, use any of the following order numbers - <br>550101000<br>550101001";
			lblOrderNumbers.ForeColor = System.Drawing.Color.Green;

			btnCreateCaseEPC.Visible = false;
			
			if (!Page.IsPostBack)
			{
				Session.Clear();
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnGetOrderDetails.Click += new System.EventHandler(this.btnGetOrderDetails_Click);
			this.btnCreateCaseEPC.Click += new System.EventHandler(this.btnCreateCaseEPC_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnGetOrderDetails_Click(object sender, System.EventArgs e)
		{
			DatabaseManager dm = new DatabaseManager(System.Configuration.ConfigurationSettings.AppSettings["ManufacturerString"]);
			DataSet dsOrderDetails = dm.GetPurchaseOrderDetails(Convert.ToInt32( txtOrderNumber.Text));
			if (dsOrderDetails.Tables[0].Rows.Count > 0) 
			{
				lblStatus.Text = "";
				lblStatus.Visible = false;
				dgOrderDetails.Visible=true;
				dgOrderDetails.DataSource = dsOrderDetails;
				dgOrderDetails.DataBind();
				btnCreateCaseEPC.Visible = true;				
			}
			else
			{
                lblStatus.Visible = true;
				lblStatus.Text = "No details available for this order number.";
				btnCreateCaseEPC.Visible = false;
				dgOrderDetails.Visible = false;
			}
		}

		private void btnCreateCaseEPC_Click(object sender, System.EventArgs e)
		{
			Server.Transfer("CaseEPCGeneration.aspx", true);
		}
	}
}
