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 ordertypemanageedit : BasePage
    {
        static string id = "";
        BLL.T_Wo_WorkOrderType WorkOrderType = new BLL.T_Wo_WorkOrderType();
        BLL.T_Wo_WorkOrderTypeState WorkOrderTypeState = new BLL.T_Wo_WorkOrderTypeState();
        BLL.T_Wo_WorkOrderState WorkOrderState = new BLL.T_Wo_WorkOrderState(); 
        static string Oldf_id;
        string typeStateSte = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Oldf_id = Request.QueryString["actionNodeID"];

                if (Request.QueryString["action"] == "Addedit")
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>typeid=" + Request.QueryString["actionNodeID"] + ";</script>");
                Model.T_Wo_WorkOrderType model = new Model.T_Wo_WorkOrderType();
                id = Request.QueryString["actionNodeID"];
                model = WorkOrderType.GetModel(OperateId);
                this.F_Ename.Text = model.F_Ename;
                this.F_ParamCode.Text = model.F_Name;
                this.textF_Sort.Text = model.F_Sort.ToString();
                this.F_ParamDes.Text = model.F_Remark;
                this.F_ParamValue.Text = model.F_Number;
                //List<Model.T_Wo_WorkOrderTypeState> typeStateModel = new BLL.T_Wo_WorkOrderTypeState().GetModelList("F_WorkOrderTypeId=8");
                List<Model.T_Wo_WorkOrderTypeState> typeStateModel = new BLL.T_Wo_WorkOrderTypeState().GetModelList("F_WorkOrderTypeId= " + Request.QueryString["actionNodeID"]);
                 if (typeStateModel.Count > 0) {
                     for (int i = 0; i < typeStateModel.Count;i++ )
                     {
                         typeStateSte += typeStateModel[i].F_WorkOrderStateId+",";
                     }
                 }
                 hfTypeStateStr.Value = typeStateSte.Trim(',');
            }
            
            }
        }
        /// <summary>
        /// 获取功能对象实体模型
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public Model.T_Wo_WorkOrderType GetModel()
        {
            Model.T_Wo_WorkOrderType model = new Model.T_Wo_WorkOrderType();
            if (OperateId > 0 && id != null)
            {
                model = WorkOrderType.GetModel(OperateId);
                model.F_ModifyDate = DateTime.Now;
                model.F_Modifyby = 1 ;
            }
            model.F_Name = F_ParamCode.Text.Trim();
            model.F_Ename = F_Ename.Text.Trim();
            model.F_Remark = F_ParamDes.Text.Trim();
            model.F_DeleteFlag = 0;
            model.F_Sort = Convert.ToInt32(textF_Sort.Text.Trim());




            return model;
        }
        /// <summary>
        /// 操作ID
        /// </summary>
        public int OperateId
        {
            get
            {
                int id = 0;
                if (Request.QueryString["actionNodeID"] != null)
                    int.TryParse(Request.QueryString["actionNodeID"].ToString(), out id);
                return id;
            }
        }
        /// <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 ParentId
        {
            get
            {
                int id = 0;
                if (Request.QueryString["parentId"] != null)
                    int.TryParse(Request.QueryString["parentId"].ToString(), out id);
                return id;
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.T_Wo_WorkOrderType new_model = GetModel();
            Model.T_Wo_WorkOrderTypeState model = new Model.T_Wo_WorkOrderTypeState();
            try
            {
                int result=0;
                int layer = 1;
                object number = null;
                string[] arr;
                int id = 0;
                string temp;
                switch (Action)
                {
                    case "add":
                        if (ParentId != 0)
                        {
                            layer =Convert.ToInt32(WorkOrderType.GetModel(Convert.ToInt32(Oldf_id)).F_Layer);
                            new_model.F_Layer = layer + 1;
                            number = WorkOrderType.maxNumber(layer+1);
                        }
                        else {
                            new_model.F_Layer = layer;
                            number = WorkOrderType.maxNumber(layer);
                        }
                        new_model.F_ParentId = OperateId;//父节点ID
                        new_model.F_ParentName = new BLL.T_Wo_WorkOrderType().GetModel(OperateId).F_Name;
                        new_model.F_CreateDate = DateTime.Now;
                        new_model.F_Createby = 1;
                        if (ParentId == 0)
                        {
                            if (number == null)
                            {
                                new_model.F_Number = "0001";
                            }
                            else
                            {
                                int num = Convert.ToInt32("1" + number) + 1;
                                new_model.F_Number = num.ToString().Substring(1);
                            }
                        }
                        else {
                            if (number == null)
                            {
                                number = WorkOrderType.maxNumber(layer, Convert.ToInt32(Oldf_id));
                                temp = number.ToString();
                                temp = temp.Substring(temp.Length - 4, 4);
                                new_model.F_Number = number.ToString() + "0001";
                            }
                            else
                            {
                                temp = number.ToString();
                                temp = temp.Substring(temp.Length - 4, 4);
                                int num = Convert.ToInt32("1" + temp) + 1;
                                number = WorkOrderType.maxNumber(layer, Convert.ToInt32(Oldf_id));
                                new_model.F_Number = number.ToString() + num.ToString().Substring(1);
                            }
                        }
                        result = WorkOrderType.Add(new_model);
                        //arr = this.txtStateid.Value.Trim().ToString().Split(',');
                        arr = hfStatelist.Value.ToString().Split(',');
                        if (arr.Length >= 1)
                        {
                            id = Convert.ToInt32(WorkOrderType.GetMaxId());
                            for (int i = 0; i < arr.Length; i++)
                            {
                                model.F_WorkOrderStateId = int.Parse(arr[i]);
                                model.F_WorkOrderTypeId = id;
                                WorkOrderTypeState.Add(model);
                            }
                        }
                        if (result > 0)
                        {
                            InsertOptLogs(0, 0, "操作成功！ordertypemanageedit.aspx页面，新建操作。", 0);
                            MessageBoxToWindow("新建成功", "提示", "success");
                        }
                        else
                        {
                            InsertOptLogs(0, 0, "操作失败！ordertypemanageedit.aspx页面，新建操作。", 1);
                            MessageBoxToWindow("新建失败", "提示", "error");
                        }
                       // MessageBoxToWindow("新建", result ? "新建成功!" : "新建失败!", result ? "success" : "error");
                        break;

                    case "addedit":
                        result = WorkOrderTypeState.Delete(Convert.ToInt32(Oldf_id));
                        arr = hfStatelist.Value.ToString().Split(',');
                        if (arr.Length > 1)
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                model.F_WorkOrderStateId = int.Parse(arr[i]);
                                model.F_WorkOrderTypeId = Convert.ToInt32(Oldf_id);
                                WorkOrderTypeState.Add(model);
                            }
                        }
                        result = WorkOrderType.Update(new_model);
                        if (result > 0)
                        {
                            InsertOptLogs(0, 0, "操作成功！ordertypemanageedit.aspx页面，修改操作。", 0);
                            MessageBoxToWindow("修改成功", "提示", "success");
                        }
                        else {
                            InsertOptLogs(0, 0, "操作失败！ordertypemanageedit.aspx页面，修改操作。", 1);
                            MessageBoxToWindow("修改失败", "提示", "error");
                        }
                   
                        break;
                }
            }
            catch (Exception ex)
            {
                InsertOptLogs(0, 0, "操作异常！ordertypemanageedit.aspx页面，异常信息："+ex.ToString(), 2);
                MessageBoxToWindow("异常信息", ex.Message, "catch");
            }
        }
        /// <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);

        }
    }
}