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 GroupPermissions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
            if (!IsPostBack)
            {
                pnlControls.Visible = false;
                pnl_pageFunctions.Visible = false;
                getGroupPages();
                getPages();
                ExceptionUtility.LogException(0, "", "54", "Page_Load", "");
            }
            lblError.Text = "";
            
        
    }

    private void getPages()
    {
        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 = "sec_group_maintain.get_group_unassigned_pages";

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


            oracom.Parameters["p_gid"].Value = Request.QueryString["gid"];
            oracom.Parameters["p_lang"].Value = "EN";

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

            oracon.Close();

            ddlPages.DataSource = ds;
            ddlPages.DataBind();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "sec_group_maintain.get_group_unassigned_pages","54", "getPages", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void getGroupPages()
    {
        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 = "SEC_GROUP_MAINTAIN.get_group_pages";

            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_grp_pages", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_gid"].Value = Request.QueryString["gid"];

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

            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.get_group_pages","54", "getGroupPages", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private bool deletePage(string pageID, 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 = "SEC_GROUP_MAINTAIN.assign_group_pages";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_action", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_add_fn_list", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_del_fn_list", 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_gid"].Value = Request.QueryString["gid"];
            oracom.Parameters["p_pid"].Value = pageID;
            oracom.Parameters["p_action"].Value = "D";
            oracom.Parameters["p_add_fn_list"].Value = OracleString.Null;
            oracom.Parameters["p_del_fn_list"].Value = OracleString.Null;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.assign_group_pages", "281.1", "deletePage", "p_pid :" + pageID + "- p_gid: " + Request.QueryString["gid"] + "- p_action: D- p_lang: EN");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.assign_group_pages", "281.1", "deletePage", "p_pid :" + pageID + "- p_gid: " + Request.QueryString["gid"] + "- p_action: D- p_lang: EN");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.assign_group_pages", "281.1", "deletePage", "p_pid :" + pageID + "- p_gid: " + Request.QueryString["gid"] + "- p_action: D- p_lang: EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool addPage(string pageID, 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 = "SEC_GROUP_MAINTAIN.assign_group_pages";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_action", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_add_fn_list", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_del_fn_list", 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_gid"].Value = Request.QueryString["gid"];
            oracom.Parameters["p_pid"].Value = pageID;
            oracom.Parameters["p_action"].Value = "A";
            oracom.Parameters["p_add_fn_list"].Value = OracleString.Null;
            oracom.Parameters["p_del_fn_list"].Value = OracleString.Null;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.assign_group_pages", "277.1","addPage", "p_gid :" + Request.QueryString["gid"] + ", p_pid: " + pageID + ", p_action: A, p_lang: EN");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.assign_group_pages","277.1", "addPage", "p_gid :" + Request.QueryString["gid"] + ", p_pid: " + pageID + ", p_action: A, p_lang: EN");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.assign_group_pages", "277.1", "addPage", "p_gid :" + Request.QueryString["gid"] + ", p_pid: " + pageID + ", p_action: A, p_lang: EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {

            if (e.CommandName.Equals("d"))
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string Uid = GridView1.DataKeys[index].Value.ToString();
                string error = "";
                ExceptionUtility.LogException(0, "", "54.281", "GridView1_Rowcommand.delete", "");
                bool temp = deletePage(Uid, ref error);
                if (temp)
                {
                    
                    lblError.Text = "Page Permission Deleted";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "54.281", "GridView1_Rowcommand.delete", error);
                    }
                    else
                        lblError.Text = error;
                }
                getGroupPages();
                
            }
            else if (e.CommandName.Equals("v"))
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string Pid = GridView1.DataKeys[index].Value.ToString();
                string pageName = GridView1.Rows[index].Cells[0].Text;
                pnl_pageFunctions.Visible = true;
                getPageFunctions(Pid);
                getGroupPageFunctions(Pid);
                ViewState["pid"] = Pid;

                lblLegend.Text = pageName;
                ExceptionUtility.LogException(0, "", "54.282", "GridView1_Rowcommand.view", "");
            }
            else if (e.CommandName.Equals("approve"))
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string Uid = GridView1.DataKeys[index].Value.ToString();
                string error = "";
                ExceptionUtility.LogException(0, "", "54.432", "GridView1_Rowcommand.approve", "");
                bool temp = approvePage(Uid, ref error);
                if (temp)
                {
                    
                    lblError.Text = "Page status updated";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "54.432", "GridView1_Rowcommand.approve", error);
                    }
                    else
                        lblError.Text = error;
                }
                getGroupPages();
                
            }
            else if (e.CommandName.Equals("decline"))
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string Uid = GridView1.DataKeys[index].Value.ToString();
                string error = "";
                ExceptionUtility.LogException(0, "", "54.433", "GridView1_Rowcommand.decline", "");
                bool temp = declinePage(Uid, ref error);
                if (temp)
                {
                    
                    lblError.Text = "Page status updated";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "54.433", "GridView1_Rowcommand.decline", error);
                    }
                    else
                        lblError.Text = error;
                }
                getGroupPages();
                
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex,"","","GridView1_Rowcommand","");
            
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = true;
        pnlGrid.Enabled = false;
        ExceptionUtility.LogException(0, "", "54.276", "btnAdd_Click", "");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        ExceptionUtility.LogException(0, "", "54.278", "btnCancel_Click", "");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string error = "";
        ExceptionUtility.LogException(0, "", "54.277", "btnSave_Click", "");
        bool saved = addPage(ddlPages.SelectedValue, ref error);
        if (saved)
        {
            
            lblError.Text = "Page Added";
        }
        else
        {
            if (Helper.OracleError(error))
            {
                lblError.Text = "An Error Occured!";
                ExceptionUtility.LogException(9, "", "54.277", "btnSave_Click", error);
            }
            else
                lblError.Text = error;
        }
        getGroupPages();
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Cells[2].Text = "Approved";
        //}
    }
    
    private void getPageFunctions(string PID)
    {
        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 = "sec_module_core.get_page_functions";

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


            oracom.Parameters["p_pid"].Value = PID;

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

            oracon.Close();

            ddl_pageFunctions.DataSource = ds;
            ddl_pageFunctions.DataBind();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "sec_module_core.get_page_functions","", "getPageFunctions","PID :"+PID +"");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void getGroupPageFunctions(string PID)
    {
        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 = "sec_group_maintain.get_all_group_page_functions";

            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_functions", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_pid"].Value = PID;
            oracom.Parameters["p_gid"].Value = Request.QueryString["gid"];

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

            oracon.Close();

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

            //GridView2.Visible = true;

            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    string fid = ds.Tables[0].Rows[i][0].ToString();
            //    CheckBoxList1.Items.FindByValue(fid).Selected = true;
            //    CheckBoxList2.Items.FindByValue(fid).Selected = true;
            //}
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "sec_group_maintain.get_all_group_page_functions","", "getGroupPageFunctions","PID: "+PID +"");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
    
    //private bool addDeleteFunctions(string pageID, ref string error)
    //{
    //    //format the add and delete arrays
    //    string addList = "", delList = "";
    //    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    //    {
    //        if (CheckBoxList1.Items[i].Selected && !CheckBoxList2.Items[i].Selected)
    //            addList += CheckBoxList1.Items[i].Value + ";";
    //        if (!CheckBoxList1.Items[i].Selected && CheckBoxList2.Items[i].Selected)
    //            delList += CheckBoxList1.Items[i].Value + ";";
    //    }
    //    delList = delList.TrimEnd(';');
    //    addList = addList.TrimEnd(';');

    //    //call the sotred procedure
    //    string DBConnection = Helper.dbConnection();

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

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

    //    oracom.CommandType = CommandType.StoredProcedure;
    //    oracom.CommandText = "SEC_GROUP_MAINTAIN.assign_group_pages";

    //    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_pid", OracleType.Number).Direction = ParameterDirection.Input;
    //    oracom.Parameters.Add("p_action", OracleType.VarChar).Direction = ParameterDirection.Input;
    //    oracom.Parameters.Add("p_add_fn_list", OracleType.VarChar).Direction = ParameterDirection.Input;
    //    oracom.Parameters.Add("p_del_fn_list", 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_gid"].Value = Request.QueryString["gid"];
    //    oracom.Parameters["p_pid"].Value = pageID;
    //    oracom.Parameters["p_action"].Value = "U";
    //    oracom.Parameters["p_add_fn_list"].Value = addList;
    //    oracom.Parameters["p_del_fn_list"].Value = delList;
    //    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 BtnSaveFUn_Click(object sender, EventArgs e)
    //{
    //    string Pid = ViewState["pid"].ToString();
    //    string error = "";
    //    bool saved = addDeleteFunctions(Pid, ref error);
    //    if (saved)
    //    {
    //        lblError.Text = "Functions Updated";
    //    }
    //    else
    //    {
    //        lblError.Text = error;
    //    }
    //    getGroupPages();
    //    pnlFun.Visible = false;
    //    pnlGrid.Enabled = true;
        
    //}

    private bool approvePage(string pageID, 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 = "SEC_GROUP_MAINTAIN.approve_sec_page";

            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_pid", 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 = Request.QueryString["gid"];
            oracom.Parameters["p_pid"].Value = pageID;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.approve_sec_page", "432.1", "approvePage", "p_pid :" + pageID + "- p_gid : " + Request.QueryString["gid"]);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.approve_sec_page","432.1", "approvePage","p_pid :"+pageID+"- p_gid : "+Request.QueryString["gid"]);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.approve_sec_page","432.1", "approvePage","p_pid :"+pageID+"- p_gid : "+Request.QueryString["gid"]);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool declinePage(string pageID, 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 = "SEC_GROUP_MAINTAIN.decline_sec_page";

            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_pid", 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 = Request.QueryString["gid"];
            oracom.Parameters["p_pid"].Value = pageID;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.decline_sec_page", "433.1", "declinePage", "pageID :" + pageID + "- p_gid: "+Request.QueryString["gid"]+"- p_lang: EN");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.decline_sec_page", "433.1", "declinePage", "pageID :" + pageID + "- p_gid: " + Request.QueryString["gid"] + "- p_lang: EN");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.decline_sec_page", "433.1", "declinePage", "pageID :" + pageID + "- p_gid: " + Request.QueryString["gid"] + "- p_lang: EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    
    private int addFunction(string groupId, string pageId, string functionId, ref string erroMsg)
    {
        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 = "SEC_GROUP_MAINTAIN.add_group_page_function";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fid", 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 = groupId;
            oracom.Parameters["p_pid"].Value = pageId;
            oracom.Parameters["p_fid"].Value = functionId;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string errorCode = oracom.Parameters["p_res_code"].Value.ToString();
            erroMsg = oracom.Parameters["p_res_msg"].Value.ToString();
            ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.add_group_page_function", "437.1", "addFunction", "groupId :" + groupId + "-pageId :" + pageId + "-functionId :" + functionId + "");
            return int.Parse(errorCode);

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.add_group_page_function","437.1", "addFunction","groupId :"+groupId +"-pageId :" +pageId +"-functionId :"+functionId+"");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
   
    protected void btn_AddFnction_Click(object sender, EventArgs e)
    {
        try
        {
            string groupId = this.Request.QueryString["gid"];
            string pageId = this.ViewState["pid"].ToString();
            string functionId = this.ddl_pageFunctions.SelectedValue.ToString();
            string erroMsg = "";

            int resCode = this.addFunction(groupId, pageId, functionId, ref erroMsg);

            if (resCode == 0)
            {
                ExceptionUtility.LogException(0, "", "54.437", "btn_AddFnction_Click", "");
                this.lblError.Text = "The Function added successfully";

                this.getGroupPageFunctions(this.ViewState["pid"].ToString());
            }
            else
            {
                ExceptionUtility.LogException(9, "", "54.437", "btn_AddFnction_Click", "");
                this.lblError.Text = erroMsg;
            }
            
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "", "54.437", "btn_AddFnction_Click", "");
        }
    }

    protected void grd_pageFunctions_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string commandName = e.CommandName;

        int selectedFunctionIndex = int.Parse(e.CommandArgument.ToString());
        string selectedFunctionId = grd_pageFunctions.DataKeys[selectedFunctionIndex].Value.ToString();
        string groupId = this.Request.QueryString["gid"];
        string pageId = this.ViewState["pid"].ToString();

        string errorMsg = "";
        int resCode;

        if (commandName.Equals("del"))
        {
            ExceptionUtility.LogException(0, "", "54.434", "grd_pageFunctions_RowCommand.delete", "");
            resCode = this.deleteFunction(groupId, pageId, selectedFunctionId, ref errorMsg);

            if (resCode != 0)
            {
                ExceptionUtility.LogException(9, "", "54.434", "grd_pageFunctions_RowCommand.delete", "");
                this.lblError.Text = errorMsg;
            }
            
        }
        else if (commandName.Equals("approve"))
        {
            ExceptionUtility.LogException(0, "", "54.435", "grd_pageFunctions_RowCommand.approve", "");
            resCode = this.approveFunction(groupId, pageId, selectedFunctionId, ref errorMsg);

            if (resCode != 0)
            {
                this.lblError.Text = errorMsg;
                ExceptionUtility.LogException(9, "", "54.434", "grd_pageFunctions_RowCommand.delete", "");
            }
            
        }
        else if (commandName.Equals("decline"))
        {
            ExceptionUtility.LogException(0, "", "54.436", "grd_pageFunctions_RowCommand.decline", "");
            resCode = this.declineFunction(groupId, pageId, selectedFunctionId, ref errorMsg);

            if (resCode != 0)
            {
                this.lblError.Text = errorMsg;
                ExceptionUtility.LogException(9, "", "54.434", "grd_pageFunctions_RowCommand.delete", "");
            }
           
        }

        this.getGroupPageFunctions(pageId);

    }

    private int deleteFunction(string groupId, string pageId, string functionId, ref string errorMsg)
    {
        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 = "SEC_GROUP_MAINTAIN.delete_function";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fid", 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 = groupId;
            oracom.Parameters["p_pid"].Value = pageId;
            oracom.Parameters["p_fid"].Value = functionId;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string errorCode = oracom.Parameters["p_res_code"].Value.ToString();
            errorMsg = oracom.Parameters["p_res_msg"].Value.ToString();
            ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.delete_function", "434.1", "deleteFunction", "groupId :" + groupId + ", pageId :" + pageId + ", p_fid: " + functionId);
            return int.Parse(errorCode);
            
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.delete_function","434.1", "deleteFunction","groupId :"+groupId +", pageId :"+pageId +", p_fid: "+functionId );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    private int declineFunction(string groupId, string pageId, string functionId, ref string errorMsg)
    {
        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 = "SEC_GROUP_MAINTAIN.decline_sec_function";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fid", 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 = groupId;
            oracom.Parameters["p_pid"].Value = pageId;
            oracom.Parameters["p_fid"].Value = functionId;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string errorCode = oracom.Parameters["p_res_code"].Value.ToString();
            errorMsg = oracom.Parameters["p_res_msg"].Value.ToString();
            ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.decline_sec_function", "436.1", "declineFunction", "groupId :" + groupId + ", pageId :" + pageId + ", functionId :" + functionId + "");
            return int.Parse(errorCode);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.decline_sec_function","436.1", "declineFunction", "groupId :" + groupId + ", pageId :" + pageId + ", functionId :" + functionId + "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    private int approveFunction (string groupId, string pageId, string functionId, ref string errorMsg)
    {
        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 = "SEC_GROUP_MAINTAIN.approve_sec_function";

            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_pid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fid", 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 = groupId;
            oracom.Parameters["p_pid"].Value = pageId;
            oracom.Parameters["p_fid"].Value = functionId;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string errorCode = oracom.Parameters["p_res_code"].Value.ToString();
            errorMsg = oracom.Parameters["p_res_msg"].Value.ToString();
            ExceptionUtility.LogException(0, "SEC_GROUP_MAINTAIN.approve_sec_function", "435.1", "approveFunction", "groupId :" + groupId + ", pageId : " + pageId + ", functionId : " + functionId + "");
            return int.Parse(errorCode);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.approve_sec_function","435.1", "approveFunction","groupId :"+groupId +", pageId : "+pageId +", functionId : "+functionId +"");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
}
