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

namespace HySoft.BaseCallCenter.Web.sysmanage
{
    public partial class deptdeit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"].ToString() == "Edit") {
                    getDeptarmentinfo();
                }
            }
        }

        /// <summary>
        /// 初始化信息
        /// </summary>
        private void getDeptarmentinfo()
        {
          Model.T_Sys_Department deparmentionModel =  new HySoft.BaseCallCenter.BLL.T_Sys_Department().GetModel(Convert.ToInt32(Request.QueryString["deptId"]));
          if (deparmentionModel != null) {
              txtDeptCode.Text = deparmentionModel.F_DeptCode;
              txtDeptName.Text = deparmentionModel.F_DeptName;
              txtPhone.Text = deparmentionModel.F_Telephone;
              txtMobile.Text = deparmentionModel.F_OtherPhone;
              txtEmail.Text = deparmentionModel.F_Email;
              txtF_Remark.Text = deparmentionModel.F_Remark;
              txtId.Value = deparmentionModel.F_DeptId.ToString();
              txtParthId.Value = deparmentionModel.F_ParentId.ToString();
              hfParthCode.Value = deparmentionModel.F_ParentCode.ToString();
              txtParthName.Value = deparmentionModel.F_ParentName.ToString();
          }
   
        }

        /// <summary>
        /// 保存单位部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int result = 0;
                HySoft.BaseCallCenter.Model.T_Sys_Department departmentModel = new Model.T_Sys_Department();
                HySoft.BaseCallCenter.BLL.T_Sys_Department departmentBll = new BLL.T_Sys_Department();
                if (Request.QueryString["action"].ToString() == "Add")
                {
                    if (departmentBll.GetModel(Convert.ToInt32(Request.QueryString["deptFarth"])) != null)
                    {
                        departmentModel.F_ParentId = departmentBll.GetModel(Convert.ToInt32(Request.QueryString["deptFarth"])).F_DeptId;
                        departmentModel.F_ParentName = departmentBll.GetModel(Convert.ToInt32(Request.QueryString["deptFarth"])).F_DeptName;
                        departmentModel.F_ParentCode = departmentBll.GetModel(Convert.ToInt32(Request.QueryString["deptFarth"])).F_DeptCode;
                    }
                }

                if (!string.IsNullOrEmpty(txtDeptCode.Text))
                {
                    departmentModel.F_DeptCode = txtDeptCode.Text;
                }
                else {
                    MessageBoxToWindow("部门编号不能为空！", "提示", "error");
                }
                if (!string.IsNullOrEmpty(txtDeptName.Text))
                {
                    departmentModel.F_DeptName = txtDeptName.Text;
                }
                else {
                    MessageBoxToWindow("部门名称不能为空！", "提示", "error");
                }
                departmentModel.F_Telephone = txtPhone.Text;
                departmentModel.F_OtherPhone = txtMobile.Text;
                departmentModel.F_Email = txtEmail.Text;
                departmentModel.F_Remark = txtF_Remark.Text;
                departmentModel.F_State = 0;

                if (Request.QueryString["action"].ToString() == "Add")
                {
                    result = departmentBll.Add(departmentModel);
                    if (result > 0)
                    {
                        MessageBoxToWindow("新增成功", "新增提示", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("新增失败", "新增提示！", "error");
                    }
                }

                if (Request.QueryString["action"].ToString() == "Edit") {
                    departmentModel.F_DeptId =Convert.ToInt32(Request.QueryString["deptId"].ToString());
                    departmentModel.F_ParentId = Convert.ToInt32(txtParthId.Value);
                    departmentModel.F_ParentCode = hfParthCode.Value;
                    departmentModel.F_ParentName = txtParthName.Value;
                    if (departmentBll.Update(departmentModel))
                    {
                        MessageBoxToWindow("修改成功", "修改提示！", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("修改失败", "修改提示！", "error");
                    }
                }

            }
            catch (Exception ex){
                SysLog.WriteLog(ex);
                MessageBoxToWindow("异常提示！" + ex.Message, "提示", "catch");
            }




        }
        #region 弹出对话框
        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="type"></param>
        public void MessageBoxToWindow(string title, string content, string type)
        {
            string script = "";
            switch (type)
            {
                case "error"://失败
                    type = "error";
                    script = "<script type='text/javascript'>  $.ligerDialog.alert('" + title + "','" + content + "','" + type + "');</script>";
                    break;
                case "success"://成功
                    type = "info";
                    script = "<script type='text/javascript'> $.ligerDialog.alert('" + title + "','" + content + "','" + type + "',CloseThis);</script>";
                    break;
                case "catch"://异常
                    type = "warning";
                    script = "<script type='text/javascript'>  $.ligerDialog.alert('" + title + "','" + content + "','" + type + "');</script>";
                    break;
                default:
                    type = "question";
                    break;
            }
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", script);
        }
        #endregion
    }
}