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

namespace JuSNS.Web.home.ibase
{
	public partial class network : JuSNS.UI.Page.UserPage
	{
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        JuSNS.Home.MyInfo ac = new JuSNS.Home.MyInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInfo(homeprovince, 0, false);
            try
            {
                string type = Request.Form["Type"];
                if (type == "City")
                {
                    LoadInfo(SelCity, 1, true);
                }
                else if (type == "Edit")
                {
                    Submit();
                }
            }
            catch { }

            getUserInfo();
        }


        //初始化基本信息
        public void getUserInfo()
        {
            DataTable dt = sp.getUserInfo(this.UserID, 0);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    getSelect(homeprovince, dt.Rows[0]["Pro"].ToString());
                    LoadInfo(SelCity, 1, false);
                    getSelect(SelCity, dt.Rows[0]["citys"].ToString());
                }
            }
        }

        public void getSelect(HtmlSelect select, string str)
        {
            for (int i = 0; i < select.Items.Count; i++)
            {
                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(1);
            }
            else if (i == 1)//加载市
            {
                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()
        {
            int HomeProv = int.Parse(Request.Form["homeprovince"]);
            int HomeCity = int.Parse(Request.Form["SelCity"]);

            if (ac.UpdateNetSet(HomeProv, HomeCity, this.UserID) == 1)
            {
                Response.Write("设置成功!");
                Response.End();
            }
        }
	}
}
