 /*(c) Copyright GBASE. 2032  All rights reserved.                 */
 /*                                                                   */
 /*This sample program is owned by International Business Machines    */
 /*Corporation or one of its subsidiaries ("IBM") and is copyrighted  */
 /*and licensed, not sold.                                            */
 /*                                                                   */
 /*You may copy, modify, and distribute this sample program in any    */
 /*form without payment to IBM,  for any purpose including developing,*/
 /*using, marketing or distributing programs that include or are      */
 /*derivative works of the sample program. Licenses to IBM patents    */
 /*are expressly excluded from this license.                          */
 /*The sample program is provided to you on an "AS IS" basis, without */
 /*warranty of any kind.  IBM HEREBY  EXPRESSLY DISCLAIMS ALL         */
 /*WARRANTIES EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO*/
 /*THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTIC-*/
 /*ULAR PURPOSE. Some jurisdictions do not allow for the exclusion or */
 /*limitation of implied warranties, so the above limitations or      */
 /*exclusions may not apply to you.  IBM shall not be liable for any  */
 /*damages you suffer as a result of using, modifying or distributing */
 /*the sample program or its derivatives.                             */
 /*                                                                   */
 /*Each copy of any portion of this sample program or any derivative  */
 /*work,  must include the above copyright notice and disclaimer of   */
 /*warranty.                                                          */
 /*                                                                   */

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DOTNETDEMO;

namespace Samples
{
	/// <summary>
	/// Summary description for OrderForm.
	/// </summary>
	public class OrderForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private ComboBox CustList;
		private System.Windows.Forms.Label label3;
		private ComboBox ItemsBox;
		private System.Windows.Forms.Button AddOrder;
		private System.Windows.Forms.Button Done;
		private bool _Initialized = false;
		private System.Windows.Forms.TextBox QtyBox;
		private System.Windows.Forms.ErrorProvider FieldErrorProv;
		private ArrayList _OrderList;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public OrderForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			IfxDemo Demo = IfxDemo.GetInstance();
			_OrderList = new ArrayList();
			DataTable CustTable = Demo.DemoCustomers;

			this.CustList.DataSource = CustTable;
			this.CustList.DisplayMember = "fname";
			this.CustList.ValueMember = "cust_num";

			DataTable ItemTable = Demo.DemoItems;
			//DataSource needs an IList implementation
			this.ItemsBox.DataSource = ItemTable;
			this.ItemsBox.DisplayMember = "description";
			this.ItemsBox.ValueMember = "item_num";
			
		}

		public ArrayList NewOrders
		{
			get
			{
				return _OrderList;
			}
		}
		public bool _IsInitialized
		{
			get
			{
				return _Initialized;
			}
			set
			{
				this._Initialized = value;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 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()
		{
			this.CustList = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.ItemsBox = new System.Windows.Forms.ComboBox();
			this.AddOrder = new System.Windows.Forms.Button();
			this.Done = new System.Windows.Forms.Button();
			this.QtyBox = new System.Windows.Forms.TextBox();
			this.FieldErrorProv = new System.Windows.Forms.ErrorProvider();
			this.SuspendLayout();
			// 
			// CustList
			// 
			this.CustList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.CustList.Location = new System.Drawing.Point(40, 56);
			this.CustList.Name = "CustList";
			this.CustList.Size = new System.Drawing.Size(121, 22);
			this.CustList.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(40, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(104, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Select Customer";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(40, 96);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "Select Items";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(192, 96);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 5;
			this.label3.Text = " Quantity";
			// 
			// ItemsBox
			// 
			this.ItemsBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.ItemsBox.Location = new System.Drawing.Point(40, 120);
			this.ItemsBox.Name = "ItemsBox";
			this.ItemsBox.Size = new System.Drawing.Size(121, 22);
			this.ItemsBox.TabIndex = 6;
			this.ItemsBox.SelectionChangeCommitted += new System.EventHandler(this.ItemsBox_SelectedIndexChanged);
			// 
			// AddOrder
			// 
			this.AddOrder.Location = new System.Drawing.Point(40, 168);
			this.AddOrder.Name = "AddOrder";
			this.AddOrder.TabIndex = 7;
			this.AddOrder.Text = "Add Order";
			this.AddOrder.Click += new System.EventHandler(this.AddOrder_Click);
			// 
			// Done
			// 
			this.Done.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Done.Location = new System.Drawing.Point(128, 168);
			this.Done.Name = "Done";
			this.Done.TabIndex = 8;
			this.Done.Text = "Done";
			// 
			// QtyBox
			// 
			this.QtyBox.Enabled = false;
			this.QtyBox.Location = new System.Drawing.Point(192, 120);
			this.QtyBox.Name = "QtyBox";
			this.QtyBox.TabIndex = 9;
			this.QtyBox.Text = "";
			// 
			// OrderForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.QtyBox,
																		  this.Done,
																		  this.AddOrder,
																		  this.ItemsBox,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.CustList});
			this.Name = "OrderForm";
			this.Text = "OrderForm";
			this.ResumeLayout(false);

		}
		#endregion

		private void ItemsBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			DataRowView MyRow = (DataRowView)this.ItemsBox.SelectedItem;
			Int16 Stock = (Int16)MyRow["stock"];
			QtyBox.Enabled = true;
			QtyBox.Text = Stock.ToString();
		}

		private void AddOrder_Click(object sender, System.EventArgs e)
		{
			DataRowView MyRow = (DataRowView)this.ItemsBox.SelectedItem;
			Int16 Stock = (Int16)MyRow["stock"];
			string NotIntegerErr = "Not an integer value."; 

			string BadQtyError = "Please enter a value " + 
									"between 1 and " + Stock;
			string Qty = QtyBox.Text;
			Int32 Qty_Val = 0;
			try
			{
				Qty_Val = Int32.Parse(Qty);
				FieldErrorProv.SetError(QtyBox,"");
				if(Qty_Val <= 0 || Qty_Val > Stock)
				{
					FieldErrorProv.SetError(QtyBox,BadQtyError);
				}
				else
				{
					FieldErrorProv.SetError(QtyBox,"");
					//Add this order to an order Array
					Order NewOrder = new Order();
					DataRowView CustRow = (DataRowView)this.CustList.SelectedItem;
					Int32 ThisCust = (Int32)CustRow["cust_num"];
					Int32 ThisItem = (Int32)MyRow["item_num"];
					NewOrder.CustomerID = ThisCust;
					NewOrder.ItemID = ThisItem;
					NewOrder.Quantity =Qty_Val ;
					NewOrder.Date = DateTime.Now.Date;
					_OrderList.Add(NewOrder);
					ItemsBox.SelectedIndex = 0;
					CustList.SelectedIndex = 0;
					QtyBox.Text = "";
					QtyBox.Enabled = false;
				}
			}
			catch
			{
				FieldErrorProv.SetError(QtyBox,NotIntegerErr);
			}
			
		
		}


	}
}
