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

public partial class Security_Users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
            pnlPassword.Visible = false;
            getGroups();
            getUsers();
        }
        lblError.Text = "";
    }

    private void getUsers()
    {
        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_sec_user";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_name", 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.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;


        oracom.Parameters["p_user_id"].Value = OracleNumber.Null;
        oracom.Parameters["p_user_name"].Value = OracleString.Null;
        
        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();

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

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

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

        RadioButtonList1.SelectedIndex = 0;
    }

    private bool deleteUser(string UserID, ref string error)
    {
        //if (Request["x"] == null || Request["y"] == null)
       // {
       //     Response.End();
       // }
        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.delete_sec_user";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_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["p_uid"].Value = UserID;
        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 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 = deleteUser(Uid, ref error);
            if (temp)
                lblError.Text = "User Delation Request Sent";
            else
                lblError.Text = error;
            getUsers();
        }
        else if (e.CommandName.Equals("u"))
        {
            

            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            pnlGrid.Enabled = false;


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

            ViewState["uid"] = Uid;
            //put the data in the controls
            txtUserID.Text = GridView1.Rows[index].Cells[1].Text;
            txtUserName.Text = GridView1.Rows[index].Cells[2].Text;

            txtPass1.Attributes.Add("value", GridView1.Rows[index].Cells[3].Text);
            txtPass2.Attributes.Add("value", GridView1.Rows[index].Cells[3].Text);

            RadioButtonList1.SelectedValue = GridView1.Rows[index].Cells[6].Text;

            int status = int.Parse(GridView1.Rows[index].Cells[15].Text);
            if (status == 1)
                rdActive.Checked = true;
            else
                rdInActive.Checked = true;

            txtPass1.Enabled = false;
            txtPass2.Enabled = false;
            txtUserID.Enabled = false;

            string super = GridView1.Rows[index].Cells[12].Text;

            if (super == "1")
                chkSuper.Checked = true;
            else
                chkSuper.Checked = false;

        }
        else if (e.CommandName.Equals("r"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlPassword.Visible = true;
            pnlGrid.Enabled = false;

            ViewState["uid"] = Uid;
            //put the data in the controls
        }
        else if (e.CommandName.Equals("approve"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool temp = approveUser(Uid, ref error);
            if (temp)
                lblError.Text = "User status updated";
            else
                lblError.Text = error;
            getUsers();
        }
        else if (e.CommandName.Equals("decline"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool temp = declineUser(Uid, ref error);
            if (temp)
                lblError.Text = "User status updated";
            else
                lblError.Text = error;
            getUsers();
        }
    }

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

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

        pnlControls.Visible = true;
        pnlGrid.Enabled = false;
        clearControls();

        txtPass1.Enabled = true;
        txtPass2.Enabled = true;
        txtUserID.Enabled = true;
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = null;
        ViewState["uid"] = null;
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        clearControls();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                bool saved = addUser(ref error);
                if (saved)
                {
                    lblError.Text = "User Data Saved";
                }
                else
                {
                    lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["uid"].ToString();
                bool saved = updateUser(ref error);
                if (saved)
                {
                    lblError.Text = "User Data Saved";
                }
                else
                {
                    lblError.Text = error;
                }
            }
            getUsers();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[13].Visible = false;
            e.Row.Cells[14].Visible = false;
        }
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Cells[9].Text= "Approved";
        //}
    }

    private bool addUser(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 = "sec_user_maintain.create_sec_user";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_password", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_super_user", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_USER_TEL_NO", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_TEL_EXTEN", OracleType.VarChar).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_user_id"].Value = txtUserID.Text;
        oracom.Parameters["p_user_name"].Value = txtUserName.Text;
        oracom.Parameters["p_USER_TEL_NO"].Value = USER_TEL_NO.Text;
        oracom.Parameters["p_TEL_EXTEN"].Value = TEL_EXTEN.Text;
        oracom.Parameters["p_password"].Value = Helper.DecPassword( txtPass1.Text );
        oracom.Parameters["p_gid"].Value = RadioButtonList1.SelectedValue;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        if (chkSuper.Checked)
            oracom.Parameters["p_super_user"].Value = "1";
        else
            oracom.Parameters["p_super_user"].Value = "0";

        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;
    }

    private bool updateUser(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 = "SEC_USER_MAINTAIN.Update_Sec_User";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_super_user", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_USER_TEL_NO", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_TEL_EXTEN", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_status", 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;

        string Uid = ViewState["uid"].ToString();
        oracom.Parameters["p_uid"].Value = Uid;
        oracom.Parameters["p_user_name"].Value = txtUserName.Text;
        oracom.Parameters["p_USER_TEL_NO"].Value = USER_TEL_NO.Text;
        oracom.Parameters["p_TEL_EXTEN"].Value = TEL_EXTEN.Text;
        string status = "";
        if (rdActive.Checked)
            status = "1";
        else
            status = "0";

        if(chkSuper.Checked)
            oracom.Parameters["p_super_user"].Value = "1";
        else
            oracom.Parameters["p_super_user"].Value = "0";

        oracom.Parameters["p_status"].Value = status;
        oracom.Parameters["p_gid"].Value = RadioButtonList1.SelectedValue;
        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;
    }

    private void clearControls()
    {
        txtPass1.Text = "";
        txtPass2.Text = "";
        txtPass1.Attributes["value"] = "";
        txtPass2.Attributes["value"] = "";
        txtUserID.Text = "";
        txtUserName.Text = "";
        RadioButtonList1.SelectedIndex = 0;
        chkSuper.Checked = false;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getUsers();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string error = "";
        bool saved = ChangePassword(ref error);
        if (saved)
        {
            pnlPassword.Visible = false;
            pnlGrid.Enabled = true;
            lblError.Text = "Password changed Succesfully. ";
        }
        else
        {
            lblError.Text = error;
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        pnlPassword.Visible = false;
        pnlGrid.Enabled = true;
    }

    private bool ChangePassword(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 = "SEC_USER_MAINTAIN.Reset_Password";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_user_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_new_pass", 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_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
        oracom.Parameters["p_user_id"].Value = ViewState["uid"].ToString();
        oracom.Parameters["p_new_pass"].Value = Helper.DecPassword(TextBox1.Text);

        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;
    }

    private bool approveUser(string UserID, ref string error)
    {
        //if (Request["x"] == null || Request["y"] == null)
        // {
        //     Response.End();
        // }
        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.approve_sec_user";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_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["p_uid"].Value = UserID;
        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;
    }
    private bool declineUser(string UserID, ref string error)
    {
        //if (Request["x"] == null || Request["y"] == null)
        // {
        //     Response.End();
        // }
        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.decline_sec_user";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_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["p_uid"].Value = UserID;
        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;
    }
}
