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_role_admin : BasePage
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //动态绑定菜单栏
            ListBind();
            //根据用户组ID获取权限
            Role_Select();
        }
    }
    //用户组权限显示
    void Role_Select()
    {
        sys_user_role sys = new sys_user_role();//实例化用户组操作类
        int r_id = 0;
        if (Request.QueryString["r_id"] != null)
        {
            r_id = int.Parse(Request.QueryString["r_id"].ToString());
        }
        DataView dv = sys.get_role(r_id) as DataView;
        for (int i = 0; i < dv.Count; i++)
        {
            string m_id=dv[i]["m_id"].ToString();
            //系统  
            if (m_id == Check_sys.ToolTip)
            {
                Check_sys.Checked = true;
            }
            foreach (ListItem s in List_sys.Items)
            {
                if (m_id == s.Value)
                {
                    s.Selected= true;
                }
            }
            //文章
            if (m_id == Check_art.ToolTip)
            {
                Check_art.Checked = true;
            }
            foreach (ListItem art in List_art.Items)
            {
                if (m_id == art.Value)
                {
                    art.Selected = true;
                }
            }
            //生成
            if (m_id == Check_htm.ToolTip)
            {
                Check_htm.Checked = true;
            }
            foreach (ListItem htm in List_htm.Items)
            {
                if (m_id == htm.Value)
                { htm.Selected = true; }
            }
            //会员
            if (m_id == Check_user.ToolTip)
            {
                Check_user.Checked = true;
            }
            foreach (ListItem u in List_user.Items)            
            {
                if (u.Value == m_id)
                {
                    u.Selected = true;
                }
            }

          
            //模版
            if (m_id == Check_mb.ToolTip)
            {
                Check_mb.Checked = true;
            }
            foreach (ListItem mb in List_mb.Items)
            {
                if (m_id == mb.Value)
                {
                    mb.Selected = true;
                }
            }
            //其他
            if (m_id == Check_oh.ToolTip)
            {
                Check_oh.Checked = true;
            }
            foreach (ListItem o in List_oh.Items)
            {
                if (m_id == o.Value)
                {
                    o.Selected = true;
                }
            }
        }
    }
    //权限栏目显示
    void ListBind()
    {
        sys_user_role sys = new sys_user_role();//实例化用户组操作类
        //DataView dv = ps.Getps("select * from Fm_menu where m_parent!=0") as DataView;
        //绑定系统操作栏目
        int m_sys_id =int.Parse( Check_sys.ToolTip);
        DataView ms = sys.get_menu(m_sys_id) as DataView;
        for (int s = 0; s < ms.Count; s++)
        {
            List_sys.Items.Add(new ListItem());
            List_sys.Items[s].Text = ms[s]["m_name"].ToString();
            List_sys.Items[s].Value = ms[s]["m_id"].ToString();
        }
        //绑定文章操作栏目
        int m_art_id = int.Parse(Check_art.ToolTip);
        DataView ma = sys.get_menu(m_art_id) as DataView;
        for (int t = 0; t < ma.Count; t++)
        {
            List_art.Items.Add(new ListItem());
            List_art.Items[t].Text = ma[t]["m_name"].ToString();
            List_art.Items[t].Value = ma[t]["m_id"].ToString();
        }
        //绑定会员操作栏目
        int m_user_id = int.Parse(Check_user.ToolTip);
        DataView mu = sys.get_menu(m_user_id) as DataView;
        for (int u = 0; u < mu.Count; u++)
        {
            List_user.Items.Add(new ListItem());
            List_user.Items[u].Value = mu[u]["m_id"].ToString();
            List_user.Items[u].Text=mu[u]["m_name"].ToString();
        }
        //绑定生成操作栏目
        int m_htm_id = int.Parse(Check_htm.ToolTip);
        DataView mh = sys.get_menu(m_htm_id) as DataView;
        for (int h = 0; h < mh.Count; h++)
        {
            List_htm.Items.Add(new ListItem());
            List_htm.Items[h].Text = mh[h]["m_name"].ToString();
            List_htm.Items[h].Value = mh[h]["m_id"].ToString();
        }
        //绑定模版操作栏目
        int m_mb_id = int.Parse(Check_mb.ToolTip);
        DataView mb = sys.get_menu(m_mb_id) as DataView;
        for (int b = 0; b < mb.Count; b++)
        {
            List_mb.Items.Add(new ListItem());
            List_mb.Items[b].Text = mb[b]["m_name"].ToString();
            List_mb.Items[b].Value = mb[b]["m_id"].ToString();
        }
        //绑定其他操作栏目
        int m_oh_id = int.Parse(Check_oh.ToolTip);
        DataView mo = sys.get_menu(m_oh_id) as DataView;
        for (int o = 0; o < mo.Count; o++)
        {
            List_oh.Items.Add(new ListItem());
            List_oh.Items[o].Text = mo[o]["m_name"].ToString();
            List_oh.Items[o].Value = mo[o]["m_id"].ToString();
        }
    }
    //保存权限设置信息
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        sys_user_role sys = new sys_user_role();//实例化用户组操作类
        int r_id = 0;
        if (Request.QueryString["r_id"] != null)
        {
            r_id = int.Parse(Request.QueryString["r_id"].ToString());
        }
        //设置一个DATATABLE装锁选择的数据
        //DataTable dt = new DataTable();
        //DataColumn dc = new DataColumn();
        //dc.ColumnName = "m_id";
        //dt.Columns.Add(dc);
        //DataColumn dc1 = new DataColumn();
        //dc1.ColumnName = "r_id";
        //dt.Columns.Add(dc1);
        if (r_id != 1)
        {
            
            Fm.EntityClass.Power power = new Fm.EntityClass.Power();
            power.r_id = r_id;
            //删除原有权限
            sys.power_del_role(power);
            //获取系统选择的权限
            bool bo = false;
            foreach (ListItem s in List_sys.Items)
            {
                if (s.Selected)
                {
                    power.m_id=int.Parse(s.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_sys.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            //获取文章所选权限
            foreach (ListItem art in List_art.Items)
            {
                if (art.Selected)
                {
                    power.m_id=int.Parse(art.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_art.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            //生成
            foreach (ListItem htm in List_htm.Items)
            {
                if (htm.Selected)
                {
                    power.m_id=int.Parse(htm.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_htm.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            //会员
            foreach (ListItem u in List_user.Items)
            {
                if (u.Selected)
                {
                    power.m_id=int.Parse(u.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_user.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            //模版
            foreach (ListItem m in List_mb.Items)
            {
                if (m.Selected)
                {
                    power.m_id=int.Parse(m.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_mb.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            //其他
            foreach (ListItem h in List_oh.Items)
            {
                if (h.Selected)
                {
                    power.m_id=int.Parse(h.Value);
                    sys.power_add(power);
                    bo = true;
                }
            }
            if (bo)
            {
                power.m_id=int.Parse(Check_oh.ToolTip);
                sys.power_add(power);
                bo = false;
            }
            Response.Redirect("../Erro.aspx?eid=该角色权限信息修改成功!&url=user/sys_role_set.aspx", false);
        }
        else
        {
            Response.Write("<script>alert('系统默认角色不允许修改');</script>");
        }
    }
}
