using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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;
using JuSNS.Home;
using JuSNS.UI.Control;
using JuSNS.Common;

namespace JuSNS.Web.home.friend
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        JuSNS.Home.Friend frd;
        //显示全部
        protected bool bsel = true;
        protected int myid;
        protected string schname = string.Empty;
        protected string appt = "我";
        protected string userName = "我的";
        protected void Page_Load(object sender, EventArgs e)
        {
            frd = new Friend();
            string r = Request.QueryString["r"];
            if (!string.IsNullOrEmpty(r))
            {
                myid = int.Parse(r);
                if (!this.PrivacyAvailable(EnumPrivacyType.Friends, GetUserID(), myid))
                {
                    PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                }
                JuSNS.Home.User us = new JuSNS.Home.User();
                UserInfo info = us.GetUserInfo(myid);
                if (myid != GetUserID())
                {
                    UserInfo mdl = new UserInfo();
                    mdl = us.GetUserInfo(myid);
                    userName = mdl.Username;
                }
            }
            else
            {
                if (this.GetUserID() == 0)
                {
                    if (string.IsNullOrEmpty(r))
                    {
                        PageError("请登录后查看。", "javascript:history.back();");
                    }
                }
                myid = GetUserID();
            }
            GetFriendClass();
            BindData();
            DataBind();
        }

        /// <summary>
        /// 好友分类
        /// </summary>
        protected void GetFriendClass()
        {
            string c = Request.QueryString["c"];
            string FStr = "<select ID=\"fClass\" onchange=\"getFValue(this);\">";
            string ListStr = string.Empty;
            FStr += "<option value=\"\">选择好友分类...</option>";
            if (c == "0")
            {
                FStr += "<option value=\"0\" selected>未分组(" + frd.GetUnClassFriend(GetUserID()) + ")</option>";
            }
            else
            {
                FStr += "<option value=\"0\">未分组(" + frd.GetUnClassFriend(GetUserID()) + ")</option>";
            }
            IDataReader dr = frd.GetFriendClass(GetUserID());
            while (dr.Read())
            {
                string count = dr["FCount"].ToString();
                string ID = dr["ID"].ToString();
                string CName = dr["CName"].ToString();
                if (c == ID)
                {
                    ListStr += "<li class=\"current\" id=\"li_list_" + ID + "\" onmouseover=\"showEdit('" + ID + "',0)\" onmouseout=\"showEdit('" + ID + "',1)\"><a href=\"/home/friend/?c=" + ID + "\">" + CName + "(" + count + ") &nbsp; <span style=\"display:none;\" id=\"edit_" + ID + "\" onclick=\"aedit('" + ID + "','" + CName + "');return false;\" class=\"c999\">编辑</span></a></li>\r\n";
                    FStr += "<option value=\"" + ID + "\" selected>" + CName + "(" + count  + ")</option>\r\n";
                }
                else
                {
                    FStr += "<option value=\"" + ID + "\">" + CName + "(" + count + ")</option>\r\n";
                    ListStr += "<li onmouseover=\"showEdit('" + ID + "',0)\" id=\"li_list_" + ID + "\"  onmouseout=\"showEdit('" + ID + "',1)\"><a href=\"/home/friend/?c=" + ID + "\">" + CName + "(" + count + ") &nbsp; <span style=\"display:none;\" id=\"edit_" + ID + "\" onclick=\"aedit('" + ID + "','" + CName + "');return false;\" class=\"c999\">编辑</span></a></li>\r\n";
                }
            }
            FStr += "</select>";
            dr.Close();
            string c0 = string.Empty;
            if (c == string.Empty || c == null)
            {
                c0 = "<li class=\"current\"><a href=\"../friend\">所有好友</a></li>";
            }
            else
            {
                c0 = "<li><a href=\"../friend\">所有好友</a></li>";
            }
            string c1 = string.Empty;
            if (c == "0")
            {
                c1 = "<li class=\"current\"><a href=\"../friend?c=0\">未分组(" + frd.GetUnClassFriend(GetUserID()) + ")</a></li>";
            }
            else
            {
                c1 = "<li><a href=\"../friend?c=0\">未分组(" + frd.GetUnClassFriend(GetUserID()) + ")</a></li>";
            }
            this.friendclass.InnerHtml = FStr;
            this.FriendcName.InnerHtml = c0 +c1+ ListStr + "<li><a href=\"../friend/group.aspx\" class=\"c999\">管理分组</a></li>";
        }

        /// <summary>
        /// 显示好友统计信息
        /// </summary>
        private void DisStat(Dictionary<NetworkInfo, int> st)
        {
            #region 处理网络统计
            if (st != null)
            {
                string n = Request.QueryString["n"];
                string s = string.Empty;
                s += "<li><a href=\"../friend?r=" + myid + "\">所有好友</a></li>";
                foreach (KeyValuePair<NetworkInfo, int> p in st)
                {
                    s += "<li";
                    if (Request.QueryString["a"] == "network" && n == p.Key.ID.ToString())
                    {
                        bsel = false;
                        s += " class=\"current\"";
                    }
                    s += "><a href=\"../friend/?r=" + myid + "&a=network&n=" + (int)p.Key.ID + "\">" + p.Key.Name + " (" + p.Value + ")</a></li>";
                }
                this.UlStatNet.InnerHtml = s;
            }
            #endregion
        }
        /// <summary>
        /// 分页绑定数据
        /// </summary>
        protected void BindData()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            string c = Request.QueryString["c"];
            string cSTR = "#0";
            if (JuSNS.Common.Input.IsInteger(c))
            {
                if (c == "0")
                {
                    cSTR = "#1";
                }
                else
                {
                    cSTR = c;
                }
            }
            string PageUrl = "../friend/";
            IList<FriendInfo> list = null;
            Dictionary<NetworkInfo, int> stat = null;
            if (Request.QueryString["a"] != null && Request.QueryString["a"].ToLower() == "network" && Request.QueryString["n"] != null)
            {
                list = frd.GetList(PageIndex, 20, out ReCount, out PgCount, myid, int.Parse(Request.QueryString["n"]), null, cSTR, out stat);
                PageUrl += "?a=network&n=" + Request.QueryString["n"];
            }
            else if (Request.QueryString["a"] != null && Request.QueryString["a"].ToLower() == "search" && Request.QueryString["n"] != null)
            {
                schname = Request.QueryString["n"];
                list = frd.GetList(PageIndex, 20, out ReCount, out PgCount, myid, 0, schname, cSTR, out stat);
                PageUrl += "?a=search&n=" + schname;
            }
            else
            {
                list = frd.GetList(PageIndex, 20, out ReCount, out PgCount, myid, 0, null, cSTR, out stat);
            }
            this.DisStat(stat);
            this.RptData.DataSource = list;
            this.RptData.DataBind();
            this.LtrCount.Text = ReCount.ToString();
            if (ReCount > 20)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }

        /// <summary>
        /// 取得心情日记信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string ShowLastMiniBlog(object con, object tm)
        {
            if (con != null && con.ToString().Trim() != string.Empty)
            {
                return Public.CheckSma(Input.FilterHTML(con.ToString())) + " <span class=\"time\">" + Public.getTimeSpan((DateTime)tm) + "</span>";
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 好友度
        /// </summary>
        /// <param name="fdr"></param>
        /// <returns></returns>
        protected string ShowFDegree(object fdr)
        {
            string slist = string.Empty;
            if (JuSNS.Common.Input.IsInteger(fdr.ToString()))
            {
                slist = JuSNS.Common.Public.ShowDfe(Convert.ToInt32(fdr.ToString()));
            }
            else
            {
                slist =JuSNS.Common.Public.ShowDfe(0);
            }
            return slist + "(" + fdr.ToString() + ")";
        }
    }
}
