using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
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.Diagnostics;

public partial class ClaimsBusinessRules : 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 = "CLAIMS_BUSINESS_RULES";
        url = HttpContext.Current.Request.Url.AbsolutePath;
        string[] urlArr = url.Split('/');
        url = urlArr[2] + "/" + urlArr[3];
        page_id = loadPageIDs(url);
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            loadEvents();
            loadAccountTypes();
            loadCPSTypes();

            ExceptionUtility.LogException(0, "", "5", "Page_Load", "");
        }
    }
    private void loadCPSTypes()
    {
        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 = "poslookup_getter.get_atmpos_claimtype";


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



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

            ddlCPSType.DataSource = ds;
            ddlCPSType.DataBind();

            oracon.Close();

            ddlCPSType.Items.Add(new ListItem("--No CPS--", "0"));
            ddlCPSType.SelectedValue = "0";


            //if (lblClaimType.Text != "100")
            //    ddlCPSType.SelectedValue = lblClaimType.Text;
            //ExceptionUtility.LogException("Success", "poslookup_getter.Get_CPS_Claim_Type", "loadCPSTypes", "claimID: " + claimID);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "poslookup_getter.Get_CPS_Claim_Type", "", "loadCPSTypes", "product ");

        }
    }

    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 loadEvents()
    {
        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_recon_acq_iss_events";

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

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

            ddlEvents.DataSource = ds;
            ddlEvents.DataBind();

            ddlEvents0.DataSource = ds;
            ddlEvents0.DataBind();

            ddlEvents.Items.Insert(0, "------ Events ------");
            oracon.Close();

            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
            {
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("41"));
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("42"));
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("43"));

                ddlEvents0.Items.Remove(ddlEvents.Items.FindByValue("41"));
                ddlEvents0.Items.Remove(ddlEvents.Items.FindByValue("42"));
                ddlEvents0.Items.Remove(ddlEvents.Items.FindByValue("43"));
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Events", "", "loadEvents", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadAccountTypes()
    {
        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_claim_acct_types";

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

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

            ddlcreditacct.DataSource = ds;
            ddlcreditacct.DataBind();

            ddldebitacct.DataSource = ds;
            ddldebitacct.DataBind();

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Events", "", "loadEvents", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(string eventID, int pageSize, int pageNumber)
    {
        string parameters = "";
        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 = "claims_business_rules_editor.get_business_rules";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

            if (eventID.Length != 0)
            {
                parameters = parameters + "p_re_code-" + eventID;
                oracom.Parameters["p_re_code"].Value = eventID;
            }
            else
                oracom.Parameters["p_re_code"].Value = OracleNumber.Null;

            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            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);

            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
            if (currentMethod.Contains("Button1_Click"))
                ExceptionUtility.LogException(0, "claims_business_rules_editor.Get_Business_Rules", "132.1", "loadResult", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BUSINESS_RULES_EDITOR.Get_Business_Rules", "132.1", "loadResult", parameters);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        btnAdd.Enabled = true;
        lblError.Text = "";
        string channelID = "", EventID = "";
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        pnlResult.Visible = true;
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "5.132", "Button1_Click", "");//channelID: " + channelID + ", eventID: " + eventID + ", pageSize: " + pageSize + ", pageNumber: " + pageNumber);

        loadResult(EventID, Helper.gridSize(), 1);
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        string channelID = "", EventID = "";
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
        loadResult(EventID, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            intCount = 0;
            ExceptionUtility.LogException(0, "", "5.8", "GridView1_RowCommand.Edit", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            pnlBR.Visible = true;
            pnlBR.Enabled = true;
            loadBR(rowID);

            ViewState["rowid"] = rowID;
            ViewState["mode"] = "u";
            addrow.Visible = false;
        }
        else if (e.CommandName.Equals("d"))
        {
            ExceptionUtility.LogException(0, "", "3.32", "GridView1_RowCommand.Delete", "");
            this.pnl_confirmation.Visible = true;
            this.pnlBR.Visible = false;
            this.pnlResult.Visible = false;
            this.pnlFilter.Visible = false;

            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            ViewState["selectedID"] = rowID;

        }
        else if (e.CommandName.Equals("vm"))
        {
            ExceptionUtility.LogException(0, "", "5.478", "GridView1_RowCommand.ViewModification", "");
            pnlBR.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);

        }
    }
    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", "atm_number: " + atm_number);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
    private void loadBR(string rowID)
    {
        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 = "claims_business_rules_editor.Get_Rule_Details";

            oracom.Parameters.Add("p_cbr_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_cbr_code"].Value = rowID;

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

            string ej = ds.Tables[0].Rows[0]["ej"].ToString();
            if (ej.Equals("1")) //IRFAN_MOD
            {
                rdEJSuccessful.Checked = true;
                rdEJUnSuccessful.Checked = false;
            }
            else
            {
                rdEJUnSuccessful.Checked = true;
                rdEJSuccessful.Checked = false;
            }

            string cps = ds.Tables[0].Rows[0]["cps"].ToString();
            //if (cps.Equals("0"))
            //    chkCPS.Checked = false;
            //else
            //    chkCPS.Checked = true;
            ddlCPSType.SelectedValue = cps;


            string base24 = ds.Tables[0].Rows[0]["switch"].ToString();
            if (base24.Equals("1"))
                chkBase24.Checked = true;
            else
                chkBase24.Checked = false;

            string base24_rev = ds.Tables[0].Rows[0]["switch_rev"].ToString();
            if (base24_rev.Equals("1"))
                chkBase24rev.Checked = true;
            else
                chkBase24rev.Checked = false;



            string finacle = ds.Tables[0].Rows[0]["host"].ToString();
            if (finacle.Equals("1"))
                chkfinacle.Checked = true;
            else
                chkfinacle.Checked = false;

            string finacle_rev = ds.Tables[0].Rows[0]["host_rev"].ToString();
            if (finacle_rev.Equals("1"))
                chkfinaclerev.Checked = true;
            else
                chkfinaclerev.Checked = false;




            string span = ds.Tables[0].Rows[0]["span"].ToString();
            if (span.Equals("1"))
                chkspan.Checked = true;
            else
                chkspan.Checked = false;

            string span_rev = ds.Tables[0].Rows[0]["span_rev"].ToString();
            if (span_rev.Equals("1"))
                chkspanrev.Checked = true;
            else
                chkspanrev.Checked = false;

            ddlcreditacct.SelectedValue = ds.Tables[0].Rows[0]["credit_acct"].ToString();
            ddldebitacct.SelectedValue = ds.Tables[0].Rows[0]["debit_acct"].ToString();

            string enabled = ds.Tables[0].Rows[0]["enabled"].ToString();
            if (enabled.Equals("1"))
                chkEnabled.Checked = true;
            else
                chkEnabled.Checked = false;


            SetChannelBitMap(ds.Tables[0].Rows[0]["RE_CODE"].ToString());

            //txtcos.Text = ds.Tables[0].Rows[0]["cut_off_start"].ToString();
            //txtcoe.Text = ds.Tables[0].Rows[0]["cut_off_end"].ToString();

            //int intChecked = int.Parse(ds.Tables[0].Rows[0]["cut_off_rule"].ToString());
            //if (intChecked == 1)
            //    CheckBox1.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["tran_date"].ToString());
            //if (intChecked == 1)
            //    CheckBox2.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["tran_time"].ToString());
            //if (intChecked == 1)
            //    CheckBox3.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["card_number"].ToString());
            //if (intChecked == 1)
            //    CheckBox4.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["rrn"].ToString());
            //if (intChecked == 1)
            //    CheckBox5.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["trace_id"].ToString());
            //if (intChecked == 1)
            //    CheckBox6.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["local_date"].ToString());
            //if (intChecked == 1)
            //    CheckBox7.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["local_time"].ToString());
            //if (intChecked == 1)
            //    CheckBox8.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["currency"].ToString());
            //if (intChecked == 1)
            //    CheckBox9.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["tran_amount"].ToString());
            //if (intChecked == 1)
            //    CheckBox10.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["settlement_amount"].ToString());
            //if (intChecked == 1)
            //    CheckBox11.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["fee_amount"].ToString());
            //if (intChecked == 1)
            //    CheckBox12.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["auth_code"].ToString());
            //if (intChecked == 1)
            //    CheckBox13.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["message_type"].ToString());
            //if (intChecked == 1)
            //    CheckBox14.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["tran_source"].ToString());
            //if (intChecked == 1)
            //    CheckBox15.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["tran_destination"].ToString());
            //if (intChecked == 1)
            //    CheckBox16.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["term_id"].ToString());
            //if (intChecked == 1)
            //    CheckBox17.Checked = true;

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["acct_number"].ToString());
            //if (intChecked == 1)
            //    CheckBox19.Checked = true;

            //ddlMaster.SelectedValue = ds.Tables[0].Rows[0]["master_table"].ToString();
            //ddlSlave.SelectedValue = ds.Tables[0].Rows[0]["slave_table"].ToString();

            //txtBefore.Text = ds.Tables[0].Rows[0]["days_before"].ToString();
            //txtAfter.Text = ds.Tables[0].Rows[0]["days_after"].ToString();

            //loadFileNames();
            //ddlFileName.SelectedValue = ds.Tables[0].Rows[0]["FILE_NAME"].ToString();

            //intChecked = int.Parse(ds.Tables[0].Rows[0]["active"].ToString());
            //if (intChecked == 1)
            //    CheckBox18.Checked = true;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BUSINESS_RULES_EDITOR.Get_Rule_Details", "", "loadBR", "rowID: " + rowID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private DataSet loadBRDetails(string rowID)
    {
        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 = "claims_business_rules_editor.Get_Rule_Details";

            oracom.Parameters.Add("p_cbr_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_cbr_code"].Value = rowID;

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

            return ds;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BUSINESS_RULES_EDITOR.Get_Rule_Details", "", "loadBRDetails", "p_br_code- " + rowID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return null;
        }
    }

    private string getChannelsBitmap(string eventName)
    {
        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 = "AUTOClaim_APPROVEPOST_BYRULE.get_related_channels_to_event";

            oracom.Parameters.Add("p_recon_event", OracleType.VarChar, 200).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_channel_bitmap", OracleType.VarChar, 16).Direction = ParameterDirection.Output;


            oracom.Parameters["p_recon_event"].Value = eventName;


            DataTable dt = new DataTable();
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            //da.SelectCommand = oraCom;
            da.Fill(dt);
            return oracom.Parameters["p_res_channel_bitmap"].Value.ToString();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "AutoClaimRuleCfg.get_related_channels_to_event", "", "getChannelsBitmap", "p_recon_event_name- " + eventName);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return null;
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlBR.Visible = false;
        pnlResult.Enabled = true;
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");//rowID: " + rowID);
    }

    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", "408.1", "loadChgID", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (intCount < 1)
            intCount++;
        else
        {
            pnlBR.Visible = false;
            lblError.Text = "Your request has been sent to Checker user for Approval";
            return;
        }
        ExceptionUtility.LogException(0, "", "5.408", "btnSave_Click", "");
        string mode = ViewState["mode"].ToString();
        string error = "";
        string RE_CODE_NEW = ddlEvents0.SelectedValue;
        string re_code_desc_new = ddlEvents0.SelectedItem.Text;

        string EJ_NEW = "";
        if (rdEJSuccessful.Enabled == false)
            EJ_NEW = "-1";
        else
            EJ_NEW = rdEJSuccessful.Checked ? "1" : "0";

        //string CPS_NEW = chkCPS.Checked ? "1" : "0";
        //string CPS_NAME_NEW = chkCPS.Checked ? "YES" : "No";
        string CPS_NEW = ddlCPSType.SelectedValue;
        string CPS_NAME_NEW = ddlCPSType.SelectedValue;

        string SPAN_NEW = chkspan.Checked ? "1" : "0";
        string SPAN_REV_NEW = chkspanrev.Checked ? "1" : "0";
        string FINACLE_NEW = chkfinacle.Checked ? "1" : "0";
        string FINACLE_REV_NEW = chkfinaclerev.Checked ? "1" : "0";
        string BASE24_NEW = chkBase24.Checked ? "1" : "0";
        string BASE24_REV_NEW = chkBase24rev.Checked ? "1" : "0";
        string CREDIT_ACCT_NEW = ddlcreditacct.SelectedValue;
        string DEBIT_ACCT_NEW = ddldebitacct.SelectedValue;
        string CREDIT_ACCT_NAME_NEW = ddlcreditacct.SelectedItem.Text;
        string DEBIT_ACCT_NAME_NEW = ddldebitacct.SelectedItem.Text;
        string ENABLED_NEW = chkEnabled.Checked ? "1" : "0";
        string ENABLED_NAME_NEW = chkEnabled.Checked ? "YES" : "No";

        if (mode.Equals("a"))
        {
            int chgId = loadChgID();
            bool saved = false;


            //event
            saved = UpdateMakerChecker(chgId, "", "RE_CODE", "", re_code_desc_new, "", RE_CODE_NEW, 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //ej
            saved = UpdateMakerChecker(chgId, "", "EJ", "", EJ_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //cps
            saved = UpdateMakerChecker(chgId, "", "CPS", "", CPS_NAME_NEW, "", CPS_NEW, 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //base24
            saved = UpdateMakerChecker(chgId, "", "SWITCH", "", BASE24_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //base24_rev
            saved = UpdateMakerChecker(chgId, "", "SWITCH_REV", "", BASE24_REV_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //finacle
            saved = UpdateMakerChecker(chgId, "", "HOST", "", FINACLE_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //finacle_rev
            saved = UpdateMakerChecker(chgId, "", "HOST_REV", "", FINACLE_REV_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //span
            saved = UpdateMakerChecker(chgId, "", "SPAN", "", SPAN_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //span_rev
            saved = UpdateMakerChecker(chgId, "", "SPAN_REV", "", SPAN_REV_NEW, "", "", 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //credit_acct
            saved = UpdateMakerChecker(chgId, "", "CREDIT_ACCT", "", CREDIT_ACCT_NAME_NEW, "", CREDIT_ACCT_NEW, 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //debit_acct
            saved = UpdateMakerChecker(chgId, "", "DEBIT_ACCT", "", DEBIT_ACCT_NAME_NEW, "", DEBIT_ACCT_NEW, 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //ENABLED
            saved = UpdateMakerChecker(chgId, "", "ENABLED", "", ENABLED_NAME_NEW, "", ENABLED_NEW, 0, ref error);
            if (saved == false)
            {
                lblError.Text = error;
                return;
            }

            //bitmap
            string bitmap = "";
            if (rdEJSuccessful.Enabled == false)
                bitmap += "N";
            else if (rdEJSuccessful.Checked)
                bitmap += "1";
            else
                bitmap += "0";

            if (chkBase24.Enabled == false)
                bitmap += "N";
            else if (chkBase24.Checked)
                bitmap += "1";
            else
                bitmap += "0";
            if (chkBase24rev.Enabled == false)
                bitmap += "N";
            else if (chkBase24rev.Checked)
                bitmap += "1";
            else
                bitmap += "0";

            if (chkfinacle.Enabled == false)
                bitmap += "N";
            else if (chkfinacle.Checked)
                bitmap += "1";
            else
                bitmap += "0";
            if (chkfinaclerev.Enabled == false)
                bitmap += "N";
            else if (chkfinaclerev.Checked)
                bitmap += "1";
            else
                bitmap += "0";

            if (chkspan.Enabled == false)
                bitmap += "N";
            else if (chkspan.Checked)
                bitmap += "1";
            else
                bitmap += "0";
            if (chkspanrev.Enabled == false)
                bitmap += "N";
            else if (chkspanrev.Checked)
                bitmap += "1";
            else
                bitmap += "0";

            saved = UpdateMakerChecker(chgId, "", "BITMAP", "", bitmap, "", "", 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 rowID = ViewState["rowid"].ToString();
            int chgId = loadChgID();
            bool compare = false;
            DataSet ds = loadBRDetails(rowID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                //string RE_CODE_OLD = ds.Tables[0].Rows[0]["re_code"].ToString();
                string EJ_OLD = ds.Tables[0].Rows[0]["ej"].ToString();
                string CPS_OLD = ds.Tables[0].Rows[0]["cps"].ToString();
                string CPS_NAME_OLD = CPS_OLD;// CPS_OLD == "1" ? "YES" : "NO";
                string SPAN_OLD = ds.Tables[0].Rows[0]["span"].ToString();
                string SPAN_REV_OLD = ds.Tables[0].Rows[0]["span_rev"].ToString();
                string FINACLE_OLD = ds.Tables[0].Rows[0]["host"].ToString();
                string FINACLE_REV_OLD = ds.Tables[0].Rows[0]["host_rev"].ToString();
                string BASE24_OLD = ds.Tables[0].Rows[0]["switch"].ToString();
                string BASE24_REV_OLD = ds.Tables[0].Rows[0]["switch_rev"].ToString();
                string CREDIT_ACCT_OLD = ds.Tables[0].Rows[0]["credit_acct"].ToString();
                string DEBIT_ACCT_OLD = ds.Tables[0].Rows[0]["debit_acct"].ToString();
                string CREDIT_ACCT_NAME_OLD = ds.Tables[0].Rows[0]["credit_acct_name"].ToString();
                string DEBIT_ACCT_NAME_OLD = ds.Tables[0].Rows[0]["debit_acct_name"].ToString();
                string ENABLED_OLD = ds.Tables[0].Rows[0]["enabled"].ToString();
                string ENABLED_NAME_OLD = ENABLED_OLD == "1" ? "YES" : "NO";

                if (EJ_NEW == EJ_OLD &&
                    CPS_NEW == CPS_OLD &&
                    SPAN_NEW == SPAN_OLD &&
                    SPAN_REV_NEW == SPAN_REV_OLD &&
                    FINACLE_NEW == FINACLE_OLD &&
                    FINACLE_REV_NEW == FINACLE_REV_OLD &&
                    BASE24_NEW == BASE24_OLD &&
                    BASE24_REV_NEW == BASE24_REV_OLD &&
                    CREDIT_ACCT_NEW == CREDIT_ACCT_OLD &&
                    DEBIT_ACCT_NEW == DEBIT_ACCT_OLD &&
                    ENABLED_NEW == ENABLED_OLD)

                    compare = true;

                if (compare == false)
                {
                    bool update = false;

                    if (EJ_NEW != EJ_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "EJ", EJ_OLD, EJ_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (CPS_NEW != CPS_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "CPS", CPS_NAME_OLD, CPS_NAME_NEW, CPS_OLD, CPS_NEW, 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (SPAN_NEW != SPAN_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "SPAN", SPAN_OLD, SPAN_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (SPAN_REV_NEW != SPAN_REV_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "SPAN_REV", SPAN_REV_OLD, SPAN_REV_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }

                    if (FINACLE_NEW != FINACLE_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "HOST", FINACLE_OLD, FINACLE_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (FINACLE_REV_NEW != FINACLE_REV_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "HOST_REV", FINACLE_REV_OLD, FINACLE_REV_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (BASE24_NEW != BASE24_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "SWITCH", BASE24_OLD, BASE24_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (BASE24_REV_NEW != BASE24_REV_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "SWITCH_REV", BASE24_REV_OLD, BASE24_REV_NEW, "", "", 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (CREDIT_ACCT_OLD != CREDIT_ACCT_NEW)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "CREDIT_ACCT", CREDIT_ACCT_NAME_OLD, CREDIT_ACCT_NAME_NEW, CREDIT_ACCT_OLD, CREDIT_ACCT_NEW, 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (DEBIT_ACCT_NEW != DEBIT_ACCT_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "DEBIT_ACCT", DEBIT_ACCT_NAME_OLD, DEBIT_ACCT_NAME_NEW, DEBIT_ACCT_OLD, DEBIT_ACCT_NEW, 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (ENABLED_NEW != ENABLED_OLD)
                    {
                        update = UpdateMakerChecker(chgId, rowID, "ENABLED", ENABLED_NAME_OLD, ENABLED_NAME_NEW, ENABLED_OLD, ENABLED_NEW, 1, ref error);
                        if (update == false)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }

                    //bitmap
                    string bitmap = "";
                    if (rdEJSuccessful.Enabled == false)
                        bitmap += "N";
                    else if (rdEJSuccessful.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";

                    if (chkBase24.Enabled == false)
                        bitmap += "N";
                    else if (chkBase24.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";
                    if (chkBase24rev.Enabled == false)
                        bitmap += "N";
                    else if (chkBase24rev.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";

                    if (chkfinacle.Enabled == false)
                        bitmap += "N";
                    else if (chkfinacle.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";
                    if (chkfinaclerev.Enabled == false)
                        bitmap += "N";
                    else if (chkfinaclerev.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";

                    if (chkspan.Enabled == false)
                        bitmap += "N";
                    else if (chkspan.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";
                    if (chkspanrev.Enabled == false)
                        bitmap += "N";
                    else if (chkspanrev.Checked)
                        bitmap += "1";
                    else
                        bitmap += "0";

                    update = UpdateMakerChecker(chgId, "", "BITMAP", "", bitmap, "", "", 1, ref error);
                    if (update == false)
                    {
                        lblError.Text = error;
                        return;
                    }

                    lblError.Text = "Your request has been sent to Checker user for Approval";

                }

            }


        }

        //loading the grid again
        string EventID = "";
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        pnlResult.Visible = true;
        pnlResult.Enabled = true;
        pnlBR.Visible = false;
        loadResult(EventID, Helper.gridSize(), 1);


    }



    protected void btnAdd_Click(object sender, EventArgs e)
    {
        intCount = 0;
        pnlBR.Visible = true;
        pnlResult.Enabled = false;
        ViewState["mode"] = "a";





        if (ddlEvents.SelectedIndex > 0)
            ddlEvents0.SelectedValue = ddlEvents.SelectedValue;


        rdEJSuccessful.Checked = true;
        // chkCPS.Checked = false;
        ddlCPSType.SelectedValue = "0"; // NO CPS

        chkspan.Checked = false;
        chkspanrev.Checked = false;
        chkBase24rev.Checked = false;
        chkBase24.Checked = false;
        chkfinaclerev.Checked = false;
        chkfinacle.Checked = false;

        ddldebitacct.SelectedIndex = 0;
        ddlcreditacct.SelectedIndex = 0;
        chkEnabled.Checked = false;
        Visible = true;

        SetChannelBitMap(ddlEvents0.SelectedValue);
        ExceptionUtility.LogException(0, "", "5.408", "btnAdd_Click", "");
    }

    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", "408.2", "UpdateMakerChecker", "chgId: " + chgId + ", pkey: " + pkey + ", columnName: " + columnName + ", oldValue: " + oldValue + " , newValue: " + newValue + " , oldValueNo: " + oldValueNo + ", NewValueNo: " + NewValueNo + " ,chgType: " + chgType);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "MAKER_CHECKER_EDITOR.Add_Maker_Checker", "408.2", "UpdateMakerChecker", "chgId: " + chgId + ", pkey: " + pkey + ", 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", "408.2", "UpdateMakerChecker", "chgId: " + chgId + ", pkey: " + pkey + ", columnName: " + columnName + ", oldValue: " + oldValue + " , newValue: " + newValue + " , oldValueNo: " + oldValueNo + ", NewValueNo: " + NewValueNo + " ,chgType: " + chgType);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 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[14].Visible = true;
                e.Row.Cells[15].Visible = false;
            }
            else
            {
                e.Row.Cells[14].Visible = false;
                e.Row.Cells[15].Visible = true;
            }
            e.Row.Cells[12].Visible = false;
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }


    protected void btn_conf_ok_Click(object sender, EventArgs e)
    {
        string rowID = ViewState["selectedID"].ToString();
        ViewState["mode"] = "d";
        string error = "";
        ExceptionUtility.LogException(0, "", "", "btn_conf_ok_Click", "");
        int chgId = loadChgID();
        bool saved = UpdateMakerChecker(chgId, rowID, "CBR_CODE", "", rowID, "", "", 2, ref error);
        if (saved)
        {
            lblError.Text = "Your request has been sent to Checker user for Approval";
        }
        else
        {
            lblError.Text = error;
            ExceptionUtility.LogException(9, "", "", "btn_conf_ok_Click", "");
        }

        this.pnl_confirmation.Visible = false;
        this.pnlFilter.Visible = true;
        this.pnlResult.Visible = true;
        ViewState["mode"] = null;
        Button1_Click(sender, e);
    }
    protected void btn_conf_cancel_Click(object sender, EventArgs e)
    {
        this.pnl_confirmation.Visible = false;
        this.pnlFilter.Visible = true;
        this.pnlResult.Visible = true;
        ExceptionUtility.LogException(0, "", "", "btn_conf_cancel_Click", "");
    }
    protected void ddlEvents0_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetChannelBitMap(ddlEvents0.SelectedValue);
    }
    protected void SetChannelBitMap(string ReCode)
    {
        string strChannelBitmap = getChannelsBitmap(ReCode);

        chkBase24.Enabled = false;
        chkBase24rev.Enabled = false;

        rdEJSuccessful.Enabled = false;
        rdEJUnSuccessful.Enabled = false;

        chkfinacle.Enabled = false;
        chkfinaclerev.Enabled = false;

        chkspan.Enabled = false;
        chkspanrev.Enabled = false;

        if (strChannelBitmap.Substring(0, 1) == "1")
        {
            rdEJSuccessful.Enabled = true;
            rdEJUnSuccessful.Enabled = true;
        }

        if (strChannelBitmap.Substring(2, 1) == "1")
        {
            chkBase24.Enabled = true;
            chkBase24rev.Enabled = true;
        }

        if (strChannelBitmap.Substring(3, 1) == "1")
        {
            chkspan.Enabled = true;
            chkspanrev.Enabled = true;
        }

        if (strChannelBitmap.Substring(5, 1) == "1")
        {
            chkfinacle.Enabled = true;
            chkfinaclerev.Enabled = true;
        }

    }
}
