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 EMaterialRegist : 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  EMaterialRegist.[EMaterialRegistId]
                                  ,EMaterialRegist.[EMaterialRegistCode]
                                  ,EMaterialRegist.[EMaterialRegistDate]
                                  ,EMaterialRegist.[DeliveryMan]
                                  ,EMaterialRegist.[UnitName]
                                  ,EMaterialRegist.[CompileMan]
                                  ,EMaterialRegist.[CompileDate]
                                  ,EMaterialRegist.[ProjectId]
                                  ,EMaterialRegist.[SystemCode]
                                  ,EMaterialRegist.[PartACode]
                                  ,EMaterialRegist.[Condition]
                                  ,EMaterialRegist.[Name]
                                  ,EMaterialRegist.[SpecificationsModel]
                                  ,EMaterialRegist.[Standard]
                                  ,EMaterialRegist.[Material]
                                  ,EMaterialRegist.[SpecialInstructions]
                                  ,EMaterialRegist.[Unit]
                                  ,EMaterialRegist.[Amount]
                                  ,EMaterialRegist.[FourLocation]
                                  ,EManufacturer.ManufacturerName as [Manufacturer]
                                  ,EMaterialRegist.[DesignUnit]
                                  ,EMaterialRegist.[RealArriveAmount]
                                  ,EMaterialRegist.[OutProve]
                                  ,EMaterialRegist.[OutPrices]
                                  ,CompileManUser.UserName as CompileManName
                                  ,EMaterialRegist.[Supply], Consumables.ConsumablesName "
                + @" FROM HJGL_EMaterialRegist AS EMaterialRegist"
                + @" LEFT JOIN Sys_User AS CompileManUser ON EMaterialRegist.CompileMan=CompileManUser.UserId"
                + @" LEFT JOIN Base_Consumables AS Consumables ON Consumables.ConsumablesId=EMaterialRegist.WMT_ID"
                + @" LEFT JOIN HJGL_EManufacturer AS EManufacturer ON EManufacturer.ManufacturerID=EMaterialRegist.Manufacturer"
                + @" LEFT JOIN Base_Project AS Project ON EMaterialRegist.ProjectId=Project.ProjectId"
                + @" WHERE EMaterialRegist.ProjectId= @ProjectId";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            if (!string.IsNullOrEmpty(this.txtEMaterialRegistCode.Text.Trim()))
            {
                strSql += " AND EMaterialRegist.SystemCode LIKE @SystemCode";
                listStr.Add(new SqlParameter("@SystemCode", "%" + this.txtEMaterialRegistCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtStartTime.Text))
            {
                strSql += " AND EMaterialRegist.CompileDate >= @DateA";
                listStr.Add(new SqlParameter("@DateA", Funs.GetNewDateTime(this.txtStartTime.Text.Trim())));
            }
            if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                strSql += " AND EMaterialRegist.CompileDate <  @DateZ";
                listStr.Add(new SqlParameter("@DateZ", Funs.GetNewDateTime(this.txtEndTime.Text.Trim()).Value.AddDays(1)));
            }
            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_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, eMaterialRegist.Unit, eMaterialRegist.WMT_ID, "", "", -decimal.Parse(eMaterialRegist.Amount));
                        db.HJGL_EMaterialRegist.DeleteOnSubmit(eMaterialRegist);
                        db.SubmitChanges();
                       // BLL.HJGL_EMaterialRegistService.DeleteEMaterialRegist(EMaterialRegistId);
                        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        
    }
}