using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using ScriptCallbacks.BusinessObjects;
using Westwind.Web.Controls;
using Westwind.Tools;

public partial class CustomerList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    /// <summary>
    /// return a simple DataTable to the client which
    /// can access the table as an Array of objects.
    /// </summary>
    /// <returns></returns>
    [CallbackMethod]
    public DataTable GetCustomerList()
    {
        busCustomer Customer = new busCustomer();
        if (Customer.GetCustomerList() < 0)
            return null;

        return Customer.DataSet.Tables["TCustomerList"];        
    }

    /// <summary>
    /// You can return data as a DataRow or an Entity
    /// object. The result on the client is actually 
    /// identical - both result in an object with properties
    /// for each of the fields of the DataRow or Entity object.
    /// </summary>
    /// <param name="CustomerId"></param>
    /// <returns></returns>
    [CallbackMethod]
    public DataRow GetCustomerRow(string CustomerId)
    {
        busCustomer Customer = new busCustomer();
        if (!Customer.Load(CustomerId))
            return null;

        return Customer.DataRow;
    }

    /// <summary>
    /// Returning an object returns the object directly to the
    /// client. The result is identical to the DataRow return
    /// (except that the fieldnames are different in case).
    /// </summary>
    /// <param name="CustomerId"></param>
    /// <returns></returns>
    [CallbackMethod]
    public CustomersRow GetCustomerEntity(string CustomerId)
    {
        busCustomer Customer = new busCustomer();
        if (!Customer.Load(CustomerId))
            return null;

        return Customer.Entity;
    }

    /// <summary>
    /// Demonstrates using POSTback form vars which in many cases
    /// is more efficient than sending data back as objects (see commented
    /// code below) since you have to somewhere read the form variables
    /// back into the object. It's more efficient to do on the server
    /// with the help of the compiler and Intellisense.
    /// </summary>
    /// <param name="CustomerId"></param>
    /// <returns></returns>
    [CallbackMethod]
    public bool SaveCustomer()
    {
        // *** List Selected Values must be explicitly read
        // *** because of the way Postback binding works without
        // *** databinding.
        string CustomerId = Request.Form["lstCustomers"];

        busCustomer Customer = new busCustomer();

        if (CustomerId == null)
            throw new ApplicationException("Invalid CustomerId");
        
        if (CustomerId == "")
        {
            if (!Customer.New())
                throw new ApplicationException("Couldn't create new customer.");
            ;
        }
        else
        {
            if (!Customer.Load(CustomerId))
                throw new ApplicationException("Couldn't load customer");
        }

        // *** Pick up values that were posted back from the
        // *** the client callback
        Customer.Entity.Contactname = this.txtContactName.Text;
        Customer.Entity.Companyname = this.txtCompany.Text;
        Customer.Entity.Address = this.txtAddress.Text;
        Customer.Entity.City = this.txtCity.Text;
        Customer.Entity.Country = this.txtCountry.Text;

        if (!Customer.Validate())
            throw new ApplicationException(Customer.ValidationErrors.ToString());
  
        if (!Customer.Save())
            throw new ApplicationException(Customer.ErrorMessage);

        return true;
    }

    //[CallbackMethod]
    //// *** this method isn't used because it is actually less 
    //// *** efficient and more work to pass data this way on the
    //// *** client. 
    //public bool SaveCustomerEntity(CustomerEntity UpdatedCustomer)
    //{
    //    if (UpdatedCustomer == null)
    //        throw new ApplicationException("Invalid Customer Information.");

    //    busCustomer Customer = new busCustomer();

    //    if (UpdatedCustomer.Customerid == "")
    //    {
    //        if (!Customer.New())
    //            throw new ApplicationException("Couldn't create new customer.");
    //    }
    //    else
    //    {
    //        if (!Customer.Load(UpdatedCustomer.Customerid))
    //            throw new ApplicationException("Couldn't load customer");
    //    }

    //    // *** Copy the values from the Entity to the actual entity
    //    // *** Required since the Entity is backed by a data row and the
    //    // *** returned entity doesn't have the DataRow set
    //    wwDataUtils.CopyObjectData(UpdatedCustomer,Customer.Entity);

    //    if (!Customer.Validate())
    //        throw new ApplicationException(Customer.ValidationErrors.ToString());

    //    if (!Customer.Save())
    //        throw new ApplicationException(Customer.ErrorMessage);

    //    return true;
    //}
}
