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)
        {
            pnlUpdate.Visible = false;
            loadCurrency();
           
        }
        lblError.Text = "";
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "CS_SETTL_INFO_EDITOR.get_Currency_lookup";

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

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

  
        ddlToCurrency.DataSource = ds;
        ddlToCurrency.DataBind();
        oracon.Close();
    }

    private void GetCSEvents( int pageSize, int pageNumber)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "CS_SETTL_INFO_EDITOR.Get_CS_Events";

        oracom.Parameters.Add("p_EVENT_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_NARRATIVE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_FROM_ACCONT", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_TO_ACCONT", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_num", OracleType.Number).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.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

        oracom.Parameters["p_EVENT_NAME"].Value = txtSerachEventName.Text;
        oracom.Parameters["p_NARRATIVE"].Value = txtnarrative.Text;
        oracom.Parameters["p_FROM_ACCONT"].Value = txtSearchFromAccount.Text;
        oracom.Parameters["p_TO_ACCONT"].Value = txtSearchToAccount.Text;

        oracom.Parameters["p_page_size"].Value = pageSize;
        oracom.Parameters["p_page_num"].Value = pageNumber;
        oracom.Parameters["p_lang"].Value = "EN";
        
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        int  nop = 0;
        if(ds.Tables[0].Rows.Count>1)
        nop = int.Parse(Math.Ceiling((double.Parse(ds.Tables[0].Rows[0]["ROW_COUNT"].ToString()) / pageSize)).ToString());

        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
        if (pageNumber == 1)
        {
            ddlPages.Items.Clear();
            for (int i = 1; i <= nop; i++)
            {
                ddlPages.Items.Add(i.ToString());
            }
        }
    }

    private bool UpdateCSEVent(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 = "CS_SETTL_INFO_EDITOR.Update_CS_Events";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_CS_Evnt_Code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_EVENT_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_NARRATIVE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_FROM_ACCONT", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_TO_ACCONT", OracleType.VarChar).Direction = ParameterDirection.Input;
   
          oracom.Parameters.Add("p_From_Acct_Crncy", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_To_Acct_Crncy", 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_CS_Evnt_Code"].Value = txtHiddeneEvenCode.Text;//
        oracom.Parameters["p_EVENT_NAME"].Value = txtUpdateEventName.Text;//
        oracom.Parameters["p_NARRATIVE"].Value = txtUpdateNarrative.Text;//
        oracom.Parameters["p_FROM_ACCONT"].Value = txtUpdateFromAccount.Text;//
        oracom.Parameters["p_TO_ACCONT"].Value = txtUpdateToAccount.Text;//

        oracom.Parameters["p_From_Acct_Crncy"].Value = ddlFromCurrency.SelectedValue;//
        oracom.Parameters["p_To_Acct_Crncy"].Value = ddlToCurrency.SelectedValue;//


        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"))
        {

            pnlGrid.Enabled = false;
            pnlUpdate.Visible = true;

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


            //put the data in the controls
            txtHiddeneEvenCode.Text = Uid;
            txtUpdateEventName.Text = GridView1.Rows[index].Cells[1].Text;

            txtUpdateNarrative.Text=     GridView1.Rows[index].Cells[2].Text;
            txtUpdateFromAccount.Text =  GridView1.Rows[index].Cells[3].Text;
            txtUpdateToAccount.Text =    GridView1.Rows[index].Cells[4].Text;

            ddlFromCurrency.SelectedValue = GridView1.Rows[index].Cells[5].Text;
            ddlToCurrency.SelectedValue=    GridView1.Rows[index].Cells[6].Text;

        }
    }
   
    protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlUpdate.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ExceptionUtility.LogEvent_Errors(0, "", "btnSave_Click", "Start");
       
        string error = "";
          
                
            bool saved = UpdateCSEVent( ref error);
            if (saved)
            {
                lblError.ForeColor = System.Drawing.Color.DarkGreen;
                lblError.Text = "Successfully Updated";
            }
            else
            {
                lblError.Text = error;
            }

            GetCSEvents(Helper.gridSize(), 1);
         

            pnlUpdate.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();

            ExceptionUtility.LogEvent_Errors(0, "", "btnSave_Click", "End");
       
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex);
            lblError.Text = ex.Message;

        }
    }

     private void clearControls()
    {
        txtUpdateEventName.Text = txtUpdateFromAccount.Text = txtUpdateToAccount.Text = txtUpdateNarrative.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;

        GetCSEvents(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));

    }
    
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogEvent_Errors(0, "", "ButtonSearch_Click", "Start");
        
        GetCSEvents(Helper.gridSize(), 1);

        ExceptionUtility.LogEvent_Errors(0, "", "ButtonSearch_Click", "End");
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetCSEvents(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
}
