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 indexedit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    InitObj(Request.QueryString["id"].Trim());
                }
                if (!string.IsNullOrEmpty(Request.QueryString["categoryid"]))
                {
                    txtF_CategoryId.Value = Request.QueryString["categoryid"].Trim();
                }
            }
        }

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

        private Model.T_QC_IndexBase T_QC_IndexBaseObj
        {
            get
            {
                Model.T_QC_IndexBase model = new Model.T_QC_IndexBase();
                if (Request.QueryString["otype"].Trim() == "modify")
                {
                    model.F_IndexId = Convert.ToInt32(Request.QueryString["id"].Trim());
                }
                else
                {
                    model.F_IndexId = 0;
                }
                try
                {
                    model.F_CategoryId = Convert.ToInt32(txtF_CategoryId.Value.Trim());
                }
                catch
                { 
                }
                try
                {
                    model.F_Score = Convert.ToInt32(txtF_Score.Text.Trim());
                }
                catch
                {
                }
                try
                {
                    model.F_Sort = Convert.ToInt32(txtF_Sort.Text.Trim());
                }
                catch
                {
                }
                model.F_Title = txtF_Title.Text.Trim();
                model.F_Remark = txtF_Remark.Text.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_IndexId.Value = value.F_IndexId.ToString();
                    this.txtF_CategoryId.Value = value.F_CategoryId.ToString();
                    this.txtF_Score.Text = value.F_Score.ToString();
                    this.txtF_Title.Text = value.F_Title;
                    this.txtF_Remark.Text = value.F_Remark;
                    this.txtF_Sort.Text = value.F_Sort.ToString();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["otype"] == "new")
                {
                    if (new BLL.T_QC_IndexBase().Add(T_QC_IndexBaseObj) > 0)
                    {
                        try
                        {
                            SetScoreByCategory(this.txtF_CategoryId.Value);
                        }
                        catch
                        { }
                        MessageBoxToWindow("新增成功", "新增提示", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("新增失败", "新增提示！", "error");
                    }
                }
                if (Request.QueryString["otype"] == "modify")
                {
                    if (new BLL.T_QC_IndexBase().Update(T_QC_IndexBaseObj))
                    {
                        try
                        {
                            SetScoreByCategory(this.txtF_CategoryId.Value);
                        }
                        catch
                        { }
                        MessageBoxToWindow("修改成功", "修改提示！", "success");
                    }
                    else
                    {
                        MessageBoxToWindow("修改失败！", "修改提示！", "error");
                    }
                }
            }
            catch
            {
                MessageBoxToWindow("保存失败！", "保存提示！", "error");
            }

        }

        private void SetScoreByCategory(string categoryid)
        {
            DataTable dt = new DataTable();
            try
            {
                Model.T_QC_IndexCategory model = new BLL.T_QC_IndexCategory().GetModel(Convert.ToInt32(categoryid));
                if (model != null)
                {
                    string parentid = model.F_ParentId.ToString();
                    dt = new BLL.T_QC_IndexBase().GetList("F_DeleteFlag=0 and (F_CategoryId=" + model.F_CategoryId + " or F_CategoryId in (select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + categoryid + " )) ").Tables[0];
                    int score=0;
                    for(int i=0;i<dt.Rows.Count;i++)
                    {
                        score+=Convert.ToInt32(dt.Rows[i]["F_Score"].ToString().Trim());
                    }
                    model.F_Expand1 = score.ToString();
                    if (new BLL.T_QC_IndexCategory().Update(model))
                    {
                        if (parentid != "0")
                        {
                            SetScoreByCategory(parentid);
                        }
                    }
                }
            }
            catch { }
            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
    }
}