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;
using JuSNS.Home;
using JuSNS.UI.Control;
using JuSNS.Common;

namespace JuSNS.Web.home.space
{
    public partial class search : JuSNS.UI.Page.UserPage
    {
        string strF="";//标识是什么类型的爱好 
        public string strValue = "";//爱好对应的值
        public string Count = "";
        public int IsFriend = 0;
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        JuSNS.Home.Friend frd = new Friend();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                strF = Request.QueryString["r[0]"];
                strValue = Request.QueryString["r[1]"];
                strValue = JuSNS.Common.Input.URLDecode(strValue);
                LoadList();
            }
            catch { }
        }

        #region 加载分页
        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            int r=0, p=0, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = null;
            SqlConditionInfo[] st = null;

            if (strF== "SchoolID")
            {
                st = new SqlConditionInfo[]{
                   new SqlConditionInfo("@where",int.Parse(strValue) ,TypeCode.Int32)
                };
            }
            else if (strF == "AcademyID")
            {
                string[] strName = strValue.Split('|');
                st = new SqlConditionInfo[]{
                    new SqlConditionInfo("@SchoolID", int.Parse(strName[0].ToString()), TypeCode.Int32),
                    new SqlConditionInfo("@AcademyID", int.Parse(strName[1].ToString()), TypeCode.Int32)
                };
            }
            else
            {
                st = new SqlConditionInfo[]{
                   new SqlConditionInfo("@where","%"+strValue+"%" ,TypeCode.String)
                };
            }

            switch (strF)
            {
                case "Favourite"://兴趣爱好
                    dt = JuSNS.Home.UtilPage.GetPage("Search_Favourite_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavMusics"://喜欢的音乐
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavMusics_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavMovies"://喜欢的电影
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavMovies_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavCartoons"://喜欢的动漫
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavCartoons_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavGames"://喜欢的游戏
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavGames_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavSports"://喜欢的运动
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavSports_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "FavBooks"://喜欢的书
                    dt = JuSNS.Home.UtilPage.GetPage("Search_FavBooks_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "AppreciateMen"://欣赏的人
                    dt = JuSNS.Home.UtilPage.GetPage("Search_AppreciateMen_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "Company"://同一公司的人
                    dt = JuSNS.Home.UtilPage.GetPage("Search_Company_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "SchoolID"://同一学校的人
                    dt = JuSNS.Home.UtilPage.GetPage("Search_SchoolID_aspx", PageIndex, 20, out r, out p, st);
                    break;
                case "AcademyID"://同一学校同院系的人
                    dt = JuSNS.Home.UtilPage.GetPage("Search_AcademyID_aspx", PageIndex, 20, out r, out p, st);
                    break;

            }

            if (strF != "Company")
            {  
                dt.Columns.Add("Company", typeof(string));
            }
            dt.Columns.Add("Career", typeof(string));
            dt.Columns.Add("Time", typeof(string));            
            dt.Columns.Add("ShoolName", typeof(string));//学校名称
            dt.Columns.Add("JuniorHigh", typeof(string));//教育程度
            dt.Columns.Add("miniTime", typeof(string));//心情日志时间
            dt.Columns.Add("minTitle", typeof(string));//心情日志标题
            dt.Columns.Add("minContent", typeof(string));//心情日志内容
            dt.Columns.Add("rightTitle", typeof(string));//提示信息

            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable dts = null;
                DataTable dtt = null;
                string SchoolName = "";
                string AcademyName = "";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Photo"] = this.GetHeadImage(Convert.ToInt32(dt.Rows[i]["UserID"]), 2);

                    #region 公司信息

                    if (strF != "Company")
                    {
                        dtt = sp.getUserInfo(int.Parse(dt.Rows[i]["UserID"].ToString()), 9);
                    }
                    else
                    {
                        dtt = dt;
                    }

                    if (dtt != null && dtt.Rows.Count > 0)
                    {
                        dt.Rows[i]["Career"] = "公司：";

                        if (dtt.Rows[0]["JoinYear"].ToString() == "" || dtt.Rows[0]["JoinMonth"].ToString() == "" || dtt.Rows[0]["JoinYear"].ToString() == "0" || dtt.Rows[0]["JoinMonth"].ToString() == "0")
                        {
                            dt.Rows[i]["Time"] = "";
                        }
                        else
                        {
                            if (dtt.Rows[0]["LeaveYear"].ToString() == "" || dtt.Rows[0]["LeaveMonth"].ToString() == "" || dtt.Rows[0]["LeaveYear"].ToString() == "0" || dtt.Rows[0]["LeaveMonth"].ToString() == "0")
                            {
                                dt.Rows[i]["Time"] = "(" + dtt.Rows[0]["JoinYear"].ToString() + "-" + dtt.Rows[0]["JoinMonth"].ToString() + "  -  现在)";
                            }
                            else
                            {
                                dt.Rows[i]["Time"] = "(" + dtt.Rows[0]["JoinYear"].ToString() + "-" + dtt.Rows[0]["JoinMonth"].ToString() + "  -  " + dtt.Rows[0]["LeaveYear"].ToString() + "-" + dtt.Rows[0]["LeaveMonth"].ToString() + ")";
                            }
                        }
                        dt.Rows[i]["Company"] = dtt.Rows[0]["Company"].ToString();
                        SchoolName = dtt.Rows[0]["Company"].ToString();
                    }
                    #endregion

                    #region 大学信息
                    dts = sp.getUserInfo(int.Parse(dt.Rows[i]["UserID"].ToString()), 10);

                    if (dts != null && dts.Rows.Count > 0)
                    {
                        int DegreeID = 0;
                        string Year = "";

                        DegreeID = int.Parse(dts.Rows[0]["DegreeID"].ToString());

                        if (dts.Rows[0]["MatriculationYear"].ToString() != "0")
                        {
                            Year = "<span class=\"time\">(" + dts.Rows[0]["MatriculationYear"].ToString() + " 年入学)</span>";
                        }

                        if (DegreeID == (int)EnumStudyGrade.JuniorHigh)
                        {//初中
                            dt.Rows[i]["JuniorHigh"] = "初中：";
                        }
                        else if (DegreeID == (int)EnumStudyGrade.SeniorHigh)
                        {//高中
                            dt.Rows[i]["JuniorHigh"] = "高中：";
                        }
                        else if (DegreeID == (int)EnumStudyGrade.College)
                        {//大学
                            dt.Rows[i]["JuniorHigh"] = "大学：";
                        }
                        else if (DegreeID == (int)EnumStudyGrade.Master)
                        {//研究生
                            dt.Rows[i]["JuniorHigh"] = "研究生：";
                        }
                        else if (DegreeID == (int)EnumStudyGrade.Doctor)
                        {//博士
                            dt.Rows[i]["JuniorHigh"] = "博士：";
                        }
                        dt.Rows[i]["ShoolName"] = dts.Rows[0]["SchoolName"].ToString() + " - " + dts.Rows[0]["Name"].ToString() + Year + "<br/>";
                    }
                    #endregion

                    #region 心情日志
                    DataTable dtm = sp.getUserInfo(int.Parse(dt.Rows[i]["UserID"].ToString()), 2);
                    if (dtm != null && dtm.Rows.Count > 0)
                    {
                        dt.Rows[i]["minTitle"] = "心情：";
                        dt.Rows[i]["miniTime"] = JuSNS.Common.Public.getTimeSpan(Convert.ToDateTime(dtm.Rows[0]["PostTime"].ToString()));
                        dt.Rows[i]["minContent"] = JuSNS.Common.Public.CheckSma(dtm.Rows[0]["Content"].ToString());
                    }
                    #endregion

                    #region 提示信息
                    AddFriend(int.Parse(dt.Rows[i]["UserID"].ToString()));
                    if (int.Parse(dt.Rows[i]["UserID"].ToString()) == UserID)
                    {
                        dt.Rows[i]["rightTitle"] = " <a href=\"../friend/?r=" + dt.Rows[i]["UserID"].ToString() + "\">查看好友</a>";
                    }
                    else
                    {
                        dt.Rows[i]["rightTitle"] += " <a href=\"javascript:void(0);\" onclick=\"mailto('" + dt.Rows[i]["UserID"].ToString() + "','')\">发送私信</a>";
                        dt.Rows[i]["rightTitle"] += " <a href=\"javascript:Greet(" + dt.Rows[i]["UserID"].ToString() + ",'" + dt.Rows[i]["UserName"].ToString() + "','" + dt.Rows[i]["Photo"].ToString() + "');\">打招呼</a>";
                        dt.Rows[i]["rightTitle"] += " <a href=\"../friend/?r=" + dt.Rows[i]["UserID"].ToString() + "\">查看好友</a>";

                        if (IsFriend == 0)
                        {
                            dt.Rows[i]["rightTitle"] += " <a href=\"javascript:AddFrd(" + dt.Rows[i]["UserID"].ToString() + ",'" + dt.Rows[i]["UserName"].ToString() + "','" + dt.Rows[i]["Photo"].ToString() + "','addok(" + dt.Rows[i]["UserID"].ToString() + ")');\" class=\"complexlink poke-send\">加为好友</a>";
                        }
                        else if (IsFriend == 3)//
                        {
                            dt.Rows[i]["rightTitle"] += " 等待好友验证";
                        }
                        else if (IsFriend == 4)
                        {
                            dt.Rows[i]["rightTitle"] += " <a href=\"../friend/req.aspx\">处理好友请求</a>";
                        }
                    }
                    #endregion
                }

                if (strF == "Company" || strF == "SchoolID")
                {
                    Count = "找到 " + dt.Rows.Count.ToString() + " 个人在 <strong>" + SchoolName + "</strong>";
                }
                else if (strF == "AcademyID")
                {
                    Count = "找到 " + dt.Rows.Count.ToString() + " 个人在 <strong>" + SchoolName + " " + AcademyName + "</strong>";
                }
                else
                {
                    Count = dt.Rows.Count.ToString() + " 个人喜欢 <strong>" + strValue + "</strong>";
                }

                RptData.DataSource = dt;
                RptData.DataBind();
            }
            //判断是否调用分页
            if (r > 20)
            {
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "Search.aspx?r[0]=" + strF + "&r[1]="+strValue);
            }
        }
        #endregion

        #region 看别人是不是自己的好友
        protected void AddFriend(int myid)
        {
            if (UserID != myid)
            {
                IsFriend = frd.GetRelation(UserID, myid);
            }
        }
        #endregion   
    }
}
