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 Spaces;
using Spaces.Controls;
using System.Collections.Generic;

public partial class Admin_LayoutSettings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindPage();
    }

    private void BindPage()
    {
        BindModules();
        BindSelect();
        
       
    }
    private void BindSelect()
    {
        UserConfig uc = Cacher.GetUserConfig(Account);//Globals.GetThemes()
        Theme the = Globals.GetThemes().FindTheme(uc.Theme);
       // Response.Write(the.Rows.Count.ToString());
        #region 绑定模块列表
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        bool enableSelectLayout = false;
        if (uc.Theme == "msn")
            enableSelectLayout = true;
        sb.AppendFormat("<script> var enableSelectLayout={0};</script>",enableSelectLayout?"true":"false");
        List<ThemeRow> checkRows;
        if (uc.Layout != null) //如果用户没有更改过布局
        {
            checkRows = uc.Layout.Rows;
        }
        else
        {
            checkRows = the.Rows;
        }
        string defLayout = uc.Layout == null ? the.Layout : uc.Layout.Layout;
        
        foreach (Module m in Spaces.Controls.Modules.GetAllModuleList(Account))
        {
            if(uc.Theme!="msn"&&m.Name.ToLower()=="blog")
            {}else{
            sb.Append("<div class=\"Part\">\r\n");
            sb.AppendFormat("<input onclick=\"selectPartType(this);\" type=\"checkbox\" title=\"{2}\"  id=\"{0}\" name=\"{1}\" value=\"{0}\" {3}><label for=\"p{0}\">{1}", m.Name, m.Title, m.Description, GetChecked(checkRows, m.Name));
            sb.Append("</label>&nbsp;<br>\n\t");
            sb.Append("</div>\r\n");
            }
        }
        modules.Text = sb.ToString();
        #endregion
        layoutJs.Text=CreateJs(checkRows, defLayout);
        return; 
        TableRow tr = new TableRow();
        #region msn风格的处理

        if (uc.Theme.ToLower() == "msn")
        {
            int rowCount = 2;
            if (uc.Layout!=null)
                rowCount = ThemeHelp.GetRowCountByLayout(uc.Layout.Layout);
          for (int i = 0; i < rowCount; i++)
         {
                TableCell tc = new TableCell();
                HtmlSelect select = new HtmlSelect();
                select.Name = "ThemeRow" + i;
                select.ID = "ThemeRow" + i;
                select.Size = 10;
                select.Attributes.Add("width","26");
                select.Attributes.Add("onchange ", "deselectColumn(this)");
                if (checkRows.Count >i)
                foreach (ThemeCell themeTc in checkRows[i].Cells)
                {

                    Module m = Spaces.Controls.Modules.FindUserModule(Account,themeTc.ModuleName);
                    if (m != null)
                    {
                        ListItem li = new ListItem(m.Title, m.Name);
                        select.Items.Add(li);
                     
                    }
                }
               
                

                tc.Controls.Add(select);
                tr.Cells.Add(tc);
                tbModules.Rows.Add(tr);
            }
         
         return;
        }
        #endregion

      
            for (int i = 0; i < checkRows.Count; i++)
            { 
                
               

                TableCell tc = new TableCell();
                HtmlSelect select = new HtmlSelect();
                select.Name = "ThemeRow" + i;
                select.ID = "ThemeRow" + i;
                select.Size = 10;
                select.Attributes.Add("width","26");
                select.Attributes.Add("onchange ", "deselectColumn(this)");
                foreach (ThemeCell themeTc in checkRows[i].Cells)
                {

                    Module m = Spaces.Controls.Modules.FindUserModule(Account, themeTc.ModuleName);
                    if (m != null)
                    {
                        ListItem li = new ListItem(m.Title, m.Name);
                        select.Items.Add(li);
                     
                    }
                }
                if (!the.Rows[i].EnableDynamic) //检查是否启用动态布局
                {
                   select.Disabled = true;
                   select.Attributes.Add("style", "background-color:#D2D2D2");
                }
                

                tc.Controls.Add(select);
                tr.Cells.Add(tc);
                tbModules.Rows.Add(tr);
            }
           
     
    }

    private string CreateJs(List<ThemeRow> checkRows,string layout)
    {
       
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script>
 function PartItem(n,t)
  {
   this.Name=n;
   this.Title=t;
  }
 function PartRows()
 {
  this.Items=new Array();
  this.Add=function(v)
  {
   this.Items[this.Items.length]=v;  
  }
 
 }
 var PartItems=new Array();");
        sb.AppendFormat("\r\n var defLayout='{0}' ;\r\n",layout);
        for (int i = 0; i < checkRows.Count; i++)
        {
            sb.AppendFormat("\r\nPartItems[{0}]=new PartRows();\r\n",i);
            foreach(ThemeCell tc in checkRows[i].Cells)
            {
                  Module m = Spaces.Controls.Modules.FindUserModule(Account, tc.ModuleName);
                if(m!=null)
            sb.AppendFormat("PartItems[{0}].Add(new PartItem('{1}','{2}'));\r\n",i,m.Name,m.Title);
            }
                        
        }
        sb.Append("</script>");
        return sb.ToString();
    }
   
    private string GetChecked(List<ThemeRow> list, string p)
    {
        string check=string.Empty;
        foreach (ThemeRow tr in list)
        {
            foreach (ThemeCell tc in tr.Cells)
            {
                if (tc.ModuleName.Trim() == p)
                    return "checked";
            }
        }
        return check;
    }

    void BindModules()
    {
       
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
}
