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 Westwind.Tools;
using Westwind.Web.Controls;

using ScriptCallbacks.BusinessObjects;
using System.Text;

public partial class ComplexObjects : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CustomerEntity Entity = new CustomerEntity();
    }
    
    [CallbackMethod]
    protected DataTable GetCustomerList()
    {
        busCustomer Customer = new busCustomer();
        if ( Customer.GetCustomerList() < 0)
            throw new ApplicationException(Customer.ErrorMessage);

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

    [CallbackMethod]
    protected CustomerEntity GetCustomerEntity(string CustomerId)
    {
        busCustomer Customer = new busCustomer();
        if (CustomerId == null || CustomerId == "")
            CustomerId = "ALFKI";

        if (!Customer.Load(CustomerId))
            throw new ApplicationException("Customer doesn't exist");

        if (this.chkReturnOrderEntities.Checked)
        {
            // *** Load array of orders into Orders property of entity
            Customer.Entity.LoadOrders();
        }
       
        return Customer.Entity;
    }

    [CallbackMethod]
    protected string ReturnCustomerEntity(CustomerEntity Customer)
    {
        StringBuilder sb = new StringBuilder("Echo from Server (" + DateTime.Now.ToString("T") + ")\r\n\r\n" +
               Customer.Companyname + "\r\n" +
               Customer.Contactname + "\r\n" +
               Customer.Address + "\r\n" + 
               Customer.PhoneNumbers.HomePhone );
        
        if (Customer.Orders != null)
        {
            sb.Append("\r\n\r\nOrders returned:\r\n");
            foreach(OrdersRow Order in Customer.Orders)
            {

                sb.Append(Order.Orderid.ToString() + " - " + Order.Orderdate.ToString("d") + "\r\n");
            }
        }

        return sb.ToString();
    }
}