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.askmanage
{
    public partial class pageredit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    LoginUser p_LoginUser = new LoginUser(this.Context);
                    txtUserId.Value = p_LoginUser.UserID.ToString();
                }
                catch { }
                BindList();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    txtId.Value = Request.QueryString["id"].Trim();
                    InitObj(Request.QueryString["id"].Trim());
                }
            }
        }

        private void BindList()
        {
            DataTable dt = new DataTable();
            try
            {
                dropQuestionCategory.Items.Clear();
                dropQuestionCategory.Items.Add(new ListItem("所有分类", ""));
                dt = new BLL.T_Ask_QuestionCategory().GetAllList().Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    dropQuestionCategory.Items.Add(new ListItem(dr["F_CategoryName"].ToString().Trim(), dr["F_CategoryId"].ToString().Trim()));
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }

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

        private Model.T_Ask_PagerInfo T_Ask_PagerInfoObj
        {
            get
            {
                Model.T_Ask_PagerInfo model = new Model.T_Ask_PagerInfo();
                if (Request.QueryString["otype"].Trim() == "modify")
                {
                    model.F_PagerId = Convert.ToInt32(Request.QueryString["id"].Trim());
                }
                else
                {
                    model.F_PagerId = 0;
                }
                model.F_Title = txtF_Title.Value.Trim();
                model.F_Remark = txtF_Remark.Value.Trim();
                try
                {
                    model.F_Times = Convert.ToInt32(txtQuestionCount.Value.Trim());
                }
                catch
                { }
                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.txtId.Value = value.F_PagerId.ToString();
                    txtF_Title.Value = value.F_Title.Trim();
                    txtF_Remark.Value = value.F_Remark.Trim();
                    txtQuestionCount.Value = value.F_Times.ToString();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            try
            {
                if (Request.QueryString["otype"] == "new")
                {
                    int pagerid = new BLL.T_Ask_PagerInfo().Add(T_Ask_PagerInfoObj);
                    if (pagerid > 0)
                    {
                        string userid = "0";
                        try
                        {
                            LoginUser p_LoginUser = new LoginUser(this.Context);
                            userid = p_LoginUser.UserID.ToString();
                        }
                        catch { }
                        dt = new BLL.T_Sys_TempItems().GetList(" F_TempName='T_Ask_PagerItems' and F_UserId=" + userid + " ORDER BY ExpandIntField3 ").Tables[0];
                        #region 操作临时数据
                        int count = dt.Rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            Model.T_Ask_PagerItems itemmodel = new Model.T_Ask_PagerItems();
                            itemmodel.F_PagerId = pagerid;
                            itemmodel.F_QuestionId = Convert.ToInt32(dt.Rows[i]["ExpandIntField1"].ToString().Trim());
                            itemmodel.F_Sort = Convert.ToInt32(dt.Rows[i]["ExpandIntField3"].ToString().Trim());
                            int itemid = new BLL.T_Ask_PagerItems().Add(itemmodel);
                            if (itemid > 0)
                            {
                                new BLL.T_Sys_TempItems().Delete(Convert.ToInt32(dt.Rows[i]["F_Id"].ToString().Trim()));
                            }
                        }
                        #endregion
                        MessageBoxToWindow("新增成功", "新增提示", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("新增失败", "新增提示！", "error");
                    }
                }
                if (Request.QueryString["otype"] == "modify")
                {
                    if (new BLL.T_Ask_PagerInfo().Update(T_Ask_PagerInfoObj))
                    {
                        MessageBoxToWindow("修改成功", "修改提示！", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("修改失败！", "修改提示！", "error");
                    }
                }
            }
            catch
            {
                MessageBoxToWindow("保存失败！", "保存提示！", "error");
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }

        }

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