using System;
using System.Data;
using System.Text;
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 JuSNS.UI.Control;

namespace JuSNS.Web.home.gift
{
    public partial class box : JuSNS.UI.Page.UserPage
    {
        JuSNS.Home.Gift gt = new JuSNS.Home.Gift();
        JuSNS.Home.User us = new JuSNS.Home.User();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.Expires = 0;
            string r = Request.QueryString["r"];
            string type = Request.QueryString["type"];
            if (type == "del")
            {
                string gID = Request.QueryString["id"];
                if (JuSNS.Common.Input.IsInteger(gID))
                {
                    if (gt.DelGift(UserID, Convert.ToInt32(gID)) > 0)
                    {
                        this.msg_succ.Visible = true;
                        this.msg_error.Visible = false;
                    }
                }
                else
                {
                    this.msg_error.Visible = true;
                    this.msg_succ.Visible = false;
                }
            }
            if (JuSNS.Common.Input.IsInteger(r))
            {
                JuSNS.Model.UserInfo M = new JuSNS.Model.UserInfo();
                M = us.GetUserInfo(Convert.ToInt32(r));
                this.mygift.Attributes.Add("class", "current");
                this.sendgift.Attributes.Add("class", "");
                this.mygift.InnerHtml ="<a href=\"../gift/box.aspx?r="+r+"\">"+M.Username+ "收到的礼物</a>";
                this.isMyGift.InnerHtml = "<a href=\"../gift/box.aspx?r=in\">查看我收到的礼物</a>";
                this.isMyGift.Attributes.Add("class", "reshow r");
            }
            else
            {
                if (r == "out")
                {
                    this.mygift.Attributes.Add("class", "");
                    this.sendgift.Attributes.Add("class", "current");
                }
                else
                {
                    this.mygift.Attributes.Add("class", "current");
                    this.sendgift.Attributes.Add("class", "");
                }
            }
            slist(r);
        }

        protected void slist(string r)
        {
            JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
            JuSNS.Model.UserInfo mdl1 = new JuSNS.Model.UserInfo();
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = gt.ShowGiftList(r, UserID, PageIndex, 25, out ReCount, out PgCount);
            string PageUrl = "box.aspx";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                string sendUid = dt.Rows[i]["SendUserID"].ToString();
                mdl = us.GetUserInfo(Convert.ToInt32(sendUid));
                sb.Append("<li>\n");
                sb.Append("<div><img src=\"" + rooDir + "/template/images/gift/" + dt.Rows[i]["pic"].ToString() + "\" title=\"" + dt.Rows[i]["gName"].ToString() + "\" /></div>\n");
                if (dt.Rows[i]["Content"].ToString().Length > 2)
                {
                    sb.Append("<div class=\"pstr reshow bold\"><a href=\"javascript:void(0);\" onclick=\"showgiftcontent('" +dt.Rows[i]["ID"].ToString() + "')\" title=\"有附言，点击查看。\">" + dt.Rows[i]["gName"].ToString() + "</a></div>\n");
                }
                else
                {
                    sb.Append("<div class=\"pstr bold\">" + dt.Rows[i]["gName"].ToString() + "</div>\n");
                }
                if (r == "out")
                {
                    mdl1 = us.GetUserInfo(Convert.ToInt32(dt.Rows[i]["UserID"].ToString()));
                    sb.Append("<div>赠送给<a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\">" + mdl1.Username + "</a></div>\n");
                }
                else
                {
                    if (dt.Rows[i]["ptype"].ToString() == "2")
                    {
                        sb.Append("<div>某人(匿名)赠送</div>\n");
                    }
                    else if (dt.Rows[i]["ptype"].ToString() == "1")
                    {
                            sb.Append("<div>[<span class=\"reshow\">悄悄</span>]<a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", sendUid) + "\">" + mdl.Username + "</a>赠送</div>\n");
                            sb.Append("<div><a href=\"../gift/?r=" + sendUid + "&uName=" + JuSNS.Common.Input.URLEncode(mdl.Username) + "\">回赠</a><span class=\"pike\">|</span><a href=\"box.aspx?r=in&type=del&id=" + dt.Rows[i]["ID"].ToString() + "\"  onclick=\"{if(confirm('确定要删除吗？')){return true;}return false;}\">删除</a></div>\n");
                    }
                    else
                    {
                        sb.Append("<div><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", sendUid) + "\">" + mdl.Username + "</a>赠送</div>\n");
                        sb.Append("<div><a href=\"../gift/?r=" + sendUid + "&uName=" + JuSNS.Common.Input.URLEncode(mdl.Username) + "\">回赠</a><span class=\"pike\">|</span><a href=\"box.aspx?r=in&type=del&id=" + dt.Rows[i]["ID"].ToString() + "\"  onclick=\"{if(confirm('确定要删除吗？')){return true;}return false;}\">删除</a></div>\n");
                    }
                }
                sb.Append("<div class=\"time\">" + JuSNS.Common.Public.getTimeEXPINSpan(DateTime.Parse(dt.Rows[i]["CreatTime"].ToString())) + "</div>\n");
                sb.Append("</li>\n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.glistc.DataSource = data;
            this.glistc.RepeatColumns = 5;
            this.glistc.RepeatDirection = RepeatDirection.Horizontal;
            this.glistc.DataBind();
            this.LtrCount.Text = ReCount.ToString();
            dt.Clear(); data.Clear();
            PageUrl += "?r=" + r + "";
            if (ReCount > 25)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }

        }
    }
}
