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();
        }
        lblError.Text = "";
    }

    private void getParams()
    {
        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();
    }

    private void getParamByID(string ID)
    {
        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();
    }

    private void setParam(int ID)
    {
        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 (ID == 4)
            oracom.Parameters["p_new_value"].Value = TextBox1.Text;
        if (ID == 5)
            if(CheckBox1.Checked)
                oracom.Parameters["p_new_value"].Value = "Y";
            else
                oracom.Parameters["p_new_value"].Value = "N";
        if (ID == 10)
            if (CheckBox2.Checked)
                oracom.Parameters["p_new_value"].Value = "Y";
            else
                oracom.Parameters["p_new_value"].Value = "N";
        if (ID == 200)
            oracom.Parameters["p_new_value"].Value = ddlLoginMethod.SelectedValue;

        oracom.ExecuteNonQuery();
        oracon.Close();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtPass.Text.Length == 0)
        {
            lblError.Text = "please enter a number";
        }
        else
        {
            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";
        }
        else
        {
            setParam(4);
            lblError.Text = "Other Claim Account saved";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //single on
        setParam(5);
        lblError.Text = "Cash Recon Single ON saved";
    }

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

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

}
