using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.HJGL.BaseInfo
{
    public partial class Manufacturer : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                this.BindGrid();
                //if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
                //{
                //    this.btnNew.Hidden = true;
                //    this.btnMenuEdit.Hidden = true;
                //    this.btnMenuDelete.Hidden = true;
                //    this.Grid1.EnableRowDoubleClickEvent = false;
                //}
            }
        }
        #endregion
        #region 类别变化事件
        /// 
        /// 类别变化事件
        /// 
        /// 
        /// 
        //protected void drpMaterialClass_TextChanged(object sender, EventArgs e)
        //{
        //    this.drpMaterialGroup.Items.Clear();
        //    Funs.FineUIPleaseSelect(this.drpMaterialGroup);
        //    this.drpMaterialGroup.SelectedValue = BLL.Const._Null;
        //}
        #endregion
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"select [ManufacturerID]
                      ,[ManufacturerName]
                      ,[ManufacturerCode]
                      ,[ProjectId]
                      ,[Memo]
                      ,[CreateDate]
                      ,[CreateId] from dbo.HJGL_EManufacturer WHERE ProjectId='"+CurrUser.LoginProjectId+"' ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtMaterialCode.Text.Trim()))
            {
                strSql += " AND ManufacturerName LIKE @ManufacturerName";
                listStr.Add(new SqlParameter("@ManufacturerName", "%" + this.txtMaterialCode.Text.Trim() + "%"));
            }
            strSql += " order by CreateDate desc";
               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();
        }
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 增加按钮事件
        /// 
        /// 增加按钮事件
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ManufacturerEdit.aspx", "新增 - ")));
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 增加厂家库按钮事件
        /// 
        /// 增加厂家库按钮事件
        /// 
        /// 
        /// 
        protected void btnLib_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("ManufacturerLibAdd.aspx", "新增 - ")));
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        
        #region 编辑
        /// 
        /// 双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑数据方法
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning);
                return;
            }
            ////双击事件 编辑权限有:编辑页面,无:查看页面 或者状态是完成时查看页面
            if (GetButtonPower(Const.BtnModify))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ManufacturerEdit.aspx?ManufacturerID={0}", Grid1.SelectedRowID, "编辑 - ")));
            }
           
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(Const.BtnDelete))
            {
                if (!string.IsNullOrEmpty(Grid1.SelectedRowID))
                {
                    string strShowNotify = string.Empty; 
                    var EManufacturer = Funs.DB.HJGL_EManufacturer.FirstOrDefault(x => x.ManufacturerID == Grid1.SelectedRowID);
                    if (EManufacturer != null)
                    {
                        Funs.DB.HJGL_EManufacturer.DeleteOnSubmit(EManufacturer);
                        Funs.DB.SubmitChanges();
                    }
                    BindGrid();
                    if (!string.IsNullOrEmpty(strShowNotify))
                    {
                        Alert.ShowInTop(strShowNotify, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        ShowNotify("删除成功!", MessageBoxIcon.Success);
                    }
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #region 判断是否可删除
        /// 
        /// 判断是否可以删除
        /// 
        /// 
        private string judgementDelete(string id)
        {
            string content = string.Empty;
            //if (Funs.DB.Pipeline_Pipeline.FirstOrDefault(x => x.MainMaterialId == id) != null || Funs.DB.Pipeline_WeldJoint.FirstOrDefault(x => x.Material1Id == id) != null)
            //{
            //    content += "已在【管线焊口信息】中使用,不能删除!";
            //}
            return content;
        }
        #endregion
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 查看按钮
        /// 
        /// 查看按钮
        /// 
        /// 
        /// 
        protected void btnView_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MaterialView.aspx?MaterialId={0}", Grid1.SelectedRowID, "查看 - ")));
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 
        /// 
        /// 
        /// 
        protected string ConvertMaterialClassCode(object MaterialClassId)
        {
            string name = string.Empty;
            return name;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected string ConvertMaterialGroupCode(object MaterialGroupId)
        {
            string name = string.Empty;
            return name;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected string ConvertSteelType(object SteelType)
        {
            string name = string.Empty;
            if (SteelType != null)
            {
                var dropValue = BLL.DropListService.HJGL_GetSteTypeList().FirstOrDefault(x => x.Value == SteelType.ToString());
                if (dropValue != null)
                {
                    name = dropValue.Text;
                }
            }
            return name;
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private bool GetButtonPower(string button)
        {
            return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGL_MaterialMenuId, button);
        }
        #endregion
    }
}