using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MUserList : System.Web.UI.Page
    {
        Stat.Cache.IConfig config = Stat.Cache.CacheFatory.CreateConfig();
        Model.Users userModel = new Stat.Model.Users();
        Model.PageInfo pageModel = new Stat.Model.PageInfo(10);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtW.Text = Plugin.Common.Rq("w");
                GetUserList();//列表
            }
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        private void GetUserList()
        {
            userModel.UserName = userModel.UserQQ = userModel.UserEmail = this.txtW.Text.Trim();
            pageModel.PageIndex = Plugin.Common.Rq_Int("page");
            pageModel.PageIndex = pageModel.PageIndex < 1 ? 1 : pageModel.PageIndex;
            FillToGvWebList(BLL.Users.Get(userModel, ref pageModel));
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="dt"></param>
        protected void FillToGvWebList(IList<Model.Users> dt)
        {
            this.gvUserList.DataSource = dt;
            this.gvUserList.DataKeyNames = new string[] { "Userid" };
            this.gvUserList.DataBind();
            for (int i = 0; i < dt.Count; i++)
            {
                if (this.gvUserList.Rows[i].Cells[2].Text.ToString() == "0")
                {
                    this.gvUserList.Rows[i].Cells[2].Text = "用户";
                }
                else if (this.gvUserList.Rows[i].Cells[2].Text.ToString() == "1")
                {
                    this.gvUserList.Rows[i].Cells[2].Text = "<font color=#0000FF>管理</font>";
                }
                else
                {
                    this.gvUserList.Rows[i].Cells[2].Text = "查看";
                }

                if (this.gvUserList.Rows[i].Cells[4].Text.ToString() == "1")
                {
                    this.gvUserList.Rows[i].Cells[4].Text = "启用";
                }
                else
                {
                    this.gvUserList.Rows[i].Cells[4].Text = "<font color=#FF0000>禁用</font>";
                }
            }
            /*-------------------------------------------------------------*/
            string strUrlParameter = "w=" + userModel.UserName;
            this.ltrPageIndex.Text = JPageIndex.GetIndex.Get(pageModel.RecordCount, pageModel.PageIndex, pageModel.PageSize,strUrlParameter, 1, 0, 1, 6);

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                //给行加属性
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#66FF66';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';");
            }
        }
        /// <summary>
        /// 删除管理员
        /// </summary>
        protected void gvUserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string Userid = this.gvUserList.DataKeys[e.RowIndex][0].ToString();

            if (Stat.BLL.Users.Delete(Userid))
            {
                Plugin.Common.Alert("操作成功！", "?");
            }
            else
            {
                Plugin.Common.Alert("操作失败！");
            }
        }
        /// <summary>
        /// 修改管理员
        /// </summary>
        protected void gvUserList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string Userid = this.gvUserList.DataKeys[e.NewEditIndex][0].ToString();
            FillUserInfo(Userid);
        }
        /// <summary>
        /// 初始化用户信息
        /// </summary>
        protected void FillUserInfo(string uid)
        {
            this.pnUserList.Visible = false;
            this.pnUserAdd.Visible = true;
            Model.Users model = Stat.BLL.Users.GetUserInfo(int.Parse(uid.ToString()));
            if (model != null && model.UserName.Length > 0)
            {
                this.txtUserName.Text = model.UserName;
                this.txtUserName.Enabled = false;
                this.hdfOldPWD.Value = model.UserPwd;
                this.hdfUserId.Value = model.UserId.ToString();
                this.rBtnList.Items.FindByValue(model.UserState.ToString()).Selected = true;
                this.txtUEmail.Text = model.UserEmail.ToString();
                this.txtUQQ.Text = model.UserQQ.ToString();
            }
        }
        /// <summary>
        /// 添加新的管理员员
        /// </summary>
        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Stat.Model.Users model = new Stat.Model.Users();
                model.UserId = Convert.ToInt32(this.hdfUserId.Value.ToString());
                model.UserPwd = this.txtUserPWD1.Text.ToString();
                model.UserState = Convert.ToInt32(this.rBtnList.SelectedValue);
                model.UserEmail = this.txtUEmail.Text;
                model.UserQQ = this.txtUQQ.Text;
                model.IsPwdMD5 = config.IsUserPwdMd5();

                if (Stat.BLL.Users.Edit(model))
                {
                    Plugin.Common.Alert("修改成功！", "?");
                }
                else
                {
                    Plugin.Common.Alert("操作失败！");
                }
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetUserList();
        }
    }
}
