using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HySoft.BaseCallCenter.Web.workordermanage.workorderset
{
    public partial class workflowEdit : BasePage
    {
        BLL.T_Wf_WorkFlow WorkOrdeFlow = new BLL.T_Wf_WorkFlow();
        int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Action == "addedit")
                {
                    if (Request.QueryString["id"] != null)
                    {
                        Model.T_Wf_WorkFlow model = new Model.T_Wf_WorkFlow();
                        id = Convert.ToInt32(Request.QueryString["id"].ToString());
                        model = WorkOrdeFlow.GetModel(id);
                        F_ParamDes.Text = model.F_XML;//描述
                        textF_Date.Text = model.F_StartDate.ToString();//排序
                        F_ParamName.Text = model.F_Name;//状态名称
                        if (model.F_State == false)
                        {
                            F_ParamState.Text ="0";
                        }
                        if(model.F_State == true)
                        {
                            F_ParamState.Text = "1";
                        }
                    }
                }
            }

        }

        /// <summary>
        /// 处理类型新增还是修改
        /// </summary>
        public string Action
        {
            get
            {
                if (Request.QueryString["action"] != null)
                {
                    return Request.QueryString["action"].ToString().ToLower();
                }
                else
                {
                    return "add";
                }
            }
        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <returns></returns>
        public bool Add()
        {
            Model.T_Wf_WorkFlow model = new Model.T_Wf_WorkFlow();
            model.F_Name = F_ParamName.Text.Trim();//名称
            if (F_ParamState.SelectedValue.Trim() == "0")
            {
                model.F_State = false;//状态
            }
            else if (F_ParamState.SelectedValue.Trim() == "1")
            {
                model.F_State = true;//状态
            }
            model.F_CreateDate = DateTime.Now;
            model.F_CreateName = "8000";
            if (!string.IsNullOrEmpty(textF_Date.Text))
            {
                model.F_StartDate =Convert.ToDateTime(textF_Date.Text);
            }
            model.F_XML = F_ParamDes.Text;//是否删除
            bool result = WorkOrdeFlow.Add(model);
            return result;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <returns></returns>
        public bool Edit()
        {
            Model.T_Wf_WorkFlow model = new Model.T_Wf_WorkFlow();
            model = new BLL.T_Wf_WorkFlow().GetModel(Convert.ToInt32(Request.QueryString["id"].ToString()));
            model.F_Name = F_ParamName.Text.Trim();//名称
            if (F_ParamState.SelectedValue.Trim() == "0")
            {
                model.F_State = false;//状态
            }
            else if (F_ParamState.SelectedValue.Trim() == "1")
            {
                model.F_State = true;//状态
            }
            model.F_EditDate = DateTime.Now;
            model.F_EditName = "8000";
            if (!string.IsNullOrEmpty(textF_Date.Text))
            {
                model.F_StartDate = Convert.ToDateTime(textF_Date.Text);
            }
            model.F_XML = F_ParamDes.Text;//
            model.F_ID = Convert.ToInt32(Request.QueryString["id"].ToString());
            bool result = WorkOrdeFlow.Update(model);
            return result;
        }

        /// <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 = "warning";
                    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 = "error";
                    script = "<script type='text/javascript'>  $.ligerDialog.alert('" + title + "','" + content + "','" + type + "');</script>";
                    break;
                default:
                    type = "question";
                    break;
            }
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", script);
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                bool result;
                string a = Request.QueryString["id"];
                if (string.IsNullOrEmpty(a) || a == "0")
                {
                    InsertOptLogs(0, 0, "操作成功！workflowEdit.aspx页面，添加操作。", 0);
                    result = Add();
                }
                else
                {
                    InsertOptLogs(0, 0, "操作成功！workflowEdit.aspx页面，修改操作。", 0);
                    result = Edit();
                }
                MessageBoxToWindow("提交成功", "提示", "success");
            }
            catch (Exception ex)
            {
                InsertOptLogs(0, 0, "操作异常！workflowEdit.aspx页面，异常信息："+ex.ToString(), 2);
                MessageBoxToWindow("异常信息", ex.Message, "catch");
            }


        }
    }
}