using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class EMaterialStatic : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                // 绑定表格
                this.BindGrid();
            }
            else if (GetRequestEventArgument() == "reloadGrid")
            {
                this.BindGrid();
            }
        }
        #endregion
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"select ConsumablesCode,records.UnitId,UnitName,
		                    ConsumablesName,
		                    ConsumablesType,
		                    MaterialCount,
		                    RecipientsCount,
		                    RecoveryCount,
		                    ElectrodeCount1,
		                    ElectrodeCount2
                            from HJGL_EMInventoryRecords as records
                            left join(select WMT_ID,UnitId,sum(RecipientsCount) as RecipientsCount,sum([RecoveryCount])as RecoveryCount
                            from  HJGL_ElectrodeRecoveryItem  ritem left join HJGL_ElectrodeRecovery  r on ritem.ElectrodeRecoveryId=r.ElectrodeRecoveryId
                            where  ProjectId = @ProjectId    
                            group by WMT_ID,UnitId
                            ) as rstatic on records.WMT_ID=rstatic.WMT_ID  and records.UnitId=rstatic.UnitId
							left join (
							select WMT_ID,UnitId,sum(ElectrodeCount) as  ElectrodeCount1
							from HJGL_ElectrodeBake
							where ProjectId = @ProjectId
							group by WMT_ID,UnitId
							) as bake on bake.WMT_ID = records.WMT_ID and bake.UnitId = records.UnitId
							left join (
							select WMT_ID,UnitId,sum(ElectrodeCount) as ElectrodeCount2
							from  HJGL_ElectrodeSecondaryBake  
							where ProjectId = @ProjectId
							group by WMT_ID,UnitId
							) as bakes on bakes.WMT_ID = records.WMT_ID and bakes.UnitId = records.UnitId
							left join Base_Consumables consumble on consumble.ConsumablesId=rstatic.WMT_ID
							left join Base_Unit unit on unit.UnitId =  records.UnitId
							where [ConsumablesCode] is not null";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            if (!string.IsNullOrEmpty(this.txtStartTime.Text))
            {
                strSql += " AND ConsumablesName like  @ConsumablesName";
                listStr.Add(new SqlParameter("@ConsumablesName", "%"+ txtEMaterialRegistCode .Text.Trim()+ "%"));
            }
            //if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            //{
            //    strSql += " AND EMaterialRegist.EMaterialRegistDate <= @EMaterialRegistDate";
            //    listStr.Add(new SqlParameter("@EMaterialRegistDate", Funs.GetNewDateTime(this.txtEndTime.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();
        }
        #endregion
        #region 增加材料到货登记及验收记录
        /// 
        /// 增加材料到货登记及验收记录
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_EMaterialRegistMenuId, Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(WindowEdit.GetShowReference(String.Format("EMaterialRegistEditNew.aspx?EMaterialRegistId={0}", string.Empty, "新增 - ")));
            }
            else
            {
                ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 删除数据
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_EMaterialRegistMenuId, Const.BtnDelete))
            {
                if (Grid1.SelectedRowIndexArray.Length > 0)
                {
                    foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                    {
                        string EMaterialRegistId = Grid1.DataKeys[rowIndex][0].ToString();
                        Model.SGGLDB db = Funs.DB;
                        Model.HJGL_EMaterialRegist eMaterialRegist = db.HJGL_EMaterialRegist.First(e => e.EMaterialRegistId == EMaterialRegistId);
                        BLL.HJGL_EMaterialRegistService.DeleteEMaterialRegistItem(this.CurrUser.LoginProjectId, eMaterialRegist.Unit, EMaterialRegistId);
                        // BLL.HJGL_EMaterialRegistService.DeleteEMaterialRegist(EMaterialRegistId);
                        db.HJGL_EMaterialRegist.DeleteOnSubmit(eMaterialRegist);
                        db.SubmitChanges();
                        this.BindGrid();
                    }
                }
            }
            else
            {
                ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 数据编辑事件
        /// 
        /// Grid行双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件通过
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        /// 
        /// 批量导入
        /// 
        /// 
        /// 
        protected void btnPackageIn_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_EMaterialRegistMenuId, Const.BtnSave))
            {
                
                PageContext.RegisterStartupScript(WindowEdit.GetShowReference(String.Format("EMaterialRegistIn.aspx", Grid1.SelectedRowID, "编辑 - ")));
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        
        /// 
        /// 编辑方法
        /// 
        private void EditData()
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_EMaterialRegistMenuId, Const.BtnSave))
            {
                if (Grid1.SelectedRowIndexArray.Length == 0)
                {
                    Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning);
                    return;
                }
                PageContext.RegisterStartupScript(WindowEdit.GetShowReference(String.Format("EMaterialRegistEditNew.aspx?EMaterialRegistId={0}", Grid1.SelectedRowID, "编辑 - ")));
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 分页 排序
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            this.BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);
            this.BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion        
    }
}