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.Home;
using JuSNS.Model;

namespace JuSNS.Web.user.share
{
    public partial class showCommentForm : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string GAction = Request.QueryString["action"];
            string GId = Request.QueryString["Id"];
            string InnId = Request.QueryString["InnId"];
            string Gsh = Request.QueryString["sh"];
            if (Gsh != null && Gsh != string.Empty)
            {
                showCommentList(Convert.ToInt32(GId));
            }
            else
            {
                if (GAction != null && GAction != string.Empty)
                {
                    Share sd = new Share();
                    if (GAction == "Post")
                    {
                        string ContentDesc = Request.QueryString["ContentDesc"];

                        #region 过滤关键字
                        string res = JuSNS.Common.Public.CheckKey(ContentDesc, true);
                        if (res != "")
                        {
                            ContentDesc = ContentDesc.Replace(ContentDesc,"***");
                        }
                        #endregion
                        sd.PostComment(int.Parse(GId), UserID, ContentDesc, 0);
                        showCommentList(Convert.ToInt32(GId));
                    }
                    else if (GAction == "Del")
                    {
                        Share sharedel = new Share();
                        sharedel.DelComment(int.Parse(GId), UserID);
                        Response.Write("删除成功");
                        Response.End();
                    }
                }
                else
                {
                    string ContentStr = string.Empty;
                    ContentStr += "<textarea name=\"showContent" + GId + "\" id=\"showContent" + GId + "\" style=\"width:97%;\" rows=\"5\" class=\"f-text\"></textarea>\r";
                    ContentStr += "<p class=\"pstr\"><input  class=\"f-button\" type=\"button\" onclick=\"javascript:postComment('" + GId + "');\" name=\"Button_" + GId + "\" id=\"Button_" + GId + "\" value=\"提交\" /> <input  class=\"f-button\" type=\"button\" onclick=\"javascript:closeComment('" + GId + "');\" name=\"close_" + GId + "\" id=\"close_" + GId + "\" value=\"关闭\" /></p>\r";
                    Response.Write(ContentStr);
                    Response.End();
                }
            }
        }

        protected void showCommentList(int ID)
        {
            string listStr = string.Empty;
            Share sd = new Share();
            IDataReader dr = sd.ShareCommentList(ID);
            while (dr.Read())
            {
                listStr += "<tr><td style=\"width:56px;\" valign=\"top\"><a href=\"/user/?r=" + dr["UserID"].ToString() + "\"><img src=\"" + GetHeadImage(Convert.ToInt32(dr["UserID"].ToString())) + "\" style=\"width:48px;height:48px;\" /></a></td>\r";
                listStr += "<td valign=\"top\">\r";
                string ShowDelFlag = string.Empty;
                if (UserID == int.Parse(dr["UserID"].ToString()))
                {
                    ShowDelFlag = "<div style=\"text-align:right;\"><span class=\"delete\"><a href=\"javascript:void(0);\" onclick=\"DelShareCommtent('" + dr["ID"].ToString() + "','" + ID + "');\" class=\"brdpst-del\">删除</a></span></div>";
                }
                string ShowDateFlag = string.Empty;
                if (JuSNS.Common.Input.IsDate(dr["PostTime"].ToString()))
                {
                    DateTime TmpDate = DateTime.Parse(dr["PostTime"].ToString());
                    ShowDateFlag = JuSNS.Common.Public.getTimeSpan(TmpDate);
                }
                listStr += "<div class=\"hdtitle\"><table width=\"100%\"><tr><td width=\"85%\"><a href=\"/user/?r=" + dr["UserID"].ToString() + "\">" + GetUserName(dr["UserID"].ToString()) + "</a> <span style=\"color:#272727;\">(" + GetUserArea(dr["UserID"].ToString()) + ")</span> <span style=\"color:#999999;\">" + ShowDateFlag + "</span></td><td width=\"15%\">" + ShowDelFlag + "</td></tr></table></div>\r";
                listStr += "<div style=\"padding:5px 0px 12px 3px;\">" + dr["Content"].ToString() + "</div>\r";
                listStr += "</td>\r";
                listStr += "</tr>\r";
            }
            dr.Close();
            Response.Write("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" style=\"width:99%;\">" + listStr.Replace("??", "？？") + "</table>");
            Response.End();
        }

        /// <summary>
        /// 获得用户名（昵称）
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string GetUserName(string userid)
        {
            JuSNS.Home.User user = new JuSNS.Home.User();
            UserInfo userinfo = new UserInfo();
            userinfo = user.GetUserInfo(Convert.ToInt32(userid));
            string getUsername = userinfo.Username;
            return getUsername;
        }

        protected string GetUserArea(string userid)
        {
            JuSNS.Home.User user = new JuSNS.Home.User();
            UserInfo userinfo = new UserInfo();
            userinfo = user.GetUserInfo(Convert.ToInt32(userid));
            int getUserArea = userinfo.Province;
            Dict dict = new Dict();
            return dict.GetAreaName(getUserArea);
        }

        /// <summary>
        /// 得到头像
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string GetUserHeadPic(string userid)
        {
            JuSNS.Home.User user = new JuSNS.Home.User();
            UserInfo userinfo = new UserInfo();
            userinfo = user.GetUserInfo(Convert.ToInt32(userid));
            return "<img src=\"/" + userinfo.PortraitDir + "\" alt=\"" + userinfo.Username + "\" style=\"width:48px;height:48px;\" /></a>";
        }
    }
}
