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

namespace KstCMS.KstCMS_Manager.SystemSet.Users.role
{
    public partial class SetRole : KstCmsBase.WebPage.ValidationPage
    {
        protected string RoleId = null;
        protected string CheckBoxNameAndValue = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            OkBtn.Click += new EventHandler(OkBtn_Click);
            if (!IsPostBack)
            {
                LitTitle.Text = Server.UrlDecode(Request.QueryString.Get("RoleName"));
                int Rid = Convert.ToInt32(Request.QueryString.Get("RoleId"));
                LitMenu.Text = ShowMenu(Rid);

            }


        }

        void OkBtn_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            RoleId = Request.QueryString.Get("RoleId");
            string CheckBoxStr = Request.Form.Get("CheckBoxNameAndValue");
            string RunCode = null;
            if (!string.IsNullOrEmpty(CheckBoxStr))
            {

                string[] CheckBoxNames = CheckBoxStr.Split('|');
                for (int i = 0; i < CheckBoxNames.Length; i++)
                {
                    if (!string.IsNullOrEmpty(CheckBoxNames[i].ToString()))
                    {
                        string[] ss = CheckBoxNames[i].Split('=');
                        if (!string.IsNullOrEmpty(Request.Form.Get(ss[0].ToString())))
                            RunCode += Request.Form.Get(ss[0].ToString()) + ",";
                        if (!string.IsNullOrEmpty(Request.Form.Get(ss[1].ToString())))
                            RunCode += Request.Form.Get(ss[1].ToString());
                        if (!string.IsNullOrEmpty(RunCode))
                            RunCode += ",";

                    }

                }

                KstCmsBase.Entities.RoleEntity RoleInfo = new KstCmsBase.Entities.RoleEntity();
                RoleInfo.RoleId = Convert.ToInt32(RoleId);
                RoleInfo.RoleName = LitTitle.Text;
                RoleInfo.RunCode = RunCode;
                KstCmsBase.DAL.Role.SetPermissions(RoleInfo);
                LitMenu.Text = ShowMenu(Convert.ToInt32(RoleId));
                ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "SuccessMsg", "alert(\"更新成功！\");", true);

            }


        }

        private string ShowMenu(int RoleId)
        {

          KstCmsBase.Collection.SysMenuCollection MsterMenuItem = KstCmsBase.DAL.SysMenu.Get_SysMasterMenu();
            string W_table = "<table  border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\r\n";
            string W_td = "<td valign=\"top\">\r\n";
            string N_table = "<table width=\"20\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" class=\"TableBorder\">\r\n";
            string N_td = "<tr><td nowrap bgcolor=\"#FFFFFF\">\r\n";
            string Html = null;
            foreach (KstCmsBase.Entities.SysMenuEntity SysMenuItem in MsterMenuItem)
            {
                CheckBoxNameAndValue += "MENU_" + SysMenuItem.MENU_ID + "=" + SysMenuItem.MENU_ID + "|";
                string N_trs = null;
                string MenuHeadtd = "<tr><td nowrap class=\"TableHeader\" style=\"text-align:left\"><input type=\"checkbox\" name=\"MENU_" +
                    SysMenuItem.MENU_ID + "\" value=\"" + SysMenuItem.MENU_ID + "\" onClick=\"check_all(this,'" + SysMenuItem.MENU_ID + "');\">";

                if (KstCmsBase.DAL.SysMenu.isShowMenu(RoleId, SysMenuItem.MENU_ID))
                    MenuHeadtd = "<tr><td nowrap class=\"TableHeader\" style=\"text-align:left\"><input type=\"checkbox\" name=\"MENU_" +
                    SysMenuItem.MENU_ID + "\" value=\"" + SysMenuItem.MENU_ID + "\" onClick=\"check_all(this,'" + SysMenuItem.MENU_ID + "');\" checked>";

                string icon = "<img src=\"" + SysRootUrl + "/KstCMS_Manager/images/menu/" + SysMenuItem.MENU_IMAGE + ".gif\"  align=\"absbottom\" /> ";
                MenuHeadtd += icon + " " + SysMenuItem.MENU_NAME + "</td></tr>\r\n";
                KstCmsBase.Collection.SysMenuCollection SubMenuItem = KstCmsBase.DAL.SysMenu.Get_ModelMenu(SysMenuItem.MENU_ID);
                foreach (KstCmsBase.Entities.SysMenuEntity ModeMenuItem in SubMenuItem)
                {

                    string SubMenuHtml = "<input type=\"checkbox\" name=\"" + SysMenuItem.MENU_ID + "\" value=\"" + ModeMenuItem.MENU_ID + "\">";
                    if (KstCmsBase.DAL.SysMenu.isShowMenu(RoleId, ModeMenuItem.MENU_ID))
                        SubMenuHtml = "<input type=\"checkbox\" name=\"" + SysMenuItem.MENU_ID + "\" value=\"" + ModeMenuItem.MENU_ID + "\" checked>";
                    icon = "<img src=\"" + SysRootUrl + "/KstCMS_Manager/images/menu/" + ModeMenuItem.MENU_IMAGE + ".gif\"  align=\"absbottom\" /> ";
                    SubMenuHtml += icon + " " + ModeMenuItem.MENU_NAME;
                    KstCmsBase.Collection.SysMenuCollection SubMenuItem1 = KstCmsBase.DAL.SysMenu.Get_SubMenu(ModeMenuItem.MENU_ID);
                    foreach (KstCmsBase.Entities.SysMenuEntity SubMenu in SubMenuItem1)
                    {

                        if (KstCmsBase.DAL.SysMenu.isShowMenu(RoleId, SubMenu.MENU_ID))
                            SubMenuHtml += "<br>&nbsp;&nbsp;<input type=\"checkbox\" name=\"" + SysMenuItem.MENU_ID + "\" value=\"" + SubMenu.MENU_ID + "\" checked>";
                        else
                            SubMenuHtml += "<br>&nbsp;&nbsp;<input type=\"checkbox\" name=\"" + SysMenuItem.MENU_ID + "\" value=\"" + SubMenu.MENU_ID + "\">";

                        icon = "<img src=\"" + SysRootUrl + "/KstCMS_Manager/images/menu/" + SubMenu.MENU_IMAGE + ".gif\"  align=\"absbottom\" /> ";
                        SubMenuHtml += icon + " " + SubMenu.MENU_NAME;
                    }
                    SubMenuHtml += "</td></tr>\r\n";


                    N_trs += N_td + SubMenuHtml;
                }
                N_trs = MenuHeadtd + N_trs;

                Html += W_td + N_table + N_trs + "</table>\r\n</td>\r\n";
            }
            W_table += "<tr>\r\n" + Html + "</tr></table>";
            return W_table;
        }
    }
}