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.Model;

namespace JuSNS.Web.user
{
    public partial class search : JuSNS.UI.Page.BasePage
    {
        protected int uID = 0;
        protected string r = string.Empty;
        protected string userName = string.Empty;
        protected string Photo = string.Empty;
        protected string Photo1 = string.Empty;
        protected string Click = string.Empty;
        protected string integral = string.Empty;
        protected string inteyb = string.Empty;
        protected int IsFriend = 0;//是否是好朋友
        protected string dongz = string.Empty;
        protected string gGetUserInfo = string.Empty;
        protected string LastMini = string.Empty;//最新的心情日志
        protected string MiniList = string.Empty;
        protected string BlogNum = string.Empty;
        protected string FriendNum = string.Empty;
        protected string FriendList = string.Empty;
        protected string Network = string.Empty;
        protected bool isAddFriend = false;
        JuSNS.Home.Friend frd = new JuSNS.Home.Friend();
        JuSNS.Home.User us = new JuSNS.Home.User();
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            r = Request.QueryString["r"];
            if (JuSNS.Common.Input.IsInteger(r))
            {
                uID = Convert.ToInt32(r);
            }
            else
            {
                uID = this.GetUserID();
            }
            if (uID == GetUserID())
            {
                Response.Redirect(rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", uID.ToString()) + "");
            }
            if (!us.CheckUser(uID))
            {
                PageError("访问发生了错误", rooDir + "/home");
            }
            IsFriends();//判断是否是好友
            if (frd.IsFriends(GetUserID(), uID))
            {
                Response.Redirect(rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", uID.ToString()) + "");
            }
            gGetUserInfo = GetUserInfo();
            dongz = GetDZ();
            MiniList = GetBlogInfo();
            FriendList = GetFriendInfo();
        }

        #region 读取会员基本信息
        public string GetUserInfo()
        {
            string strInfo = string.Empty;
            DataTable dt = sp.getUserInfo(uID, 0);
            if (dt != null && dt.Rows.Count > 0)
            {
                userName = dt.Rows[0]["UserName"].ToString();
                this.ptitle.InnerText = userName + "的空间，您无法完全查看！";
                Photo = this.GetHeadImage(uID, 3);
                Photo1 = this.GetHeadImage(uID, 2);
                if (JuSNS.Common.Input.IsInteger(dt.Rows[0]["Click"].ToString()))
                {
                    Click = dt.Rows[0]["Click"].ToString();
                }

                if (JuSNS.Common.Input.IsInteger(dt.Rows[0]["integral"].ToString()))
                {
                    integral = dt.Rows[0]["integral"].ToString();
                }
                if (JuSNS.Common.Input.IsInteger(dt.Rows[0]["inteyb"].ToString()))
                {
                    inteyb = dt.Rows[0]["inteyb"].ToString();
                }
                dt.Columns.Add("Info", typeof(string));
                if (dt.Rows[0]["Province"].ToString() != "")
                {
                    Network = "<a href=\"" + rooDir + "/network/?r=" + dt.Rows[0]["Pro"].ToString() + "\">" + dt.Rows[0]["Province"].ToString() + "</a>";
                    //Province = dt.Rows[0]["Province"].ToString();
                }
                if (dt.Rows[0]["Sex"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">性别：</label>" + dt.Rows[0]["Sex"].ToString() + "</div>";
                }
                if (dt.Rows[0]["Constellation"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">星座：</label>" + dt.Rows[0]["Constellation"].ToString() + "</div>";
                }
                string birthStr = string.Empty;
                if (!string.IsNullOrEmpty(dt.Rows[0]["Birthday"].ToString()) && JuSNS.Common.Input.IsDate(dt.Rows[0]["Birthday"].ToString()))
                {
                    DateTime gBirthDay = DateTime.Parse(dt.Rows[0]["Birthday"].ToString());
                    switch (dt.Rows[0]["BirthidayDisplay"].ToString())
                    {
                        case "0":
                            birthStr = gBirthDay.Year + "年" + gBirthDay.Month + "月" + gBirthDay.Day + "日";
                            break;
                        case "1":
                            birthStr = gBirthDay.Month + "月" + gBirthDay.Day + "日";
                            break;
                        case "2":
                            birthStr = gBirthDay.Year + "年";
                            break;
                    }
                }
                if (birthStr != string.Empty)
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">生日：</label>" + birthStr + "</div>";
                }

                if (dt.Rows[0]["Province"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">所在地：</label>" + dt.Rows[0]["Province"].ToString() + " " + dt.Rows[0]["City"].ToString() + "</div>";
                }
                if (dt.Rows[0]["HomeProv"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">家乡：</label>" + dt.Rows[0]["HomeProv"].ToString() + " " + dt.Rows[0]["HomeCity"].ToString() + "</div>";
                }
                if (dt.Rows[0]["VocName"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">行业：</label>" + dt.Rows[0]["VocName"].ToString() + "</div>";
                }
                if (dt.Rows[0]["integral"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">积分：</label>" + dt.Rows[0]["integral"].ToString() + "</div>";
                }
                if (dt.Rows[0]["inteyb"].ToString() != "")
                {
                    strInfo += "<div class=\"baseuserinfo\"><label class=\"f-info\">" + JuSNS.Config.UiConfig.gName + "：</label>" + dt.Rows[0]["inteyb"].ToString() + "</div>";
                }
            }
            dt.Clear(); dt.Dispose();
            return strInfo;
        }
        #endregion

        #region 动作
        public string GetDZ()
        {
            string strDZ = string.Empty;
            string quanR = string.Empty;
            if (uID != GetUserID())
            {
                JuSNS.Model.PrivacyInfo info = us.GetPrivacy(uID);
                //if (CheckSpace((int)info.PrivacyLasso))//照片圈圈隐私
                //{
                    strDZ = this.quanR();
                    strDZ += "<li><a href=\"" + rooDir + "/home/mail/to.aspx?userid=" + uID + "\">发送私信</a></li>";
                    strDZ += "<li><a href=\"javascript:\" onclick=\"javascript:Greet('" + uID + "','" + userName + "','" + Photo1 + "');\">打招呼</a></li>";
                    string userXML = "~/user/config/xml/" + uID.ToString() + ".xml";
                    if (JuSNS.Common.Public.getXML(userXML, "isOpenGift", "0", 1) == "1")
                    {
                        strDZ += "<li><a href=\"" + rooDir + "/home/gift?r=" + uID.ToString() + "&uName=" + JuSNS.Common.Input.URLEncode(userName) + "\">给他送礼物</a></li>";
                    }
                    if (IsFriend == 0)
                    {
                        //strDZ += "<li><a href=\"javascript:;\" onclick=\"javascript:AddFrd(" + uID + ",'" + userName + "','" + Photo1 + "','addok(" + uID + ")');\" id=\"add_as_friend\">加为好友</a></li>";
                    }
                    else if (IsFriend == 3)
                    {
                        strDZ += "<li>等待好友验证</li>";
                        isAddFriend = true;
                    }
                    else if (IsFriend == 4)
                    {
                        strDZ += "<li><a href=\"" + rooDir + "/home/friend/req.aspx\">处理好友请求</a></li>";
                    }
                //}
                //else
                //{
                //    strDZ = "该好友已设置隐私！";
                //}
            }
            else
            {
                strDZ = this.quanR() + "<li><a href=\"" + rooDir + "/home/Space/edit.aspx\">编辑我的资料</a></li>";
            }
            return strDZ;
        }
        #endregion

        #region 看别人是不是自己的好友
        protected void IsFriends()
        {
            if (GetUserID() != uID)
            {
                JuSNS.Home.Friend frd = new JuSNS.Home.Friend();
                IsFriend = frd.GetRelation(GetUserID(), uID);
            }
        }
        #endregion
        #region  隐私判断
        public bool CheckSpace(int Privacy)
        {
            bool Privacyflag = false;
            switch (Privacy)
            {
                case 0://全站用户可见
                    Privacyflag = true;
                    break;
                case 1://相同网络用户及好友可见
                    if (IsFriend == 2 || sp.CheckSameNet(this.GetUserID(), uID))
                    {
                        Privacyflag = true;
                    }
                    break;
                case 2://仅好友可见
                    if (IsFriend == 2 || uID == this.GetUserID())
                    {
                        Privacyflag = true;
                    }
                    break;
                case 3://仅自己可见
                    if (uID == this.GetUserID())
                    {
                        Privacyflag = true;
                    }
                    break;
            }
            return Privacyflag;
        }
                #endregion
        #region 读取会员心情日记
        public string GetBlogInfo()
        {
            //if (uID != GetUserID())
            //{
            //    JuSNS.Model.PrivacyInfo info = us.GetPrivacy(uID);
            //    if (!CheckSpace((int)info.PrivacyMiniBlog))//迷你隐私
            //    {
            //        return "该好友已设置隐私！";
            //    }
            //}
            string strBlog = string.Empty;
            DataTable dt = sp.getUserInfo(uID, 2);
            if (dt != null && dt.Rows.Count > 0)
            {
                BlogNum = dt.Rows.Count.ToString();
                dt.Columns.Add("Time", typeof(string));
                int n = dt.Rows.Count;

                if (n > 4) { n = 4; }
                string imgStr = string.Empty;
                string textStr = string.Empty;
                for (int i = 0; i < n; i++)
                {
                    string GetContent = JuSNS.Common.Public.CheckSma(dt.Rows[i]["Content"].ToString());
                    if (i == 0)
                    {
                        LastMini = GetContent + " <a id=\"addcomment\"  href=\"" + rooDir + "/home/mood/?mid=" + dt.Rows[i]["ID"].ToString() + "#re_" + dt.Rows[i]["ID"].ToString() + "\">回复(" + dt.Rows[i]["MCount"].ToString() + ")</a>"; //最新的第一条心情日记
                        continue;
                    }
                    dt.Rows[i]["Time"] = JuSNS.Common.Public.getTimeSpan(Convert.ToDateTime(dt.Rows[i]["PostTime"].ToString()));
                    strBlog += "<li>" + GetContent + " <a id=\"addcomment\"  href=\"" + rooDir + "/home/mood/?mid=" + dt.Rows[i]["ID"].ToString() + "#re_" + dt.Rows[i]["ID"].ToString() + "\">回复(" + dt.Rows[i]["MCount"].ToString() + ")</a>  <span class=\"time\">" + dt.Rows[i]["Time"].ToString() + "</span></li>";
                }
            }
            dt.Clear(); dt.Dispose();
            return strBlog;
        }
        #endregion
        #region 读取会员的好友
        public string GetFriendInfo()
        {
            string str = string.Empty;
            DataTable dt = sp.getUserInfo(uID, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                FriendNum = dt.Rows.Count.ToString();
                int n = dt.Rows.Count;
                if (n > 6)
                {
                    n = 6;
                }

                for (int i = 0; i < n; i++)
                {
                    dt.Rows[i]["FilePath"] = this.GetHeadImage(int.Parse(dt.Rows[i]["UserID"].ToString()));
                    str += "<li><a href=\"" + rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\"><img src=\"" + dt.Rows[i]["FilePath"].ToString() + "\" style=\"width:48px;height:48px;\" alt=\"" + dt.Rows[i]["UserName"].ToString() + "\"/><span>" + dt.Rows[i]["UserName"].ToString() + "</span></a></li>";
                }
            }
            dt.Clear(); dt.Dispose();
            return str;
        }
       #endregion
        #region  圈的照片数
        public string quanR()
        {
            //读取被我圈的照片数
            DataTable nu = sp.getUserInfo(uID, 23);
            string Number = "0";
            if (nu != null)
            {
                if (nu.Rows.Count > 0)
                {
                    Number = nu.Rows[0]["number"].ToString();
                }
            }

            if (uID == this.GetUserID())
            {
                Number = "<li><a href=\"" + rooDir + "/home/album/Quan.aspx?r=" + uID + "\">我被圈的照片(" + Number + ")</a></li>";
            }
            else
            {
                Number = "<li><a href=\"" + rooDir + "/home/album/Quan.aspx?r=" + uID + "\">" + userName + "被圈的照片" + "(" + Number + ")</a></li>";
            }
            return Number;
        }
                #endregion
    }
}
