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 System.Collections.Generic;
public partial class Admin_SelectTemplate : System.Web.UI.Page
{
    UserConfig userConfig;
    protected void Page_Load(object sender, EventArgs e)
    {
        templateList.ItemCommand += new DataListCommandEventHandler(templateList_ItemCommand);
        templateList.ItemCreated += new DataListItemEventHandler(templateList_ItemCreated);
        userConfig = Cacher.GetUserConfig(Account);
       
            BindList();
    }

    private void BindList()
    {
        List<Theme> list =Globals.GetThemes().ThemeList;
        templateList.DataSource = list;
          userConfig.Theme = "jiuyue";
        Users.UpdateUserConfig(userConfig);
        templateList.DataBind();
    }

    void templateList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "select")
        {
            string themeName = e.CommandArgument.ToString().Trim();
            if (Account!="xiaoxiao"&& themeName.ToLower() != "msn")
            {
                System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
                sb1.Append(@"<script type='text/javascript' language='javascript'>");
                sb1.AppendFormat("_error_msg_show('您所在的用户组不能选择该模板!')") ;
                sb1.Append("</script>");
                Message.Text = sb1.ToString();
                return;
            }
          
            Theme the = Globals.GetThemes().FindTheme(themeName);
            UserLayout ul = new UserLayout();
            ul.Theme = the.Name;
            ul.Style = the.Style;
            userConfig.Theme = the.Name;
            foreach (ThemeRow tr in the.Rows)
            {
                foreach (ThemeCell tc in tr.Cells)
                {
                    if (userConfig.Layout!=null)
                   foreach(ThemeRow utr in userConfig.Layout.Rows)
                   {
                       foreach (ThemeCell utc in utr.Cells)
                       {
                           if (tc.ModuleName.ToLower() == utc.ModuleName.ToLower())
                           {
                               tc.Parameter = utc.Parameter;
                           }
                       }
                   }
                }
                ul.Rows.Add(tr);
            }
            ul.Layout = the.Layout;
            if (userConfig.Theme.ToLower() == "msn")
                ul.Theme = "Fusion";
            userConfig.Layout = ul;

            Users.UpdateUserConfig(userConfig);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript' language='javascript'>");
            sb.AppendFormat("_win_confirm_msg_show('您已成功更改了页面模板，是否现在查看？','window.open(\"{0}\")','','120','80')", Globals.GetSiteUrls().Default(userConfig.Account));
            sb.Append("</script>");
            Message.Text = sb.ToString();

        }
        BindList();
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
    void templateList_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Theme the = e.Item.DataItem as Theme;
            if (the != null)
            {
                Literal litlTitle = e.Item.FindControl("litlTitle") as Literal;
                litlTitle.Text = the.Title;
                Image imgTheme = e.Item.FindControl("imgTheme") as Image;
                if (the.OutlineMap.Trim() != string.Empty)
                {
                    imgTheme.ImageUrl = string.Format("{0}/themes/{1}/{2}",Globals.ApplicationPath,the.Name,the.OutlineMap);
                    imgTheme.ToolTip = the.Title;
                    imgTheme.Attributes.Add("Width", "120");
                    imgTheme.Attributes.Add("Height", "120");
                }
                LinkButton lbtnSelect = e.Item.FindControl("lbtnSelect") as LinkButton;
                lbtnSelect.CommandName = "select";
                lbtnSelect.CommandArgument = the.Name;

                Literal litlDescription = e.Item.FindControl("litlDescription") as Literal;
                litlDescription.Text = the.Description;
                Literal litlDynamic = e.Item.FindControl("litlDynamic") as Literal;
                if (the.Dynamic)
                {
                    litlDynamic.Text = "该模板是动态模板，您可以自定义页面布局和样式";
                }
                else
                {
                    litlDynamic.Text = "该模板是静态模板";
                }
                Literal litlCurrentSelect = e.Item.FindControl("litlCurrentSelect") as Literal;
                if (the.Name == userConfig.Theme)
                {
                    litlCurrentSelect.Text = "<font color=red>该模板是您当前选用的模板</font>";
                    lbtnSelect.Visible = false;
                }
            }
        }

    }

   
}
