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

    private void getGroups()
    {
        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_Sec_Group";

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


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

            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_Sec_Group","", "getGroups","");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private bool deleteGroup(string groupID, 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.Delete_Sec_Group";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_gid"].Value = groupID;
            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.Delete_Sec_Group", "286.2", "deleteGroup", "groupID : " + groupID + "p_lang: EN");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(1, "SEC_GROUP_MAINTAIN.Delete_Sec_Group", "286.2", "deleteGroup", "groupID : " + groupID + "p_lang: EN");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.Delete_Sec_Group","286.2", "deleteGroup", "groupID : " + groupID + "p_lang: EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool canDeleteGroup(string groupID, 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.Can_Delete";

            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_gid"].Value = groupID;
            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, "SEC_GROUP_MAINTAIN.Can_Delete", "286.1", "canDeleteGroup", "groupID : " + groupID + "p_lang:EN");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.Can_Delete", "286.1", "canDeleteGroup", "groupID : " + groupID + "p_lang:EN");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.Can_Delete","286.1", "canDeleteGroup", "groupID : " + groupID + "p_lang:EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool addGroup(string groupName, string groupDesc, 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.Create_Sec_Group";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_group_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_group_desc", 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_group_name"].Value = groupName;
            oracom.Parameters["p_group_desc"].Value = groupDesc;
            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, "", "431.1", "addGroup", "groupName : " + groupName + ", groupDesc : " + groupDesc);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "", "431.1", "addGroup", "groupName : " + groupName + ", groupDesc : " + groupDesc);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "","431.1" ,"addGroup", "groupName : " + groupName + ", groupDesc : " + groupDesc);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool updateGroup(string groupID, string groupName, string groupDesc, string status, 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.Update_Sec_Group";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gid", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_group_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_group_desc", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status", 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 = groupID;
            oracom.Parameters["p_group_name"].Value = groupName;
            oracom.Parameters["p_group_desc"].Value = groupDesc;
            oracom.Parameters["p_status"].Value = status;
            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.Update_Sec_Group", "431.2", "updateGroup", "groupID : " + groupID + ", groupName : " + groupName + ", groupDesc : " + groupDesc + ", status : " + status);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.Update_Sec_Group", "431.2", "updateGroup", "groupID : " + groupID + ", groupName : " + groupName + ", groupDesc : " + groupDesc + ", status : " + status);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility .LogException (ex,"SEC_GROUP_MAINTAIN.Update_Sec_Group","431.2","updateGroup","groupID : "+groupID +", groupName : "+groupName +", groupDesc : "+groupDesc +", status : " +status);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false ;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


        if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool temp = canDeleteGroup(Uid,ref error);
            if (temp)
            {
                ExceptionUtility.LogException(0, "", "55.286", "GridView1_RowCommand.DELETE", "");
                temp = deleteGroup(Uid, ref error);
                if (temp)
                {
                    
                    lblError.Text = "Group Delete request sent.";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "55.286", "GridView1_Rowcommand.delete", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            else
            {
                ExceptionUtility.LogException(9, "", "55.286", "GridView1_RowCommand.DELETE", "");
                lblError.Text = error;
            }
            getGroups();
            
        }
        else if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";

            this.lbl_addUpdateLegandText.Text = "Update Group \"" + GridView1.Rows[index].Cells[0].Text + "\"";  

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

            ViewState["uid"] = Uid;
            //put the data in the controls
            txtGroupName.Text = GridView1.Rows[index].Cells[0].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[1].Text;
            string status = GridView1.Rows[index].Cells[2].Text;
            if (status.Equals("1"))
                rdActive.Checked = true;
            else
                rdInActive.Checked = true;
            ExceptionUtility.LogException(0, "", "55.287", "GridView1_RowCommand.UPDATE", "");
        }
        else if (e.CommandName.Equals("vp"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ExceptionUtility.LogException(0, "", "55.288", "GridView1_RowCommand.VIEW PERMISSION", "");
            Response.Redirect("GroupPermissions.aspx?gid=" + Uid);
            
        }
        else if (e.CommandName.Equals("vr"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ExceptionUtility.LogException(0, "", "55.428", "GridView1_RowCommand.VIEW REPORTS", "");
            Response.Redirect("ReportsGroupPermissions.aspx?gid=" + Uid);
            
        }
        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, "", "55.429", "GridView1_RowCommand.APPROVE", "");
            bool temp = approveGroup(Uid, ref error);
            if (temp)
            {
                
                lblError.Text = "Group status updated";
            }
            else
            {
                ExceptionUtility.LogException(9, "", "55.429", "GridView1_RowCommand.APPROVE", "");
                lblError.Text = error;
            }
            getGroups();
            
        }
        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, "", "55.430", "GridView1_RowCommand.DECLINE", "");
            bool temp = declineGroup(Uid, ref error);
            if (temp)
            {
                
                lblError.Text = "Group status updated";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An Error Occured!";
                    ExceptionUtility.LogException(9, "", "55.430", "GridView1_Rowcommand.decline", error);
                }
                else
                    lblError.Text = error;
            }
            getGroups();
            
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ViewState["mode"] = "a";

                this.lbl_addUpdateLegandText.Text = "Add New Group";

                pnlControls.Visible = true;
                pnlGrid.Enabled = false;
                clearControls();
                ExceptionUtility.LogException(0, "", "55.283", "btnAdd_Click", "");
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "", "55.283", "btnAdd_Click", "");
            }
        }

    protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ViewState["mode"] = null;
                ViewState["uid"] = null;
                pnlControls.Visible = false;
                pnlGrid.Enabled = true;
                clearControls();
                ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "", "", "btnCancel_Click", "");
            }
        }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                ExceptionUtility.LogException(0, "", "55.431", "btnSave_Click", "");
                bool saved = addGroup(txtGroupName.Text, txtDesc.Text, ref error);
                if (saved)
                {
                    
                    lblError.Text = "Group Saved";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "55.431", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["uid"].ToString();
                string status = "";
                if (rdActive.Checked)
                    status = "1";
                else
                    status = "0";
                ExceptionUtility.LogException(0, "", "55.431", "btnSave_Click", "");
                bool saved = updateGroup(Uid, txtGroupName.Text, txtDesc.Text, status, ref error);
                if (saved)
                {
                    
                    lblError.Text = "Group Saved";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "55.432", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            getGroups();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
            
        }
        catch (Exception ex)
        {
            if (ex.Message.Equals("ORA-00001: unique constraint (NCB.SEC_USER_UK) violated\n"))
            {
                //this user id already exists...
                setMessage("This group name already exists");
            }
            else
            {
                ExceptionUtility.LogException(ex, "", "", "btnSave_Click", "");
            }
        }
    }

    #region Business Functions

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

    #endregion

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

    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }

    private bool approveGroup(string groupID, 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_group";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_gid"].Value = groupID;
            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_group", "429.1", "approveGroup", "groupID : " + groupID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.approve_sec_group", "429.1", "approveGroup", "groupID : " + groupID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.approve_sec_group","429.1", "approveGroup","groupID : "+groupID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool declineGroup(string groupID, 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_group";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_gid"].Value = groupID;
            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_group", "430.1", "declineGroup", "groupID : " + groupID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "SEC_GROUP_MAINTAIN.decline_sec_group", "430.1", "declineGroup", "groupID : " + groupID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "SEC_GROUP_MAINTAIN.decline_sec_group","430.1", "declineGroup", "groupID : " + groupID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
}
