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

namespace HySoft.BaseCallCenter.Web.sysmanage
{
    public partial class functiontreeedit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["getPuction"].ToString() == "Edit")
                {
                    getOrderInformerEdit(Request.QueryString["id"].ToString());
                }
            }
        }
        /// <summary>
        /// 初始化信息
        /// </summary>
        private void getOrderInformerEdit(string Flage)
        {
            DataTable dt = new DataTable();
            DataTable dtItems = new DataTable();
            string sql = "select * from T_Sys_ModuleFunctions where F_FunctionId='" + Flage + "'";
            dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                F_Name.Text = dt.Rows[0]["F_Name"].ToString();
                F_FunctionCode.Text = dt.Rows[0]["F_FunctionCode"].ToString();
                F_OptUrl.Text = dt.Rows[0]["F_OptUrl"].ToString();
                F_Remark.Text = dt.Rows[0]["F_Remark"].ToString();
                F_Sort.Text = dt.Rows[0]["F_Sort"].ToString();
                hdF_ParentId.Value = dt.Rows[0]["F_ParentId"].ToString();
            }
        }
        /// <summary>
        /// 保存订单信息。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            HySoft.BaseCallCenter.Model.T_Sys_ModuleFunctions orderModel = new Model.T_Sys_ModuleFunctions();
            HySoft.BaseCallCenter.BLL.T_Sys_ModuleFunctions orderBll = new BLL.T_Sys_ModuleFunctions();
            try
            {
                orderModel.F_Name = F_Name.Text.Trim();
                orderModel.F_FunctionCode = F_FunctionCode.Text.Trim();
                orderModel.F_OptUrl = F_OptUrl.Text.Trim();
                orderModel.F_Remark = F_Remark.Text.Trim();
                orderModel.F_Sort = 0;
                if (F_Sort.Text.Trim() == "")
                {
                    orderModel.F_Sort = 0;
                }
                else
                {
                    try { orderModel.F_Sort = Convert.ToInt32(F_Sort.Text.Trim()); }
                    catch { orderModel.F_Sort = 0; }
                }
                orderModel.F_StateFlag = Convert.ToInt32(hdradiolist.Value);              
                orderModel.F_Layer = Convert.ToInt32(Request.QueryString["iconcls"]);
                if (Request.QueryString["type"] == "0")
                {
                    DataTable dt = new DataTable();
                    DataTable dtItems = new DataTable();
                    string sql = "select * from T_Sys_ModuleFunctions where F_FunctionId='" + Request.QueryString["id"] + "'";
                    dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        orderModel.F_ParentId = Convert.ToInt32(dt.Rows[0]["F_ParentId"].ToString());
                        orderModel.F_ParentCode = dt.Rows[0]["F_ParentCode"].ToString();
                        orderModel.F_Layer = Convert.ToInt32(dt.Rows[0]["F_Layer"].ToString());
                    }
                    if (Request.QueryString["getPuction"] == "Add")
                    {
                        if (orderBll.Add(orderModel)>0)
                        {
                            MessageBoxToWindow("新增成功", "新增提示", "success");
                        }
                        else
                        {
                            MessageBoxToWindow("新增失败！", "新增提示！", "error");
                        }
                    }
                }
                else
                {
                    orderModel.F_Layer = Convert.ToInt32(Request.QueryString["iconcls"])+1;
                    orderModel.F_ParentId = Convert.ToInt32(Request.QueryString["id"]);
                    DataTable dt = new DataTable();
                    DataTable dtItems = new DataTable();
                    string sql = "select * from T_Sys_ModuleFunctions where F_FunctionId='" + Request.QueryString["id"] + "'";
                    dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        orderModel.F_ParentCode = dt.Rows[0]["F_FunctionCode"].ToString();
                        orderModel.F_Layer = Convert.ToInt32(dt.Rows[0]["F_Layer"].ToString()) + 1;
                    }
                    
                    if (Request.QueryString["getPuction"] == "Add")
                    {
                        if (orderBll.Add(orderModel)>0)
                        {
                            MessageBoxToWindow("新增成功", "新增提示", "success");
                        }
                        else
                        {
                            MessageBoxToWindow("新增失败！", "新增提示！", "error");
                        }
                    }
                }


                if (Request.QueryString["getPuction"] == "Edit")
                {
                    orderModel.F_FunctionId = Convert.ToInt32(Request.QueryString["id"]);
                    orderModel.F_ParentId = Convert.ToInt32(hdF_ParentId.Value);
                    if (orderBll.Update(orderModel))
                    {
                        MessageBoxToWindow("修改成功", "修改提示！", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("修改失败！", "修改提示！", "error");
                    }
                }
            }
            catch
            {
                MessageBoxToWindow("新增失败！", "新增提示！", "error");
            }

        }

        #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
    }
}