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

using Telerik.Web.UI;
using System.Security.Principal;

using StaticAppGlobals;

public partial class CentralTicketing_Interfaces_UserAssignedTicketListingsByStatus : BaseUserControl //System.Web.UI.UserControl
{

    protected int SelectedTab
    {
        get
        {
            return Convert.ToInt32(SelectedTabNumber.Value);
        }
        set
        {
            SelectedTabNumber.Value = value.ToString();
        }

    }

    protected List<getCTicketsAssignedSystemsByStatusResult> UsersTicketListInSystemsByStatus
    {
        get
        {
            try
            {
                return (List<getCTicketsAssignedSystemsByStatusResult>)HttpContext.Current.Session["UsersTicketListInSystemsByStatus"];
            }
            catch
            {
                HttpContext.Current.Session["UsersTicketListInSystemsByStatus"] = cticketservice.getCTicketsAssignedSystemsListByStatus(UserProfile.user_login, id_system);
                return (List<getCTicketsAssignedSystemsByStatusResult>)HttpContext.Current.Session["UsersTicketListInSystemsByStatus"];
            }
        }
        set
        {
            HttpContext.Current.Session["UsersTicketListInSystemsByStatus"] = value;
        }
    }

    CentralTicketing_Interfaces_WorkListViewControlByStatus CTicketListControl;

    public int id_system
    {
        get { return (int)ViewState["id_system"]; }
        set { ViewState["id_system"] = value; }
    }

    public string ViewingTicketStatus
    {
        get
        {
            try
            {
                return ViewState["ViewingTicketStatus"].ToString();
            }
            catch
            {
                ViewState["ViewingTicketStatus"] = "";
                return "";
            }
        }
        set { ViewState["ViewingTicketStatus"] = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        //UserProfile = new CentralUserProfile(UserId);
        base.Page_Load();

        string culprite = GetPostBackControl();

        DoTabs();
        if (TicketsTypeListTabStrip.SelectedIndex == -1)
        {
            TicketsTypeListTabStrip.SelectedIndex = 0;
            SelectedTab = 0;
        }
        if (TicketsTypeListTabStrip.Tabs.Count > 0)
        {
            ViewingTicketStatus = UsersTicketListInSystemsByStatus[TicketsTypeListTabStrip.SelectedIndex].system_status;
            //AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystemsByType[SelectedTab].id_system);
            //TicketsTypeListTabStrip.SelectedTab.PageViewID = "CTicketPageView" + SelectedTab.ToString();
            TicketsTypeListTabStrip.SelectedTab.PageView.Selected = true;
        }

    }

    public string GetPostBackControl()
    {
        try
        {
            Control control = null;
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            (Page.FindControl("TxtEventTarget") as TextBox).Text = ctrlname;

            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
            // if __EVENTTARGET is null, the control is a button type and we need to 
            // iterate over the form collection to find it

            //ctrlname = "TicketsWorklist$ctl01$TicketsTypeListTabStrip"
            //"TicketsWorklist_ctl00_TicketsTypeListTabStrip"

            else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                foreach (string ctl in Page.Request.Form)
                {
                    // handle ImageButton controls ...
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = Page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control.ClientID;
        }
        catch
        {
            return "";
        }
    }  

    public void Refresh()
    {
        if (TicketsTypeListTabStrip.SelectedIndex != -1)
        {
            DoTabs();
            RefreshList();
        }
    }

    public void RefreshList()
    {
        TicketsTypeListTabStrip.SelectedIndex = SelectedTab;
        TicketsTypeListTabStrip.MultiPage.SelectedIndex = SelectedTab;
        CTicketListControl = (CentralTicketing_Interfaces_WorkListViewControlByStatus)TicketsTypeListTabStrip.MultiPage.Controls[SelectedTab].Controls[0];
        CTicketListControl.Refresh();
    }

    public void RowSelected(object sender, EventArgs e)
    {
    }

    public void setTimer(int msecs)
    {
        TicketListingsTimer.Interval = msecs;
    }

    protected void TicketListingsTimer_Tick(object sender, EventArgs e)
    {
        Refresh();
    }

    protected void DoTabs()
    {
        int TabNumber = 0;
        UsersTicketListInSystemsByStatus = cticketservice.getCTicketsAssignedSystemsListByStatus(UserProfile.user_login, id_system);
        foreach (getCTicketsAssignedSystemsByStatusResult CTicketList in UsersTicketListInSystemsByStatus)
        {
            if (TicketsTypeListTabStrip.Tabs.Count() > TabNumber)
            {
                TicketsTypeListTabStrip.Tabs[TabNumber].Text = CTicketList.system_label;
                if (SelectedTab == TabNumber)
                {
                    ViewingTicketStatus = CTicketList.system_status;
                    TicketsTypeListTabStrip.SelectedIndex = SelectedTab;
                    TicketsTypeListTabStrip.MultiPage.SelectedIndex = SelectedTab;
                }
                AddWorkListViewControl(TabNumber, (int)CTicketList.id_system, CTicketList.system_status);
            }
            else
            {
                if (SelectedTab == TabNumber)
                {
                    ViewingTicketStatus = CTicketList.system_status;
                }
                TicketsTypeListTabStrip.Tabs.Add(CreateTab(CTicketList.system_label, TabNumber, (int)CTicketList.id_system));
                AddWorkListViewControl(TabNumber, (int)CTicketList.id_system, CTicketList.system_status);
            }
            TabNumber++;
        }
    }

    private RadTab CreateTab(string Tabtext, int TabNumber, int id_system)
    {
        RadTab tab = new RadTab();
        tab.Text = Tabtext;
        tab.PageViewID = "CTicketPageView" + TabNumber.ToString();
        tab.TabIndex = (short)TabNumber;

        CreatePageview(TabNumber, id_system);

        return tab;
    }

    protected void CreatePageview(int index, int id_system)
    {
        RadPageView pageView = new RadPageView();
        pageView.ID = "CTicketPageView" + index.ToString();
        pageView.Visible = true;
        pageView.Enabled = true;
        pageView.EnableViewState = true;

        TicketsListMultiPage.PageViews.Add(pageView);
    }

    protected void AddWorkListViewControl(int index, int id_system, string ticket_status)
    {
        RadPageView pageView = TicketsListMultiPage.PageViews[index];
        CTicketListControl = new CentralTicketing_Interfaces_WorkListViewControlByStatus();
        //CentralTicketing_Interfaces_WorkListViewControl CTicketListControl = new CentralTicketing_Interfaces_WorkListViewControl();
        CTicketListControl.ID = "CTicketListControl" + index.ToString();

        CTicketListControl = (CentralTicketing_Interfaces_WorkListViewControlByStatus)LoadControl("~/CentralTicketing/Interfaces/WorkListViewControlByStatus.ascx");

        CTicketListControl.RowSelected += new EventHandler(RowSelected);

        CTicketListControl.id_system = id_system;
        CTicketListControl.ViewingTicketStatus = ticket_status;
        CTicketListControl.EnableViewState = true;

        pageView.Controls.Add(CTicketListControl);

        //CTicketListControl.Refresh();
    }

    protected void TicketsTypeListTabStrip_TabClick(object sender, RadTabStripEventArgs e)
    {
        SelectedTab = e.Tab.Index;
        //ViewingTicketType = UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].ticket_type;
        //AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].id_system, UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].ticket_type);
        //e.Tab.PageViewID = "CTicketPageView" + SelectedTab.ToString(); ;
        //e.Tab.PageView.Selected = true;
        //(Page.FindControl("BottomPane") as RadPane).Collapsed = true;

    }

    protected void TicketsTypeListTabStrip_Load(object sender, EventArgs e)
    {
        //if (TicketsTypeListTabStrip.SelectedIndex != -1)
        //{
        //    SelectedTab = TicketsTypeListTabStrip.SelectedIndex;
        //    ViewingTicketType = UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].ticket_type;
        //    AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].id_system, UsersTicketListInSystemsByType[TicketsTypeListTabStrip.SelectedIndex].ticket_type);
        //    TicketsTypeListTabStrip.SelectedTab.PageViewID = "CTicketPageView" + SelectedTab.ToString(); ;
        //    TicketsTypeListTabStrip.SelectedTab.PageView.Selected = true;
        //    (Page.FindControl("BottomPane") as RadPane).Collapsed = true;
        //}
    }
}
