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

namespace HySoft.BaseCallCenter.Web.qualitymanage
{
    public partial class indexcategoryedit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    InitObj(Request.QueryString["id"].Trim());
                }
                if (!string.IsNullOrEmpty(Request.QueryString["categoryid"]))
                {
                    dropF_Category.Value = Request.QueryString["categoryid"].Trim();
                }
            }
        }

        private void BindList()
        {
            DataTable dt = new DataTable();
            try
            {
                dropF_Category.Items.Clear();
                dropF_Category.Items.Add(new ListItem("根分类", "0"));
                dt = new BLL.T_QC_IndexCategory().GetList(" F_DeleteFlag=0 and F_ParentId=0 order by F_Sort ").Tables[0];

                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dropF_Category.Items.Add(new ListItem(dt.Rows[i]["F_CategoryName"].ToString().Trim(), dt.Rows[i]["F_CategoryId"].ToString().Trim()));
                    }
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }

        void InitObj(string id)
        {
            try
            {
                T_QC_IndexCategoryObj = new BLL.T_QC_IndexCategory().GetModel(Convert.ToInt32(id));
            }
            catch (Exception ex)
            {
            }
        }

        private Model.T_QC_IndexCategory T_QC_IndexCategoryObj
        {
            get
            {
                Model.T_QC_IndexCategory model = new Model.T_QC_IndexCategory();
                if (Request.QueryString["otype"].Trim() == "modify")
                {
                    model.F_CategoryId = Convert.ToInt32(Request.QueryString["id"].Trim());
                }
                else
                {
                    model.F_CategoryId = 0;
                }

                //model.F_CategoryNumber = F_CategoryNumber;
                model.F_CategoryName = txtF_CategoryName.Text.Trim();

                try
                {
                    model.F_Sort = Convert.ToInt32(txtF_Sort.Text.Trim());
                }
                catch
                { }

                model.F_ParentId = Convert.ToInt32(dropF_Category.Value.Trim());

                model.F_CreateOn = DateTime.Now;
                try
                {
                    LoginUser p_LoginUser = new LoginUser(this.Context);
                    model.F_CreateBy = p_LoginUser.UserID;
                }
                catch { }
                model.F_DeleteFlag = 0;

                return model;
            }
            set
            {
                if (value != null)
                {
                    this.txtF_CategoryId.Value = value.F_CategoryId.ToString();
                    txtF_CategoryName.Text = value.F_CategoryName.Trim();
                    txtF_Sort.Text = value.F_Sort.ToString();
                    dropF_Category.Value = value.F_ParentId.ToString();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["otype"] == "new")
                {
                    if (new BLL.T_QC_IndexCategory().Add(T_QC_IndexCategoryObj) > 0)
                    {
                        MessageBoxToWindow("新增成功", "新增提示", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("新增失败", "新增提示！", "error");
                    }
                }
                if (Request.QueryString["otype"] == "modify")
                {
                    if (new BLL.T_QC_IndexCategory().Update(T_QC_IndexCategoryObj))
                    {
                        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
    }
}