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.askmanage
{
    public partial class questionedit : 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["categoryid"]))
                {
                    dropQuestionCategory.SelectedValue = Request.QueryString["categoryid"].Trim();
                }
                if (!string.IsNullOrEmpty(Request.QueryString["selecttype"]))
                {
                    dropQuestionType.SelectedValue = Request.QueryString["selecttype"].Trim();
                }
                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_QuestionObj = new BLL.T_Ask_Question().GetModel(Convert.ToInt32(id));
            }
            catch (Exception ex)
            {
            }
        }

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

                model.F_CategoryId = Convert.ToInt32(dropQuestionCategory.SelectedValue.Trim());
                model.F_Type = Convert.ToInt32(dropQuestionType.SelectedValue.Trim());
                model.F_Title = txtF_Title.Value.Trim();
                model.F_Content = "";
                model.F_CreateOn = DateTime.Now;
                model.F_SortModel = Convert.ToInt32(dropSortModel.SelectedValue.Trim());
                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_QuestionId.ToString();
                    dropQuestionCategory.SelectedValue = value.F_CategoryId.ToString();
                    dropQuestionType.SelectedValue = value.F_Type.ToString();
                    dropSortModel.SelectedValue = value.F_SortModel.ToString();
                    txtF_Title.Value = value.F_Title;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            try
            {
                string[] arr1 = new string[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
                string[] arr2 = new string[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"};
                if (Request.QueryString["otype"] == "new")
                {
                    Model.T_Ask_Question model=T_Ask_QuestionObj;

                    #region 获取内容

                    if (dropQuestionType.SelectedValue != "1")
                    {
                        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_QuestionItems' and F_UserId=" + userid + " ORDER BY ExpandIntField3 ").Tables[0];
                        string content = "";
                        int count = dt.Rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            string vsort = "";
                            if (dropSortModel.SelectedValue == "1")
                            {
                                vsort = arr2[i];
                            }
                            else
                            {
                                vsort = arr1[i];
                            }
                            content += vsort + "." + dt.Rows[i]["ExpandVchField1"].ToString().Trim() + ";";
                        }

                        model.F_Content = content;

                    }

                    #endregion

                    int questionid=new BLL.T_Ask_Question().Add(model);
                    if (questionid > 0)
                    {
                        #region 操作临时数据
                        int count = dt.Rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            Model.T_Ask_QuestionItems itemmodel = new Model.T_Ask_QuestionItems();
                            itemmodel.F_QuestionId = questionid;
                            itemmodel.F_ItemName = dt.Rows[i]["ExpandVchField1"].ToString().Trim();
                            itemmodel.F_Sort = Convert.ToInt32(dt.Rows[i]["ExpandIntField3"].ToString().Trim());
                            itemmodel.F_SortModel = Convert.ToInt32(dropSortModel.SelectedValue);
                            int itemid=new BLL.T_Ask_QuestionItems().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")
                {
                    Model.T_Ask_Question model = T_Ask_QuestionObj;

                    #region 获取内容

                    if (dropQuestionType.SelectedValue != "1")
                    {
                        dt = new BLL.T_Ask_QuestionItems().GetList(" F_QuestionId=" + txtId.Value.Trim() + " ORDER BY F_Sort ").Tables[0];
                        string content = "";
                        int count = dt.Rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            string vsort = "";
                            if (dropSortModel.SelectedValue == "1")
                            {
                                vsort = arr2[i];
                            }
                            else
                            {
                                vsort = arr1[i];
                            }
                            content += vsort + "." + dt.Rows[i]["F_ItemName"].ToString().Trim() + ";";
                        }

                        model.F_Content = content;

                    }

                    #endregion
                    if (new BLL.T_Ask_Question().Update(model))
                    {
                        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
    }
}