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 Secyrity_GroupReports : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.loadReportsGroups();
            ExceptionUtility.LogException(0, "", "74", "Page_Load", "");
        }
        //lbl_error.Text = "";

    }

    private void loadReportsGroups()
    {
        try
        {
            int resCode;

            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "REPORTS_PERMISSIONS_OPS.GET_REPORTS_GROUPS_LIST";

            oracom.Parameters.Add("p_current_uid", 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_current_uid"].Value = Session["uid"].ToString(); ;
            oracom.Parameters["p_lang"].Value = "EN";

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

            oracon.Close();

            resCode = int.Parse(oracom.Parameters["p_res_code"].Value.ToString());

            if (resCode == 10)
            {
                //this.lbl_error.Text = "Sorry, No Reports Groups Found...";
                Notifications.showMessage("Sorry, No Reports Groups Found!", this, Notifications.errorMessage, false);
            }
            else
            {
                this.ddl_reportsGroups.DataSource = ds;
                this.ddl_reportsGroups.DataBind();

                this.ddl_reportsGroups.Items.Insert(0, "---------------------------------------");
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.GET_REPORTS_GROUPS_LIST", "", "loadReportsGroups", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadPremittedReports(int reportGroupId, int groupId)
    {
        try
        {
            int resCode;

            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "REPORTS_PERMISSIONS_OPS.GET_PERMITTED_REPORTS_LIST";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_reports_group_id", OracleType.Number).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_current_uid"].Value = Session["uid"].ToString(); ;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_group_id"].Value = groupId;
            oracom.Parameters["p_reports_group_id"].Value = reportGroupId;

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

            oracon.Close();

            resCode = int.Parse(oracom.Parameters["p_res_code"].Value.ToString());

            if (resCode == 10)
            {
                //this.lbl_error.Text = "Sorry, No Reports Found...";
                Notifications.showMessage("Sorry, No Reports Found!", this, Notifications.errorMessage, false);
            }
            else
            {
                this.grd_reports.DataSource = ds;
                this.grd_reports.DataBind();
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.GET_PERMITTED_REPORTS_LIST", "", "loadPremittedReports", "groupId: " + groupId + ", reportGroupId: " + reportGroupId);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadReports(int reportGroupId)
    {
        try
        {
            int resCode;

            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "REPORTS_PERMISSIONS_OPS.GET_REPORTS_LIST";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_reports_group_id", OracleType.Number).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_current_uid"].Value = Session["uid"].ToString(); ;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_reports_group_id"].Value = reportGroupId;

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

            oracon.Close();

            resCode = int.Parse(oracom.Parameters["p_res_code"].Value.ToString());

            if (resCode == 10)
            {
                //this.lbl_error.Text = "Sorry, No Reports Found...";
                Notifications.showMessage("Sorry, No Reports Found!", this, Notifications.errorMessage, false);
            }
            else
            {
                this.ddl_reports.DataSource = ds;
                this.ddl_reports.DataBind();

                this.ddl_reports.Items.Insert(0, "------ Reports ------");
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.GET_REPORTS_LIST", "", "loadReports", "reportGroupId: " + reportGroupId);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private bool assignReportToGroup(int reportId, int reportGroupId, int groupId, int 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 = "REPORTS_PERMISSIONS_OPS.ASSIGN_REPORT_TO_GROUP";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_report_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_reports_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status", OracleType.Number).Direction = ParameterDirection.Input;

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

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

            oracom.Parameters["p_group_id"].Value = groupId;
            oracom.Parameters["p_reports_group_id"].Value = reportGroupId;
            oracom.Parameters["p_report_id"].Value = reportId;
            oracom.Parameters["p_status"].Value = status;

            oracom.ExecuteNonQuery();
            oracon.Close();

            string resCode = oracom.Parameters["p_res_code"].Value.ToString();

            error = oracom.Parameters["p_res_msg"].Value.ToString();

            if (resCode == "0")
            {
                ExceptionUtility.LogException(0, "REPORTS_PERMISSIONS_OPS.ASSIGN_REPORT_TO_GROUP", "405.1", "assignReportToGroup", "groupId: " + groupId + ", reportGroupId: " + reportGroupId + ", reportId: " + reportId + ", status: " + status);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "REPORTS_PERMISSIONS_OPS.ASSIGN_REPORT_TO_GROUP", "405.1", "assignReportToGroup", "groupId: " + groupId + ", reportGroupId: " + reportGroupId + ", reportId: " + reportId + ", status: " + status);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.ASSIGN_REPORT_TO_GROUP", "405.1", "assignReportToGroup", "groupId: " + groupId + ", reportGroupId: " + reportGroupId + ", reportId: " + reportId + ", status: " + status);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool unassignReportFromGroup(int reportId, int 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 = "REPORTS_PERMISSIONS_OPS.UNASSIGN_REPORT_FROM_GROUP";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_report_id", OracleType.Number).Direction = ParameterDirection.Input;

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

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

            oracom.Parameters["p_group_id"].Value = groupId;
            oracom.Parameters["p_report_id"].Value = reportId;

            oracom.ExecuteNonQuery();
            oracon.Close();

            string resCode = oracom.Parameters["p_res_code"].Value.ToString();

            error = oracom.Parameters["p_res_msg"].Value.ToString();

            if (resCode == "0")
            {
                ExceptionUtility.LogException(0, "REPORTS_PERMISSIONS_OPS.UNASSIGN_REPORT_FROM_GROUP", "406.1", "unassignReportFromGroup", "groupId: " + groupId + ", reportId: " + reportId);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "REPORTS_PERMISSIONS_OPS.UNASSIGN_REPORT_FROM_GROUP", "406.1", "unassignReportFromGroup", "groupId: " + groupId + ", reportId: " + reportId);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.UNASSIGN_REPORT_FROM_GROUP", "406.1", "unassignReportFromGroup", "groupId: " + groupId + ", reportId: " + reportId);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool updatePermittedReportStatus(int reportId, int groupId, int 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 = "REPORTS_PERMISSIONS_OPS.UPDATE_PERMITTED_REPORT_STATUS";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_report_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status", OracleType.Number).Direction = ParameterDirection.Input;

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

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

            oracom.Parameters["p_group_id"].Value = groupId;
            oracom.Parameters["p_report_id"].Value = reportId;
            oracom.Parameters["p_status"].Value = status;

            oracom.ExecuteNonQuery();
            oracon.Close();

            string resCode = oracom.Parameters["p_res_code"].Value.ToString();

            error = oracom.Parameters["p_res_msg"].Value.ToString();

            if (resCode == "0")
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.UPDATE_PERMITTED_REPORT_STATUS", "", "updatePermittedReportStatus", "groupId: " + groupId + ", reportId: " + reportId + ", status: " + status);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void ddl_reportsGroups_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (this.ddl_reportsGroups.SelectedIndex != 0)
        {
            //this.pnlGrid.Visible = true;

            int selectedReportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue);
            int groupId = int.Parse(Request.QueryString["gid"].ToString());

            this.loadPremittedReports(selectedReportGroupId, groupId);
            ExceptionUtility.LogException(0, "", "", "ddl_reportsGroups_SelectedIndexChanged", "");
        }
        else
        {
            //this.pnlGrid.Visible = false;
            ExceptionUtility.LogException(9, "", "", "ddl_reportsGroups_SelectedIndexChanged", "");
        }


    }

    protected void grd_reports_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grd_reports.PageIndex = e.NewPageIndex;

        int selectedReportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue);
        int groupId = int.Parse(Request.Params["gid"].ToString());

        this.loadPremittedReports(selectedReportGroupId, groupId);
        ExceptionUtility.LogException(0, "", "", "grd_reports_PageIndexChanging", "");
    }

    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        //this.pnlControls.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btn_Cancel_Click", "");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //this.pnlControls.Visible = true;

        int selectedReportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue.ToString());
        this.loadReports(selectedReportGroupId);
        ExceptionUtility.LogException(0, "", "", "btn_add_Click", "");
        Notifications.showModal("modal-xl", this);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.ddl_reports.SelectedIndex > 0)
        {
            int reportId = int.Parse(this.ddl_reports.SelectedValue.ToString());
            int groupId = int.Parse(Request.QueryString["gid"].ToString());
            int reportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue.ToString());
            int status = 0;
            string error = null;
            ExceptionUtility.LogException(0, "", "74.405", "btn_save_Click", "");
            bool isAssigned = this.assignReportToGroup(reportId, reportGroupId, groupId, status, ref error);

            if (!isAssigned)
            {

                Notifications.showMessage(error, this, Notifications.errorMessage, false);

            }
            else
            {
                //this.pnlControls.Visible = false;
                this.loadPremittedReports(reportGroupId, groupId);
                //this.lbl_error.Text = "The Report Added Successfully";
                Notifications.showMessage("The Report Added Successfully", this, Notifications.successMessage, true);

            }
        }
        else
        {
            Notifications.showMessage("Please select a report", this, Notifications.errorMessage, false);
        }

    }

    protected void grd_reports_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = int.Parse(e.CommandArgument.ToString());
        int reportId = int.Parse(this.grd_reports.DataKeys[index].Value.ToString());
        int groupId = int.Parse(Request.QueryString["gid"].ToString());
        int reportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue);

        string error = null;

        if (e.CommandName.Equals("d"))
        {
            ExceptionUtility.LogException(0, "", "74.406", "grd_reports_RowCommand.delete", "");
            bool isUnassigned = this.unassignReportFromGroup(reportId, groupId, ref error);

            if (isUnassigned)
            {
                //this.lbl_error.Text = "The request for deletion is sent Succeffully";
                Notifications.showMessage("The request for deletion is sent Succeffully", this, Notifications.successMessage, false);
                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                ExceptionUtility.LogException(9, "", "74.406", "grd_reports_RowCommand.delete", "");
                //this.lbl_error.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, false);
            }

        }
        else if (e.CommandName.Equals("approve"))
        {
            //bool isUpdated = this.updatePermittedReportStatus(reportId, groupId, 1, ref error);
            ExceptionUtility.LogException(0, "", "74.407", "grd_reports_RowCommand.approve", "");
            bool isUpdated = approveRejectReport(reportId, groupId, 1, ref error);

            if (isUpdated)
            {
                //this.lbl_error.Text = "The report's status is updated Successfully...";
                Notifications.showMessage("The report's status is updated Successfully", this, Notifications.successMessage, false);
                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                ExceptionUtility.LogException(9, "", "74.407", "grd_reports_RowCommand.approve", "");
                //this.lbl_error.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, false);
            }

        }
        else if (e.CommandName.Equals("decline"))
        {
            //bool isUpdated = this.updatePermittedReportStatus(reportId, groupId, 0, ref error);
            ExceptionUtility.LogException(0, "", "74.407", "grd_reports_RowCommand.decline", "");
            bool isUpdated = approveRejectReport(reportId, groupId, 0, ref error);

            if (isUpdated)
            {
                //this.lbl_error.Text = "The report's status is updated Successfully...";
                Notifications.showMessage("The report's status is updated Successfully", this, Notifications.successMessage, false);
                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                ExceptionUtility.LogException(9, "", "74.407", "grd_reports_RowCommand.decline", "");
                //this.lbl_error.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, false);
            }

        }

    }

    private bool approveRejectReport(int reportId, int groupId, int action, 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 = "REPORTS_PERMISSIONS_OPS.APPROVE_DECLINE_REPORT";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_group_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_report_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_action", OracleType.Number).Direction = ParameterDirection.Input;

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

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

            oracom.Parameters["p_group_id"].Value = groupId;
            oracom.Parameters["p_report_id"].Value = reportId;
            oracom.Parameters["p_action"].Value = action;

            oracom.ExecuteNonQuery();
            oracon.Close();

            string resCode = oracom.Parameters["p_res_code"].Value.ToString();

            error = oracom.Parameters["p_res_msg"].Value.ToString();

            if (resCode == "0")
            {
                ExceptionUtility.LogException(0, "REPORTS_PERMISSIONS_OPS.APPROVE_DECLINE_REPORT", "407.1", "approveRejectReport", "groupId: " + groupId + ", reportId: " + reportId + ", action: " + action);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "REPORTS_PERMISSIONS_OPS.APPROVE_DECLINE_REPORT", "407.1", "approveRejectReport", "groupId: " + groupId + ", reportId: " + reportId + ", action: " + action);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "REPORTS_PERMISSIONS_OPS.APPROVE_DECLINE_REPORT", "407.1", "approveRejectReport", "groupId: " + groupId + ", reportId: " + reportId + ", action: " + action);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
}