using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HySoft.BaseCallCenter.Web.sysmanage
{
    public partial class DeptlistAddandEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["categoryid"]))
                {
                    txtF_ParentId.Value = Request.QueryString["categoryid"].Trim();
                }
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    Initialize(Request.QueryString["id"]);
                }
            }
        }


        #region 信息初始化

        void Initialize(string id)
        {
            T_Department = new BLL.T_Sys_Department().GetModel(Convert.ToInt32(id));
        }
        #endregion

        #region 公共属性
        private Model.T_Sys_Department T_Department
        {
            get
            {
                Model.T_Sys_Department _model = new Model.T_Sys_Department();

                if (txtF_CategoryId.Value.Trim() != "")
                {
                    _model.F_DeptId = Convert.ToInt32(txtF_CategoryId.Value.Trim());
                }
                else
                {
                    _model.F_DeptId = 0;
                }
                _model.F_DeptName = txtF_CategoryName.Text.Trim();
                try
                {

                    _model.F_ParentId = Convert.ToInt32(txtF_ParentId.Value.Trim());
                }
                catch
                {
                    _model.F_ParentId = 0;
                }
                try
                {
                    _model.F_Sort = Convert.ToInt32(txtF_Sort.Text.Trim());
                }
                catch
                {
                    _model.F_Sort = 0;
                }
                _model.F_State = 0;
                return _model;
            }
            set
            {
                if (value != null)
                {
                    this.txtF_CategoryId.Value = value.F_DeptId.ToString();
                    txtF_ParentId.Value = value.F_ParentId.ToString();
                    this.txtF_CategoryName.Text = value.F_DeptName;
                    this.txtF_Sort.Text = value.F_Sort.ToString();
                }
            }
        }
        #endregion

        #region 保存信息

        private void SaveInfo()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"].Trim() == "Add")
                    {
                        int bl = new BLL.T_Sys_Department().Add(T_Department);
                        if (bl > 0)
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                        }
                    }
                    if (Request.QueryString["action"].Trim() == "Modify")
                    {
                        bool bl = new BLL.T_Sys_Department().Update(T_Department);
                        if (bl)
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                        }
                    }
                }
            }
            catch
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('error')</script>");
            }
        }

        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }





    }
}