using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using BLL;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.HJGL.HotProcessHard
{
    public partial class HotProessReportItem : PageBase
    {
        #region 定义项
        public string hotProessTrustItemId
        {
            get
            {
                return (string)ViewState["hotProessTrustItemId"];
            }
            set
            {
                ViewState["hotProessTrustItemId"] = value;
            }
        }
        public string WeldJointId
        {
            get
            {
                return (string)ViewState["WeldJointId"];
            }
            set
            {
                ViewState["WeldJointId"] = value;
            }
        }
        /// 
        /// 报告集合
        /// 
        private List HotProessReportList = new List();
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                this.hotProessTrustItemId = Request.Params["HotProessTrustItemId"];
                var hotProessFeedback = BLL.HotProessTrustItemService.GetHotProessTrustItemById(hotProessTrustItemId);
                if (hotProessFeedback != null) {
                    if (!string.IsNullOrEmpty(hotProessFeedback.IsCompleted.ToString())) {
                        drpIsCompleted.SelectedValue = hotProessFeedback.IsCompleted.Value.ToString();
                    }
                    if (!string.IsNullOrEmpty(hotProessFeedback.WeldJointId)) {
                        var getWeldJoint = WeldJointService.GetViewWeldJointById(hotProessFeedback.WeldJointId);
                        if (getWeldJoint != null)
                        {
                            this.WeldJointId = getWeldJoint.WeldJointId;
                            this.lbWeldJointCode.Text = getWeldJoint.WeldJointCode;
                            var getPipeline = PipelineService.GetPipelineByPipelineId(getWeldJoint.PipelineId);
                            if (getPipeline != null)
                            {
                                this.lbPipeLineCode.Text = getPipeline.PipelineCode;
                            }
                        }
                    }
                }
                
                //// 绑定表格
                this.BindGrid();
            }
        }
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            var getReports = HotProessReportService.GetHotProessReportListById(hotProessTrustItemId);
            foreach (var item in getReports)
            {
                Model.HJGL_HotProess_Report NewReport = new Model.HJGL_HotProess_Report();
                NewReport.HotProessReportId = item.HotProessReportId;
                NewReport.HotProessTrustItemId = item.HotProessTrustItemId;
                NewReport.WeldJointId = item.WeldJointId;
                NewReport.PointCount = item.PointCount;
                NewReport.RequiredT = item.RequiredT;
                NewReport.ActualT = item.ActualT;
                NewReport.RequestTime = item.RequestTime;
                NewReport.ActualTime = item.ActualTime;
                NewReport.RecordChartNo = item.RecordChartNo;
                HotProessReportList.Add(NewReport);
            }
            Grid1.DataSource = HotProessReportList;
            Grid1.DataBind();
        }
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        
        #endregion
        #region 增加按钮事件
        /// 
        /// 增加按钮事件
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotProessReportMenuId, Const.BtnAdd))
            {
                HotProessReportList = GetDetails();
                Model.HJGL_HotProess_Report NewReport = new Model.HJGL_HotProess_Report();
                NewReport.HotProessReportId = SQLHelper.GetNewID(typeof(Model.HJGL_HotProess_Report));
                NewReport.HotProessTrustItemId = hotProessTrustItemId;
                NewReport.WeldJointId =WeldJointId;
                HotProessReportList.Add(NewReport);
                Grid1.DataSource = HotProessReportList;
                Grid1.DataBind();
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        private List GetDetails()
        {
            HotProessReportList.Clear();
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                string PointCount = values.Value("PointCount");
                string RequiredT = values.Value("RequiredT");
                string ActualT = values.Value("ActualT");
                string RequestTime = values.Value("RequestTime");
                string ActualTime = values.Value("ActualTime");
                string RecordChartNo = values.Value("RecordChartNo");
                Model.HJGL_HotProess_Report NewReport = new Model.HJGL_HotProess_Report();
                NewReport.HotProessReportId = Grid1.Rows[i].DataKeys[0].ToString(); 
                NewReport.HotProessTrustItemId = hotProessTrustItemId;
                NewReport.WeldJointId = Grid1.Rows[i].DataKeys[1].ToString();
                if (!string.IsNullOrEmpty(PointCount))
                {
                    NewReport.PointCount = Convert.ToInt32(PointCount);
                }
                NewReport.RequiredT = RequiredT;
                NewReport.ActualT = ActualT;
                NewReport.RequestTime = RequestTime;
                NewReport.ActualTime = ActualTime;
                NewReport.RecordChartNo = RecordChartNo;
                HotProessReportList.Add(NewReport);
            }
            return HotProessReportList;
        }
        #endregion
        #region 保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string hotProessTrustItemId = Request.Params["HotProessTrustItemId"];
            var hotProessFeedback = BLL.HotProessTrustItemService.GetHotProessTrustItemById(hotProessTrustItemId);
            hotProessFeedback.IsCompleted = Convert.ToBoolean(drpIsCompleted.SelectedValue);
            hotProessFeedback.IsHardness = Convert.ToBoolean(drpIsCompleted.SelectedValue);
            
            BLL.HotProessTrustItemService.UpdateHotProessFeedback(hotProessFeedback);
            ///保存热处理报告
            HotProessReportService.DeleteAllHotProessReportById(hotProessTrustItemId);
            HotProessReportList = GetDetails();
            foreach (var item in HotProessReportList)
            {
                Model.HJGL_HotProess_Report NewReport = new Model.HJGL_HotProess_Report();
                NewReport.HotProessReportId = item.HotProessReportId;
                NewReport.HotProessTrustItemId = item.HotProessTrustItemId;
                NewReport.WeldJointId = item.WeldJointId;
                NewReport.PointCount = item.PointCount;
                NewReport.RequiredT = item.RequiredT;
                NewReport.ActualT = item.ActualT;
                NewReport.RequestTime = item.RequestTime;
                NewReport.ActualTime = item.ActualTime;
                NewReport.RecordChartNo = item.RecordChartNo;
                BLL.HotProessReportService.AddHotProessReport(NewReport);
            }
            Alert.ShowInTop("保存成功", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotProessReportMenuId, Const.BtnDelete))
            {
                if (Grid1.SelectedRowIndexArray.Length > 0)
                {
                    string strShowNotify = string.Empty;
                    foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                    {
                        string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                        var hotProessReport = BLL.HotProessReportService.GetHotProessReportById(rowID);
                        if (hotProessReport != null)
                        {
                            string cont = judgementDelete(rowID);
                            if (string.IsNullOrEmpty(cont))
                            {
                                BLL.AttachFileService.DeleteAttachFile(Funs.RootPath, rowID, Const.HJGL_HotProessReportMenuId);//删除附件
                                BLL.HotProessReportService.DeleteHotProessReportById(rowID);
                            }
                        }
                    }
                    BindGrid();
                    if (!string.IsNullOrEmpty(strShowNotify))
                    {
                        Alert.ShowInTop(strShowNotify, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        ShowNotify("删除成功!", MessageBoxIcon.Success);
                    }
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #region 判断是否可删除
        /// 
        /// 判断是否可以删除
        /// 
        /// 
        private string judgementDelete(string id)
        {
            string content = string.Empty;
            //if (Funs.DB.Project_ProjectUser.FirstOrDefault(x => x.UserId == id) != null)
            //{
            //    content += "已在【项目用户】中使用,不能删除!";
            //}            
            return content;
        }
        #endregion
        #endregion
        #region 行点击事件
        /// 
        /// Grid行点击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string hotProessReportId = Grid1.DataKeys[e.RowIndex][0].ToString();
            HotProessReportList = GetDetails();
            if (e.CommandName == "attchUrl")
            {
                PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/HotProcessHard&menuId={1}&edit=0", hotProessReportId, BLL.Const.HJGL_HotProessReportMenuId)));
            }
            if (e.CommandName == "del")//删除
            {
               var Report = HotProessReportList.FirstOrDefault(x => x.HotProessReportId == hotProessReportId);
                if (Report != null)
                {
                    HotProessReportList.Remove(Report);
                }
                this.Grid1.DataSource = HotProessReportList;
                this.Grid1.DataBind();
            }
        }
        #endregion
    }
}