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_Role 的摘要说明。
    /// </summary>
    public partial class Admin_Admin_Role : System.Web.UI.Page
    {

        AdminRoleManage MyClassBLL = new AdminRoleManage();
        AdminRoleInfo model = new AdminRoleInfo();

        #region 权限变量
        private bool WorkRight = RightsCheck.HasRight("MA_1");
        #endregion 

        protected string strOperation = "";
        protected void Page_Load(object sender, System.EventArgs e)
        {

            if (!IsPostBack)
            {
                string Action = Request["Action"];

                if (Action == "Add")
                {
                    RightsCheck.CheckWorkPower("MA_1");

                    BT_Add.Text = "添加";
                    GetPower();
                    this.GV_List.Visible = false;
                    Panel1.Visible = true;
                }
                else if (Action == "Edit")
                {
                    RightsCheck.CheckWorkPower("MA_1");
                    model = MyClassBLL.EditLoad(Request["ID"]);

                    TB_RoleName.Text = model.RoleName;
                    strOperation = model.RolePower;

                    GetPower();
                    BT_Add.Text = "修改";
                    this.GV_List.Visible = false;
                    Panel1.Visible = true;

                }
                else if (Action == "ReadOnly")
                {
                    model = MyClassBLL.EditLoad(Request["ID"]);

                    TB_RoleName.Text = model.RoleName;
                    strOperation = model.RolePower;

                    GetPower();
                    BT_Add.Visible = false;
                    this.GV_List.Visible = false;
                    CB_ALL.Visible = false;
                    Panel1.Visible = true;
                }
                else
                {
                    this.GV_List.Visible = true;
                    Panel1.Visible = false;
                    BindGrid();
                }
            }
        }
        private void BindGrid()
        {
            MyClassBLL.BindList(this.GV_List);
            MyJavascript.RegJS(this, "attachMouseEvent('" + this.GV_List.ClientID + "');");
        }

        private void GetPower()
        {
            DataList1.DataSource = new BLL.RightsManage().GetPower();
            DataList1.DataBind();
        }
        protected DataSet BindPower(object RootID)
        {
            return new BLL.RightsManage().BindPower(RootID);
        }


        protected void BT_Add_Click(object sender, System.EventArgs e)
        {
            string str = Request.Form["chkOperation"];

            string Action = Request["Action"];
            if (Action == "Add")
            {
                model.RoleName = TB_RoleName.Text.Trim();
                model.RolePower = str;
                MyClassBLL.Add(model);


            }
            else if (Action == "Edit")
            {
                model.ID = Request["ID"];
                model.RoleName = TB_RoleName.Text.Trim();
                model.RolePower = str;

                MyClassBLL.Edit(model);
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            model.ID = GV_List.DataKeys[e.RowIndex].Value.ToString();
            model.RoleName = GV_List.Rows[e.RowIndex].Cells[1].Text;
            MyClassBLL.Delete(model);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell = e.Row.Cells[3];
                HyperLink btnEdit = (HyperLink)myTableCell.Controls[1];
                if (WorkRight == false)
                {
                    btnEdit.Attributes.Add("onclick", "alert('您没有修改权限');return false;");
                }


                myTableCell = e.Row.Cells[4];
                ImageButton btnDel = (ImageButton)myTableCell.Controls[0];
                if (WorkRight == true )
                {
                    btnDel.OnClientClick = "if(confirm('您真的要删除吗？属于该角色的管理员要一起删掉的哦！')==false){ return;}";
                   
                }
                else
                {
                    btnDel.OnClientClick = "alert('您没有删除权限');return;";
                   
                }
            }
        }
    }
}