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;

namespace JuSNS.Web.home.space
{
    public partial class template : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache"; 
            string r = Request.QueryString["r"];
            if (r == "get")
            {
                JuSNS.Home.Space SP = new JuSNS.Home.Space();
                int sID = Convert.ToInt32(Request.QueryString["id"].ToString());
                IDataReader rd = SP.GetSpaceTemplate(sID);
                if (rd.Read())
                {
                    //扣点
                    JuSNS.Home.User US = new JuSNS.Home.User();
                    int IPoint = Convert.ToInt32(rd["IPoint"].ToString());
                    int GPoint = Convert.ToInt32(rd["GPoint"].ToString());
                    IDataReader DR = US.GetUserPoint(UserID);
                    if (DR.Read())
                    {
                        if (IPoint > Convert.ToInt32(DR["integral"].ToString()))
                        {
                            DR.Close();
                            PageError("您的积分点数不够，不能更换此模板！", rooDir + "/home/space/template.aspx", true);
                            //this.Div_Error.Visible = true;
                            //this.Div_Error.InnerHtml = "您的积分点数不够，不能更换此模板！";
                        }
                        if (GPoint > Convert.ToInt32(DR["inteyb"].ToString()))
                        {
                            DR.Close();
                            PageError("您的" + JuSNS.Config.UiConfig.gName + "不够，不能更换此模板！", rooDir + "/home/space/template.aspx", true);
                            //this.Div_Error.Visible = true;
                            //this.Div_Error.InnerHtml = "您的" + JuSNS.Config.UiConfig.gName + "不够，不能更换此模板！";
                        }
                    }
                    DR.Close();
                    if (GPoint != 0 || IPoint != 0)
                    {
                        if (IPoint != 0)
                        {
                            US.updateinte(UserID, IPoint, 0, 1, "选择空间模板");
                        }
                        if (GPoint != 0)
                        {
                            US.updateinte(UserID, GPoint, 1, 1, "选择空间模板");
                        }
                    }
                    //更新XML
                    JuSNS.Common.Public.getXML("~/user/config/xml/" + UserID + ".xml", "stylename", rd["TPath"].ToString(), 0);
                    JuSNS.Common.Public.getXML("~/user/config/xml/" + UserID + ".xml", "backimg", "", 0);
                    PageRight("设置个人空间模板成功！<a href=\"" + rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", this.UserID.ToString()) + "\">预览我的空间</a>", rooDir + "/home/space/template.aspx");
                    //this.Div_Succ.Visible = true;
                    //this.Div_Succ.InnerHtml = "设置个人空间模板成功！<a href=\"" + rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", this.UserID.ToString()) + "\">预览我的空间</a>";
                }
                rd.Close();
            }
            tlist();
        }

        public string GetTPATH(object id, object tpath, object ipoint, object gpoint)
        {
            string listStr = string.Empty;
            string gTPath = JuSNS.Common.Public.getXML("~/user/config/xml/" + UserID + ".xml", "stylename", "", 1);
            if (tpath.ToString() != gTPath)
            {
                listStr += "<a href=\"template.aspx?r=get&id=" + id.ToString() + "\">使用</a>";
            }
            else
            {
                listStr += "<label class=\"reshow\">使用中</label>";
            }
            return listStr;
        }

        protected void tlist()
        {
            JuSNS.Home.Space sp = new JuSNS.Home.Space();
            int r = 0, p = 0, PageIndex = 0, pagsize = 48;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = null;

            dt = sp.GetSpaceTemplateList(PageIndex, pagsize, out r, out p, null);
            if (dt != null)
            {
                RptData.DataSource = dt;
                RptData.DataBind();
            }

            //判断是否调用分页
            if (pagsize > 48)
            {
                DivPage.InnerHtml = JuSNS.UI.Control.PageNavigator.Pagination(PageIndex, p, "template.aspx");
            }
        }


        protected string GetReview(object pic)
        {
            return rooDir + "/uploads/template/user/" + pic.ToString();
        }
    }
}
