using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using BLL;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class ShowWeldInfo : PageBase
    {
        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();
            }
        }
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT WeldInfo.ConsumablesId, WeldInfo.ConsumablesCode, WeldInfo.ConsumablesName, WeldInfo.SteelFormat,
                                     case WeldInfo.ConsumablesType when '1' then '焊丝' when '2' then '焊条' when '3' then '焊剂' else '' end as WeldTypeName,WeldInfo.WeldUnit,v.StockAmount
                                FROM Base_Consumables AS WeldInfo
                                LEFT JOIN (SELECT WeldId, sum(ISNULL(Amount,0)-ISNULL(UsingAmount,0)) AS StockAmount
                                             FROM dbo.Weld_StockIn 
                                            WHERE (ISNULL(Amount,0)-ISNULL(UsingAmount,0))>0  
                                            GROUP BY WeldId) v ON v.WeldId = WeldInfo.ConsumablesId
                                WHERE 1=1";
            List listStr = new List();
            if (drpWeldType.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND WeldInfo.ConsumablesType = @WeldTypeId";
                listStr.Add(new SqlParameter("@WeldTypeId", drpWeldType.SelectedValue));
            }
            if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
            {
                if (this.txtCode.Text.Contains("*"))
                {
                    string[] weldNames = this.txtCode.Text.Trim().Split('*');
                    strSql += " AND ConsumablesCode LIKE @ConsumablesCode1 AND ConsumablesCode LIKE @ConsumablesCode2 ";
                    listStr.Add(new SqlParameter("@ConsumablesCode1", "%" + weldNames[0] + "%"));
                    listStr.Add(new SqlParameter("@ConsumablesCode2", "%" + weldNames[1] + "%"));
                }
                else
                {
                    strSql += " AND ConsumablesCode LIKE @ConsumablesCode";
                    listStr.Add(new SqlParameter("@ConsumablesCode", "%" + this.txtCode.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 Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 分页下拉选择
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void drpWeldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region Grid双击事件
        /// 
        /// Grid双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            string itemsString = this.Grid1.SelectedRowID;
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(itemsString)
                   + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}