using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;

public partial class Business_Checker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (Request.QueryString["chgId"]!=null)
            {
               
                pnl_filters.Visible = false;
                getMakerChecker();
                btnClose.Visible = true;
            }
            else
            {
                btnClose.Visible = false;
            }
            
            loadPages();
            loadRequestTypes();
            loadStatus();
           
        }
        lblError.Text = "";
    }

    private void loadPages()
    {
        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_configuration_pages";
       
        oracom.Parameters.Add("p_pages", OracleType.Cursor).Direction = ParameterDirection.Output;

        

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

        ddlPages.DataSource = ds;
        ddlPages.DataBind();
        ddlPages.Items.Insert(0, "------ Pages ------");
        oracon.Close();
    }

    private bool CheckVarchar(string tableName,string columnName)
    {
        string dataType = "";
        bool isVarchar=false ;
        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_column_names";
        oracom.Parameters.Add("p_table_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_columns", OracleType.Cursor).Direction = ParameterDirection.Output;

        oracom.Parameters["p_table_name"].Value = tableName ;
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);
        oracon.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["column_name"].ToString().Trim() == columnName.ToUpper ())
                {                    
                    dataType = ds.Tables[0].Rows[i]["data_type"].ToString();
                    if (dataType.Contains("VARCHAR2"))
                        isVarchar = true;
                }
            }
        }
        return isVarchar;

    }

    private void loadRequestTypes()
    {
        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_change_type";
        oracom.Parameters.Add("p_chg_types", OracleType.Cursor).Direction = ParameterDirection.Output;

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

        ddlRequestType .DataSource = ds;
        ddlRequestType.DataBind();
      
        //ddlRequestType.Items.Insert(0, new ListItem("------ Request Types ------", "ct_code"));
        ddlRequestType.Items.Insert(0, "------ Request Types ------");
        oracon.Close();
    }

    private void loadStatus()
    {
        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_change_status";
        oracom.Parameters.Add("p_chg_status", OracleType.Cursor).Direction = ParameterDirection.Output;

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

        ddlStatus.DataSource = ds;
        ddlStatus.DataBind();
     
        //ddlStatus.Items.Insert(0, new ListItem("------ Status ------", "sc_code"));
        ddlStatus.Items.Insert(0, "------ Status ------");

        oracon.Close();
    }
    private void getMakerChecker()
    {
        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_Maker_Checker_Master";

            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_page_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_status", 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;

            

            if (Request.QueryString["chgId"] == null)
                oracom.Parameters["p_chg_id"].Value = OracleNumber.Null;
            else
                oracom.Parameters["p_chg_id"].Value = int.Parse(Request.QueryString["chgId"]);
            
            if (ddlPages.SelectedIndex==0 || ddlPages .SelectedIndex ==-1 )
                oracom.Parameters["p_page_id"].Value = OracleNumber.Null;            
            else            
                oracom.Parameters["p_page_id"].Value =ddlPages.SelectedValue ;
            
            if (ddlRequestType .SelectedIndex ==0 || ddlRequestType .SelectedIndex ==-1)            
                oracom.Parameters["p_chg_type"].Value = OracleNumber .Null;            
            else            
                oracom.Parameters["p_chg_type"].Value = ddlRequestType .SelectedValue ;
            
            if (ddlStatus.SelectedIndex ==0 || ddlStatus .SelectedIndex ==-1)
                oracom.Parameters["p_chg_status"].Value = OracleNumber.Null;
            else            
                oracom.Parameters["p_chg_status"].Value = ddlStatus.SelectedValue;            
            
            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();
        }
        catch (Exception ex)
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    private void getMakerCheckerDetails(int chgId,bool paging)
    {
        try
        {
            ViewState["count"] = null;
            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_Maker_Checker_Detail";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_mkt_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).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_total_pages", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_chg_id"].Value = chgId;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            //oracom.Parameters["p_page_size"].Value = pageSize;
            //oracom.Parameters["p_page_num"].Value = pageNumber;
            //oracom.Parameters["p_mkt_code"].Value = OracleNumber.Null;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            //ViewState["count"] = ds.Tables[0].Rows.Count;
            oracon.Close();
            
            GridView2.DataSource = ds;
            GridView2.AllowPaging = paging;
            GridView2.DataBind();
           
            if(ds.Tables[0].Rows[0]["checker_notes"].ToString().Trim().Length !=0)
            txtCheckerNotes.Text = ds.Tables[0].Rows[0]["checker_notes"].ToString();
        }
        catch (Exception ex)
        {
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
    }

   

    private bool decline(int chgId, string tableName, string columnName, string oldValue, string changedRecId, int chgType, 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 = "MAKER_CHECKER_EDITOR.decline_maker_checker";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        //oracom.Parameters.Add("p_mkt_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_chg_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_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_chg_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_checker_notes", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        //oracom.Parameters["p_mkt_code"].Value = mktCode;
        oracom.Parameters["p_chg_id"].Value = chgId;
        oracom.Parameters["p_table_name"].Value = tableName;
        oracom.Parameters["p_column_name"].Value = columnName;
        oracom.Parameters["p_old_value"].Value = oldValue;
        oracom.Parameters["p_changed_rec_id"].Value = changedRecId;
        oracom.Parameters["p_chg_type"].Value = chgType;
        oracom.Parameters["p_checker_notes"].Value = txtCheckerNotes .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 approve(int chgId, string tableName,string columnName,string newValue, string changedRecId, int chgType, 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 = "MAKER_CHECKER_EDITOR.approve_maker_checker";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        //oracom.Parameters.Add("p_mkt_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_chg_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_new_value", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_chg_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_checker_notes", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        //oracom.Parameters["p_mkt_code"].Value = mktCode;
        oracom.Parameters["p_chg_id"].Value = chgId;
        oracom.Parameters["p_table_name"].Value = tableName;
        oracom.Parameters["p_column_name"].Value = columnName;
        oracom.Parameters["p_new_value"].Value = newValue;
        oracom.Parameters["p_changed_rec_id"].Value = changedRecId;
        oracom.Parameters["p_chg_type"].Value = chgType;
        oracom.Parameters["p_checker_notes"].Value = ViewState ["checkerNotes"];
        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_RowCommand(object sender, GridViewCommandEventArgs e)
    {

       
        if (e.CommandName.Equals("vd"))
        {
            txtCheckerNotes.Text = "";
            pnlControls.Visible = true;
            pnlDetails.Visible = true;
            //pnlGrid.Enabled = false;
            int index = int.Parse(e.CommandArgument.ToString());
            int chgId = int.Parse(GridView1.DataKeys[index].Value.ToString());
            ViewState["chgID"] = chgId;
            getMakerCheckerDetails(chgId,true);
           

            if (Request.QueryString["chgID"] != null)
                pnlControls.Visible = false;
            if (int.Parse(GridView1.Rows[index].Cells[10].Text) == 1 || int.Parse(GridView1.Rows[index].Cells[10].Text) == 2)
            {
                btnApprove.Visible = false;
                btnCancel.Visible = false;
                btnCancel1.Visible = false;
            }
            else
            {
                btnApprove.Visible = true ;
                btnCancel.Visible = true ;
                btnCancel1.Visible = true ;
            }
            
           
        }

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


    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getMakerChecker();
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
                     
           
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
                getMakerChecker();
    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        string error = "";
        bool temp = false,isVarchar = false;        
        int chgID=0,chgType=0,pageId=0;
        string pageName="",tableName="",tableNamePrimary="",tableNameSecondary="",tableName3="",columnName="",newValue="",newValueNo="",changedRecId="",changedRecIdPrimary="",changedRecIdSecondary="",changedRecId3="",originalValue="",strColumns = "",strValues = "",strColumns1="",strColumns3="",strValues1="",strValues3="",strColumnsConstant="",strValuesConstant="";

        ViewState["checkerNotes"] = txtCheckerNotes.Text;
        getMakerCheckerDetails(int.Parse (ViewState["chgID"].ToString ()),false );
       
               foreach(GridViewRow row in GridView2.Rows )
                {
                    chgID = int.Parse(row.Cells[1].Text);
                    pageId = int.Parse(row.Cells[2].Text);
                    pageName = row.Cells[3].Text;
                    tableName = row.Cells[4].Text.Trim();
                    columnName = row.Cells[5].Text.Trim();
                    newValue = row.Cells[7].Text.Trim();
                    newValueNo = row.Cells[9].Text.Trim();
                    changedRecId = row.Cells[10].Text;
                    chgType = int.Parse(row.Cells[11].Text);
                    pageName = row.Cells[16].Text;
                    if (newValueNo.Length ==0)
                        originalValue = newValue;
                    else
                        originalValue = newValueNo;
                   
                    isVarchar = CheckVarchar(tableName, columnName);
                    strColumnsConstant = strColumnsConstant + row.Cells[5].Text + ",";
                    if (pageName.Equals  ("GL Setup"))
                    {
                        if (tableName == "gl_acct_table")
                        {
                            tableNamePrimary = tableName;
                            changedRecIdPrimary = changedRecId;
                            strColumns = strColumns + row.Cells[5].Text + ",";
                            if (isVarchar == true)
                            {
                                strValues = strValues + "'" + originalValue + "'" + ",";
                            }
                            else
                            {
                                if (originalValue == string.Empty)
                                {
                                    originalValue = OracleNumber.Null.ToString();
                                }
                                strValues = strValues + originalValue + ",";

                            }
                        }
                        else
                        {
                            tableNameSecondary = tableName;
                            changedRecIdSecondary = changedRecId;
                            strColumns1 = strColumns1 + row.Cells[5].Text + ",";
                            if (isVarchar == true)
                            {
                                strValues1 = strValues1 + "'" + originalValue + "'" + ",";
                            }
                            else
                            {
                                if (originalValue == string.Empty)
                                {
                                    originalValue = OracleNumber.Null.ToString();
                                }
                                strValues1 = strValues1 + originalValue + ",";

                            }
                        }

                    }
                    if (pageName.Equals ("Add Bank"))
                    {
                        if (tableName == "span")
                        {
                            tableNamePrimary = tableName;
                            changedRecIdPrimary = changedRecId;
                            strColumns = strColumns + row.Cells[5].Text + ",";
                            if (isVarchar == true)
                            {
                                strValues = strValues + "'" + originalValue + "'" + ",";
                            }
                            else
                            {
                                if (originalValue == string.Empty)
                                {
                                    originalValue = OracleNumber.Null.ToString();
                                }
                                strValues = strValues + originalValue + ",";

                            }
                        }
                        else if (tableName == "lkp_bank_bin")
                        {
                            tableNameSecondary = tableName;
                            changedRecIdSecondary = changedRecId;
                            strColumns1 = strColumns1 + row.Cells[5].Text + ",";
                            if (isVarchar == true)
                            {
                                strValues1 = strValues1 + "'" + originalValue + "'" + ",";
                            }
                            else
                            {
                                if (originalValue == string.Empty)
                                {
                                    originalValue = OracleNumber.Null.ToString();
                                }
                                strValues1 = strValues1 + originalValue + ",";

                            }
                        }
                        else
                        {
                            tableName3 = tableName;
                            changedRecId3 = changedRecId;
                            strColumns3 = strColumns3 + row.Cells[5].Text + ",";
                            if (isVarchar == true)
                            {
                                strValues3 = strValues3 + "'" + originalValue + "'" + ",";
                            }
                            else
                            {
                                if (originalValue == string.Empty)
                                {
                                    originalValue = OracleNumber.Null.ToString();
                                }
                                strValues3 = strValues3 + originalValue + ",";

                            }
                        }

                    }
                    if (isVarchar == true)
                    {
                        strValuesConstant = strValuesConstant + "'" + originalValue + "'" + ",";
                    }
                    else
                    {
                        if (originalValue == string.Empty)
                        {
                            originalValue = OracleNumber .Null.ToString ();
                        }
                       strValuesConstant = strValuesConstant + originalValue + ",";
                        
                    }
                    
                    if(chgType  !=0)
                        temp = approve(chgID, tableName, columnName, originalValue, changedRecId, chgType, ref error);
                    
                }
                if (chgType == 0)
                {
                    strColumns = strColumns.TrimEnd(',');
                    strValues = strValues.TrimEnd(',');
                    strColumns1 = strColumns1.TrimEnd(',');
                    strValues1 = strValues1.TrimEnd(',');
                    strColumns3 = strColumns3.TrimEnd(',');
                    strValues3 = strValues3.TrimEnd(',');
                    strColumnsConstant = strColumnsConstant.TrimEnd(',');
                    strValuesConstant = strValuesConstant.TrimEnd(',');
                    if (pageId == 14)
                    {
                        temp = approve(chgID, tableNamePrimary, strColumns, strValues, changedRecIdPrimary, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                                //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                        temp = approve(chgID, tableNameSecondary, strColumns1, strValues1, changedRecIdSecondary, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                                //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                    }
                    else if (pageId == 1)
                    {
                        temp = approve(chgID, tableNamePrimary, strColumns, strValues, changedRecIdPrimary, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                               //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                        temp = approve(chgID, tableNameSecondary, strColumns1, strValues1, changedRecIdSecondary, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                                //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                        temp = approve(chgID, tableName3, strColumns3, strValues3, changedRecId3, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                                //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                    }
                    else
                    {
                        temp = approve(chgID, tableName, strColumnsConstant, strValuesConstant, changedRecId, chgType, ref error);
                        if (temp == false)
                        {
                            lblError.Text = error;
                            if (error.Contains("Exception - ORA-00001: unique constraint"))
                            {
                                lblError.Text = "Please note this record already exists";
                                pnlDetails.Visible = false;
                                pnlControls.Visible = false;
                                //getMakerCheckerDetails(chgID, true);
                                return;
                            }
                        }
                    }
                }
            
            
            if (temp)
            {
                lblError.Text = "Your request has been processed successfully";

            }
            else
            {
                lblError.Text = error;
                if (error.Contains("Exception - ORA-00001: unique constraint"))
                    lblError.Text = "Entered " + columnName + " exists";
            }

            getMakerCheckerDetails(chgID,true);
        
        txtCheckerNotes.Text = "";
        pnlControls.Visible = false;
        pnlDetails.Visible = false;
        //getMakerChecker(int.Parse(ddlPages .SelectedValue), ddlRequestType .SelectedValue ,ddlStatus .SelectedValue);
        getMakerChecker();
       
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string error = "";
        int chgID = 0, chgType = 0;
        string tableName = "", columnName = "", oldValue = "", oldValueNo = "", originalValue = "", changedRecId = "";
        foreach (GridViewRow gvr in GridView2.Rows)
        {
            chgID = int.Parse(gvr.Cells[1].Text.Trim ());
            tableName = gvr.Cells[4].Text.Trim();
            columnName = gvr.Cells[5].Text.Trim();
            oldValue = gvr.Cells[6].Text.Trim ();
            oldValueNo = gvr.Cells[8].Text.Trim();            
            if (oldValueNo.Length ==0)
                originalValue = oldValue;
            else
                originalValue = oldValueNo;
            changedRecId = gvr.Cells[10].Text;
            chgType = int.Parse(gvr.Cells[11].Text);

            bool temp = decline(chgID, tableName, columnName, originalValue, changedRecId, chgType, ref error);
            if (temp)
            {
                lblError.Text = "Request rejected";
               
            }
            else
            {
                lblError.Text = error;
                break;
            }
            getMakerCheckerDetails(chgID,true);
        }
        txtCheckerNotes.Text = "";
        pnlControls.Visible = false;
        pnlDetails.Visible = false;
        getMakerChecker();
       
    }

   
    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[13].Visible = false;
            e.Row.Cells[16].Visible = false;
        }
    }
    protected void btnCancel1_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlDetails.Visible = false;
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName.Contains ("vd"))
        {
            Session["eventID"] = null;
            int index = int.Parse(e.CommandArgument.ToString());
            string URL = GridView2.Rows[index].Cells[3].Text;
            string[] newURL = URL.Split('/');
            URL = newURL[1];
            Session["eventID"] = GridView2.Rows[index].Cells[10].Text;

            string popup = "<script language='javascript'>" +
                            "window.open('"+URL+"');" +
                            "</script>";           
            ClientScript.RegisterStartupScript(this.GetType(), "", popup, false);

        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (e.Row.Cells[i].Text.Length == 0 || e.Row.Cells[i].Text.Equals("&nbsp;") || e.Row.Cells[i].Text.Equals("") || e.Row.Cells[i].Text.Equals(string.Empty))
                {
                    e.Row.Cells[i].Text = string.Empty;
                }
            }
            int type = int.Parse(e.Row.Cells[11].Text);
            int status = int.Parse(e.Row.Cells[13].Text);
            if (type == 0 || type ==1)
            {
                e.Row.Cells[15].Enabled = false;
            }
            else
            {
                e.Row.Cells[15].Enabled = true;
            }
            if (status == 0)
            {
                e.Row.Cells[15].Enabled = true;
            }
            else
            {
                e.Row.Cells[15].Enabled = false;
            }
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {

    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        getMakerCheckerDetails(int.Parse(ViewState["chgID"].ToString()),true);
    }
}