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

namespace Stat.web.Manager
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Master : System.Web.UI.Page
    {
        Model.Users userModel = new Stat.Model.Users();
        Model.PageInfo pageModel = new Stat.Model.PageInfo(10);
        Security.ISecurity seacurity = Security.DataFatory.CreateSecurity();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string ac = Plugin.Common.Rq("ac");

                if (ac.ToLower() == "add")
                {
                    AddMaster();
                }
                else
                {
                    this.txtW.Text = Plugin.Common.Rq("w");
                    GetUserList();//列表
                }
            }



        }

        private void AddMaster()
        {
            this.pnUserList.Visible = false;
            this.pnPower.Visible = false;
            this.pnUserAdd.Visible = true;
        }
        /// <summary>
        /// 管理员列表
        /// </summary>
        private void GetUserList()
        {
            userModel.UserName = userModel.UserQQ = userModel.UserEmail = this.txtW.Text.Trim();
            userModel.UserGroup = 1;
            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;
            this.pnPower.Visible = false;
            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();
                this.txtSysID.Text = model.SystemID.ToString();
            }
        }
        /// <summary>
        /// 保存管理员 信息
        /// </summary>
        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            Stat.Model.Users model = new Stat.Model.Users();
            model.UserId = Convert.ToInt32("0" + 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 = true;
            model.SystemID = Convert.ToInt32("0" + this.txtSysID.Text.Trim());

            if (model.UserId > 0)
            {
                if (Stat.BLL.Users.Edit(model)) { Plugin.Common.Alert("修改成功！", "?"); }
                else { Plugin.Common.Alert("操作失败！"); }
            }
            else
            {
                model.UserName = this.txtUserName.Text.Trim();
                model.UserGroup = 1;
                if (Stat.BLL.Users.Add(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();
        }
        /// <summary>
        /// 编辑权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "editPower")
            {
                this.pnPower.Visible = true;
                this.pnUserAdd.Visible = false;
                this.pnUserList.Visible = false;
               
                int id = int.Parse(e.CommandArgument.ToString());
                this.hdfEditMasterId.Value = id.ToString();
                Model.Users master = BLL.Users.GetUserInfo(id);
                string admin_power = master.UserPower == null ? "" : master.UserPower;
                this.ltrMasterName.Text = master.UserName;
                this.ltrPowerList.Text = seacurity.GetMasterPowerList(admin_power);
            }
        }
        /// <summary>
        /// 保存权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            string admin_power = Request.Form["admin_power"] != null ? Request.Form["admin_power"].ToString() : "";

            Model.Users model = new Stat.Model.Users();
            model.UserId = int.Parse(this.hdfEditMasterId.Value);
            model.UserPower = admin_power; //系统权限

            if (model.UserId > 0)
            {
                if (!BLL.Users.EditPower(model))
                {
                    Plugin.Common.Alert("操作失败！", "?");
                }
                else
                {
                    seacurity.ReloadCurrentUser();
                    Plugin.Common.Alert("操作成功！", "?");
                }
            }
        }
    }
}
