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_WorkListViewControlByStatus : BaseUserControl //System.Web.UI.UserControl
{
    //protected IIdentity UserId = HttpContext.Current.User.Identity;
    //protected CentralUserProfile UserProfile;
    //protected CTicketsServiceClient cticketservice = new CTicketsServiceClient("WSHttpBinding_ICTicketsService1");
    protected string VirtualHostURL = System.Configuration.ConfigurationSettings.AppSettings["VirtualHostURL"];
    protected int system_id;

    public event EventHandler RowSelected;

    protected GridItemCollection gridSelectedItems = new GridItemCollection();
    protected List<ctickets_v_worklist> worklist = new List<ctickets_v_worklist>();

    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();
        if (ViewingTicketStatus == "")
            ViewingTicketStatus = "";
    }

    public void Refresh()
    {
        TicketsListGrid.Rebind();
    }

    protected void TicketsListGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGrid thisRadgrid = (RadGrid)source;

        if (thisRadgrid.SelectedItems.Count > 0)
        {
            gridSelectedItems = thisRadgrid.SelectedItems;
        }

        ctickets_worklist_order worklist_order = cticketservice.GetWorklistOrder(UserProfile.login);

        if (worklist_order != null)
        {
            if (worklist_order.order_fieldname != "" && worklist_order.order_fieldname != null)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = worklist_order.order_fieldname;
                expression.SetSortOrder((worklist_order.order_ref.EndsWith(".UP") ? "Ascending" : "Descending"));
                TicketsListGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        if (id_system != 0)
        {
            worklist = cticketservice.GetWorkListView(UserProfile.user_login, id_system);
        }
        else
        {
            worklist = cticketservice.GetWorkListView(UserProfile.user_login);
        }

        if (ViewingTicketStatus != "")
            TicketsListGrid.DataSource = worklist.Where(w => w.system_status == ViewingTicketStatus).ToList();
        else
            TicketsListGrid.DataSource = worklist;

    }

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

    }


    protected void BtnResolveTicket_Click(object sender, EventArgs e)
    {
        GridItem CurrentGridItem = (GridItem)((sender as ImageButton).Parent.Parent);

        int id_ticket = Convert.ToInt32(CurrentGridItem.OwnerTableView.DataKeyValues[CurrentGridItem.ItemIndex]["id_ticket"].ToString());
        //CentralTicketing_Interfaces_TicketDetails TicketDetails = (CentralTicketing_Interfaces_TicketDetails)Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.FindControl("TicketDetails1");

        //TicketDetails.ShowTicketDetails(id_ticket);
        //cticketservice.CreateTicketLock(id_ticket, UserProfile.user_login);
        //Parent.Parent.Parent.Parent.Refresh();
        TicketsListGrid.Rebind();

    }

    protected void TicketsListGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RowSelected != null) { RowSelected(this, e); }

        RadGrid thisRadgrid = (RadGrid)sender;
        int id_ticket;

        if (gridSelectedItems.Count > 0)
        {
            id_ticket = Convert.ToInt32(gridSelectedItems[0].OwnerTableView.DataKeyValues[gridSelectedItems[0].ItemIndex]["id_ticket"].ToString());
        }
        else
        { 
            id_ticket = Convert.ToInt32(thisRadgrid.SelectedItems[0].OwnerTableView.DataKeyValues[thisRadgrid.SelectedItems[0].ItemIndex]["id_ticket"].ToString());
        }
        CentralTicketing_Interfaces_TicketDetails TicketDetails = (CentralTicketing_Interfaces_TicketDetails)Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.FindControl("TicketDetails1");

        TicketDetails.ShowTicketDetails(id_ticket);
        TicketDetails.RequestRefresh();

    }

    protected void TicketsListGrid_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        string sortstring = " ORDER BY ctickets_worklist."; 
        string refstring;
        string fieldname;

        if (e.NewSortOrder == GridSortOrder.Descending)
        {
            fieldname = e.SortExpression;
            sortstring += e.SortExpression + " DESC";
            refstring = "COL" + e.Item.DataSetIndex.ToString() + ".DOWN";
        }
        else if (e.NewSortOrder == GridSortOrder.Ascending)
        { 
            fieldname = e.SortExpression;
            sortstring += e.SortExpression;
            refstring = "COL" + e.Item.DataSetIndex.ToString() + ".UP";
        }
        else
        { 
            fieldname = "";
            sortstring = "";
            refstring = "";
        }

        cticketservice.UpdateWorklistOrder(UserProfile.login, refstring, sortstring, fieldname);

    }

    protected void TicketsListGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            if (((ctickets_v_worklist)(e.Item.DataItem)).user_lock == ""
                || ((ctickets_v_worklist)(e.Item.DataItem)).user_lock == null
                || ((ctickets_v_worklist)(e.Item.DataItem)).user_lock == (Page as BasePage).UserProfile.user_login
                )
            {
                (dataItem["ButtonsColumn"].FindControl("BtnResolveTicket") as ImageButton).Enabled = true;
                (dataItem["ButtonsColumn"].FindControl("BtnResolveTicket") as ImageButton).OnClientClick = "open_popup_window('" + VirtualHostURL + "/CentralTicketing/WorkConsole/ResolveWindow.aspx', "
                    + ((ctickets_v_worklist)(e.Item.DataItem)).id_ticket.ToString() + ", '', '', "
                + ((ctickets_v_worklist)(e.Item.DataItem)).system_width.ToString() + ", "
                + ((ctickets_v_worklist)(e.Item.DataItem)).system_height.ToString() + ");return(false);";//window.Reload();
            }
            else
            {
                (dataItem["ButtonsColumn"].FindControl("BtnResolveTicket") as ImageButton).Enabled = false;
                (dataItem["ButtonsColumn"].FindControl("BtnResolveTicket") as ImageButton).ImageUrl = "~/images/resolve_button_inactive.gif";
                (dataItem["ButtonsColumn"].FindControl("BtnResolveTicket") as ImageButton).ToolTip = "Ticket Assumido por " + ((ctickets_v_worklist)(e.Item.DataItem)).user_lock;
                   
            }
            (dataItem["ButtonsColumn"].FindControl("BtnViewTicket") as ImageButton).Enabled = true;
            (dataItem["ButtonsColumn"].FindControl("BtnViewTicket") as ImageButton).OnClientClick = "open_popup_window('" + VirtualHostURL + "/CentralTicketing/WorkConsole/ViewerWindow.aspx', "
                + ((ctickets_v_worklist)(e.Item.DataItem)).id_ticket.ToString() + ", '', '', "
            + ((ctickets_v_worklist)(e.Item.DataItem)).system_width.ToString() + ", "
            + ((ctickets_v_worklist)(e.Item.DataItem)).system_height.ToString() + ");";//return(false);
        }
    }

    protected void TicketsListGrid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        if (e != null)
        { 
        }
    }
}
