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

namespace JuSNS.Web.home.space
{
    public partial class edit : JuSNS.UI.Page.UserPage
    {
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        public static string GetTiShiDivStr = string.Empty;
        public bool isFirst = false;
        public string param = string.Empty;
        public string isMarr = string.Empty;
        public string path = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getUserInfo();
                GetMyState();
                path = this.GetHeadPath(this.UserID);
                string isHeadPic = this.GetHeadPath(this.UserID);
                string f = Request.QueryString["f"];
                if (f == "1")
                {
                    isFirst = true;
                    param = "?f=1";
                    GetTiShiDivStr = "<div class=\"msg-warn\">您是第一次登陆本系统，请您完善基本资料。以方便未来的朋友认识您！特别是您的<a href=\""+rooDir+"/home/space/userHeadPhoto.aspx\">头像</a>！</div>";
                }
                else
                {
                    if (!string.IsNullOrEmpty(isHeadPic))
                    {
                        isFirst = false;
                        param = string.Empty;
                        GetTiShiDivStr = string.Empty;
                    }
                    else
                    {
                        isFirst = true;
                        GetTiShiDivStr = "<div class=\"msg-warn\">您还没上传头像！为方便朋友找到您！请及时<a href=\"" + rooDir + "/home/space/userHeadPhoto.aspx\">上传头像</a>，也可以使用<a href=\"" + rooDir + "/home/space/UserHead-Camera.aspx?r=" + this.UserID + "\">摄像头拍摄头像</a>。</div>";
                    }
                }

            }

            try
            {
                string type = Request.Form["Type"];
                string id = Request.Form["id"];

                if (type == "City")
                {
                    LoadInfo(SelCity, 3, true);
                }
                else if (type == "Edit")
                {
                    Submit();
                }
            }
            catch { }
        }

        public void GetMyState()
        {
            string listSTR = string.Empty;
            UserInfo mdl = new UserInfo();
            User us = new User();
            mdl = us.GetUserInfo(this.UserID);
            int Stat = (int)mdl.State;
            switch (Stat)
            {
                case 0:
                    listSTR = "<strong class=\"reshow\">帐号状态：</strong>注册未验证邮件";
                    break;
                case 2:
                    listSTR = "<strong class=\"reshow\">帐号状态：</strong>状态正常！但未验证邮件。<a href=\"../../EmailActive.aspx?email=" + mdl.Email + "&r="+this.UserID+"\">发送验证进行电子邮件验证。</a>";
                    break;
                case 5:
                    listSTR = "<strong class=\"reshow\">帐号状态：</strong>已验证电子邮件(" + mdl.Email + ")。状态正常。";
                    break;
            }
            this.GetState.InnerHtml = listSTR;
        }

        //初始化基本信息
        public void getUserInfo()
        {
            DataTable dt = sp.getUserInfo(this.UserID, 0);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (JuSNS.Common.Input.IsDate(dt.Rows[0]["birthday"].ToString()))
                {
                    this.birth.Value = DateTime.Parse(dt.Rows[0]["birthday"].ToString()).ToShortDateString();
                }
                getSelect(gender, dt.Rows[0]["Sex"].ToString(), true);//性别
                getSelect(birthPrivacy, dt.Rows[0]["BirthidayDisplay"].ToString(), false);
                isMarr = dt.Rows[0]["Marriage"].ToString();

                LoadInfo(zodiac, 2, false);//加载星座
                getSelect(zodiac, dt.Rows[0]["Constellation"].ToString(), true);

                LoadInfo(homeprovince, 1, false);
                getSelect(homeprovince, dt.Rows[0]["HomeProv"].ToString(), true);

                LoadInfo(SelCity, 3, false);//加载城市
                getSelect(SelCity, dt.Rows[0]["HomeCity"].ToString(), true);

                LoadInfo(vocation, 0, false);//加载行业
                getSelect(vocation, dt.Rows[0]["VocName"].ToString(), true);
            }
        }

        public void getSelect(HtmlSelect select, string str, bool bl)
        {
            for (int i = 0; i < select.Items.Count; i++)
            {
                if (bl)
                {
                    if (str == select.Items[i].Text)
                    {
                        select.Value = select.Items[i].Value;
                        break;
                    }
                }
                else
                {
                    if (str == select.Items[i].Value)
                    {
                        select.Value = select.Items[i].Value;
                        break;
                    }
                }
            }
        }

        public void LoadInfo(HtmlSelect select, int i, bool bl)
        {
            DataTable dt = null;
            if (i == 0)//加载行业
            {
                dt = sp.getConstellation(2);
            }
            else if (i == 1)//加载省
            {
                dt = sp.getConstellation(1);
            }
            else if (i == 2)//加载星座
            {
                dt = sp.getConstellation(0);
            }
            else if (i == 3)//加载市
            {
                int id;
                try
                {
                    id = int.Parse(Request.Form["id"]);
                }
                catch
                {
                    id = int.Parse(this.homeprovince.Value);
                }
                dt = sp.getUserInfo(id, 14);
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string str = "";
                    if (bl)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            str += dt.Rows[j]["ID"].ToString() + "," + dt.Rows[j]["Name"].ToString() + "|";
                        }
                        Response.Write(str);
                        Response.End();
                    }
                    else
                    {
                        select.DataSource = dt;
                        select.DataTextField = "Name";
                        select.DataValueField = "ID";
                        select.DataBind();
                        select.SelectedIndex = 0;
                    }
                }
            }
        }


        //更新会员信息
        public void Submit()
        {
            UserBasicInfo uf = new UserBasicInfo();
            uf.UserID = this.UserID;
            uf.Sex = int.Parse(Request.Form["sex"]);
            if (!JuSNS.Common.Input.IsDate(Request.Form["birth"]))
            {
                Response.Write("3");
                Response.End();
            }
            else
            { 
                DateTime gTime=DateTime.Parse(Request.Form["birth"]);
                int n =gTime.Year;
                int n1=DateTime.Now.Year;
                int rn=n1-n;
                if (rn < 10)
                {
                    Response.Write("4");
                    Response.End();
                }
            }
            uf.Birthday = Convert.ToDateTime(Request.Form["birth"]);
            uf.BirthidayDisplay = int.Parse(Request.Form["birthPrivacy"]);
            uf.Constellation = int.Parse(Request.Form["zodiac"]);
            uf.HomeProv = int.Parse(Request.Form["homeprovince"]);
            uf.HomeCity = int.Parse(Request.Form["SelCity"]);
            uf.Vocation = int.Parse(Request.Form["vocation"]);
            uf.Marriage = int.Parse(Request.Form["Marriage"]);
            if (sp.UpdateUserInfo(uf, this.UserID, 0) == 1)
            {
                if (!string.IsNullOrEmpty(path))
                {
                    Response.Write("1");
                    Response.End();
                }
                else
                {
                    Response.Write("2");
                    Response.End();
                }
            }
        }
    }
}
