using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

using StaticAppGlobals;

public partial class CentralTicketing_Interfaces_OpenListControl : 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 object ListDatasource;
    protected int system_id;

    protected GridItemCollection gridSelectedItems = new GridItemCollection();

    public object DataSource
    {
        get
        {
            //return (object)ViewState["DataSource"];
            return ListDatasource;
        }
        set
        {
            //ViewState["DataSource"] = value;
            ListDatasource = value;
            TicketsListGrid.DataSource = ListDatasource;
        }
    }
    public int id_system
    {
        get
        {
            //return (int)ViewState["id_system"];
            return system_id;
        }
        set
        {
            //ViewState["id_system"] = value;
            system_id = value;
        }
    }

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

    public void Refresh()
    {
        TicketsListGrid.DataSource = DataSource;
        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;
        }

        TicketsListGrid.DataSource = cticketservice.GetWorkList(UserProfile.user_login, id_system);
    }

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

    }

    protected void TicketsListGrid_SelectedIndexChanged(object sender, EventArgs 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);

    }

    protected void TicketsListGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            (dataItem["ButtonsColumn"].FindControl("BtnViewTicket") as ImageButton).Enabled = true;
            (dataItem["ButtonsColumn"].FindControl("BtnViewTicket") as ImageButton).OnClientClick = "open_popup_window('" + VirtualHostURL + "/CentralTicketing/WorkConsole/ViewerWindow.aspx', "
                + ((ShowUsersOpenlistResult)(e.Item.DataItem)).id_ticket.ToString() + ", '', '', "
            + ((ShowUsersOpenlistResult)(e.Item.DataItem)).system_width.ToString() + ", "
            + ((ShowUsersOpenlistResult)(e.Item.DataItem)).system_height.ToString() + ");";//return(false);
        }
    }

}
