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.sys
{
    public partial class areaadd : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "sys";
        JuSNS.Home.User us = new JuSNS.Home.User();
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Form["Type"] == "City")
                {
                    this.txtID.Value = Request.Form["r"];
                    LoadInfo(this.selArea1, 1,true);
                }

                if (Request.QueryString["Type"] == "Add")
                {
                    AddArea();
                }

                if (Request.Form["Type"] == null && Request.QueryString["Type"] == null)
                {
                    if (!IsPostBack)
                    {
                        this.txtID.Value = Request.QueryString["r"];
                        LoadInfo(this.selArea0, 0, false);
                        LoadInfo(this.selArea1, 1, false);
                    }
                }
            }
            catch { }
        }

        #region 绑定地区
        public void LoadInfo(HtmlSelect select, int r, bool bl)
        {
            DataTable dt = null;
            if (r == 0)//一级地区
            {
                dt = sp.getConstellation(1);
            }
            else if (r == 1)//二级地区
            {
                int id;
                try
                {
                    id = int.Parse(Request.Form["id"]);
                }
                catch
                {
                    id = int.Parse(this.selArea0.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;
                    }
                }
            }
        }
        #endregion

        #region 添加地区
        public void AddArea()
        {
            int n = us.UpdateAddArea(0, int.Parse(Request.QueryString["ParentID"]), Request.QueryString["name"], 0);
            if (n== 1)
            {
                Response.Redirect("area.aspx");
            }
            else if(n==2)
            {
                PageError("添加地区失败,地区名称已经存在!", "AreaList.aspx");
            }
            else
            {
                PageError("添加地区失败!", "AreaList.aspx");
            }
        }
        #endregion    
    }
}
