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.UI.Control;

namespace JuSNS.Web.system.user
{
    public partial class UserEdit : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "user";
        JuSNS.Home.User us = new JuSNS.Home.User();
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtUserID.Value = Request.QueryString["r"];

            try
            {
                string type = Request.Form["Type"];
                if (type == "City")
                {
                    LoadInfo(this.DrpCity, 2, true);
                }
                else
                {
                    if (!Page.IsPostBack)
                    {
                        getInfo();
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void getInfo()
        {
            string id = Request.QueryString["r"];
            if (string.IsNullOrEmpty(id))
                PageError("参数传递错误!", "index.aspx");

            JuSNS.Model.UserInfo info = us.GetUserInfo(int.Parse(id));

            LoadInfo(this.DrpProvince, 1, false);// 绑定省
            this.DrpProvince.Value = info.Province.ToString();
            LoadInfo(this.DrpCity, 2, false);//绑定市

            this.txtEmail.Text = info.Email;
            this.txtName.Text = info.Username;
            this.DrpCity.Value = info.City.ToString();
            this.integral.Text = info.integral.ToString();
            this.inteyb.Text = info.inteyb.ToString();
            this.Click.Text = info.Click.ToString();
            this.spanLoginNumber.InnerHtml = info.LoginTimes.ToString();
            this.spanLastLoginTime.InnerHtml = info.LastLoginTime.ToString();
            this.spanLastLoginIP.InnerHtml = info.LastLoginIp;
            this.spanRegTime.InnerHtml = info.RegTime.ToString();
            this.spanRegIP.InnerHtml = info.RegIP;
            this.txthidPwd.Value = info.Password;
            this.Portrait.Value = info.Portrait.ToString();
            int ivID =info.InviterID;
            if (ivID > 0)
            {
                this.InviterID.InnerHtml = "<a href=\"../../user?r=" + info.InviterID + "\" target=\"_blank\">" + us.GetUserName(info.InviterID) + "</a>";
            }
            else
            {
                this.InviterID.InnerHtml = "自行注册";
            }
            switch (info.State)
            {
                case EnumUserState.Register:
                    this.radState.SelectedValue = "0";
                    break;
                case EnumUserState.Lock:
                    this.radState.SelectedValue = "1";
                    break;
                case EnumUserState.NormalNotEmail:
                    this.radState.SelectedValue = "2";
                    break;
                case EnumUserState.Normal:
                    this.radState.SelectedValue = "5";
                    break;
            }
        }

        public void LoadInfo(HtmlSelect select, int i, bool bl)
        {
            DataTable dt = null;
            if (i == 1)//加载省
            {
                dt = sp.getConstellation(1);
            }
            else if (i == 2)//加载市
            {
                int id;
                try
                {
                    id = int.Parse(Request.Form["id"]);
                }
                catch
                {
                    id = int.Parse(this.DrpProvince.Value);
                }
                dt = sp.getUserInfo(id, 14);
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    if (bl)
                    {
                        string str = "";
                        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;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            UserInfo uf = new UserInfo();

            uf.Email = this.txtEmail.Text;
            uf.Username = this.txtName.Text;
            uf.State = (EnumUserState)int.Parse(this.radState.SelectedValue);
            uf.Province = int.Parse(Request.Form["DrpProvince"]);
            uf.City = int.Parse(Request.Form["DrpCity"]);
            uf.Portrait = Convert.ToInt32(this.Portrait.Value);
            if (this.txtPwd.Text=="")
            {
                uf.Password = this.txthidPwd.Value;
            }
            else
            {
                uf.Password = JuSNS.Common.Input.MD5(this.txtPwd.Text.Trim(), false);
            }
            int ub = us.UpdateBer(int.Parse(this.txtUserID.Value), Convert.ToInt32(this.integral.Text), Convert.ToInt32(this.inteyb.Text), Convert.ToInt32(this.Click.Text));
            int uu = us.UpdateUser(uf, int.Parse(this.txtUserID.Value));
            if (ub > 0 && uu > 0)
            {
                Response.Redirect("userEdit.aspx?r=" + this.txtUserID.Value + "");
            }
            else
            {
                PageError("修改会员信息失败!", "index.aspx");
            }
        }
    }
}
