using BE;
using BL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class frmCuenta : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Combo_PlanDeCuenta();
            Listar();
        }
    }

    private void Combo_PlanDeCuenta()
    {
        BLPlanDeCuentas oBLPlanDeCuentas = new BLPlanDeCuentas();
        List<BEPlanDeCuentas> loPlanDeCuentas;

        loPlanDeCuentas = oBLPlanDeCuentas.fnSelPlanDeCuentasDin("Estado=0" + " and pdc.EstablecimientoID=" + Session["EmpresaID"].ToString().Trim() + " and pdc.NivelSaldoId <> (select Valor From ParametrosGenerales Where ParametroGeneralID = 5001) ", "2 asc, 3 asc", 0);

        for (int i = 0; i < loPlanDeCuentas.Count; i++)
        {
            cbxPlanCta.Items.Insert(i, new RadComboBoxItem(loPlanDeCuentas[i].CodigoPlanCuenta + " -> " + loPlanDeCuentas[i].NombrePlanCuenta.Trim()));
            //cbxPlanCta.Items.Insert(i, new RadComboBoxItem(loPlanDeCuentas[i].NombrePlanCuenta.Trim()));
            cbxPlanCta.Items[i].Value = loPlanDeCuentas[i].PlanCuentaID.ToString().Trim();

            
        }

        cbxPlanCta.Items.Insert(0, new RadComboBoxItem("Seleccione..."));
        cbxPlanCta.Items[0].Value = "0";

        cbxPlanCta.DataBind();


        oBLPlanDeCuentas = null;
        loPlanDeCuentas = null;
    }
    private void Listar()
    {
        SqlConnection sqlConnection1 = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;

        cmd.CommandText = "UCO_Cuentas_Select";
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter BancoCuentaID = cmd.Parameters.Add("@BancoCuentaID", SqlDbType.Int);
        BancoCuentaID.Value = Request.QueryString["BancoCuentaID"];
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();

        reader = cmd.ExecuteReader();
        // Data is accessible through the DataReader object here.


        while (reader.Read())
        {
            //Console.WriteLine(reader.GetString(0));
            //showMessage(reader["NombreFamilia"].ToString());
            this.txtNombreCuenta.Text = reader["NumeroCuenta"].ToString().Trim();
            this.txtNombreBanco.Text = reader["NombreBanco"].ToString().Trim();
            this.cbxPlanCta.SelectedValue = reader["PlancuentaID"].ToString().Trim();
        }

        sqlConnection1.Close();
        //close DataReader
        reader.Close();

    }

    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        SqlConnection sqlConnection1 = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;

        cmd.CommandText = "UCO_Cuentas_Modifica";
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter BancoCuentaID = cmd.Parameters.Add("@BancoCuentaID", SqlDbType.Int);
        BancoCuentaID.Value = Request.QueryString["BancoCuentaID"].ToString().Trim();
        SqlParameter PlancuentaID = cmd.Parameters.Add("@PlancuentaID", SqlDbType.Int);
        PlancuentaID.Value = this.cbxPlanCta.SelectedValue.ToString().Trim();
  
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();

        cmd.ExecuteNonQuery();

        sqlConnection1.Close();
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);

    }

    protected void btnVolver_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", true);
    }

    private static string GetConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["SHIOL_CONTA_BTHConnectionString"].ConnectionString;
    }

}