using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using BLL;
using System.Linq;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class StockInit : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                this.drpWeldType.DataTextField = "Text";
                this.drpWeldType.DataValueField = "Value";
                this.drpWeldType.DataSource = BLL.DropListService.HJGL_ConsumablesTypeList();
                this.drpWeldType.DataBind();
                Funs.FineUIPleaseSelect(this.drpWeldType);
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT StockIn.StockInId,StockIn.WeldId,StockIn.Amount,StockIn.Weight,Users.UserName AS Materialman,StockIn.InStockDate,
                                     Weld.WeldCode,Weld.ConsumablesCode,Weld.WeldSpec, Weld.ConsumablesType,StockIn.Warrantybook,
                                    (unit.UnitName+'('+store.UnitStoreName+')') AS UnitStoreName,
                                    (CASE WHEN (StockIn.AttachUrl!='' AND StockIn.AttachUrl IS NOT NULL) THEN '是' ELSE '否' END) IsUploadAttach,
                                    StockIn.RecycleWeight
                              FROM dbo.Weld_StockIn AS StockIn
                              LEFT JOIN dbo.Base_Consumables AS Weld ON Weld.ConsumablesId=StockIn.WeldId
                              LEFT JOIN dbo.Sys_User AS Users ON Users.UserId=StockIn.Materialman
                              LEFT JOIN dbo.Weld_UnitStore store ON store.UnitStoreId=StockIn.UnitStoreId
							  LEFT JOIN dbo.Base_Unit unit ON unit.UnitId=store.UnitId
                              WHERE StockIn.Flag='0'";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtConsumablesCode.Text.Trim()))
            {
                strSql += " AND Weld.ConsumablesCode LIKE @ConsumablesCode";
                listStr.Add(new SqlParameter("@ConsumablesCode", "%" + this.txtConsumablesCode.Text.Trim() + "%"));
            }
            if (drpWeldType.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND Weld.ConsumablesType = @WeldTypeId";
                listStr.Add(new SqlParameter("@WeldTypeId", drpWeldType.SelectedValue));
            }
            if (CurrUser.UserId != Const.sysglyId && CurrUser.UserId != Const.hfnbdId)
            {
                strSql += " AND store.UnitId = @UnitId";
                listStr.Add(new SqlParameter("@UnitId", CurrUser.UnitId));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            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 Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #region 弹出编辑窗口关闭事件
        /// 
        /// 弹出编辑窗体关闭事件
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void drpWeldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 增加按钮
        /// 
        /// 增加按钮
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGL_StockInitMenuId, BLL.Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("StockInitEdit.aspx?StockInId={0}", string.Empty, "编辑 - ")));
            }
        }
        #endregion
        #region 编辑
        /// 
        /// 双击Grid事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            btnMenuView_Click(null, null);
        }
        /// 
        /// 编辑按钮
        /// 
        /// 
        /// 
        //protected void btnMenuEdit_Click(object sender, EventArgs e)
        //{
        //    this.EditData();
        //}
        ///// 
        ///// 编辑数据方法
        ///// 
        //private void EditData()
        //{
        //    if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGL_StockInitMenuId, BLL.Const.BtnModify))
        //    {
        //        if (Grid1.SelectedRowIndexArray.Length == 0)
        //        {
        //            Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
        //            return;
        //        }
        //        string id = Grid1.SelectedRowID;
        //        if (!string.IsNullOrEmpty(id))
        //        {
        //            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("StockInitEdit.aspx?StockInId={0}", id, "编辑 - ")));                     
        //        }
        //    }
        //    else
        //    {
        //        Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
        //        return;
        //    }
        //}
        /// 
        /// 查看初始化明细
        /// 
        /// 
        /// 
        protected void btnMenuView_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            string id = Grid1.SelectedRowID;
            if (!string.IsNullOrEmpty(id))
            {
                var q = BLL.StockInService.GetStockInById(id);
                if (q.UnitStoreId != null)
                {
                    var s = BLL.UnitStoreService.GetUnitStoreById(q.UnitStoreId);
                    if (s != null && s.UnitId == CurrUser.UnitId)
                    {
                        PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("StockInitEdit.aspx?StockInId={0}", id, "编辑 - ")));
                    }
                    else
                    {
                        Alert.ShowInTop("非本单位的数据,不能修改!", MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGL_StockInitMenuId, BLL.Const.BtnDelete))
            {
                if (Grid1.SelectedRowIndexArray.Length > 0)
                {
                    bool isShow = false;
                    if (Grid1.SelectedRowIndexArray.Length == 1)
                    {
                        isShow = true;
                    }
                    foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                    {
                        string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                        var stockIn = BLL.StockInService.GetStockInById(rowID);
                        if (this.judgementDelete(rowID, isShow))
                        {
                            BLL.StockInService.DeleteStockInById(rowID);
                            BLL.LogService.AddSys_Log(this.CurrUser, stockIn.Warrantybook, stockIn.StockInId, BLL.Const.HJGL_UnitStoreMenuId, "删除库存初始化");
                        }
                    }
                    this.BindGrid();
                    ShowNotify("删除数据成功!", MessageBoxIcon.Success);
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 判断是否可删除
        /// 
        /// 
        /// 
        /// 
        private bool judgementDelete(string rowID, bool isShow)
        {
            string content = string.Empty;
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content);
                }
                return false;
            }
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 获取焊材相同的数量和
        /// 
        /// 
        /// 
        protected string ConvertAmount(object weldId)
        {
            decimal? sumAmount = 0;
            if (weldId != null)
            {
                var stockIns = BLL.StockInService.GetStockInListByWeldId(weldId.ToString());
                if (stockIns != null)
                {
                    foreach (var item in stockIns)
                    {
                        if (item.Flag == "0")
                        {
                            sumAmount += item.Amount;
                        }
                    }
                }
            }
            return string.Format("{0:0.##}", sumAmount);
        }
        /// 
        /// 获取焊材相同的重量和
        /// 
        /// 
        /// 
        protected string ConvertWeight(object weldId)
        {
            decimal? sumWeight = 0;
            if (weldId != null)
            {
                var stockIns = BLL.StockInService.GetStockInListByWeldId(weldId.ToString());
                if (stockIns != null)
                {
                    foreach (var item in stockIns)
                    {
                        if (item.Flag == "0")
                        {
                            sumWeight += item.Weight;
                        }
                    }
                }
            }
            return string.Format("{0:0.##}", sumWeight);
        }
        #endregion
        /// 
        /// 得到焊材类型
        /// 
        /// 
        /// 
        protected string ConvertConsumablesType(object ConsumablesType)
        {
            string name = string.Empty;
            if (ConsumablesType != null)
            {
                var dropValue = BLL.DropListService.HJGL_ConsumablesTypeList().FirstOrDefault(x => x.Value == ConsumablesType.ToString());
                if (dropValue != null)
                {
                    name = dropValue.Text;
                }
            }
            return name;
        }
    }
}