using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fm.Bussiness;
public partial class admin_user_sys_user_role : BasePage
{
  
  
    protected void Page_Load(object sender, EventArgs e)
    {
        GetPage(""); 
       
        if (!IsPostBack)
        {
            ListBind();
           Radio1.Checked = true;
        }
    }
    //查询数据并且分页
    void GetPage(string where)
    {
        FormPaging1.TableName = "Fm_role_view";
        FormPaging1.ColName = "r_id";
        FormPaging1.Where = where;
        FormPaging1.Gv = GridView1;
    }
    //用户级别绑定
    void ListBind()
    {
        public_select ps = new public_select();
        DropDownList1.DataSource=ps.Getps("Fm_group","*","");
        DropDownList1.DataTextField = "g_name";
        DropDownList1.DataValueField = "g_id";
        DropDownList1.DataBind();
    }

    //保存信息
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        sys_user_role sys = new sys_user_role();//实例化权限组操作类
        Fm.EntityClass.Role role = new Fm.EntityClass.Role();
        if (Radio1.Checked)
        {
            role.r_name=txt_rname.Text;
            role.g_id = int.Parse(DropDownList1.SelectedValue);
            if (sys.role_add(role))
            {
                Response.Redirect("../Erro.aspx?eid=新角色信息添加成功!&url=user/sys_user_role.aspx", false);
            }
        }
        else if (Radio2.Checked)
        {
            if (hiddle_rid.Value != "")
            {
                role.r_id=int.Parse(hiddle_rid.Value);
                role.r_name=txt_rname.Text;
                role.g_id=int.Parse(DropDownList1.SelectedValue);
                if (sys.role_update(role))
                {
                    Response.Redirect("../Erro.aspx?eid=该角色组信息修改成功!&url=user/sys_user_role.aspx", false);
                }
            }
            else
            {
                Response.Write("<script>alert('请选择需要编辑的用户组');</script>");
            }
        }
        else
        { }

    }
    //用户组信息编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        public_select ps = new public_select();
        int r_id=int.Parse( GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        if (r_id != 1)
        {
            DataView dv = ps.Getps("Fm_role_view","*","r_id=" + r_id + "");
            hiddle_rid.Value = r_id.ToString();
            ViewState["g_id"] = dv[0]["g_id"].ToString();
            txt_rname.Text = dv[0]["r_name"].ToString();
            Radio2.Checked = true;
            //if (ViewState["g_id"] != null)
            // {
            ListBind();
            foreach (ListItem i in DropDownList1.Items)
            {
                if (i.Value == ViewState["g_id"].ToString())
                {
                    i.Selected = true;
                }
            }
        }
        else
        {
            Response.Write("<script>alert('系统默认角色不允许修改');</script>");
        }
       // }

    }
    //删除用户组信息
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        sys_user_role sys = new sys_user_role();//实例化权限组操作类

        int r_id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        if (r_id != 1)
        {
            Fm.EntityClass.Role role = new Fm.EntityClass.Role();
            role.r_id = r_id;
            if (sys.role_del(role))
            {
                Response.Redirect("../Erro.aspx?eid=该角色信息删除成功!&url=user/sys_user_role.aspx", false);
            }
        }
        else
        {
            Response.Write("<script>alert('系统默认角色不允许删除');</script>");
        }
    }
}
