using System;
using System.Collections.Generic;
using System.Linq;
using BLL;
using System.Data;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.HJGL.HotProcessHard
{
    public partial class HotProessTrustEdit : PageBase
    {
        #region 定义项
        /// 
        /// 热处理委托主键
        /// 
        public string HotProessTrustId
        {
            get
            {
                return (string)ViewState["HotProessTrustId"];
            }
            set
            {
                ViewState["HotProessTrustId"] = value;
            }
        }
        /// 
        /// 装置主键
        /// 
        public string InstallationId
        {
            get
            {
                return (string)ViewState["InstallationId"];
            }
            set
            {
                ViewState["InstallationId"] = value;
            }
        }
        /// 
        /// 单位主键
        /// 
        public string UnitId
        {
            get
            {
                return (string)ViewState["UnitId"];
            }
            set
            {
                ViewState["UnitId"] = value;
            }
        }
        /// 
        /// 区域主键
        /// 
        public string WorkAreaId
        {
            get
            {
                return (string)ViewState["WorkAreaId"];
            }
            set
            {
                ViewState["WorkAreaId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.HotProessTrustId = Request.Params["HotProessTrustId"];
                this.InstallationId = Request.Params["InstallationId"];
                this.UnitId = Request.Params["UnitId"];
                this.WorkAreaId = Request.Params["WorkAreaId"];
                this.PageInfoLoad(); ///加载页面 
                List lists = BLL.HotProessTrustItemService.GetViewHotProessTrustItem(this.CurrUser.LoginProjectId, this.HotProessTrustId);
                this.BindGrid(lists); ////初始化页面
            }
        }
        #endregion
        #region 加载页面输入提交信息
        /// 
        /// 加载页面输入提交信息
        /// 
        private void PageInfoLoad()
        {
            var trust = BLL.HotProess_TrustService.GetHotProessTrustById(this.HotProessTrustId);
            if (trust != null)
            {
                //this.ProjectId = trust.ProjectId;
                this.txtHotProessTrustNo.Text = trust.HotProessTrustNo;
                if (trust.ProessDate.HasValue)
                {
                    this.txtProessDate.Text = string.Format("{0:yyyy-MM-dd}", trust.ProessDate);
                }
                this.InstallationId = trust.InstallationId;
                if (!string.IsNullOrEmpty(trust.UnitId))
                {
                    this.txtUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(trust.UnitId);
                    this.UnitId = trust.UnitId;
                }
                if (!string.IsNullOrEmpty(trust.WorkAreaId))
                {
                    this.txtWorkAreaCode.Text = BLL.WorkAreaService.GetWorkAreaCodeByWorkAreaId(trust.WorkAreaId);
                    this.WorkAreaId = trust.WorkAreaId;
                }
                if (!string.IsNullOrEmpty(trust.Tabler))
                {
                    this.txtTabler.Text = BLL.UserService.GetUserNameByUserId(trust.Tabler);
                }
                this.txtRemark.Text = trust.Remark;
            }
            else
            {
                string code = "";
                if (!string.IsNullOrEmpty(UnitId))
                {
                    var unit = BLL.UnitService.GetUnitByUnitId(UnitId);
                    this.txtUnitName.Text = unit.UnitName;
                    code += unit.UnitCode + "-";
                }
                if (!string.IsNullOrEmpty(WorkAreaId))
                {
                    var workArea = BLL.WorkAreaService.getWorkAreaByWorkAreaId(WorkAreaId);
                    this.txtWorkAreaCode.Text = workArea.WorkAreaName;
                    code += workArea.WorkAreaCode + "-";
                }
                this.txtTabler.Text = this.CurrUser.UserName;
                this.SimpleForm1.Reset(); //重置所有字段
                code += "PWHT-";
                this.txtHotProessTrustNo.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode5ByProjectId", "dbo.HJGL_HotProess_Trust", "HotProessTrustNo", this.CurrUser.LoginProjectId, code);
            }
        }
        #endregion
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindGrid(List lists)
        {
            DataTable tb = this.LINQToDataTable(lists);
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        #region 查找需要热处理的焊口
        /// 
        /// 查找需要热处理的焊口
        /// 
        /// 
        /// 
        protected void ckSelect_Click(object sender, EventArgs e)
        {
            string weldJointIds = string.Empty;
            for (int i = 0; i < Grid1.Rows.Count; i++)
            {
                string jotId = Grid1.DataKeys[i][0].ToString();
                weldJointIds += jotId + "|";
            }
            if (weldJointIds != string.Empty)
            {
                weldJointIds = weldJointIds.Substring(0, weldJointIds.Length - 1);
            }
            string strList = this.UnitId + "|" + this.WorkAreaId + "|" + this.HotProessTrustId;
            string window = String.Format("HotProessTrustItemEdit.aspx?strList={0}&weldJointIds={1}", strList, weldJointIds, "编辑 - ");
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdItemsString.ClientID) + Window1.GetShowReference(window));
        }
        #endregion
        #region Grid 关闭弹出窗口事件
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            string weldJointIds = string.Empty;
            if (!string.IsNullOrEmpty(hdItemsString.Text))
            {
                weldJointIds = hdItemsString.Text.Substring(0, hdItemsString.Text.LastIndexOf('|'));
                List lists = BLL.HotProess_TrustService.GetHotProessTrustAddItem(weldJointIds);
                this.BindGrid(lists);
            }
        }
        #endregion
        #region 热处理委托 提交事件
        /// 
        /// 编辑热处理委托
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotProessTrustMenuId, Const.BtnSave))
            {
                if (BLL.HotProess_TrustService.IsExistTrustCode(this.txtHotProessTrustNo.Text, this.HotProessTrustId, this.CurrUser.LoginProjectId))
                {
                    ShowNotify("委托单号已存在,请重新录入!", MessageBoxIcon.Warning);
                    return;
                }
                string unitWorkId = Request.Params["unitWorkId"];
                Model.HJGL_HotProess_Trust newHotProessTrust = new Model.HJGL_HotProess_Trust();
                newHotProessTrust.HotProessTrustNo = this.txtHotProessTrustNo.Text.Trim();
                newHotProessTrust.ProessDate = Funs.GetNewDateTime(this.txtProessDate.Text.Trim());
                newHotProessTrust.InstallationId = this.InstallationId;
                newHotProessTrust.ProjectId = this.CurrUser.LoginProjectId;
                newHotProessTrust.UnitId = this.UnitId;
                newHotProessTrust.WorkAreaId = this.WorkAreaId;
                newHotProessTrust.Tabler = this.CurrUser.UserId;
                newHotProessTrust.Remark = this.txtRemark.Text.Trim();
                if (!string.IsNullOrEmpty(this.HotProessTrustId))
                {
                    newHotProessTrust.HotProessTrustId = this.HotProessTrustId;
                    BLL.HotProess_TrustService.UpdateHotProessTrust(newHotProessTrust);
                    //BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Resources.Lan.ModifyPWHT);
                }
                else
                {
                    this.HotProessTrustId = SQLHelper.GetNewID(typeof(Model.HJGL_HotProess_Trust));
                    newHotProessTrust.HotProessTrustId = this.HotProessTrustId;
                    BLL.HotProess_TrustService.AddHotProessTrust(newHotProessTrust);
                    //BLL.Sys_LogService.AddLog(BLL.Const.System_3,this.CurrUser.LoginProjectId, this.CurrUser.UserId, Resources.Lan.AddPWHT);
                }
                //BLL.HotProessTrustItemService.DeleteHotProessTrustItemById(this.HotProessTrustId);
                this.CollectGridJointInfo();//收集Grid页面信息,增加明细
                ShowNotify("保存成功!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(newHotProessTrust.HotProessTrustId)
                  + ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 收集Grid页面信息,提交明细
        /// 
        /// 收集Grid页面信息,提交明细
        /// 
        /// 
        private void CollectGridJointInfo()
        {
            JArray mergedData = Grid1.GetMergedData();
            var items = from x in Funs.DB.HJGL_HotProess_TrustItem select x;
            foreach (JObject mergedRow in mergedData)
            {
                JObject values = mergedRow.Value("values");
                Model.HJGL_HotProess_TrustItem newTrustItem = new Model.HJGL_HotProess_TrustItem();
                newTrustItem.HotProessTrustId = this.HotProessTrustId;
                newTrustItem.WeldJointId = values.Value("WeldJointId").ToString();
                string hotProessTrustItemId = values.Value("HotProessTrustItemId").ToString();
                var item = items.FirstOrDefault(x => x.HotProessTrustItemId == hotProessTrustItemId);
                if (item == null)
                {
                    BLL.HotProessTrustItemService.AddHotProessTrustItem(newTrustItem);
                }
            }
        }
        #endregion     
        #region 右键删除事件
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hdItemsString.Text))
            {
                this.hdItemsString.Text = this.hdItemsString.Text.Substring(0, this.hdItemsString.Text.LastIndexOf('|'));
            }
            var trust = BLL.HotProess_TrustService.GetHotProessTrustById(this.HotProessTrustId);
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                List GetHotProessTrustItem = new List();
                if (!string.IsNullOrEmpty(this.hdItemsString.Text))
                {
                    GetHotProessTrustItem = BLL.HotProess_TrustService.GetHotProessTrustAddItem(this.hdItemsString.Text);
                }
                else if (string.IsNullOrEmpty(this.hdItemsString.Text) && this.HotProessTrustId != null)
                {
                    GetHotProessTrustItem = BLL.HotProess_TrustService.GetHotProessTrustItem(this.CurrUser.LoginProjectId, this.HotProessTrustId);
                }
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    var item = GetHotProessTrustItem.FirstOrDefault(x => x.WeldJointId == rowID);
                    if (item != null)
                    {
                        if (string.IsNullOrEmpty(this.HotProessTrustId))   //新增记录可直接删除
                        {
                            GetHotProessTrustItem.Remove(item);
                        }
                        else
                        {
                            if (item.OldIsCompleted != null)
                            {
                                ShowNotify("已生成热处理报告,不能删除!", MessageBoxIcon.Warning);
                                return;
                            }
                            else
                            {
                                GetHotProessTrustItem.Remove(item);
                                BLL.HotProessTrustItemService.DeleteHotProessTrustItemByItemId(item.HotProessTrustItemId);
                            }
                        }
                    }
                }
                BindGrid(GetHotProessTrustItem);
                ShowNotify("删除成功!", MessageBoxIcon.Success);
            }
        }
        #endregion        
    }
}