using System;
using System.Web.UI;
using Northwind45.BusinessObject;
using System.Web.Services;
 
namespace Northwind45
{
    public partial class GridViewAddEdit_Products : System.Web.UI.Page
    {
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DdlSupplierID.DataSource = Suppliers.SelectSuppliersDropDownListData();
                DdlSupplierID.DataBind();
 
                DdlCategoryID.DataSource = Categories.SelectCategoriesDropDownListData();
                DdlCategoryID.DataBind();
 
            }
        }
 
        protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            Functions.GridViewRowDataBound(sender, e, 1);
        }
 
        protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            Functions.GridViewRowCreated(sender, e, 1);
        }
 
        public ProductsCollection GetGridData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            return Products.SelectSkipAndTake(maximumRows, startRowIndex, out totalRowCount, sortByExpression);
        }
 
        public SuppliersCollection GetSuppliersDropDownListData()
        {
            return Suppliers.SelectSuppliersDropDownListData();
        }
 
        public CategoriesCollection GetCategoriesDropDownListData()
        {
            return Categories.SelectCategoriesDropDownListData();
        }
 
        public void DeleteGridItem(int productID)
        {
            try
            {
                Products.Delete(productID);
            }
            catch (Exception ex)
            {
                Functions.ShowModalError(ex, this);
            }
        }
 
        protected void BtnAddRecord_Click(object sender, EventArgs e)
        {
            AddOrUpdateRecord("add");
        }
 
        protected void BtnUpdateRecord_Click(object sender, EventArgs e)
        {
            AddOrUpdateRecord("update");
        }
 
        private void AddOrUpdateRecord(string operation)
        {
            if (IsValid)
            {
                Products objProducts;
 
                if (operation == "update")
                    objProducts = Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(HfldProductID.Value));
                else
                {
                    objProducts = new Products();
                }
 
                objProducts.ProductName = TxtProductName.Text;
                objProducts.Discontinued = CbxDiscontinued.Checked;
 
                if (String.IsNullOrEmpty(DdlSupplierID.SelectedValue))
                    objProducts.SupplierID = null;
                else
                    objProducts.SupplierID = Convert.ToInt32(DdlSupplierID.SelectedValue);
 
                if (String.IsNullOrEmpty(DdlCategoryID.SelectedValue))
                    objProducts.CategoryID = null;
                else
                    objProducts.CategoryID = Convert.ToInt32(DdlCategoryID.SelectedValue);
 
                if (String.IsNullOrEmpty(TxtQuantityPerUnit.Text))
                    objProducts.QuantityPerUnit = null;
                else
                    objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text;
 
                if (String.IsNullOrEmpty(TxtUnitPrice.Text))
                    objProducts.UnitPrice = null;
                else
                    objProducts.UnitPrice = Convert.ToDecimal(TxtUnitPrice.Text);
 
                if (String.IsNullOrEmpty(TxtUnitsInStock.Text))
                    objProducts.UnitsInStock = null;
                else
                    objProducts.UnitsInStock = Convert.ToInt16(TxtUnitsInStock.Text);
 
                if (String.IsNullOrEmpty(TxtUnitsOnOrder.Text))
                    objProducts.UnitsOnOrder = null;
                else
                    objProducts.UnitsOnOrder = Convert.ToInt16(TxtUnitsOnOrder.Text);
 
                if (String.IsNullOrEmpty(TxtReorderLevel.Text))
                    objProducts.ReorderLevel = null;
                else
                    objProducts.ReorderLevel = Convert.ToInt16(TxtReorderLevel.Text);
 
                // the insert method returns the newly created primary key
                int newlyCreatedPrimaryKey;
 
                try
                {
                    if (operation == "update")
                        objProducts.Update();
                    else
                        newlyCreatedPrimaryKey = objProducts.Insert();
                }
                catch (Exception ex)
                {
                    if (operation == "update")
                        Functions.ShowModalError(ex, this"An error occured during item update!");
                    else
                        Functions.ShowModalError(ex, this"An error occured during item addition!");
                }
 
                GridView1.DataBind();
            }
        }
 
        [WebMethod]
        public static Products GetProducts(string productID)
        {
            return Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(productID));
        }
    }
}