using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ASPNET.StarterKit.Portal {
            
    public abstract class Users : ASPNET.StarterKit.Portal.PortalModuleControl {
        protected System.Web.UI.WebControls.Literal Message;
        protected System.Web.UI.WebControls.DropDownList allUsers;
        protected System.Web.UI.WebControls.LinkButton addNew;
        protected System.Web.UI.WebControls.ImageButton EditBtn;
        protected System.Web.UI.WebControls.ImageButton DeleteBtn;

        int tabIndex = 0;
        int tabId = 0;

        //*******************************************************
        //
        // The Page_Load server event handler on this user control is used
        // to populate the current roles settings from the configuration system
        //
        //*******************************************************

        private void Page_Load(object sender, System.EventArgs e) {

            // Verify that the current user has access to access this page
            if (PortalSecurity.IsInRoles("Admins") == false) {
                Response.Redirect("~/Admin/EditAccessDenied.aspx");
            }

            if (Request.Params["tabid"] != null) {
                tabId = Int32.Parse(Request.Params["tabid"]);
            }
            if (Request.Params["tabindex"] != null) {
                tabIndex = Int32.Parse(Request.Params["tabindex"]);
            }

            // If this is the first visit to the page, bind the role data to the datalist
            if (Page.IsPostBack == false) {

                BindData();
            }
        }

        //*******************************************************
        //
        // The DeleteUser_Click server event handler is used to add
        // a new security role for this portal
        //
        //*******************************************************

        private void DeleteUser_Click(Object Sender, ImageClickEventArgs e) {

            // get user id from dropdownlist of users
            UsersDB users = new UsersDB();
            users.DeleteUser(Int32.Parse(allUsers.SelectedItem.Value));
        
            // Rebind list
            BindData();
        }
    
        //*******************************************************
        //
        // The EditUser_Click server event handler is used to add
        // a new security role for this portal
        //
        //*******************************************************

        private void EditUser_Click(Object Sender, CommandEventArgs e) {

            // get user id from dropdownlist of users
            int userId = -1;
            String _userName = "";
        
            if (e.CommandName == "edit") {
        
                userId = Int32.Parse(allUsers.SelectedItem.Value);
                _userName = allUsers.SelectedItem.Text;
            }
        
            // redirect to edit page
            Response.Redirect("~/Admin/ManageUsers.aspx?userId=" + userId + "&username=" + _userName + "&tabindex=" + tabIndex + "&tabid=" + tabId);
        }
    
        //*******************************************************
        //
        // The BindData helper method is used to bind the list of 
        // users for this portal to an asp:DropDownList server control
        //
        //*******************************************************

        private void BindData() {
    
            // change the message between Windows and Forms authentication
            if (Context.User.Identity.AuthenticationType != "Forms")
                Message.Text = "用户必须注册后才能查看安全信息。用户可以使用注册页面来添加自己，管理员可以使用上面的安全角色功能将用户添加至特定的角色中。该部分允许管理员直接管理用户和他们的安全角色。";
            else
                Message.Text = "域用户不必注册就可以访问公开的门户内容。管理员可以使用以上安全角色功能添加域用户至特定角色。该部分允许管理员直接管理用户和他们的安全角色。";

            // Get the list of registered users from the database
            RolesDB roles = new RolesDB();
        
            // bind all portal users to dropdownlist
            allUsers.DataSource = roles.GetUsers();
            allUsers.DataBind();
        }
    
        public Users() {
            this.Init += new System.EventHandler(Page_Init);
        }

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

		#region Web Form Designer generated code
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
			this.EditBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(this.EditUser_Click);
			this.DeleteBtn.Click += new System.Web.UI.ImageClickEventHandler(this.DeleteUser_Click);
			this.addNew.Command += new System.Web.UI.WebControls.CommandEventHandler(this.EditUser_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
    }
}
