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)
        {
            this.loadReportsGroups();
            this.pnlGrid.Visible = false;
            this.pnlControls.Visible = false;
            LiteralGroupName.Text = Request.QueryString["GName"];
        }
        lbl_error.Text = "";
    }

    private void loadReportsGroups()
    {
        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...";
        }
        else
        {
            this.ddl_reportsGroups.DataSource = ds;
            this.ddl_reportsGroups.DataBind();

            this.ddl_reportsGroups.Items.Insert(0, "------ Reports Groups ------");
        }
    }

    private void loadPremittedReports(int reportGroupId, int groupId)
    {
        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...";
        }
        else
        {
            this.grd_reports.DataSource = ds;
            this.grd_reports.DataBind();
        }
    }

    private void loadReports(int reportGroupId)
    {
        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...";
        }
        else
        {
            this.ddl_reports.DataSource = ds;
            this.ddl_reports.DataBind();

            this.ddl_reports.Items.Insert(0, "------ Reports ------");
        }
    }

    private bool assignReportToGroup(int reportId, int reportGroupId, int groupId, int status, 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 = "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")
            return true;
        else
            return false;
    }

    private bool unassignReportFromGroup(int reportId, int groupId, 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 = "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")
            return true;
        else
            return false;
    }

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

    }
    
    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);
        }
        else
        {
            this.pnlGrid.Visible = false;
        }
    }

    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);
    }
      
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        this.pnlControls.Visible = false;
    }

    protected void btn_add_Click(object sender, EventArgs e)
    {
        this.pnlControls.Visible = true;
        
        int selectedReportGroupId = int.Parse(this.ddl_reportsGroups.SelectedValue.ToString());
        this.loadReports(selectedReportGroupId);
    }
    
    protected void btn_save_Click(object sender, EventArgs e)
    {
        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;

        bool isAssigned = this.assignReportToGroup(reportId, reportGroupId, groupId, status, ref error);

        if (!isAssigned)
        {
            this.lbl_error.Text = error;
        }
        else
        {
            this.pnlControls.Visible = false;
            this.loadPremittedReports(reportGroupId, groupId);
            this.lbl_error.Text = "The Report Added Successfully";
        }

    }
    
    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"))
        {
            bool isUnassigned = this.unassignReportFromGroup(reportId, groupId, ref error);

            if (isUnassigned)
            {
                this.lbl_error.Text = "The request for deletion is sent Succeffully";

                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                this.lbl_error.Text = error;
            }
        }
        else if (e.CommandName.Equals("approve"))
        {
            //bool isUpdated = this.updatePermittedReportStatus(reportId, groupId, 1, ref error);

            bool isUpdated = approveRejectReport(reportId, groupId, 1, ref error);

            if (isUpdated)
            {
                this.lbl_error.Text = "The report's status is updated Successfully...";

                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                this.lbl_error.Text = error;
            }
        }
        else if (e.CommandName.Equals("decline"))
        {
            //bool isUpdated = this.updatePermittedReportStatus(reportId, groupId, 0, ref error);

            bool isUpdated = approveRejectReport(reportId, groupId, 0, ref error);

            if (isUpdated)
            {
                this.lbl_error.Text = "The report's status is updated Successfully...";

                this.loadPremittedReports(reportGroupId, groupId);
            }
            else
            {
                this.lbl_error.Text = error;
            }
        }

    }

    private bool approveRejectReport(int reportId, int groupId, int action, 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 = "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")
            return true;
        else
            return false;

    }
}
