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 basedataedit : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                hfAddFlage.Value = Request.QueryString["Flasge"].ToString();
                if (Request.QueryString["Flasge"].ToString() == "AddData")
                {
                    getDataType();
                }
                if (Request.QueryString["Flasge"].ToString() == "Edit") {
                    getDataTypeEdit();
                }

                if (Request.QueryString["Flasge"].ToString() == "EditGrid")
                {
                    getDataGridEdit();
                }
            }
        }

        private void getDataGridEdit()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ClickSingId"]))
            {
                DataTable dt = DBUtility.DbHelperSQL.Query("select * from T_Sys_DictionaryValue where F_DictionaryValueId='" + Request.QueryString["ClickSingId"].ToString() + "'").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    DataTable dt1 = DBUtility.DbHelperSQL.Query("select * from T_Sys_DictionaryBase where F_DictionaryFlag='" + dt.Rows[0]["F_DictionaryFlag"].ToString() + "'").Tables[0];
                    if (dt1.Rows.Count > 0) {
                        txtDictionaryFlag.Text = dt1.Rows[0]["F_DictionaryFlag"].ToString();
                        txtDictionaryName.Text = dt1.Rows[0]["F_DictionaryName"].ToString();
                    }
                    txtValueCode.Text = dt.Rows[0]["F_ValueCode"].ToString();
                    txtName.Text = dt.Rows[0]["F_Name"].ToString();
                    txtSort.Text = dt.Rows[0]["F_Sort"].ToString();
                    txtId.Value = dt.Rows[0]["F_DictionaryValueId"].ToString();
                }
            }
        }
        /// <summary>
        /// 修改类型的时候的初始化方法
        /// </summary>
        private void getDataTypeEdit()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["NodeidEdit"]))
            {
                DataTable dt = DBUtility.DbHelperSQL.Query("select * from T_Sys_DictionaryBase where F_DictionaryFlag='" + Request.QueryString["NodeidEdit"].ToString() + "'").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    txtDictionaryFlag.Text = dt.Rows[0]["F_DictionaryFlag"].ToString();
                    txtDictionaryName.Text = dt.Rows[0]["F_DictionaryName"].ToString();
                    txtSort.Text = dt.Rows[0]["F_Sort"].ToString();
                }
            }
        }
        /// <summary>
        /// 初始化数据类型
        /// </summary>
        private void getDataType()
        {
            if(!string.IsNullOrEmpty(Request.QueryString["Nodeid"])){
             DataTable dt=   DBUtility.DbHelperSQL.Query("select * from T_Sys_DictionaryBase where F_DictionaryFlag='" + Request.QueryString["Nodeid"].ToString() + "'").Tables[0];
             if (dt.Rows.Count > 0) {
                 txtDictionaryFlag.Text = dt.Rows[0]["F_DictionaryFlag"].ToString();
                 txtDictionaryName.Text = dt.Rows[0]["F_DictionaryName"].ToString();
             }
            }
            
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int res = 0;
            bool relut = false;
            try{
             HySoft.BaseCallCenter.Model.T_Sys_DictionaryBase  dictionaryBaseModel = new Model.T_Sys_DictionaryBase();
             HySoft.BaseCallCenter.BLL.T_Sys_DictionaryBase dictionaryBaseBll = new BLL.T_Sys_DictionaryBase();
             HySoft.BaseCallCenter.Model.T_Sys_DictionaryValue dictionaryValueModel = new Model.T_Sys_DictionaryValue();
             HySoft.BaseCallCenter.BLL.T_Sys_DictionaryValue dictionaryValueBll = new BLL.T_Sys_DictionaryValue();

             if (Request.QueryString["Flasge"].ToString() == "Add" || Request.QueryString["Flasge"].ToString() == "AddData")
             {
                 if (Request.QueryString["Flasge"].ToString() == "Add")
                 {
                     DataTable dt = DBUtility.DbHelperSQL.Query("select * from T_Sys_DictionaryBase where F_DictionaryFlag='" + txtDictionaryFlag.Text + "'").Tables[0];
                     if (dt.Rows.Count > 0) {
                         MessageBoxToWindow("当前标识名称存在，请更换！", "提示", "error");
                     }
                     else
                     {
                         if (string.IsNullOrEmpty(txtDictionaryFlag.Text) || string.IsNullOrEmpty(txtDictionaryName.Text))
                         {
                             MessageBoxToWindow("当前有为空信息，请填写！", "提示", "error");
                         }else{
                         dictionaryBaseModel.F_DictionaryFlag = txtDictionaryFlag.Text;
                         dictionaryBaseModel.F_DictionaryName = txtDictionaryName.Text;
                         if (!string.IsNullOrEmpty(txtSort.Text))
                         {
                             dictionaryBaseModel.F_Sort = Convert.ToInt32(txtSort.Text);
                         }
                         dictionaryBaseModel.F_State = "0";
                         dictionaryBaseModel.F_Describe = "ZH";
                         //res = dictionaryBaseBll.Add(dictionaryBaseModel);
                         }
                     }
                 }
                if (Request.QueryString["Flasge"].ToString() == "AddData") {

                    if (string.IsNullOrEmpty(txtValueCode.Text) || string.IsNullOrEmpty(txtName.Text))
                    {
                        MessageBoxToWindow("当前有为空信息，请填写！", "提示", "error");
                    }
                    else
                    {
                        dictionaryValueModel.F_DictionaryFlag = txtDictionaryFlag.Text;
                        dictionaryValueModel.F_ValueCode = txtValueCode.Text;
                        dictionaryValueModel.F_Name = txtName.Text;
                        dictionaryValueModel.F_State = "0";
                        if (!string.IsNullOrEmpty(txtSort.Text))
                        {
                            dictionaryValueModel.F_Sort = Convert.ToInt32(txtSort.Text);
                        }
                        res = dictionaryValueBll.Add(dictionaryValueModel);
                    }
                }
                if (res > 0)
                {
                    MessageBoxToWindow("新增成功", "新增提示", "success");
                }
                else
                {
                    MessageBoxToWindow("新增失败", "新增提示！", "error");
                }
             }
             #region 修改信息
             if (Request.QueryString["Flasge"].ToString() == "Edit" || Request.QueryString["Flasge"].ToString() == "EditGrid") {
                 if (Request.QueryString["Flasge"].ToString() == "Edit") {
                     dictionaryBaseModel.F_DictionaryFlag = txtDictionaryFlag.Text;
                     dictionaryBaseModel.F_DictionaryName = txtDictionaryName.Text;
                     dictionaryBaseModel.F_State = "0";
                     dictionaryBaseModel.F_Describe = "ZH";
                     if (!string.IsNullOrEmpty(txtSort.Text))
                     {
                         dictionaryBaseModel.F_Sort = Convert.ToInt32(txtSort.Text);
                     }
                     relut = dictionaryBaseBll.Update(dictionaryBaseModel);
                 }
                 if (Request.QueryString["Flasge"].ToString() == "EditGrid")
                 {
                     dictionaryValueModel.F_DictionaryFlag = txtDictionaryFlag.Text;
                     dictionaryValueModel.F_ValueCode = txtValueCode.Text;
                     dictionaryValueModel.F_Name = txtName.Text;
                     dictionaryValueModel.F_State = "0";
                     if (!string.IsNullOrEmpty(txtSort.Text))
                     {
                         dictionaryValueModel.F_Sort = Convert.ToInt32(txtSort.Text);
                     }
                     dictionaryValueModel.F_DictionaryValueId = Convert.ToInt32(txtId.Value);
                     relut = dictionaryValueBll.Update(dictionaryValueModel);
                 }

                 if (relut)
                 {
                     MessageBoxToWindow("修改成功", "修改提示", "success");
                 }
                 else
                 {
                     MessageBoxToWindow("修改失败", "修改提示！", "error");
                 }
             }


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