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;

namespace JuSNS.Web.user.Space
{
    public partial class ChooseShool : JuSNS.UI.Page.UserPage
    {
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        public int myid;
        public string DegreeID = "";
        public string content = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["r"] != null && Request.QueryString["r"].Trim() != string.Empty)
                    myid = int.Parse(Request.QueryString["r"]);
                else
                    myid = this.UserID;

                try
                {
                    this.txtDegreeID.Value = Request.QueryString["DegreeID"];
                    this.txtFlag.Value = Request.QueryString["flag"];

                    if (this.txtDegreeID.Value == "1" || this.txtDegreeID.Value == "2")
                    {
                        LoadPr(1);
                    }
                    else
                    {
                        LoadPr(2);
                    }
                }
                catch { }

                try
                {
                    string type = Request.Form["Type"];
                    if (type == "Shool")
                    {
                        LoadShool(true);
                    }
                    else if (type == "City")
                    {
                        if (Request.Form["bl"] == "true")
                        {
                            LoadCity(int.Parse(Request.Form["AreaID"]), true);
                        }
                        else
                        {
                            LoadCity(int.Parse(Request.Form["AreaID"]), false);
                        }
                    }
                    else if (type == "Search")
                    {
                        LoadShool(false);
                    }
                }
                catch { }
            }
        }

        #region 加载省
        public void LoadPr(int i)
        {
            DataTable dt = null;
            dt = sp.getConstellation(1);//一级城市
           
            if (dt != null && dt.Rows.Count > 0)
            {
                if (i == 1)
                {
                    selPr.DataSource = dt;
                    selPr.DataTextField = "Name";
                    selPr.DataValueField = "ID";
                    selPr.DataBind();
                    selPr.SelectedIndex = 0;

                    DataTable dts = sp.getUserInfo(int.Parse(selPr.Value),14);//二级城市

                    if (dts != null && dts.Rows.Count > 0)
                    {
                            #region 三级城市
                        DataTable dtt = sp.getUserInfo(int.Parse(dts.Rows[0]["ID"].ToString()), 14);
                            if (dtt != null && dtt.Rows.Count > 0)
                            {
                                selCity.DataSource = dts;
                                selCity.DataTextField = "Name";
                                selCity.DataValueField = "ID";
                                selCity.DataBind();
                                selCity.SelectedIndex = 0;

                                for (int j = 0; j < dtt.Rows.Count; j++)
                                {
                                    content += "<li><a href=\"javascript:LoadShool(" + dtt.Rows[j]["ID"].ToString() + ");\">" + dtt.Rows[j]["Name"].ToString() + "</a></li>";
                                }
                            }
                            else //没有三级城市
                            {
                                for (int j = 0; j < dts.Rows.Count; j++)
                                {
                                    content += "<li><a href=\"javascript:LoadShool(" + dts.Rows[j]["ID"].ToString() + ");\">" + dts.Rows[j]["Name"].ToString() + "</a></li>";
                                }
                            }
                            dtt.Clear();
                            #endregion
                    }
                    dts.Clear();
                }
                else if (i == 2)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        content += "<li><a href=\"javascript:LoadShool(" + dt.Rows[j]["ID"].ToString() + ");\">" + dt.Rows[j]["Name"].ToString() + "</a></li>";
                    }
                }
            }
            dt.Clear();
            this.spanContent.InnerHtml = "<ul >" + content + "</ul>";
        }
        #endregion

        #region 加载市
        public void LoadCity(int prID,bool bl)
        {
            DataTable dt = null;
            dt = sp.getUserInfo(prID,14);//二/三级城市

            if (dt != null && dt.Rows.Count > 0)
            {
                string str_City = "";

                if (bl)
                {
                    #region 三级城市
                    DataTable dtt = sp.getUserInfo(int.Parse(dt.Rows[0]["ID"].ToString()),14);
                    if (dtt != null && dtt.Rows.Count > 0)
                    {
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            str_City += dt.Rows[k]["ID"].ToString() + "," + dt.Rows[k]["Name"].ToString() + "|";
                        }

                        for (int j = 0; j < dtt.Rows.Count; j++)
                        {
                            str_City += "<li><a href=\"javascript:LoadShool(" + dtt.Rows[j]["ID"].ToString() + ");\">" + dtt.Rows[j]["Name"].ToString() + "</a></li>";
                        }
                    }
                    else //没有三级城市
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            str_City += "<li><a href=\"javascript:LoadShool(" + dt.Rows[j]["ID"].ToString() + ");\">" + dt.Rows[j]["Name"].ToString() + "</a></li>";
                        }
                    }
                    dtt.Clear();
                }
                else
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        str_City += "<li><a href=\"javascript:LoadShool(" + dt.Rows[j]["ID"].ToString() + ");\">" + dt.Rows[j]["Name"].ToString() + "</a></li>";
                    }
                }
                #endregion
                Response.Write(str_City);
                Response.End();
            }
        }
        #endregion

        #region 加载学校
        public void LoadShool(bool bl)
        {
            int area = int.Parse(Request.Form["AreaID"]);
            int DegreeID = int.Parse(Request.Form["DegreeID"]);
            DataTable dt =null;

            if (bl){dt=sp.getShool(area, DegreeID);}
            else//搜索
            {
                string name=Request.Form["Name"];
                dt = sp.searchShool(name,area,DegreeID);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                string shool = "";
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    shool += "<li><a href=\"javascript:getShool('" + dt.Rows[j]["SchoolName"].ToString() + "'," + dt.Rows[j]["SchoolID"].ToString() + ");\">" + dt.Rows[j]["SchoolName"].ToString() + "</a></li>";
                }
                Response.Write(shool);
                Response.End();
            }
            dt.Clear(); dt.Dispose();
           
        }
        #endregion
    }
}
