using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
namespace FineUIPro.Web.HotProcessHard
{
    public partial class HardTrust : PageBase
    {
        #region 定义项
        /// 
        /// 硬度委托主键
        /// 
        public string HardTrustID
        {
            get
            {
                return (string)ViewState["HardTrustID"];
            }
            set
            {
                ViewState["HardTrustID"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                this.HardTrustID = string.Empty;
                this.InitTreeMenu();//加载树
            }
        }
        #endregion
        #region 加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            this.tvControlItem.Nodes.Clear();
            TreeNode rootNode = new TreeNode();
            var project = BLL.Base_ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
            rootNode.Text = "[" + project.ProjectCode + "]" + project.ProjectName;
            rootNode.NodeID = "0";
            rootNode.Expanded = true;
            this.tvControlItem.Nodes.Add(rootNode);
            var trusts = (from x in Funs.DB.Hard_Trust
                          where x.ProjectId == this.CurrUser.LoginProjectId
                          select x).ToList();
            this.BindNodes(rootNode, trusts);
        }
        /// 
        ///  绑定树节点
        /// 
        /// 
        private void BindNodes(TreeNode node, List trusts)
        {
            foreach (var item in trusts)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = item.HardTrustNo;
                newNode.NodeID = item.HardTrustID;
                newNode.ToolTip = item.HardTrustNo;
                newNode.CommandName = Resources.Lan.RequestSheetNumber;
                newNode.EnableClickEvent = true;
                node.Nodes.Add(newNode);
            }
        }
        #endregion
        #region 点击TreeView
        /// 
        /// 点击TreeView
        /// 
        /// 
        /// 
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            this.HardTrustID = tvControlItem.SelectedNodeID;
            this.BindGrid();
        }
        #endregion
        #region 数据绑定
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        /// 
        /// 数据绑定
        /// 
        private void BindGrid()
        {
            this.SetTextTemp();
            this.PageInfoLoad(); ///页面输入提交信息
            string strSql = string.Empty;
            List listStr = new List();
            if (this.tvControlItem.SelectedNode.CommandName == Resources.Lan.RequestSheetNumber)
            {
                strSql = @"SELECT * "
                     + @" FROM dbo.View_Hard_TrustItem AS Trust"
                     + @" WHERE Trust.HardTrustID=@HardTrustID";
                listStr.Add(new SqlParameter("@HardTrustID", this.HardTrustID));
            }
            if (!string.IsNullOrEmpty(this.txtPipelineCode.Text.Trim()))
            {
                strSql += @" and Trust.PipelineCode like @PipelineCode ";
                listStr.Add(new SqlParameter("@PipelineCode", "%" + this.txtPipelineCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtWeldJointCode.Text.Trim()))
            {
                strSql += @" and Trust.WeldJointCode like @WeldJointCode ";
                listStr.Add(new SqlParameter("@WeldJointCode", "%" + this.txtWeldJointCode.Text.Trim() + "%"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            // 2.获取当前分页数据
            //var table = this.GetPagedDataTable(Grid1, tb1);
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #region 加载页面输入提交信息
        /// 
        /// 加载页面输入提交信息
        /// 
        private void PageInfoLoad()
        {
            this.SimpleForm1.Reset(); ///重置所有字段
            var trust = Funs.DB.View_Hard_Trust.FirstOrDefault(x => x.HardTrustID == this.HardTrustID);
            if (trust != null)
            {
                this.txtHardTrustNo.Text = trust.HardTrustNo;
                this.txtHardTrustUnit.Text = trust.HardTrustUnitName;
                this.txtInstallation.Text = trust.InstallationName;
                this.txtCheckUnit.Text = trust.CheckUnitName;
                this.txtHardTrustMan.Text = trust.HardTrustManName;
                if (trust.HardTrustDate != null)
                {
                    this.txtHardTrustDate.Text = string.Format("{0:yyyy-MM-dd}", trust.HardTrustDate);
                }
                this.txtHardnessMethod.Text = trust.HardnessMethod;
                this.txtHardnessRate.Text = trust.HardnessRate;
                this.txtStandards.Text = trust.Standards;
                this.txtInspectionNum.Text = trust.InspectionNum;
                this.txtCheckNum.Text = trust.CheckNum;
                this.txtTestWeldNum.Text = trust.TestWeldNum;
                this.txtSendee.Text = trust.Sendee;
                this.txtDetectionTime.Text = trust.DetectionTimeStr;
            }
        }
        #endregion
        /// 
        /// 情况
        /// 
        private void SetTextTemp()
        {
            this.txtHardTrustNo.Text = string.Empty;
            this.txtHardTrustUnit.Text = string.Empty;
            this.txtInstallation.Text = string.Empty;
            this.txtCheckUnit.Text = string.Empty;
            this.txtHardTrustMan.Text = string.Empty;
            this.txtHardTrustDate.Text = string.Empty;
            this.txtHardnessMethod.Text = string.Empty;
            this.txtHardnessRate.Text = string.Empty;
            this.txtStandards.Text = string.Empty;
            this.txtInspectionNum.Text = string.Empty;
            this.txtCheckNum.Text = string.Empty;
            this.txtTestWeldNum.Text = string.Empty;
            this.txtSendee.Text = string.Empty;
            this.txtDetectionTime.Text = string.Empty;
        }
        #endregion
        #region 分页排序
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #endregion
        #region 硬度委托 维护事件
        /// 
        /// 增加硬度委托
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotHardManageEditMenuId, Const.BtnAdd))
            {
                this.SetTextTemp();
                string window = String.Format("HardTrustEdit.aspx?HardTrustID={0}", string.Empty, "新增 - ");
                PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdHardTrustID.ClientID)
                  + Window1.GetShowReference(window));
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #region 编辑硬度委托
        /// 
        /// 编辑硬度委托
        /// 
        /// 
        /// 
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotHardManageEditMenuId, Const.BtnSave))
            {
                if (this.tvControlItem.SelectedNode != null)
                {
                    Model.Hard_Trust trust = BLL.Hard_TrustService.GetHardTrustById(this.tvControlItem.SelectedNodeID);
                    if (trust != null)
                    {
                        string window = String.Format("HardTrustEdit.aspx?HardTrustID={0}", this.HardTrustID, "编辑 - ");
                        PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdHardTrustID.ClientID)
                          + Window1.GetShowReference(window));
                    }
                    else
                    {
                        ShowNotify(Resources.Lan.SelectLeastOneRecord, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify(Resources.Lan.SelectLeastOneRecord, MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 删除硬度委托
        /// 
        /// 删除硬度委托
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_HotHardManageEditMenuId, Const.BtnDelete))
            {
                if (this.tvControlItem.SelectedNode != null)
                {
                    Model.Hard_Trust trust = BLL.Hard_TrustService.GetHardTrustById(this.tvControlItem.SelectedNodeID);
                    if (trust != null)
                    {
                        var hardTrustItems = BLL.Hard_TrustItemService.GetHardTrustItemByHardTrustId(this.HardTrustID);
                        foreach (var hardTrustItem in hardTrustItems)
                        {
                            //更新热处理委托明细的口已做硬度委托
                            Model.HotProess_TrustItem hotProessTrustItem = BLL.HotProessTrustItemService.GetHotProessTrustItemById(hardTrustItem.HotProessTrustItemId);
                            if (hotProessTrustItem != null)
                            {
                                hotProessTrustItem.IsTrust = null;
                                BLL.HotProessTrustItemService.UpdateHotProessTrustItem(hotProessTrustItem);
                            }
                            //删除硬度报告记录
                            BLL.Hard_ReportService.DeleteHard_ReportsByHardTrustItemID(hardTrustItem.HardTrustItemID);
                        }
                        BLL.Hard_TrustItemService.DeleteHardTrustItemById(this.HardTrustID);
                        BLL.Hard_TrustService.DeleteHardTrustById(this.HardTrustID);
                        //BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Resources.Lan.DeleteHardTrust);
                        Alert.ShowInTop(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success);
                        this.InitTreeMenu();
                        this.Grid1.DataSource = null;
                        this.Grid1.DataBind();
                        this.SetTextTemp();
                    }
                    else
                    {
                        ShowNotify(Resources.Lan.SelectDeleteRecord, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify(Resources.Lan.SelectDeleteRecord, MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #endregion
        #region 关闭弹出窗口及刷新页面
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            this.HardTrustID = this.hdHardTrustID.Text;
            this.BindGrid();
            //this.InitTreeMenu();
            this.hdHardTrustID.Text = string.Empty;
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void Tree_TextChanged(object sender, EventArgs e)
        {
            this.InitTreeMenu();
            //this.BindGrid();
        }
        #endregion
    }
}