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 Business_SetParam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            getParams();
            ExceptionUtility.LogException(0, "", "25", "Page_Load", "");
        }
        lblError.Text = "";
    }

    private void getParams()
    {
        try
        {
            string DBConnection = Helper.dbConnectionLive();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "SYSTEM_PARAM_EDITOR.get_parametes";

            oracom.Parameters.Add("p_current_uid", 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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

            txtPass.Text = ds.Tables[0].Rows[0][2].ToString();
            TextBox1.Text = ds.Tables[0].Rows[1][2].ToString();
            string temp = ds.Tables[0].Rows[2][2].ToString();
            if (temp.Equals("Y"))
                CheckBox1.Checked = true;

            temp = ds.Tables[0].Rows[3][2].ToString();
            if (temp.Equals("Y"))
                CheckBox2.Checked = true;

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SYSTEM_PARAM_EDITOR.get_parametes","", "getParams", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void getParamByID(string ID)
    {
        try
        {
            string DBConnection = Helper.dbConnectionLive();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "SYSTEM_PARAM_EDITOR.get_parameter_by_id";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_id", 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_param_id"].Value = ID;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

            ddlLoginMethod.SelectedValue = ds.Tables[0].Rows[0][2].ToString();

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SYSTEM_PARAM_EDITOR.get_parameter_by_id","", "get_parameter_by_id", "ID: "+ID );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void setParam(int ID)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "SYSTEM_PARAM_EDITOR.edit_parameter";

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

            oracom.Parameters.Add("p_param_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_value", 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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.Parameters["p_param_id"].Value = ID;
            if (ID == 2)
            {
                oracom.Parameters["p_new_value"].Value = txtPass.Text;
                if (txtPass.Text.Length > 0)
                    parameters = "p_new_value-" + txtPass.Text;
            }
            if (ID == 4)
            {
                oracom.Parameters["p_new_value"].Value = TextBox1.Text;
                if (TextBox1.Text.Length > 0)
                    parameters = "p_new_value-" + TextBox1.Text;
            }
            if (ID == 5)
                if (CheckBox1.Checked)
                {
                    oracom.Parameters["p_new_value"].Value = "Y";
                    parameters = "p_new_value-Y";
                }
                else
                {
                    oracom.Parameters["p_new_value"].Value = "N";
                    parameters = "p_new_value-N";
                }
            if (ID == 10)
                if (CheckBox2.Checked)
                {
                    parameters = "p_new_value-Y";
                    oracom.Parameters["p_new_value"].Value = "Y";
                }
                else
                {
                    parameters = "p_new_value-N";
                    oracom.Parameters["p_new_value"].Value = "N";
                }
            if (ID == 200)
            {
                oracom.Parameters["p_new_value"].Value = ddlLoginMethod.SelectedValue;
                if (ddlLoginMethod.SelectedIndex > 0)
                    parameters = "p_new_value-" + ddlLoginMethod.SelectedItem.Text;
            }

            oracom.ExecuteNonQuery();
            oracon.Close();
            ExceptionUtility.LogException(0, "SYSTEM_PARAM_EDITOR.edit_parameter", "168.1", "setParam", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SYSTEM_PARAM_EDITOR.edit_parameter","", "setParam",parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtPass.Text.Length == 0)
        {
            lblError.Text = "please enter a number";
            ExceptionUtility.LogException(9, "", "25.168", "btnSave_Click", "");
        }
        else
        {
            ExceptionUtility.LogException(0, "", "25.168", "btnSave_Click","");
            setParam(2);
            lblError.Text = "Password Validity Period saved";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //claim
        if (TextBox1.Text.Length == 0)
        {
            lblError.Text = "please enter a number";
            ExceptionUtility.LogException(9, "", "25.167", "Button1_Click", "");
        }
        else
        {
            ExceptionUtility.LogException(0, "", "25.167", "Button1_Click", "");
            setParam(4);
            lblError.Text = "Other Claim Account saved";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //single on
        ExceptionUtility.LogException(0, "", "25.166", "Button2_Click", "");
        setParam(5);
        lblError.Text = "Cash Recon Single ON saved";
        
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        //use current session
        ExceptionUtility.LogException(0, "", "25.165", "Button3_Click", "");
        setParam(10);
        lblError.Text = "Use Current Session saved";
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        //use current session
        ExceptionUtility.LogException(0, "", "", "Button4_Click", "");
        setParam(200);
        lblError.Text = "Login Method saved";
    }

}
