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

namespace JuSNS.Web.home.gbook
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        public int uID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string r = Request.QueryString["r"];
            uID = this.GetUserID();
            this.Button1.Attributes.Add("onclick", "javascript:return checkdata();");
            this.ptitle.InnerText = "我的留言本";
            this.bName.InnerHtml = "我的留言本";
            if (JuSNS.Common.Input.IsInteger(r))
            {
                uID = Convert.ToInt32(r);
                UserInfo mdl = new UserInfo();
                User us = new User();
                mdl = us.GetUserInfo(uID);
                this.ptitle.InnerText = mdl.Username + "的留言本";
                this.bName.InnerHtml = mdl.Username + "的留言本";
            }
            if (GetUserID() ==0)
            {
                this.form1.Visible = false;
            }
            if (string.IsNullOrEmpty(r)||r==this.GetUserID().ToString())
            {
                this.form1.Visible = false;
            }
            string a = Request.QueryString["a"];
            if (a == "record")
            {
                if (r != "-1")
                {
                    if (!this.PrivacyAvailable(EnumPrivacyType.LeaveWord, GetUserID(), Convert.ToInt32(r)))
                    {
                        PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                    }
                }
                GbookList(2);
                this.form1.Visible = false;
            }
            else
            {
                if (r != "-1")
                {
                    if (!this.PrivacyAvailable(EnumPrivacyType.LeaveWord, GetUserID(), Convert.ToInt32(r)))
                    {
                        PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                    }
                    GbookList(0);
                }
                else
                {
                    this.isPub.Visible = false;
                    this.Button1.Text = "给管理员留言";
                    this.ptitle.InnerText = "给管理员留言";
                    this.bName.InnerHtml = "给管理员留言";
                    GbookList(1);
                }
            }
            string gErr = Request.QueryString["e"];
            if (gErr == "1")
            {
                this.sERR.Visible = true;
                this.sSucc.Visible = false;
                this.sERR.InnerHtml = "留言失败！";
            }
            else if (gErr == "0")
            {
                this.sERR.Visible = false;
                this.sSucc.Visible = true;
                this.sSucc.InnerHtml = "留言成功。";
            }
        }

        protected void GbookList(int Num)
        {
            int ReCount=0;
            int PgCount = 0;
            int PageIndex = 1;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["page"]);
            }
            DataTable dt = null;
            if (Num == 0)
            {
                dt = UtilPage.GetPage("leaveword_aspx", PageIndex, 20, out ReCount, out PgCount, new SqlConditionInfo("@Receiver", uID, TypeCode.Int32));
            }
            else if (Num == 1)
            {
                dt = UtilPage.GetPage("leaveword_manage_aspx", PageIndex, 20, out ReCount, out PgCount, new SqlConditionInfo("@Receiver", this.GetUserID(), TypeCode.Int32));
            }
            else if (Num == 2)
            {
                SqlConditionInfo[] sts = new SqlConditionInfo[2];
                sts[0] = new SqlConditionInfo("@Receiver", Convert.ToInt32(Request.QueryString["r"]), TypeCode.Int32);
                sts[1] = new SqlConditionInfo("@SendID", Convert.ToInt32(Request.QueryString["s"]), TypeCode.Int32);
                dt = UtilPage.GetPage("leaveword_record_aspx", PageIndex, 20, out ReCount, out PgCount, sts);
            }
            DataTable data = new DataTable();
            data.Columns.Add("Gbook", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                string reshowStr = string.Empty;
                sb.Append("<li id=\"book_li_" + dt.Rows[i]["ID"].ToString() + "\">\n");
                sb.Append("<div class=\"body-2m-left image nl\"><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["SendID"].ToString()) + "\"><img style=\"width:48px;height:48px;\" src=\"" + GetHeadImage(Convert.ToInt32(dt.Rows[i]["SendID"].ToString()), 1) + "\" /> </a></div>\n");
                string DelContent = string.Empty;

                string UserName = string.Empty;
                
                string diagStr = string.Empty;
                string aName = string.Empty;
                if (Num != 1)
                {
                    aName = "<span class=\"tshow\">(" + dt.Rows[i]["Name"].ToString() + ")";
                    UserName = dt.Rows[i]["UserName"].ToString();
                }
                else
                {
                    UserName = "管理员";
                }
                string op = string.Empty;
                string mess = string.Empty;
                if (dt.Rows[i]["SendID"].ToString() == this.GetUserID().ToString())
                {
                    DelContent = "&nbsp; <a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定要删除留言吗？删除留言将删除回复！</li></ul>','删除留言',function(){delbook('" + dt.Rows[i]["ID"].ToString() + "','" + dt.Rows[i]["RecID"].ToString() + "')})\">删除</a>";
                    UserName = "<span class=\"reshow bold\">你自己</span>";
                }
                else
                {
                    op = " &nbsp;<a href=\"../gbook?r=" + dt.Rows[i]["SendID"].ToString() + "\">回复(" + dt.Rows[i]["ReplayCount"].ToString() + ")</a>&nbsp; ";
                    mess = "<a href=\"javascript:void(0);\" onclick=\"mailto('" + dt.Rows[i]["SendID"].ToString() + "','')\">发消息</a>";
                    if (Num != 1)
                    {
                        diagStr = "<a href=\"index.aspx?s=" + dt.Rows[i]["RecID"].ToString() + "&r=" + dt.Rows[i]["SendID"].ToString() + "&a=record\">对话录</a>";
                    }
                }
                if (Num != 1)
                {
                    diagStr = "<a href=\"index.aspx?s=" + dt.Rows[i]["RecID"].ToString() + "&r=" + dt.Rows[i]["SendID"].ToString() + "&a=record\">对话录</a>";
                }
                sb.Append("<div class=\"body-2m-right nl\"><p class=\"hdtitle\"><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["SendID"].ToString()) + "\">" + UserName + "</a> " + aName + " &nbsp; " + JuSNS.Common.Public.getTimeEXPINSpan(DateTime.Parse(dt.Rows[i]["PostTime"].ToString())) + "</span> " + diagStr + "</p> <p class=\"contentb\" style=\"padding-left:6px;\">" + JuSNS.Common.Public.CheckSma(dt.Rows[i]["Content"].ToString()) + "</p><p class=\"op\">" + op + "" + DelContent + "</p></div>\n");
                sb.Append("<div class=\"spline\"></div>\n");
                sb.Append("</li>\n");
                dr["Gbook"] = sb.ToString();
                data.Rows.Add(dr);
            }
            string PageUrl = "../gbook?r=" + uID + "";
            this.GbookLists.Visible = true;
            this.GbookLists.DataSource = data;
            this.GbookLists.DataBind();
            this.LtrNum.Text = "共" + ReCount.ToString() + "个留言";
            if (ReCount > 20)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            GetIsNotEmail();
            string r = Request.QueryString["r"];
            int RecID=0;
            if (r == "-1")
            {
                RecID = -1;
            }
            else if (JuSNS.Common.Input.IsInteger(r))
            {
                RecID = Convert.ToInt32(r);
            }
            else
            {
                RecID = this.GetUserID();
            }
            string gbookConent = this.TXTContent.Text;
            int isPub = 0;
            if (this.isPub.Checked)
            {
                isPub = 1;
            }
            JuSNS.Home.GBook lw = new JuSNS.Home.GBook();
            int n = lw.Edit(0, this.GetUserID(), RecID, gbookConent, 0, isPub);
            if (n > 0)
            {
                Response.Redirect("../gbook?r=" + RecID + "&e=0");
            }
            else
            {
                Response.Redirect("../gbook?r=" + RecID + "&e=1");
            }
        }
    }
}
