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_ATM : System.Web.UI.Page
{
    public int page_id = 0;
    public string table_name = "";
    string url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        table_name = "atm_group_tbl";
        url = HttpContext.Current.Request.Url.AbsolutePath;
        string[] urlArr = url.Split('/');
        url = urlArr[2] + "/" + urlArr[3];
        page_id = loadPageIDs(url);
        if (!IsPostBack)
        {
            loadCurrency();
            loadGroups();

            this.pnl_confirmation.Visible = false;
        }
    }
    private int loadPageIDs(string URL)
    {
        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());

    }
    private int loadChgID()
    {
        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;
    }

    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)
        {
            return 0;
        }
    }

    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 = "LOOKUP_GETTER.get_curr";

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

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

        ddlCass1.DataSource = ds;
        ddlCass1.DataBind();
        ddlCass2.DataSource = ds;
        ddlCass2.DataBind();
        ddlCass3.DataSource = ds;
        ddlCass3.DataBind();
        ddlCass4.DataSource = ds;
        ddlCass4.DataBind();
        ddlCass5.DataSource = ds;
        ddlCass5.DataBind();
        ddlCass6.DataSource = ds;
        ddlCass6.DataBind();

        oracon.Close();
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "ATM_GROUP_EDITOR.get_atm_groups";

        
        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gid", 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_gid"].Value = OracleNumber .Null;
        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();
    }
    private DataSet loadGroupsDetails(int gid)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "ATM_GROUP_EDITOR.get_atm_groups";


        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gid", 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_gid"].Value = gid;
        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();
        return ds;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        pnlControls.Visible = true;
        pnlResult.Enabled = false;
        ViewState["mode"] = "a";
        
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            txtName.Text = GridView1.Rows[index].Cells[0].Text.ToString().Replace("&nbsp;","");

            txtCass1Value.Text = GridView1.Rows[index].Cells[2].Text.ToString().Replace("&nbsp;", "");
            txtCass2Value.Text = GridView1.Rows[index].Cells[4].Text.ToString().Replace("&nbsp;", "");
            txtCass3Value.Text = GridView1.Rows[index].Cells[6].Text.ToString().Replace("&nbsp;", "");
            txtCass4Value.Text = GridView1.Rows[index].Cells[8].Text.ToString().Replace("&nbsp;", "");
            txtCass5Value.Text = GridView1.Rows[index].Cells[10].Text.ToString().Replace("&nbsp;", "");
            txtCass6Value.Text = GridView1.Rows[index].Cells[12].Text.ToString().Replace("&nbsp;", "");

            if (GridView1.Rows[index].Cells[18].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[18].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass1.SelectedValue = GridView1.Rows[index].Cells[18].Text.ToString();

            if (GridView1.Rows[index].Cells[19].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[19].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass2.SelectedValue = GridView1.Rows[index].Cells[19].Text.ToString();

            if (GridView1.Rows[index].Cells[20].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[20].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass3.SelectedValue = GridView1.Rows[index].Cells[20].Text.ToString();

            if (GridView1.Rows[index].Cells[21].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[21].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass4.SelectedValue = GridView1.Rows[index].Cells[21].Text.ToString();

            if (GridView1.Rows[index].Cells[22].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[22].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass5.SelectedValue = GridView1.Rows[index].Cells[22].Text.ToString();

            if (GridView1.Rows[index].Cells[23].Text.ToString().Replace("&nbsp;", "").Length > 0
                && !GridView1.Rows[index].Cells[23].Text.ToString().Replace("&nbsp;", "").Equals("0"))
                ddlCass6.SelectedValue = GridView1.Rows[index].Cells[23].Text.ToString();


            pnlControls.Visible = true;
            pnlResult.Enabled = false;

            ViewState["mode"] = "u";
            ViewState["rowid"] = rowID;
        }
        else if (e.CommandName.Equals("vm"))
        {
            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("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            ViewState["selectedGroupID"] = rowID;

            this.pnlResult.Visible = false;
            this.pnl_confirmation.Visible = true;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string GROUP_NAME_NEW,CASS1_CURR_NEW, CASS1_CURR_DESC_NEW, CASS1_VALUE_NEW, CASS2_CURR_NEW, CASS2_CURR_DESC_NEW, CASS2_VALUE_NEW, CASS3_CURR_NEW, CASS3_CURR_DESC_NEW, CASS3_VALUE_NEW, CASS4_CURR_NEW, CASS4_CURR_DESC_NEW, CASS4_VALUE_NEW, CASS5_CURR_NEW, CASS5_CURR_DESC_NEW, CASS5_VALUE_NEW, CASS6_CURR_NEW, CASS6_CURR_DESC_NEW, CASS6_VALUE_NEW;
            string GROUP_NAME_OLD,CASS1_CURR_OLD, CASS1_CURR_DESC_OLD, CASS1_VALUE_OLD, CASS2_CURR_OLD, CASS2_CURR_DESC_OLD, CASS2_VALUE_OLD, CASS3_CURR_OLD, CASS3_CURR_DESC_OLD, CASS3_VALUE_OLD, CASS4_CURR_OLD, CASS4_CURR_DESC_OLD, CASS4_VALUE_OLD, CASS5_CURR_OLD, CASS5_CURR_DESC_OLD, CASS5_VALUE_OLD, CASS6_CURR_OLD, CASS6_CURR_DESC_OLD, CASS6_VALUE_OLD;
            GROUP_NAME_NEW = txtName.Text;
            if (txtCass1Value.Text.Length != 0)
            {
                 CASS1_CURR_NEW = ddlCass1.SelectedValue;
                 CASS1_CURR_DESC_NEW=ddlCass1.SelectedItem .Text ;
                 CASS1_VALUE_NEW = txtCass1Value.Text.Trim();
            }
            else
            {
                 CASS1_CURR_NEW ="";
                 CASS1_CURR_DESC_NEW="";
                 CASS1_VALUE_NEW = "";
            }

            if (txtCass2Value.Text.Length != 0)
            {
                CASS2_CURR_NEW = ddlCass2.SelectedValue;
                CASS2_CURR_DESC_NEW=ddlCass2.SelectedItem.Text ;
                CASS2_VALUE_NEW = txtCass2Value.Text.Trim();
            }
            else
            {
                CASS2_CURR_NEW = "";
                CASS2_CURR_DESC_NEW="";
                CASS2_VALUE_NEW ="";
            }

            if (txtCass3Value.Text.Length != 0)
            {
                CASS3_CURR_NEW = ddlCass3.SelectedValue;
                CASS3_CURR_DESC_NEW=ddlCass3.SelectedItem .Text  ;
                CASS3_VALUE_NEW = txtCass3Value.Text.Trim();
            }
            else
            {
                CASS3_CURR_NEW = "";
                CASS3_CURR_DESC_NEW ="";
                CASS3_VALUE_NEW = "";
            }

            if (txtCass4Value.Text.Length != 0)
            {
                CASS4_CURR_NEW = ddlCass4.SelectedValue;
                CASS4_CURR_DESC_NEW=ddlCass4 .SelectedItem .Text ;
                CASS4_VALUE_NEW = txtCass4Value.Text.Trim();
            }
            else
            {
                CASS4_CURR_NEW = "";
                CASS4_CURR_DESC_NEW ="";
                CASS4_VALUE_NEW = "";
            }

            if (txtCass5Value.Text.Length != 0)
            {
                CASS5_CURR_NEW = ddlCass5.SelectedValue;
                CASS5_CURR_DESC_NEW=ddlCass5 .SelectedItem .Text  ;
                CASS5_VALUE_NEW = txtCass5Value.Text.Trim();
            }
            else
            {
                CASS5_CURR_NEW = "";
                CASS5_CURR_DESC_NEW="";
                CASS5_VALUE_NEW = "";
            }


            if (txtCass6Value.Text.Length != 0)
            {
                CASS6_CURR_NEW = ddlCass6.SelectedValue;
                CASS6_CURR_DESC_NEW=ddlCass6.SelectedItem .Text;
                CASS6_VALUE_NEW = txtCass6Value.Text.Trim();
            }
            else
            {
                CASS6_CURR_NEW = "";
                CASS6_CURR_DESC_NEW="";
                CASS6_VALUE_NEW = "";
            }
            
            string error = "";
            if (mode.Equals("a"))
            {
                int chgId = loadChgID();                
                bool saved = false;
                saved = UpdateMakerChecker(chgId, "", "GROUP_NAME", "", GROUP_NAME_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "STATUS", "", "1", "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS1_CURR", "", CASS1_CURR_DESC_NEW, "", CASS1_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS1_VALUE", "", CASS1_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS2_CURR", "", CASS2_CURR_DESC_NEW, "", CASS2_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS2_VALUE", "", CASS2_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS3_CURR", "", CASS3_CURR_DESC_NEW, "", CASS3_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS3_VALUE", "", CASS3_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS4_CURR", "", CASS4_CURR_DESC_NEW, "", CASS4_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS4_VALUE", "", CASS4_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS5_CURR", "", CASS5_CURR_DESC_NEW, "", CASS5_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS5_VALUE", "", CASS5_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS6_CURR", "", CASS6_CURR_DESC_NEW, "", CASS6_CURR_NEW, 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }
                saved = UpdateMakerChecker(chgId, "", "CASS6_VALUE", "", CASS6_VALUE_NEW, "", "", 0, ref error);
                if (saved == false)
                {
                    lblError.Text = error;
                    return;
                }

                lblError.Text = "Your request has been sent to Checker user for Approval";
               
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["rowid"].ToString();
                int chgId = loadChgID();
                bool compare = false;
                DataSet ds = loadGroupsDetails(int.Parse(Uid));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GROUP_NAME_OLD = ds.Tables[0].Rows[0]["GROUP_NAME"].ToString();
                    CASS1_CURR_OLD = ds.Tables[0].Rows[0]["CASS1_CURR_VALUE"].ToString();
                    CASS1_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS1_CURR"].ToString();
                    CASS1_VALUE_OLD = ds.Tables[0].Rows[0]["CASS1_VALUE"].ToString();
                    CASS2_CURR_OLD = ds.Tables[0].Rows[0]["CASS2_CURR_VALUE"].ToString();
                    CASS2_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS2_CURR"].ToString();
                    CASS2_VALUE_OLD = ds.Tables[0].Rows[0]["CASS2_VALUE"].ToString();
                    CASS3_CURR_OLD = ds.Tables[0].Rows[0]["CASS3_CURR_VALUE"].ToString();
                    CASS3_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS3_CURR"].ToString();
                    CASS3_VALUE_OLD = ds.Tables[0].Rows[0]["CASS3_VALUE"].ToString();
                    CASS4_CURR_OLD = ds.Tables[0].Rows[0]["CASS4_CURR_VALUE"].ToString();
                    CASS4_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS4_CURR"].ToString();
                    CASS4_VALUE_OLD = ds.Tables[0].Rows[0]["CASS4_VALUE"].ToString();
                    CASS5_CURR_OLD = ds.Tables[0].Rows[0]["CASS5_CURR_VALUE"].ToString();
                    CASS5_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS5_CURR"].ToString();
                    CASS5_VALUE_OLD = ds.Tables[0].Rows[0]["CASS5_VALUE"].ToString();
                    CASS6_CURR_OLD = ds.Tables[0].Rows[0]["CASS6_CURR_VALUE"].ToString();
                    CASS6_CURR_DESC_OLD = ds.Tables[0].Rows[0]["CASS6_CURR"].ToString();
                    CASS6_VALUE_OLD = ds.Tables[0].Rows[0]["CASS6_VALUE"].ToString();

                    if (CASS1_CURR_NEW == CASS1_CURR_OLD && CASS1_VALUE_NEW == CASS1_VALUE_OLD &&
                       CASS2_CURR_NEW == CASS2_CURR_OLD && CASS2_VALUE_NEW == CASS2_VALUE_OLD &&
                       CASS3_CURR_NEW == CASS3_CURR_OLD && CASS3_VALUE_NEW == CASS3_VALUE_OLD &&
                       CASS4_CURR_NEW == CASS4_CURR_OLD && CASS4_VALUE_NEW == CASS4_VALUE_OLD &&
                       CASS5_CURR_NEW == CASS5_CURR_OLD && CASS5_VALUE_NEW == CASS5_VALUE_OLD &&
                       CASS6_CURR_NEW == CASS6_CURR_OLD && CASS6_VALUE_NEW == CASS6_VALUE_OLD)
                        compare = true;
                    if (compare == false)
                    {
                        bool update = false;
                        if (GROUP_NAME_NEW != GROUP_NAME_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "GROUP_NAME", GROUP_NAME_OLD, GROUP_NAME_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS1_CURR_NEW != CASS1_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS1_CURR", CASS1_CURR_DESC_OLD, CASS1_CURR_DESC_NEW, CASS1_CURR_OLD, CASS1_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS1_VALUE_NEW != CASS1_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS1_VALUE", CASS1_VALUE_OLD, CASS1_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS2_CURR_NEW != CASS2_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS2_CURR", CASS2_CURR_DESC_OLD, CASS2_CURR_DESC_NEW, CASS2_CURR_OLD, CASS2_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS2_VALUE_NEW != CASS2_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS2_VALUE", CASS2_VALUE_OLD, CASS2_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS3_CURR_NEW != CASS3_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS3_CURR", CASS3_CURR_DESC_OLD, CASS3_CURR_DESC_NEW, CASS3_CURR_OLD, CASS3_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS3_VALUE_NEW != CASS3_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS3_VALUE", CASS3_VALUE_OLD, CASS3_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS4_CURR_NEW != CASS4_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS4_CURR", CASS4_CURR_DESC_OLD, CASS4_CURR_DESC_NEW, CASS4_CURR_OLD, CASS4_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS4_VALUE_NEW != CASS4_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS4_VALUE", CASS4_VALUE_OLD, CASS4_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS5_CURR_NEW != CASS5_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS5_CURR", CASS5_CURR_DESC_OLD, CASS5_CURR_DESC_NEW, CASS5_CURR_OLD, CASS5_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS5_VALUE_NEW != CASS5_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS5_VALUE", CASS5_VALUE_OLD, CASS5_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }

                        if (CASS6_CURR_NEW != CASS6_CURR_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS6_CURR", CASS6_CURR_DESC_OLD, CASS6_CURR_DESC_NEW, CASS6_CURR_OLD, CASS6_CURR_NEW, 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }
                        if (CASS6_VALUE_NEW != CASS6_VALUE_OLD)
                        {
                            update = UpdateMakerChecker(chgId, Uid, "CASS6_VALUE", CASS6_VALUE_OLD, CASS6_VALUE_NEW, "", "", 1, ref error);
                            if (update == false)
                            {
                                lblError.Text = error;
                                return;
                            }
                        }

                        lblError.Text = "Your request has been sent to Checker user for Approval";
                      
                    }
                }
            }
            loadGroups();
            ViewState["mode"] = null;
            ViewState["rowid"] = null;
            pnlControls.Visible = false;
            pnlResult.Enabled = true;
        }
        catch (Exception ex)
        {
        }
    }

    private bool addGroup(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 = "ATM_GROUP_EDITOR.add_atm_group";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_GROUP_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS1_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS1_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS2_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS2_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS3_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS3_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS4_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS4_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS5_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS5_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS6_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS6_VALUE", 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_GROUP_NAME"].Value = txtName.Text;

        if (txtCass1Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS1_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS1_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS1_CURR"].Value = ddlCass1.SelectedValue;
            oracom.Parameters["p_CASS1_VALUE"].Value = txtCass1Value.Text;
        }

        if (txtCass2Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS2_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS2_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS2_CURR"].Value = ddlCass2.SelectedValue;
            oracom.Parameters["p_CASS2_VALUE"].Value = txtCass2Value.Text;
        }

        if (txtCass3Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS3_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS3_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS3_CURR"].Value = ddlCass3.SelectedValue;
            oracom.Parameters["p_CASS3_VALUE"].Value = txtCass3Value.Text;
        }

        if (txtCass4Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS4_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS4_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS4_CURR"].Value = ddlCass4.SelectedValue;
            oracom.Parameters["p_CASS4_VALUE"].Value = txtCass4Value.Text;
        }

        if (txtCass5Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS5_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS5_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS5_CURR"].Value = ddlCass5.SelectedValue;
            oracom.Parameters["p_CASS5_VALUE"].Value = txtCass5Value.Text;
        }

        if (txtCass6Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS6_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS6_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS6_CURR"].Value = ddlCass6.SelectedValue;
            oracom.Parameters["p_CASS6_VALUE"].Value = txtCass6Value.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;
    }

    private bool updateGroup(string rowID, 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 = "ATM_GROUP_EDITOR.update_atm_group";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_GROUP_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS1_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS1_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS2_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS2_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS3_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS3_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS4_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS4_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS5_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS5_VALUE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS6_CURR", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CASS6_VALUE", 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_gid"].Value = rowID;
        oracom.Parameters["p_GROUP_NAME"].Value = txtName.Text;

        if (txtCass1Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS1_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS1_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS1_CURR"].Value = ddlCass1.SelectedValue;
            oracom.Parameters["p_CASS1_VALUE"].Value = txtCass1Value.Text;
        }

        if (txtCass2Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS2_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS2_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS2_CURR"].Value = ddlCass2.SelectedValue;
            oracom.Parameters["p_CASS2_VALUE"].Value = txtCass2Value.Text;
        }

        if (txtCass3Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS3_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS3_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS3_CURR"].Value = ddlCass3.SelectedValue;
            oracom.Parameters["p_CASS3_VALUE"].Value = txtCass3Value.Text;
        }

        if (txtCass4Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS4_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS4_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS4_CURR"].Value = ddlCass4.SelectedValue;
            oracom.Parameters["p_CASS4_VALUE"].Value = txtCass4Value.Text;
        }

        if (txtCass5Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS5_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS5_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS5_CURR"].Value = ddlCass5.SelectedValue;
            oracom.Parameters["p_CASS5_VALUE"].Value = txtCass5Value.Text;
        }

        if (txtCass6Value.Text.Length == 0)
        {
            oracom.Parameters["p_CASS6_CURR"].Value = OracleNumber.Null;
            oracom.Parameters["p_CASS6_VALUE"].Value = OracleNumber.Null;
        }
        else
        {
            oracom.Parameters["p_CASS6_CURR"].Value = ddlCass6.SelectedValue;
            oracom.Parameters["p_CASS6_VALUE"].Value = txtCass6Value.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;
    }

    private bool deleteGroup(string GID, 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 = "ATM_GROUP_EDITOR.delete_atm_group";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gid", 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_gid"].Value = GID;

        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_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;
            e.Row.Cells[18].Visible = false;
            e.Row.Cells[19].Visible = false;
            e.Row.Cells[20].Visible = false;
            e.Row.Cells[21].Visible = false;
            e.Row.Cells[22].Visible = false;
            e.Row.Cells[23].Visible = false;
        }
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadGroups();
    }

    protected void btn_conf_ok_Click(object sender, EventArgs e)
    {
        string rowID = ViewState["selectedGroupID"].ToString();
        ViewState["mode"] = "d";
        string error = "";
        int chgId = loadChgID();       
        bool delete = false;
        delete = UpdateMakerChecker(chgId, rowID, "GID", rowID, "", "", "", 2, ref error);
        if(delete==true )        
        {
            lblError.Text = "Your request has been sent to Checker user for Approval";
        }
        else
        {
            lblError.Text = error;
        }

        this.pnlResult.Visible = true;
        this.pnl_confirmation.Visible = false;

        loadGroups();
    }
    protected void btn_conf_cancel_Click(object sender, EventArgs e)
    {
        this.pnlResult.Visible = true;
        this.pnl_confirmation.Visible = false;
    }
    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[15].Enabled = true;
                e.Row.Cells[16].Enabled = false;
                e.Row.Cells[17].Enabled = false;
            }
            else
            {
                e.Row.Cells[15].Enabled = false;
                e.Row.Cells[16].Enabled = true;
                e.Row.Cells[17].Enabled = true;
            }
        }

    }
    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")
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
