using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Diagnostics;

public partial class UpdateExchangeRate : System.Web.UI.Page
{



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            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";
            

            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;

            Notifications.showModal("modal-xl", this);

        }



    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = null;
        ViewState["uid"] = null;
        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.Text = "Exchange Rate Updated";
                    Notifications.showMessage("Exchange Rate Updated", this, Notifications.successMessage, true);
                }
                else
                {
                    lblError.Text = error;
                    Notifications.showMessage(error, this, Notifications.errorMessage, true);
                }
            }
            getExchangeRates();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            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_PreRender(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            GridView1.UseAccessibleHeader = true;
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }
    
}