using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.OracleClient;

public partial class sysparams : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
           
            getExchangeRates();
        }
        lblError.Text = "";
    }

    private void getExchangeRates()
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_currency_exchange";

        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

  
  
    private bool updateXERate( ref string error)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.Update_Currency_Exchange";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Curr_Code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Curr_Xe", OracleType.VarChar).Direction = ParameterDirection.Input;
          oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;


        oracom.Parameters["p_Curr_Code"].Value = txtCurrCode.Text;
        oracom.Parameters["p_Curr_Xe"].Value = txtXERate.Text;
        oracom.Parameters["p_lang"].Value = "EN";
      
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

 
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


      if (e.CommandName.Equals("u"))
       {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            pnlGrid.Enabled = false;

            ViewState["uid"] = Uid;
            //put the data in the controls
            txtCurrCode.Text = GridView1.Rows[index].Cells[0].Text;

            txtCurrName.Text = GridView1.Rows[index].Cells[1].Text;

            txtXERate.Text = GridView1.Rows[index].Cells[2].Text;
            
       }
     


    }

   
    protected void btnCancel_Click(object sender, EventArgs e)
        {
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
           if (mode.Equals("u"))
            {
                
               bool saved = updateXERate( ref error);
                if (saved)
                {
                    lblError.ForeColor = System.Drawing.Color.DarkGreen;
                    lblError.Text = "Exchange Rate Updated";
                }
                else
                {
                    lblError.Text = error;
                }
            }
            getExchangeRates();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {
            
        }
    }

     private void clearControls()
    {
        txtCurrCode.Text = "";
        txtCurrName.Text = "";
        txtXERate.Text = "";
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
        //    e.Row.Cells[4].Visible = false;
        //    e.Row.Cells[5].Visible = false;
        //
        }
    }

    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getExchangeRates();
    }
}
