using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.InformationProject
{
    public partial class LawRegulationIdentifyEdit : PageBase
    {
        #region 定义项
        /// 
        /// 法律法规辨识主键
        /// 
        public string LawRegulationIdentifyId
        {
            get
            {
                return (string)ViewState["LawRegulationIdentifyId"];
            }
            set
            {
                ViewState["LawRegulationIdentifyId"] = value;
            }
        }
        /// 
        /// 项目id
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.ProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                this.LawRegulationIdentifyId = Request.Params["lawRegulationIdentifyId"];
                drpLawsRegulationsType.DataTextField = "Name";
                drpLawsRegulationsType.DataValueField = "Name";
                drpLawsRegulationsType.DataSource = BLL.LawsRegulationsTypeService.GetLawsRegulationsTypeList();
                drpLawsRegulationsType.DataBind();
                this.hdLawRegulationId.Text = string.Empty;
                Model.Law_LawRegulationIdentify lawRegulationIdentify = BLL.LawRegulationIdentifyService.GetLawRegulationIdentifyByLawRegulationIdentifyId(LawRegulationIdentifyId);
                if (lawRegulationIdentify != null)
                {
                    this.ProjectId = lawRegulationIdentify.ProjectId;
                    this.txtLawRegulationIdentifyCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.LawRegulationIdentifyId);
                    this.txtCompileMan.Text = BLL.UserService.GetUserNameByUserId(lawRegulationIdentify.IdentifyPerson);
                    this.txtCheckDate.Text = string.Format("{0:yyyy-MM-dd}", lawRegulationIdentify.IdentifyDate);
                    this.txtRemark.Text = lawRegulationIdentify.Remark;
                    var viewLaw = from x in Funs.DB.View_Law_LawRegulationSelectedItem where x.LawRegulationIdentifyId == LawRegulationIdentifyId orderby x.LawRegulationCode select x;
                    if (viewLaw.Count() > 0)
                    {
                        this.Grid1.DataSource = viewLaw;
                        this.Grid1.DataBind();
                    }
                }
                else
                {
                    this.txtCompileMan.Text = this.CurrUser.UserName;
                    this.txtCheckDate.Text = string.Format("{0:yyyy-MM-dd}", System.DateTime.Now);
                    ////自动生成编码
                    this.txtLawRegulationIdentifyCode.Text = BLL.CodeRecordsService.ReturnCodeByMenuIdProjectId(BLL.Const.LawRegulationIdentifyMenuId, this.ProjectId, this.CurrUser.UnitId);
                }
                ///初始化审核菜单
                this.ctlAuditFlow.MenuId = BLL.Const.LawRegulationIdentifyMenuId;
                this.ctlAuditFlow.DataId = this.LawRegulationIdentifyId;
                this.ctlAuditFlow.ProjectId = this.ProjectId;
                this.ctlAuditFlow.UnitId = this.CurrUser.UnitId;
            }
        }
        #endregion
        #region 提交按钮
        /// 
        /// 提交按钮
        /// 
        /// 
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.ctlAuditFlow.NextStep == BLL.Const.State_1 && this.ctlAuditFlow.NextPerson == BLL.Const._Null)
            {
                ShowNotify("请选择下一步办理人!", MessageBoxIcon.Warning);
                return;
            }
            this.SaveData(BLL.Const.BtnSubmit);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData(BLL.Const.BtnSave);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(string type)
        {
            Model.Law_LawRegulationIdentify lawRegulationIdentify = new Model.Law_LawRegulationIdentify
            {
                LawRegulationIdentifyCode = this.txtLawRegulationIdentifyCode.Text.Trim(),
                ProjectId = this.ProjectId,
                IdentifyPerson = this.CurrUser.UserId,
                IdentifyDate = Funs.GetNewDateTime(this.txtCheckDate.Text.Trim()),
                UpdateDate = DateTime.Now,
                Remark = this.txtRemark.Text.Trim(),
                ////单据状态
                States = BLL.Const.State_0
            };
            if (type == BLL.Const.BtnSubmit)
            {
                lawRegulationIdentify.States = this.ctlAuditFlow.NextStep;
                if (lawRegulationIdentify.States == BLL.Const.State_2)
                {
                    lawRegulationIdentify.VersionNumber = BLL.SQLHelper.RunProcNewId2("SpGetVersionNumber", "Law_LawRegulationIdentify", "VersionNumber", this.ProjectId);
                }
            }
            if (!string.IsNullOrEmpty(this.LawRegulationIdentifyId))
            {
                lawRegulationIdentify.LawRegulationIdentifyId = this.LawRegulationIdentifyId;
                BLL.LawRegulationIdentifyService.UpdateLawRegulationIdentify(lawRegulationIdentify);
                BLL.LogService.AddSys_Log(this.CurrUser, lawRegulationIdentify.LawRegulationIdentifyCode, this.LawRegulationIdentifyId, BLL.Const.LawRegulationIdentifyMenuId, BLL.Const.BtnModify);
            }
            else
            {
                this.LawRegulationIdentifyId = SQLHelper.GetNewID(typeof(Model.Law_LawRegulationIdentify));
                lawRegulationIdentify.LawRegulationIdentifyId =  this.LawRegulationIdentifyId;
                BLL.LawRegulationIdentifyService.AddLawRegulationIdentify(lawRegulationIdentify);
                BLL.LogService.AddSys_Log(this.CurrUser, lawRegulationIdentify.LawRegulationIdentifyCode, this.LawRegulationIdentifyId, BLL.Const.LawRegulationIdentifyMenuId, BLL.Const.BtnAdd);
            }
            
            if (lawRegulationIdentify != null)
            {
                List lawsRegulationsTypes = BLL.LawsRegulationsTypeService.GetLawsRegulationsTypeList();
                BLL.LawRegulationSelectedItemService.DeleteLawRegulationSelectedItemByLawRegulationIdentifyId(LawRegulationIdentifyId);               
                JArray mergedData = Grid1.GetMergedData();
                foreach (JObject mergedRow in mergedData)
                {
                    string status = mergedRow.Value("status");
                    JObject values = mergedRow.Value("values");
                    Model.Law_LawRegulationSelectedItem lawRegulationSelectedItem = new Model.Law_LawRegulationSelectedItem
                    {
                        LawRegulationIdentifyId = this.LawRegulationIdentifyId,
                        LawRegulationId = values.Value("LawRegulationId").ToString()
                    };
                    Model.Base_LawsRegulationsType lawsRegulationsType = lawsRegulationsTypes.FirstOrDefault(x => x.Name == values.Value("LawsRegulationsTypeId").ToString());
                    if (lawsRegulationsType != null)
                    {
                        lawRegulationSelectedItem.LawRegulationGrade = lawsRegulationsType.Id;
                    }
                    lawRegulationSelectedItem.FitPerfomance = values.Value("FitPerfomance").ToString();
                    BLL.LawRegulationSelectedItemService.AddLawRegulationSelectedItem(lawRegulationSelectedItem);
                }
            }
            ////保存流程审核数据         
            this.ctlAuditFlow.btnSaveData(this.ProjectId,BLL.Const.LawRegulationIdentifyMenuId, this.LawRegulationIdentifyId, (type == BLL.Const.BtnSubmit ? true : false), string.Format("{0:yyyy-MM-dd}", lawRegulationIdentify.IdentifyDate), "../InformationProject/LawRegulationIdentifyView.aspx?LawRegulationIdentifyId={0}");
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.LawRegulationIdentifyId))
            {
                SaveData(BLL.Const.BtnSave);
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/LawRegulationIdentify&menuId={1}", this.LawRegulationIdentifyId, BLL.Const.LawRegulationIdentifyMenuId)));
        }
        #endregion
        #region 重选事件
        private static List pageItem = new List();
        /// 
        /// 重选事件
        /// 
        /// 
        /// 
        protected void btnReSet_Click(object sender, EventArgs e)
        {
            pageItem.Clear();
            JArray mergedData = Grid1.GetMergedData();
            foreach (JObject mergedRow in mergedData)
            {
                string status = mergedRow.Value("status");
                JObject values = mergedRow.Value("values");
                Model.View_Law_LawRegulationSelectedItem newItem = new Model.View_Law_LawRegulationSelectedItem
                {
                    LawRegulationIdentifyId = this.LawRegulationIdentifyId,
                    LawRegulationId = values.Value("LawRegulationId").ToString()
                };
                Model.Base_LawsRegulationsType lawsRegulationsType = Funs.DB.Base_LawsRegulationsType.FirstOrDefault(x => x.Name == values.Value("LawsRegulationsTypeId").ToString());
                if (lawsRegulationsType != null)
                {
                    newItem.LawsRegulationsTypeId = lawsRegulationsType.Id;
                    newItem.LawsRegulationsTypeName = lawsRegulationsType.Name;
                }
                newItem.FitPerfomance = values.Value("FitPerfomance").ToString();
                pageItem.Add(newItem);
                this.hdLawRegulationId.Text += newItem.LawRegulationId + ",";
            }
            string window = String.Format("CreateLawRegulationIdentify.aspx?LawRegulationIds={0}", this.hdLawRegulationId.Text, "选择 - ");
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdLawRegulationId.ClientID) + Window1.GetShowReference(window));
        }
        /// 
        /// 页面关闭事件
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, EventArgs e)
        {
            string LawRegulationIds = this.hdLawRegulationId.Text;
            var viewLaw = new List();
            if (!string.IsNullOrEmpty(LawRegulationIds))
            {
                List LawRegulationIdLists = Funs.GetStrListByStr(LawRegulationIds, ',');
                foreach (var id in LawRegulationIdLists)
                {
                    var law = BLL.LawRegulationListService.GetViewLawRegulationListById(id);
                    if (law != null)
                    {
                        Model.View_Law_LawRegulationSelectedItem newLawitem = new Model.View_Law_LawRegulationSelectedItem
                        {
                            LawRegulationSelectedItemId = SQLHelper.GetNewID(typeof(Model.Law_LawRegulationSelectedItem)),
                            LawRegulationIdentifyId = this.LawRegulationIdentifyId,
                            LawRegulationId = law.LawRegulationId,
                            LawRegulationCode = law.LawRegulationCode,
                            LawsRegulationsTypeId = law.LawsRegulationsTypeId,
                            LawsRegulationsTypeName = law.LawsRegulationsTypeName,
                            LawRegulationName = law.LawRegulationName,
                            ApprovalDate = law.ApprovalDate,
                            EffectiveDate = law.EffectiveDate,
                            Description = law.Description
                        };
                        if (law.Description.Length > 41)
                        {
                            newLawitem.ShortDescription = law.Description.Substring(0, 40);
                        }
                        else
                        {
                            newLawitem.ShortDescription = law.Description;
                        }
                        if (pageItem.Count() > 0)
                        {
                            var viewSelectItem = pageItem.FirstOrDefault(x => x.LawRegulationIdentifyId == this.LawRegulationIdentifyId && x.LawRegulationId == law.LawRegulationId);
                            if (viewSelectItem != null)
                            {
                                newLawitem.LawsRegulationsTypeId = viewSelectItem.LawsRegulationsTypeId;
                                newLawitem.LawsRegulationsTypeName = viewSelectItem.LawsRegulationsTypeName;
                                newLawitem.FitPerfomance = viewSelectItem.FitPerfomance;
                            }
                        }
                        viewLaw.Add(newLawitem);
                    }
                }
            }
            this.Grid1.DataSource = viewLaw;
            this.Grid1.DataBind();
            this.hdLawRegulationId.Text = string.Empty;
        }
        #endregion
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("法律法规辨识" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = 500;
            this.Grid1.DataSource = from x in Funs.DB.View_Law_LawRegulationSelectedItem where x.LawRegulationIdentifyId == LawRegulationIdentifyId orderby x.LawRegulationCode select x;
            this.Grid1.DataBind();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
#pragma warning disable CS0108 // “LawRegulationIdentifyEdit.GetGridTableHtml(Grid)”隐藏继承的成员“PageBase.GetGridTableHtml(Grid)”。如果是有意隐藏,请使用关键字 new。
        /// 
        /// 导出方法
        /// 
        /// 
        /// 
        private string GetGridTableHtml(Grid grid)
#pragma warning restore CS0108 // “LawRegulationIdentifyEdit.GetGridTableHtml(Grid)”隐藏继承的成员“PageBase.GetGridTableHtml(Grid)”。如果是有意隐藏,请使用关键字 new。
        {            
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            sb.Append("");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("| {0} | ", column.HeaderText);
            }
            sb.Append("
");
            foreach (GridRow row in grid.Rows)
            {
                grid.Columns[0].Visible = false;
                sb.Append("");
                foreach (GridColumn column in grid.Columns)
                {
                    string html = row.Values[column.ColumnIndex].ToString();
                    if (column.ColumnID == "tfNumber")
                    {
                        html = (row.FindControl("labNumber") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfDescription")
                    {
                        html = (row.FindControl("lblDescription") as AspNet.Label).Text;
                    }
                    sb.AppendFormat("| {0} | ", html);
                }
                sb.Append("
");
            }
            sb.Append("
");
            return sb.ToString();
        }
        #endregion
    }
}