using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TopWinCMS.Common;
using TopWinCMS.BLL;
using TopWinCMS.Model;


namespace TopWinCMS.WebUI.Admin.ControlPanel
{
    public partial class Area : System.Web.UI.Page
    {
        AreaManage MyClassBLL = new AreaManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LB_Path.Text = "控制面板 → <a href='Area.aspx'>地区管理</a>";

                MyClassBLL.BindListBox("0", LB_Area1);
            }
        }

        #region 网页属性
        /// <summary>
        /// 当前选择的ID
        /// </summary>
        private int SelectID
        {
            set
            {
                ViewState["SelectID"] = value;
            }
            get
            {
                return ViewState["SelectID"] == null ? 0 : int.Parse(ViewState["SelectID"].ToString());
            }
        }
        /// <summary>
        /// 当前选择的Name
        /// </summary>
        private string SelectName
        {
            set
            {
                ViewState["SelectName"] = value;
            }
            get
            {
                return ViewState["SelectName"] == null ? string.Empty : ViewState["SelectName"].ToString();
            }
        }
        /// <summary>
        /// 地区级别
        /// </summary>
        private int LevelArea
        {
            set
            {
                ViewState["LevelArea"] = value;
            }
            get
            {
                return ViewState["LevelArea"] == null ? 0 : int.Parse(ViewState["LevelArea"].ToString());
            }
        }
        private bool IsEdit
        {
            set
            {
                ViewState["IsEdit"] = value;
            }
            get
            {
                return ViewState["IsEdit"] == null ? false  : bool.Parse(ViewState["IsEdit"].ToString());
            }
        }
        #endregion

        protected void LB_Area1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyClassBLL.BindListBox(LB_Area1.SelectedValue, LB_Area2);
            LB_Area3.Items.Clear();
            LB_Area4.Items.Clear();

            SelectID = int.Parse(LB_Area1.SelectedValue);
            SelectName = LB_Area1.SelectedItem.Text;
        }

        protected void LB_Area2_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyClassBLL.BindListBox(LB_Area2.SelectedValue, LB_Area3);
            LB_Area4.Items.Clear();


            SelectID = int.Parse(LB_Area2.SelectedValue);
            SelectName = LB_Area2.SelectedItem.Text;
            LevelArea = 3;
        }

        protected void LB_Area3_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyClassBLL.BindListBox(LB_Area3.SelectedValue, LB_Area4);


            SelectID = int.Parse(LB_Area3.SelectedValue);
            SelectName = LB_Area3.SelectedItem.Text;
            LevelArea = 4;
        }

        protected void LB_Area4_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectID = int.Parse(LB_Area4.SelectedValue);
            SelectName = LB_Area4.SelectedItem.Text;
            LevelArea = 4;
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void BT_EditThis_Click(object sender, EventArgs e)
        {
            TB_Name.Text = SelectName;

            IsEdit = true;
            BT_OK.Text = "编辑";
        }

        protected void BT_Delete_Click(object sender, EventArgs e)
        {

        }
        #region 添加或修改
        private void Add()
        {

            AreaInfo model = new AreaInfo();
            if (IsEdit == false )
            {
                model.ID = SelectID;
                model.LevelArea = LevelArea;
            }
            else
            {
                 model.ID = 0;
            }
            model.Name = TB_Name.Text.Trim();
            if (CB_IsOneArea.Checked == true)
            {
                model.ParentID = 0;
            }
            else if (CB_IsOneArea.Checked == false && SelectID > 0)
            {
                model.ParentID = SelectID;
            }
            

            string strErr = null;

            new AreaManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
            }
            else
            {
                TB_Name.Text = string.Empty;
                SelectID = 0;
                SelectName = "";
                LevelArea = 0;
                IsEdit = false;
                BT_OK.Text = "添加";
            }
        }
        #endregion

    }
}