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

namespace HySoft.BaseCallCenter.Web.workordermanage.workorderset
{
    public partial class alarmmangeTypeEdit : BasePage
    {
        BLL.T_Wo_TypeAlarmDate bll = new BLL.T_Wo_TypeAlarmDate();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                labTypeTxt.Text = TypeName;
               // BindAreaList();

                if (Action == "edit")
                {
                    Model.T_Wo_TypeAlarmDate model = new Model.T_Wo_TypeAlarmDate();
                    model = bll.GetModel(Id);
                    txtGaoJingNumber.Text = model.F_Gcount.ToString();
                    ddlGaojingUnit.SelectedValue = model.F_Gunit.ToString();
                    txtChaoshiNumber.Text = model.F_Ccount.ToString();
                    ddlChaoShiUnit.SelectedValue = model.F_Cunit.ToString();
                }
            }
        }
        /// <summary>
        /// 处理类型新增还是修改
        /// </summary>
        public string Action
        {
            get
            {
                if (Request.QueryString["action"] != null)
                {
                    return Request.QueryString["action"].ToString().ToLower();
                }
                else
                {
                    return "add";
                }

            }
        }
        /// <summary>
        /// 类型ID
        /// </summary>
        public int FunctionTypeId
        {
            get
            {
                int id = 0;
                if (Request.QueryString["f_worktypeId"] != null)
                    int.TryParse(Request.QueryString["f_worktypeId"].ToString(), out id);
                return id;
            }


        }

        /// <summary>
        /// 记录ID
        /// </summary>
        public int Id
        {
            get
            {
                int id = 0;
                if (Request.QueryString["f_id"] != null)
                    int.TryParse(Request.QueryString["f_id"].ToString(), out id);
                return id;
            }
        }

        /// <summary>
        /// 类型名称
        /// </summary>
        public string TypeName
        {
            get
            {
                if (Request.QueryString["f_worktypeId"] != null)
                {
                    return DBUtility.DbHelperSQL.GetSingle("select F_name from T_Wo_WorkOrderType where F_WorkOrderTypeId='" + Request.QueryString["f_worktypeId"] + "'").ToString().ToLower();
                }
                else
                {
                    return "";
                }

            }
        }
        public string GetUnit(string val)
        {
            switch (val)
            {
                case "0":
                    return "分";
                case "1":
                    return "小时";
                case "2":
                    return "天";
                default:
                    return "";
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.T_Wo_TypeAlarmDate new_model = GetModel();
            //Common.LoginUser user = new Common.LoginUser(this.Context);
            new_model.F_ModifyBy = 1;//user.UserID;//修改人

            try
            {
                bool result;
                switch (Action)
                {
                    case "add":
                        new_model.F_Createby = 1;
                        new_model.F_CreateDate = DateTime.Now;
                        result = bll.Add(new_model);
                        if (result)
                        {
                            InsertOptLogs(0, 0, "操作成功！alarmmangeTypeEdit.aspx页面，新建操作。", 0);
                            MessageBoxToWindow("新增成功", "新增提示！", "success");
                        }
                        else
                        {
                            InsertOptLogs(0, 0, "操作失败！alarmmangeTypeEdit.aspx页面，新建操作。", 1);
                            MessageBoxToWindow("新增失败", "新增提示！", "error");
                        }
                        break;
                    case "edit":
                        result = bll.Update(new_model);
                        if (result)
                        {
                            InsertOptLogs(0, 0, "操作成功！alarmmangeTypeEdit.aspx页面，修改操作。", 0);
                            MessageBoxToWindow("修改成功", "修改提示！", "success");
                        }
                        else {
                            InsertOptLogs(0, 0, "操作失败！alarmmangeTypeEdit.aspx页面，修改操作。", 1);
                            MessageBoxToWindow("修改失败", "修改提示！", "error");
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                InsertOptLogs(0, 0, "操作异常！alarmmangeTypeEdit.aspx页面，异常信息："+ex.ToString(), 2);
                MessageBoxToWindow("异常信息", ex.Message, "catch");
            }
        }

        /// <summary>
        /// 获取功能对象实体模型
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public Model.T_Wo_TypeAlarmDate GetModel()
        {
            Model.T_Wo_TypeAlarmDate model = new Model.T_Wo_TypeAlarmDate();


            if (Id > 0)
            {
                model = bll.GetModel(Id);
            }

            try
            {
                //model.F_DeptId = ddlArea.SelectedValue != "" ? int.Parse(ddlArea.SelectedValue) : 0;//区域id
                //model.F_DeptCode = ddlArea.SelectedValue ="0";//? "0" : (bll_c.GetModel(int.Parse(ddlArea.SelectedValue)).F_DEPTCODE);//若是全局 存0空,若不是存对应Code
                model.F_WorkOrderTypeId = FunctionTypeId;//类型id
                model.F_Gcount = txtGaoJingNumber.Text.Trim() == "" ? 0 : int.Parse(txtGaoJingNumber.Text.Trim());
                model.F_Gunit = ddlGaojingUnit.SelectedValue == "" ? -1 : int.Parse(ddlGaojingUnit.SelectedValue);

                model.F_Ccount = txtChaoshiNumber.Text.Trim() == "" ? 0 : int.Parse(txtChaoshiNumber.Text.Trim());
                model.F_Cunit = ddlChaoShiUnit.SelectedValue == "" ? -1 : int.Parse(ddlChaoShiUnit.SelectedValue);

                //model.F_IsMain = ddlArea.SelectedValue == "0" ? 1 : 0;//全局参数
                model.F_ModifyDate = DateTime.Now;//最后修改时间
                model.F_DeleteFlag = 0;

            }
            catch (Exception)
            {

            }
            return model;
        }
        /// <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);

        }
    }
}