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 System.Text;
using System.IO;

public partial class HoverList : System.Web.UI.Page
{
    private busInvoice Invoice = null;
    int Value = 2;
    
    protected void Page_Load(object sender, EventArgs e)
    {
            Invoice = new busInvoice();

            // *** query for a callback - the callback in this
            // *** case creates it's own raw Response and exits (Response.End())
            if (this.LookupPanel.IsCallback)
                this.ProcessCallback();

            // *** If checked we programmatically change the
            // *** behavior of the HoverPanel. Note all of these
            // *** settings can be made declaratively on the control.
            if (this.chkFullInvoice.Checked)
            {
                // *** Go to a separate page	
                this.LookupPanel.ServerUrl = "~/InvoiceAbstract.aspx";
           	
	            // *** Make it so that hover operations don't close the window
                this.LookupPanel.AutoCloseHoverWindow = false;
	            
                this.LookupPanel.PanelOpacity = 1;
                this.LookupPanel.ShadowOffset = 5;
            	
	            /// *** Make the panel draggable and closable
                this.LookupPanel.Width = Unit.Pixel(525); // explicit width
	            this.LookupPanel.Draggable = true;
	            this.LookupPanel.Closable = true;
	            this.LookupPanel.DragHandleID = "PanelHeader";

                //*** Assign the output for the HTML to the inner Content Panel
                this.LookupPanel.HtmlTargetClientId = "PanelContent";

                // *** Move the hover window right from the cursor by 100 pixels
                this.LookupPanel.HoverOffsetRight = 100;
           }

            // *** Handle the GridView display
            int ResultCount = Invoice.GetLastInvoices(100);
            if (ResultCount < 0)
            {
                this.ShowError(Invoice.ErrorMessage);
                return;
            }

            this.gvInvoiceList.DataSource = Invoice.DataSet.Tables["TInvoiceList"].DefaultView;
            this.gvInvoiceList.DataBind();
    }
    
    protected void ShowError(string Message)
    {
        this.lblErrorMessage.Text = Message;
        this.lblErrorMessage.Visible = true;

    }
    protected void gvInvoiceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvInvoiceList.PageIndex = e.NewPageIndex;
        this.gvInvoiceList.DataBind();
    }

    /// <summary>
    /// This is the method that handles the actual callback. It simply
    /// calls out to a business object to load up the invoice detail
    /// and calls a routine that generates the HTML display for it which
    /// is reused in many places of this demo
    /// </summary>
    protected void ProcessCallback()
    {
        string Output = "";

        // *** Use 'static' Html generation in business object
        string strOrderId = Request.QueryString["Id"];

        int OrderId = -1;
        if (!int.TryParse(strOrderId, out OrderId))
            Response.End();

        // *** Notice that we can access form variables here
        // *** IF we specified to POST variables from the HoverPanel
        if (!this.chkFullInvoice.Checked)
            Output = ShowInvoiceDetail(OrderId);
        else
            // *** This code actually never fires because
            // *** we've re-routed the callback DIRECTLY
            // *** to the InvoiceAbstract.aspx page

            // *** Demonstrate dynamically loading content
            // *** from another page.
            Output = ShowAbstractInvoice(OrderId);

        // *** No further output
        Response.Write(Output);
        Response.End();
    }
    
    /// <summary>
    /// Display HTML generated from a packaged routine
    /// </summary>
    /// <param name="OrderId"></param>
    /// <returns></returns>
    protected string ShowInvoiceDetail(int OrderId)
    {
        // *** Create Invoice Bus Object and load Order
        busInvoice Invoice = new busInvoice();
        if (!Invoice.Load(OrderId))
            return "";

        // *** Generate Html for lineitems
        return Invoice.HtmlLineItems();
    }


    /// <summary>
    /// Dynamically load another page and capture its
    /// output to return to the client. 
    /// 
    /// This is contrived in this example, since wwHoverPanel
    /// can directly go to this URL, but it serves how you 
    /// can keep processing context inside of a single page.
    /// </summary>
    /// <param name="OrderId"></param>
    /// <returns></returns>
    protected string ShowAbstractInvoice(int OrderId)
    {
        // *** Call out to another ASPX page to handle 
        // *** Html generation 
        string ErrorMessage = "";
        string Output = AspTextMerge("InvoiceAbstract.aspx?id=" + OrderId.ToString(), out ErrorMessage);
        return Output;
    }


    /// <summary>
    /// Generic routine that can be used to execute another ASP.NET page
    /// and capture the output from the process as a string.
    /// </summary>
    /// <param name="TemplatePageAndQueryString"></param>
    /// <param name="ErrorMessage"></param>
    /// <returns></returns>
    public static string AspTextMerge(string TemplatePageAndQueryString, out string ErrorMessage)
    {
        string MergedText = "";
        ErrorMessage = "";

        // *** Save the current request information
        HttpContext Context = HttpContext.Current;

        // *** Fix up the path to point at the templates directory
        TemplatePageAndQueryString = Context.Request.ApplicationPath + "/" + TemplatePageAndQueryString;

        // *** Now call the other page and load into StringWriter
        StringWriter sw = new StringWriter();
        try
        {
            // *** IMPORTANT: Child page's FilePath still points at current page
            //                QueryString provided is mapped into new page and then reset
            Context.Server.Execute(TemplatePageAndQueryString, sw, false);
            MergedText = sw.ToString();
        }
        catch (Exception ex)
        {
            MergedText = null;
            ErrorMessage = ex.Message;
        }

        return MergedText;
    }

} 
