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;
using System.Data.OracleClient;

public partial class Security_SecurityReports : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] == null)
            Response.Redirect("../Security/LoginPage.aspx");


        int userGroupID = Helper.getCurrentUserGroup(int.Parse(Session["uid"].ToString()));      

        if (!IsPostBack)
        {
            this.btn_execute.Enabled = false;
            
            this.tbl_rpt_userProfile.Visible = false;
            this.tbl_rpt_userActivities.Visible = false;
            this.tbl_rpt_userViolations.Visible = false;

           // this.ddl_userProfile_users.Enabled = false;

            this.loadPremittedReports(1, userGroupID);
            this.loadGroups();
        }

        this.lbl_error.Text = "";
    }

    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_ACTIVE_PERMITTED_REPORTS";

        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 == 9)
        {
            Response.Redirect("../Business/default.aspx?err=1");
        }
        else
        {
            this.ddl_reports.DataSource = ds;
            this.ddl_reports.DataBind();

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

    private void loadGroups()
    {
        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();

        this.ddl_userProfile_userGroup.DataSource = ds;
        this.ddl_userProfile_userGroup.DataBind();

        this.ddl_userProfile_userGroup.Items.Insert(0, "------ Groups ------");
    }
/*
    private void loadUsers (int groupId)
    {
        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_user_maintain.GET_USERS_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_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_group_id"].Value = groupId;

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

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

        oracon.Close();

        this.ddl_userProfile_users.DataSource = ds;
        this.ddl_userProfile_users.DataBind();

        this.ddl_userProfile_users.Items.Insert(0, "------ Users ------");
    }*/
    
    private void hide ()
    {
        this.tbl_rpt_userProfile.Visible = false;
        this.tbl_rpt_userActivities.Visible = false;
        this.tbl_rpt_userViolations.Visible = false;

        switch (this.ddl_reports.SelectedValue)
        {
            case "49":
                this.tbl_rpt_userProfile.Visible = true;
                break;

            case "50":
                this.tbl_rpt_userActivities.Visible = true;
                break;

            case "51":
                this.tbl_rpt_userViolations.Visible = true;
                break;

            case "52":
                this.tbl_rpt_userProfile.Visible = true;
                break;
        }
    }

    protected void ddl_reports_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.hide();

        if (this.ddl_reports.SelectedIndex != 0)
        {
            this.btn_execute.Enabled = true;
        }
        else
        {
            this.btn_execute.Enabled = false;
        }


    }
    
    protected void ddl_userProfile_userGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedGroupId;

        selectedGroupId = int.Parse(this.ddl_userProfile_userGroup.SelectedValue);

        //if (this.ddl_userProfile_userGroup.SelectedIndex != 0)
        //{
        //    selectedGroupId = int.Parse(this.ddl_userProfile_userGroup.SelectedValue);

        //   // this.ddl_userProfile_users.Enabled = true;

        //   // this.loadUsers(selectedGroupId);
        //}
        //else
        //{
        //    this.ddl_userProfile_users.Enabled = false;
        //    this.ddl_userProfile_users.Items.Clear();
        //}
    }
    
    protected void btn_execute_Click(object sender, EventArgs e)
    {
        string selectedReportId = this.ddl_reports.SelectedValue;

        if (selectedReportId.Equals("49"))
        {
            if (this.ddl_userProfile_userGroup.SelectedIndex == 0 )//|| this.ddl_userProfile_users.SelectedIndex == 0)
            {
                this.lbl_error.Text = "Please select a specific Group and User.";
            }
            else
            {
                string p1 = Session["uid"].ToString();
                //string p2 = this.ddl_userProfile_users.SelectedValue;
                string p2 = this.ddl_userProfile_userGroup.SelectedValue;

                //ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                //            "window.open('../Reports/Default.aspx?rpt=1049&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "','Report')", true);


                ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                            "window.open('../Reports/Default.aspx?rpt=1049&p1=" + p1 + "&p2=" + p2 +  "','Report')", true);

            }
        }

        if (selectedReportId.Equals("50"))
        {
            string userID = this.txt_userActivities_userId.Text;
            string fromDate = this.dtp_userActivities_fromDate.date;
            string toDate = this.dtp_userActivities_toDate.date;

            int temp;
            //!int.TryParse(userID, out temp)
            if (userID.Length == 0 )
            {
                this.lbl_error.Text = "Please enter a valid User ID Value.";
            }
            else if (fromDate.Length == 0 || toDate.Length == 0)
            {
                this.lbl_error.Text = "Please Select ALL Dates.";
            }
            else
            {
                string p1 = userID;
                string p2 = fromDate;
                string p3 = toDate;

                ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                            "window.open('../Reports/Default.aspx?rpt=1050&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "','Report')", true);
            }
        }

        if (selectedReportId.Equals("51"))
        {
            string userID = this.txt_userViolations_userId.Text;
            string fromDate = this.dtp_userViolations_fromDate.date;
            string toDate = this.dtp_userActivities_toDate.date;

            string p1 = Session["uid"].ToString();
            string p2;
            string p3;
            string p4;

            if (userID.Length == 0)
            {
                p2 = "null";
            }
            else
            {
                p2 = userID;
            }

            if (fromDate.Length == 0)
            {
                p3 = "null";
            }
            else
            {
                p3 = fromDate;
            }

            if (toDate.Length == 0)
            {
                p4 = "null";
            }
            else
            {
                p4 = toDate;
            }

            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1051&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "','Report')", true);
        }



        if (selectedReportId.Equals("52"))
        {
            if (this.ddl_userProfile_userGroup.SelectedIndex == 0)//|| this.ddl_userProfile_users.SelectedIndex == 0)
            {
                this.lbl_error.Text = "Please select a specific Group and User.";
            }
            else
            {
                string p1 = Session["uid"].ToString();
                string p2 = this.ddl_userProfile_userGroup.SelectedValue;
                string p3 = this.ddl_userProfile_userGroup.SelectedItem.Text;
                ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                    "window.open('../Reports/Default.aspx?rpt=1052&p1=" + p1 + "&p2=" + p2 +  "&p3=" + p3 + "','Report')", true);

            }
        }
    }
}