using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin
{
    /// <summary>
    /// Admin_Admin 的摘要说明。
    /// </summary>
    public partial class Admin_Admin : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.CompareValidator CompareValidator1;

        AdminManage MyClassBLL = new AdminManage();
        AdminRoleManage MyAdminRole = new AdminRoleManage();

        #region 权限变量
        private bool AdminAdd = RightsCheck.HasRight("MA_2");
        private bool AdminRoleChange = RightsCheck.HasRight("MA_3");
        private bool AdminDel = RightsCheck.HasRight("MA_4");
        #endregion

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (AdminAdd == AdminRoleChange == AdminDel == false)
            {
                RightsCheck.CheckWorkPower("MA_2");
            }


            string Action = Request["Action"];
            BT_ChangePSW.Attributes.Add("onclick", "return CheckPSW1();");
            Bt_AddAdmin.Attributes.Add("onclick", "return CheckPSW2();");

            if (!(IsPostBack))
            {
                if (Action == "ModifyPwd")
                {
                    if (Convert.ToInt16(AdminLoginInfo.RoleID) != 1 & (Request["UserName"] != AdminLoginInfo.UserName))
                    {
                        MsgBox.Alert_History("你只能修改自己的密码！", -1);
                        return;
                    }

                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    LB_UserName.Text = Request["UserName"];
                }
                else if (Action == "Add")
                {
 
                    RightsCheck.CheckWorkPower("MA_2");

                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    Panel3.Visible = false;
                    MyAdminRole.LoadRole(DDL_RoleID);

                }
                else
                {

                    BindList();
                }
            }
        }

        private void BindList()
        {
            MyClassBLL.BindList(GV_List, PageNavigation1);

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + GV_List.ClientID + "');");
        }

        protected void BT_ChangePSW_Click(object sender, System.EventArgs e)
        {
            AdminInfo model = new AdminInfo();
            model.UserName = Request["UserName"];
            model.Password = TB_OldPSW.Text;

            LB_Message.Text = MyClassBLL.ChangPSW(model, TB_New_PSW1.Text);
        }

        protected void BT_AddAdmin_Click(object sender, System.EventArgs e)
        {
            TopWinCMS.Model.AdminInfo model = new TopWinCMS.Model.AdminInfo();
            model.UserName = TB_UserName.Text;
            model.Password = TB_Password1.Text;
            model.RoleID = DDL_RoleID.SelectedValue;

            MyClassBLL.Add(model, UpdatePanel2);

            BindList();
            Panel3.Visible = true;
            Panel1.Visible = false;
            Panel2.Visible = false;
        }
        protected DataView LoadRole()
        {
            return MyAdminRole.LoadRole();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            BindList();

            DropDownList DDL = ((DropDownList)GV_List.Rows[GV_List.EditIndex].Cells[2].Controls[1]);

            string ID = GV_List.DataKeys[GV_List.EditIndex].Value.ToString();

            DDL.SelectedValue = MyClassBLL.GetAdminRole(ID).ToString();
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            AdminInfo model = new AdminInfo();

            model.ID = GV_List.DataKeys[e.RowIndex].Value.ToString();
            model.RoleID = ((DropDownList)GV_List.Rows[e.RowIndex].Cells[2].Controls[1]).SelectedValue;
            model.PassCode = ((DropDownList)GV_List.Rows[e.RowIndex].Cells[2].Controls[1]).SelectedItem.Text;

            MyClassBLL.UpdateRole(model);

            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell = e.Row.Cells[9];
                if (myTableCell.Controls.Count >0)
                {
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];

                    if (AdminDel == false)
                    {
                        btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                    }
                    else
                    {
                        btnDel.Attributes.Add("onclick", "alert('您没有删除权限？');return false");
                    }

                    if ((e.Row.Cells[1].Text) == AdminLoginInfo.UserName)
                    {
                        e.Row.Cells[8].Controls[0].Visible = false;
                        e.Row.Cells[9].Controls[0].Visible = false;
                    }
                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            AdminInfo model = new AdminInfo();
            model.ID = GV_List.DataKeys[e.RowIndex].Value.ToString();
            model.UserName = GV_List.Rows[e.RowIndex].Cells[1].Text;

            MyClassBLL.Delete(model);

            MsgBox.Alert("删除成功!", UpdatePanel2);

            BindList();
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            GV_List.PageIndex = e.NewPageIndex - 1;
            BindList();
        }
    }
}