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;
using System.Diagnostics;

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

    public int page_id = 0;
    public string table_name = "";
    string url = "";
    static int intCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        table_name = "sys_parmtrs";
        url = HttpContext.Current.Request.Url.AbsolutePath;
        string[] urlArr = url.Split('/');
        url = urlArr[2] + "/" + urlArr[3];
        page_id = loadPageIDs(url);
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
            pnlCOntrols2.Visible = false;
            ExceptionUtility.LogException(0, "", "76", "Page_Load", "");
            getParams();
           
        }
        lblError.Text = "";
    }
    private int loadPageIDs(string URL)
    {
        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 = "lookup_getter.get_pages";

            oracom.Parameters.Add("p_url", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pages", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_url"].Value = URL;

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

            oracon.Close();
            return int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.get_pages","", "loadPageIDs", "URL: " + URL);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }

    }
    private void getParams()
    {
        try
        {
            string currentMethod = "";
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            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.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);

            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            if(currentMethod.Contains ("Page_Load"))
                ExceptionUtility.LogException(0, "system_param_editor.get_parametes", "1.1", "getParams", "");
            else if (currentMethod.Contains("GridView1_PageIndexChanging"))
                ExceptionUtility.LogException(0, "system_param_editor.get_parametes", "2.1", "getParams", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.get_parametes","1.1", "getParams", "");
            GridView1.DataSource = null ;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private DataSet getParamsDetails(int paramId)
    {
        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.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 = paramId;
            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);

            oracon.Close();
            ExceptionUtility.LogException(0, "system_param_editor.get_parameter_by_id", "", "getParamsDetails", "paramID: " + paramId);
            return ds;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.get_parameter_by_id","", "getParamsDetails", "paramID: "+paramId );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return null;
        }
    }


    private bool deleteParameter(string paramID, ref string error)
    {
        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.delete_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_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_param_id"].Value = paramID;
            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;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.delete_parameter","", "deleteParameter", "paramID: "+paramID );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false ;
        }
    }

    private bool addParameter(ref string error)
    {
        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.add_parameter";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_value_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_value_lkp", 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_param_name"].Value = txtName.Text;
            oracom.Parameters["p_param_value_type"].Value = ddlRecordType.SelectedValue;
            oracom.Parameters["p_param_value_lkp"].Value = txtDesc.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;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.add_parameter","", "addParameter", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool updateParameter(string paramID, ref string error)
    {
        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.update_parameter_def";

            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_param_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_value_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_param_value_lkp", 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_param_id"].Value = paramID;
            oracom.Parameters["p_param_name"].Value = txtName.Text;
            oracom.Parameters["p_param_value_type"].Value = ddlRecordType.SelectedValue;
            oracom.Parameters["p_param_value_lkp"].Value = txtDesc.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;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.update_parameter_def","", "updateParameter", "paramID: "+paramID );
            return false;
        }
    }

    private bool updateParameterValue(string paramID, string value, ref string error)
    {
        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_param_id"].Value = paramID;
            oracom.Parameters["p_new_value"].Value = value;
            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")
            {
                //ExceptionUtility.LogException(0, "system_param_editor.edit_parameter", "updateParameterValue", "paramID: " + paramID + ", value: " + value);
                return true;
            }
            else
            {
                //ExceptionUtility.LogException(9, "system_param_editor.edit_parameter", "updateParameterValue", "paramID: " + paramID + ", value: " + value);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "system_param_editor.edit_parameter","", "updateParameterValue", "paramID: " + paramID+", value: "+value);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


        if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["paramId"] = Uid;
            string error = "";
            int chgId = loadChgID();
            ExceptionUtility.LogException(0, "", "76.443", "GridView1_RowCommand.Delete", "");
            bool temp = UpdateMakerChecker(chgId, Uid, "SYS_PARMTR_ID", Uid, "", "", "", 2, ref error);
            if(temp)
                lblError.Text = "Your request has been sent to Checker user for Approval";
            else
                lblError.Text = error;
            getParams();
        }
        else if (e.CommandName.Equals("vm"))
        {
            ExceptionUtility.LogException(0, "", "", "GridView1_RowCommand.View_Modification", "");
            pnlControls.Visible = false;
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            int chgId = loadMaxChgID(rowID);
            Response.Redirect("Checker.aspx?chgId=" + chgId);
        }
        else if (e.CommandName.Equals("u"))
        {
            intCount = 0;
            ExceptionUtility.LogException(0, "", "76.440", "GridView1_RowCommand.Update", "");
            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
            txtName.Text = GridView1.Rows[index].Cells[0].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[8].Text.Replace("&nbsp;", "");
            ddlRecordType.SelectedValue = GridView1.Rows[index].Cells[7].Text;
        }
        else if (e.CommandName.Equals("p"))
        {
            ExceptionUtility.LogException(0, "", "", "GridView1_RowCommand.Post", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            pnlCOntrols2.Visible = true;
            pnlGrid.Enabled = false;

            ViewState["uid"] = Uid;
            //get the lookup type
            string type = GridView1.Rows[index].Cells[7].Text;
            ViewState["type"] = type;
            ViewState["value"] = GridView1.Rows[index].Cells[1].Text.Replace("&nbsp;", ""); ;
            //show control with a similar type
            if (type.Equals("0"))
            {
                txtValue.Visible = true;
                ddlValue.Visible = false;
                chkBox.Visible = false;

                txtValue.Text = GridView1.Rows[index].Cells[1].Text.Replace ("&nbsp;","");
                if ((Uid == "502") || (Uid == "503"))
                    txtValue.Attributes.Add("onKeyPress", "return KeyPressFuncNumberOnly();");
                
            }
            else if (type.Equals("1"))
            {
                txtValue.Visible = false;
                ddlValue.Visible = false;
                chkBox.Visible = true;

                string chkValue = GridView1.Rows[index].Cells[1].Text;
                if (chkValue.Equals("Y"))
                    chkBox.Checked = true;
            }
            else if (type.Equals("2"))
            {
                txtValue.Visible = false;
                ddlValue.Visible = true;
                chkBox.Visible = false;

                //create items in ddl
                string list = GridView1.Rows[index].Cells[8].Text;
                string[] valueList = list.Split(';');
                ddlValue.Items.Clear();
                foreach (string s in valueList)
                {
                    ddlValue.Items.Add(new ListItem(s, s));
                }
                try
                {
                    ddlValue.SelectedValue = GridView1.Rows[index].Cells[1].Text;
                }
                catch (Exception ex)
                {
                    ExceptionUtility.LogException(ex, "", "", "Gridview1_RowCommand", "");
                    //Response.Redirect("../ErrorPages/ErrorPage.aspx");
                }
            }






            txtName.Text = GridView1.Rows[index].Cells[0].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[8].Text.Replace("&nbsp;", "");
            ddlRecordType.SelectedValue = GridView1.Rows[index].Cells[7].Text;
        }
        
    }

    protected void btnAdd_Click(object sender, EventArgs e)
        {
            intCount = 0;
            ViewState["mode"] = "a";
            pnlControls.Visible = true;
            pnlGrid.Enabled = false;
            clearControls();
            ExceptionUtility.LogException(0, "", "76.439", "btnAdd_Click", "");
        }

    protected void btnCancel_Click(object sender, EventArgs e)
        {
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
            ExceptionUtility.LogException(0, "", "76.441", "btnCancel_Click", "");
        }
    private int loadChgID()
    {
        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 = "maker_checker_editor.get_chg_id";


            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

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

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            return nop;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_chg_id","", "loadChgID", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
    private string loadSysParmId()
    {
        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 = "lookup_getter.get_sys_param_id";

            oracom.Parameters.Add("v_param_id", OracleType.Number).Direction = ParameterDirection.ReturnValue;

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

            string paramId = oracom.Parameters["v_param_id"].Value.ToString();
            oracon.Close();
            return paramId;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.get_sys_param_id","", "loadSysParmId", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return null ;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (intCount < 1)
            intCount++;
        else
        {
            pnlControls .Visible = false;
            pnlCOntrols2.Visible = false;
            lblError.Text = "Your request has been sent to Checker user for Approval";
            return;
        }
        try
        {
            string mode = ViewState["mode"].ToString();
            string SYS_PARMTR_NAME_NEW = txtName.Text.Trim();
            string SYS_PARMRT_VALUE_TYPE_NEW = ddlRecordType.SelectedValue;
            string SYS_PARMRT_VALUE_TYPE_DESC_NEW = ddlRecordType.SelectedItem.Text;
            string SYS_PARMTR_VALUE_LKP_NEW = txtDesc.Text.Trim();
            string error = "";
            if (mode.Equals("a"))
            {
                int chgId = loadChgID();
                string pkey=loadSysParmId();
                bool saved = false;
                ExceptionUtility.LogException(0, "", "76.440", "btnSave_Click", "");
                saved = UpdateMakerChecker(chgId, pkey, "SYS_PARMTR_ID", "", pkey, "", "", 0, ref error);
                if (saved == false)
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, pkey, "SYS_PARMTR_NAME", "", SYS_PARMTR_NAME_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, pkey, "SYS_PARMRT_VALUE_TYPE","", SYS_PARMRT_VALUE_TYPE_DESC_NEW,"",SYS_PARMRT_VALUE_TYPE_NEW, 0, ref error);
                if (saved == false)
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, pkey, "SYS_PARMTR_VALUE_LKP", "", SYS_PARMTR_VALUE_LKP_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                    return;
                }

                lblError.Text = "Your request has been sent to Checker user for Approval";
              
            }
            else if (mode.Equals("u"))
            {
                int  Uid = int.Parse (ViewState["uid"].ToString());
                int chgId = loadChgID();
                bool compare = false;                
                DataSet ds=getParamsDetails (Uid);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string SYS_PARMTR_NAME_OLD = ds.Tables[0].Rows[0]["sys_parmtr_name"].ToString ();
                    string SYS_PARMRT_VALUE_TYPE_OLD = ds.Tables[0].Rows[0]["SYS_PARMRT_VALUE_TYPE"].ToString ();
                    string SYS_PARMRT_VALUE_TYPE_DESC_OLD="";
                    if (int.Parse(SYS_PARMRT_VALUE_TYPE_OLD) == 0)
                        SYS_PARMRT_VALUE_TYPE_DESC_OLD = "TextBox";
                    else if (int.Parse(SYS_PARMRT_VALUE_TYPE_OLD) == 1)
                        SYS_PARMRT_VALUE_TYPE_DESC_OLD = "CheckBox";
                    else
                        SYS_PARMRT_VALUE_TYPE_DESC_OLD = "List";
                    string SYS_PARMTR_VALUE_LKP_OLD = ds.Tables[0].Rows[0]["SYS_PARMTR_VALUE_LKP"].ToString ();

                    if (SYS_PARMTR_NAME_NEW == SYS_PARMTR_NAME_OLD && SYS_PARMRT_VALUE_TYPE_NEW == SYS_PARMRT_VALUE_TYPE_OLD && SYS_PARMTR_VALUE_LKP_NEW == SYS_PARMTR_VALUE_LKP_OLD)
                        compare = true;
                    if (compare == false)
                    {
                        bool update = false;
                        if (SYS_PARMTR_NAME_NEW != SYS_PARMTR_NAME_OLD)
                        {
                            update=UpdateMakerChecker(chgId, Uid.ToString(), "sys_parmtr_name", SYS_PARMTR_NAME_OLD, SYS_PARMTR_NAME_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                if (Helper.OracleError(error))
                                {
                                    lblError.Text = "An Error Occured!";
                                    ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                                }
                                else
                                    lblError.Text = error;
                                return;
                            }
                        }
                        if (SYS_PARMRT_VALUE_TYPE_NEW != SYS_PARMRT_VALUE_TYPE_OLD)
                        {
                            update=UpdateMakerChecker(chgId, Uid.ToString(), "SYS_PARMRT_VALUE_TYPE", SYS_PARMRT_VALUE_TYPE_DESC_OLD, SYS_PARMRT_VALUE_TYPE_DESC_NEW, SYS_PARMRT_VALUE_TYPE_OLD, SYS_PARMRT_VALUE_TYPE_NEW, 1, ref error);
                            if (update == false)
                            {
                                if (Helper.OracleError(error))
                                {
                                    lblError.Text = "An Error Occured!";
                                    ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                                }
                                else
                                    lblError.Text = error;
                                return;
                            }
                        }
                        if (SYS_PARMTR_VALUE_LKP_NEW != SYS_PARMTR_VALUE_LKP_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid.ToString(), "SYS_PARMTR_VALUE_LKP", SYS_PARMTR_VALUE_LKP_OLD, SYS_PARMTR_VALUE_LKP_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                if (Helper.OracleError(error))
                                {
                                    lblError.Text = "An Error Occured!";
                                    ExceptionUtility.LogException(9, "", "76.440", "btnSave_Click", error);
                                }
                                else
                                    lblError.Text = error;
                                return;
                            }
                        }
                        lblError.Text = "Your request has been sent to Checker user for Approval";
                    }
                }
               
            }
            getParams();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "","76.440", "btnSave_Click", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
    private bool UpdateMakerChecker(int chgId, string pkey, string columnName, string oldValue, string newValue, string oldValueNo, string NewValueNo, int chgType, ref string error)
    {
        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 = "MAKER_CHECKER_EDITOR.Add_Maker_Checker";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_table_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_column_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_old_value", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_value", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_maker_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_made_on_date", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checker_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checked_on_date", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_status", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checker_notes", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_old_number", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_number", 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["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_page_id"].Value = page_id;
            oracom.Parameters["p_table_name"].Value = table_name;
            oracom.Parameters["p_chg_id"].Value = chgId;
            oracom.Parameters["p_chg_type"].Value = chgType;
            oracom.Parameters["p_column_name"].Value = columnName;
            oracom.Parameters["p_new_value"].Value = newValue;
            oracom.Parameters["p_old_value"].Value = oldValue;
            oracom.Parameters["p_maker_id"].Value = Session["uid"].ToString();
            oracom.Parameters["p_made_on_date"].Value = OracleDateTime.Null;
            oracom.Parameters["p_checker_id"].Value = OracleNumber.Null;
            oracom.Parameters["p_checked_on_date"].Value = OracleDateTime.Null;
            oracom.Parameters["p_changed_rec_id"].Value = pkey;
            oracom.Parameters["p_chg_status"].Value = 0;
            oracom.Parameters["p_checker_notes"].Value = OracleString.Null;

            if (oldValueNo.Trim() == "")
                oracom.Parameters["p_old_number"].Value = OracleNumber.Null;
            else
                oracom.Parameters["p_old_number"].Value = oldValueNo;
            if (NewValueNo.Trim() == "")
                oracom.Parameters["p_new_number"].Value = OracleNumber.Null;
            else
                oracom.Parameters["p_new_number"].Value = NewValueNo;

            oracom.Parameters["p_lang"].Value = "EN";

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "MAKER_CHECKER_EDITOR.Add_Maker_Checker", "440.1", "UpdateMakerChecker", "chgId: " + chgId + ", columnName: " + columnName + ", oldValue: " + oldValue + " , newValue: " + newValue + " , oldValueNo: " + oldValueNo + ", NewValueNo: " + NewValueNo + " ,chgType: " + chgType);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "MAKER_CHECKER_EDITOR.Add_Maker_Checker", "440.1", "UpdateMakerChecker", "chgId: " + chgId + ", columnName: " + columnName + ", oldValue: " + oldValue + " , newValue: " + newValue + " , oldValueNo: " + oldValueNo + ", NewValueNo: " + NewValueNo + " ,chgType: " + chgType);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "MAKER_CHECKER_EDITOR.Add_Maker_Checker","440.1", "UpdateMakerChecker", "chgId: " + chgId + ", columnName: " + columnName + ", oldValue: " + oldValue + " , newValue: " + newValue + " , oldValueNo: " + oldValueNo + ", NewValueNo: " + NewValueNo + " ,chgType: " + chgType);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnCancelValue_Click(object sender, EventArgs e)
    {
        pnlCOntrols2.Visible = false;
        pnlGrid.Enabled = true;
        clearControls();
        ExceptionUtility.LogException(0, "", "440.1", "btnCancelValue_Click", "");
    }

    protected void btnSaveValue_Click(object sender, EventArgs e)
    {
        try
        {
            string type = ViewState["type"].ToString();
            string error = "", value = "";
            
            if (type.Equals("0"))
                value = txtValue.Text.Trim ();
            else if (type.Equals("1"))
            {
                if(chkBox.Checked)
                    value = "Y";
                else
                    value = "N";
            }
            else if (type.Equals("2"))
            value = ddlValue.SelectedValue;


            string Uid = ViewState["uid"].ToString();
            //bool saved = updateParameterValue(Uid, value, ref error);
            string parameterValue=ViewState["value"].ToString ();
            int chgId = loadChgID();
            if (parameterValue == value)
                return;
            ExceptionUtility.LogException(0, "", "76.445", "btnSaveValue_Click", "");
            bool saved = UpdateMakerChecker(chgId ,Uid ,"SYS_PARMTR_VALUE",parameterValue,value ,"","",1,ref error);
            if (saved)
            {
                lblError.Text = "Your request has been sent to Checker user for Approval";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An Error Occured!";
                    ExceptionUtility.LogException(9, "", "76.445", "btnSaveValue_Click", error);
                }
                else
                    lblError.Text = error;
                
            }
            
            getParams();
            ViewState["type"] = null;
            ViewState["uid"] = null;
            pnlCOntrols2.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {

        }
    }

    private void clearControls()
    {
        txtName.Text = "";
        txtDesc.Text = "";
    }

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

    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ExceptionUtility.LogException(0, "", "2", "GridView1_PageIndexChanging", "");
        GridView1.PageIndex = e.NewPageIndex;
        getParams();
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {

            int app_status = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "app_status"));

            if (app_status == 0)
            {
                e.Row.Cells[4].Visible = true;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[9].Visible = false;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[6].Visible = true;
                e.Row.Cells[9].Visible = true;
            }
        }
    }
    private int loadMaxChgID(string atm_number)
    {
        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 = "maker_checker_editor.get_max_chg_id";

            oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_changed_rec_id"].Value = atm_number;

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

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            return nop;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_max_chg_id","", "loadMaxChgID", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
}
