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

namespace JuSNS.Web.home.search
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        protected string province = string.Empty;
        protected string cityarray = string.Empty;
        protected string title = string.Empty;
        public int gUser = 0;
        protected string Pr = null;
        protected string City = null;
        protected string beginYear = null;
        protected string YearEnd = null;
        protected string Sex = null;
        protected string IsTx = null;
        protected string Key = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            gUser = GetCUserID();
            string gGuestSearch = JuSNS.Common.Public.GetXMLValue("GuestSearch");
            if (gGuestSearch == "0" && gUser == 0)
            {
                PageError("会员才能搜索！", "javascript:history.back()",true);
            }
            #region 获取参数值
            if (!IsPostBack)
            {
                try
                {
                    Pr = Request.QueryString["pr"];
                    City = Request.QueryString["City"];
                    beginYear = Request.QueryString["Begin"];
                    YearEnd = Request.QueryString["End"];
                    Sex = Request.QueryString["Sex"];
                    IsTx = Request.QueryString["TX"];
                    Key = Request.QueryString["namekey"];
                }
                catch
                {
                    Pr = "0";
                    City = "0";
                    beginYear = "0";
                    YearEnd = "0";
                    Sex = "2";
                    IsTx = "0";
                    Key = string.Empty;
                }

                if (string.IsNullOrEmpty(Pr))
                {
                    Pr = "0";
                }
                if (string.IsNullOrEmpty(City))
                {
                    City = "0";
                }
                if (string.IsNullOrEmpty(beginYear))
                {
                    beginYear = "0";
                }
                if (string.IsNullOrEmpty(YearEnd))
                {
                    YearEnd = "0";
                }
                if (string.IsNullOrEmpty(Sex))
                {
                    Sex = "2";
                }
                if (string.IsNullOrEmpty(IsTx))
                {
                    IsTx = "0";
                }
            }
            #endregion

            #region 加载省市
            if (!IsPostBack)
            {
                Dict dict = new Dict();
                IList<DictAreaInfo> list = dict.GetArea();
                foreach (DictAreaInfo info in list)
                {
                    if (info.ParentID.Equals(0))
                    {
                        if (int.Parse(Pr) == info.ID)
                        {
                            province += "<option value=\"" + info.ID + "\" selected=\"selected\">" + info.Name + "</option>";
                        }
                        else
                        {
                            province += "<option value=\"" + info.ID + "\">" + info.Name + "</option>";
                        }
                    }
                    else
                    {
                        if (!cityarray.Equals(string.Empty))
                            cityarray += ",";
                        cityarray += "new Array('" + info.ParentID + "','" + info.ID + "','" + info.Name + "')";
                    }
                } DataBind();
            }
            #endregion

            #region 初始化选择项
            this.txtCity.Value = City;
            this.txtBegin.Value = beginYear;
            this.txtEnd.Value = YearEnd;
            this.txtSex.Value = Sex;
            if (IsTx == "1")
            {
                this.CheTX.Checked = true;
            }
            #endregion

            getUser();
        }

        public void getUser()
        {
            if (int.Parse(YearEnd) > 0)
            {
                if (int.Parse(beginYear) > int.Parse(YearEnd))
                {
                    title = "年龄范围无效,请重新选择！";
                    return;
                }
            }
            int r = 0, p = 0, PageIndex = 0, pagsize = 30;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = null;
            if (!string.IsNullOrEmpty(Key))
            {
                SqlConditionInfo st = new SqlConditionInfo("@key", Key, TypeCode.String);
                st.Blur = 3;
                dt = UtilPage.GetPage("Search_Name_User", PageIndex, 30, out r, out p, st);
            }
            else
            {
                dt = UtilPage.GetUserPage(int.Parse(Pr), int.Parse(City), int.Parse(beginYear), int.Parse(YearEnd), int.Parse(Sex), int.Parse(IsTx), PageIndex, 30, out r, out p, null);
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    JuSNS.UI.Page.BasePage bp = new JuSNS.UI.Page.BasePage();
                    dt.Columns.Add("brith", typeof(string));

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

                        #region 生日显示判断
                        if (dt.Rows[i]["Birthday"].ToString() != null && dt.Rows[i]["Birthday"].ToString() != string.Empty && dt.Rows[i]["BirthidayDisplay"].ToString() != "3")
                        {
                            string[] brith = dt.Rows[i]["Birthday"].ToString().Split('-');

                            if (brith.Length == 3)
                            {
                                string year = brith[0].ToString();
                                if (year != "1900" && year != "1901" && year != "1902" && year != "1903" && year != "1905")
                                {
                                    if (dt.Rows[i]["BirthidayDisplay"].ToString() == "0" || dt.Rows[i]["BirthidayDisplay"].ToString() == "2")//显示年月日
                                    {
                                        birthday = int.Parse(DateTime.Now.Year.ToString()) - int.Parse(year);
                                        if (birthday < 60 && birthday > 12)
                                        {
                                            dt.Rows[i]["brith"] = birthday + " 岁";
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        #region 显示爱好
                        string mystr = "";
                        if (dt.Rows[i]["Favourite"].ToString() != null && dt.Rows[i]["Favourite"].ToString() != string.Empty)
                        {
                            string[] str = dt.Rows[i]["Favourite"].ToString().Split(',');

                            for (int k = 0; k < str.Length; k++)
                            {
                                if (str[k].ToString().Trim() != "")
                                {
                                    mystr += str[k].ToString() + "  ";
                                }
                            }

                            if (mystr.Length > 15) { mystr = mystr.Substring(0, 15); }
                            dt.Rows[i]["Favourite"] = "爱好:" + mystr;
                        }
                        #endregion
                    }
                    RptData.DataSource = dt;
                    RptData.DataBind();
                }
                else
                {
                    title = "没有您要找的朋友！";
                }
            }
            else
            {
                title = "没有您要找的朋友！";
            }
            //判断是否调用分页
            if (pagsize > 30)
            {
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "Search.aspx?namekey="+Key+"&Pr=" + Pr + "&City=" + City + "&Begin=" + beginYear + "&End=" + YearEnd + "&Sex=" + Sex + "&TX=" + IsTx);
            }
            else
            {
                DivPage.InnerHtml = string.Empty;
            }
        }

        protected string AddFriend(object uid, object unm)
        {
            string ret = string.Empty;
            string upic = GetHeadImage(Convert.ToInt32(uid.ToString()), 2);
            if (isTimeOut())
            {
                ret = "<div id=\"li_req_" + uid.ToString() + "\"><a href=\"javascript:void(0);\" onclick=\"javascript:AddFriend(" + uid.ToString() + ",'" + unm.ToString() + "','" + upic.ToString() + "','li_req_" + uid.ToString() + "');\">加为好友</a></div>";
            }
            else
            {
            int usid = Convert.ToInt32(uid);
            Friend frd = new Friend();
            if (usid != this.GetUserID())
            {
                int n = frd.GetRelation(this.GetUserID(), usid);
                if (n == 0)//无任何关系
                {
                    ret = "<div id=\"li_req_" + usid + "\"><a href=\"javascript:void(0);\" onclick=\"javascript:AddFriend(" + usid + ",'" + unm + "','" + upic + "','li_req_" + uid + "');\">加为好友</a></div>";
                }
                else if (n == 3)//已发出好友申请,等待确认好友申请
                {
                    ret = "<div>等待验证</div>";
                }
                else if (n == 4)//4被申请
                {
                    ret = "<div><a href=\"../friend/req.aspx\">处理请求</a></div>";
                }
            }
                
            }
            return ret;
        }
    }
}
