 /*(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;
using GBS.Data.GBasedbt;

namespace Samples
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class IfxDemoForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid DemoDataGrid;
		private System.Windows.Forms.Button AddOrder;
		private System.Windows.Forms.Button ViewItems;
		private System.Windows.Forms.Button ViewOrders;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button DelOrder;
		private System.Windows.Forms.Button UpdCust;
		private System.Windows.Forms.Button ActiveCust;
		private IfxDemo _IfxDemo = null;
		public enum _CONTENTS: int
		{
			ORDERS,
			ITEMS,
			CUSTOMERS
		};

		public IfxDemoForm()
		{
			//
			// Required for Windows Form Designer support
			//

			InitializeComponent();
			_IfxDemo = IfxDemo.GetInstance();
				
			LoadGridData(_CONTENTS.ORDERS);

			
		}


		public void LoadGridData(_CONTENTS Contents)
		{
			if(Contents == _CONTENTS.ORDERS)
			{
				//use the data source datatables that IfxDemo has built
				DemoDataGrid.DataSource = _IfxDemo.DemoOrders;
			}
			if(Contents == _CONTENTS.ITEMS)
			{
				DemoDataGrid.DataSource = _IfxDemo.DemoItems;
			
			}
			DemoDataGrid.Select(0);
			
//			DemoDataGrid.SetDataBinding
		}
		/// <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.ViewOrders = new System.Windows.Forms.Button();
			this.DemoDataGrid = new System.Windows.Forms.DataGrid();
			this.AddOrder = new System.Windows.Forms.Button();
			this.ViewItems = new System.Windows.Forms.Button();
			this.DelOrder = new System.Windows.Forms.Button();
			this.UpdCust = new System.Windows.Forms.Button();
			this.ActiveCust = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.DemoDataGrid)).BeginInit();
			this.SuspendLayout();
			// 
			// ViewOrders
			// 
			this.ViewOrders.Location = new System.Drawing.Point(16, 232);
			this.ViewOrders.Name = "ViewOrders";
			this.ViewOrders.TabIndex = 0;
			this.ViewOrders.Text = "View Orders";
			this.ViewOrders.Click += new System.EventHandler(this.ViewOrders_Click);
			// 
			// DemoDataGrid
			// 
			this.DemoDataGrid.DataMember = "";
			this.DemoDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.DemoDataGrid.Location = new System.Drawing.Point(16, 32);
			this.DemoDataGrid.Name = "DemoDataGrid";
			this.DemoDataGrid.ReadOnly = true;
			this.DemoDataGrid.Size = new System.Drawing.Size(576, 184);
			this.DemoDataGrid.TabIndex = 1;
			// 
			// AddOrder
			// 
			this.AddOrder.Location = new System.Drawing.Point(96, 232);
			this.AddOrder.Name = "AddOrder";
			this.AddOrder.TabIndex = 2;
			this.AddOrder.Text = "Add Order";
			this.AddOrder.Click += new System.EventHandler(this.AddOrder_Click);
			// 
			// ViewItems
			// 
			this.ViewItems.Location = new System.Drawing.Point(176, 232);
			this.ViewItems.Name = "ViewItems";
			this.ViewItems.TabIndex = 3;
			this.ViewItems.Text = "View Items";
			this.ViewItems.Click += new System.EventHandler(this.ViewItems_Click);
			// 
			// DelOrder
			// 
			this.DelOrder.Location = new System.Drawing.Point(256, 232);
			this.DelOrder.Name = "DelOrder";
			this.DelOrder.Size = new System.Drawing.Size(80, 23);
			this.DelOrder.TabIndex = 4;
			this.DelOrder.Text = "Delete Order";
			this.DelOrder.Click += new System.EventHandler(this.DelOrder_Click);
			// 
			// UpdCust
			// 
			this.UpdCust.Location = new System.Drawing.Point(344, 232);
			this.UpdCust.Name = "UpdCust";
			this.UpdCust.Size = new System.Drawing.Size(104, 23);
			this.UpdCust.TabIndex = 5;
			this.UpdCust.Text = "Update Customer";
			this.UpdCust.Click += new System.EventHandler(this.UpdCust_Click);
			// 
			// ActiveCust
			// 
			this.ActiveCust.Location = new System.Drawing.Point(456, 232);
			this.ActiveCust.Name = "ActiveCust";
			this.ActiveCust.Size = new System.Drawing.Size(136, 23);
			this.ActiveCust.TabIndex = 6;
			this.ActiveCust.Text = "View Active Customers";
			this.ActiveCust.Click += new System.EventHandler(this.ActiveCust_Click);
			// 
			// IfxDemoForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 293);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.ActiveCust,
																		  this.UpdCust,
																		  this.DelOrder,
																		  this.ViewItems,
																		  this.AddOrder,
																		  this.DemoDataGrid,
																		  this.ViewOrders});
			this.Name = "IfxDemoForm";
			this.Text = "GBasedbt .NET Demo";
			((System.ComponentModel.ISupportInitialize)(this.DemoDataGrid)).EndInit();
			this.ResumeLayout(false);

		}
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new IfxDemoForm());
		}
		#endregion

		private void ViewOrders_Click(object sender, System.EventArgs e)
		{
			LoadGridData(_CONTENTS.ORDERS);
			DelOrder.Enabled = true;
		}

		private void ViewItems_Click(object sender, System.EventArgs e)
		{
			LoadGridData(_CONTENTS.ITEMS);
			DelOrder.Enabled = false;
		}

		private void AddOrder_Click(object sender, System.EventArgs e)
		{
			OrderForm Order = new OrderForm();
			DialogResult Res = Order.ShowDialog();
			if(Res == DialogResult.OK)
			{
				ArrayList Orders = Order.NewOrders;
				_IfxDemo.CreateOrders(Orders);
				LoadGridData(_CONTENTS.ORDERS);
				
			}
			
		}

		private void DelOrder_Click(object sender, System.EventArgs e)
		{
			string DelMsg = "Do you want to permanently delete the selected order ?";
			DialogResult Res = 
				MessageBox.Show(DelMsg,"Confirmation",MessageBoxButtons.YesNo);
			if(Res == DialogResult.No)
				return;
			//Get the id of current selected row
			int SelRow = DemoDataGrid.CurrentCell.RowNumber;
			DataTable OrdersTable = (DataTable)DemoDataGrid.DataSource;
			DataRow ThisRow = OrdersTable.Rows[SelRow];

			//These two statements can be used to mark the row for 
			//deletion in the DataSet itself and perform the updates
			//based on the changes in the dataset.
//			ThisRow.Delete();
//			_IfxDemo.DeleteOrders();

			//The following delete will use the command object
			int Order_Num = (int)ThisRow["order_num"];
			int Cust_Num = (int)ThisRow["cust_num"];
			Order DelOrder = new Order();
			DelOrder.OrderID = Order_Num;
			DelOrder.CustomerID = Cust_Num;
			ArrayList Orders = new ArrayList();
			Orders.Add(DelOrder);
			_IfxDemo.DeleteOrders(Orders);
			LoadGridData(_CONTENTS.ORDERS);
			DemoDataGrid.Update();
		}

		private void UpdCust_Click(object sender, System.EventArgs e)
		{
			CustomerRecord CustForm = new CustomerRecord();
			CustForm.ShowDialog();
		}

		private void ActiveCust_Click(object sender, System.EventArgs e)
		{
			ActiveCustomers ActiveCusts = new ActiveCustomers();
			ActiveCusts.ShowDialog();
		}

	}

}
