using System;
using System.Collections;
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.Globalization;

using BE;
using BL;
using Utilitaria;

public partial class frmEmpresa : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Usuario"] == null)
        {
            Response.Redirect(Utilitaria.Cls_Enums.CerrarSesion.Salir);
        }
        if (!IsPostBack)
        {
            ViewState["EMP"] = Request.QueryString["EMP"].ToString();
            ViewState["CAT"] = Request.QueryString["CAT"].ToString();

            //try
            //{
                //Convert.ToInt32(ViewState["EMP"]);
                GridColumn column = tk_Grid.MasterTableView.GetColumnSafe("Ruc");
                column.CurrentFilterValue = ViewState["EMP"].ToString();
                column.CurrentFilterFunction = GridKnownFunction.StartsWith;
            //}
            //catch
            //{
            //    GridColumn column = tk_Grid.MasterTableView.GetColumnSafe("Razonsocial");
            //    column.CurrentFilterValue = ViewState["EMP"].ToString();
            //    column.CurrentFilterFunction = GridKnownFunction.StartsWith;
            //}
        }
    }

    private void listar(String EMP, String Categoria)
    {
        BEEmpresas objBEEmpresas = new BEEmpresas();
        BLEmpresas objBLEmpresas = new BLEmpresas();

        String Where = "";

        Where = "Ruc LIKE '" + EMP + "%'";
           
            if (Categoria != "0")
            {
              // tk_Grid.DataSource = objBLEmpresas.fnSelEmpresasDin("CategoriaId=" + Categoria + " and " + Where, "razonsocial");
                tk_Grid.DataSource = objBLEmpresas.fnSelEmpresasDinPopUp(  Where, "razonsocial");
            }
            else
            {
                tk_Grid.DataSource = objBLEmpresas.fnSelEmpresasDinPopUp(Where, "razonsocial");
            }

        //////////}
       
        //tk_Grid.Rebind();

        objBEEmpresas = null;
        objBLEmpresas = null;
    }

    protected void tk_Grid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        //GridColumn column = tk_Grid.MasterTableView.GetColumnSafe("Ruc");
        
        //listar(column.CurrentFilterValue, ViewState["CAT"].ToString());

        if (!IsPostBack)
        {
            GridColumn column = tk_Grid.MasterTableView.GetColumnSafe("Ruc");
            GridColumn column2 = tk_Grid.MasterTableView.GetColumnSafe("Razonsocial");
            column.CurrentFilterFunction = GridKnownFunction.StartsWith;
            column2.CurrentFilterFunction = GridKnownFunction.StartsWith;
            //column.CurrentFilterValue = ViewState["CPC"].ToString();
            if (column.CurrentFilterValue != "")
                listar(column.CurrentFilterValue, ViewState["CAT"].ToString());
            else if (column2.CurrentFilterValue != "")
                listar(column2.CurrentFilterValue, ViewState["CAT"].ToString());
            else
                listar("", ViewState["CAT"].ToString());
        }
        else
        {
            listar("", ViewState["CAT"].ToString());
        }
    }

    protected void tk_Grid_ItemCommand(object source, GridCommandEventArgs e)
    {

    }

    protected void tk_Grid_ItemDataBound(object sender, GridItemEventArgs e)
    {

    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        //showMessage(tk_Grid.SelectedItems[0].OwnerTableView.DataKeyValues[tk_Grid.SelectedItems[0].ItemIndex]["PlanCuentaID"].ToString());
        Int32 Seleccion = tk_Grid.SelectedItems.Count;
        if (Seleccion == 0)
        {
            showMessage("Seleccione una empresa");
        }
        else
        {
            String EmpresaID = tk_Grid.SelectedItems[0].OwnerTableView.DataKeyValues[tk_Grid.SelectedItems[0].ItemIndex]["EmpresaID"].ToString().Trim();
            Session["EMPID"] = EmpresaID;
            ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('EMP');", true);
        }
    }
    protected void showMessage(string strMessage)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "alert('" + strMessage + "');", true);

    }
    protected void tk_Grid_PreRender(object sender, EventArgs e)
    {
    }
    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        string argumentoAjx = (e.Argument);
        string[] datos = argumentoAjx.Split(",".ToCharArray());
        if (datos[0] == "seleccionaFila")
        {
            Session["EMPID"] = datos[1];
            ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('EMP');", true);
        }
    }
}
