using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
namespace FineUIPro.Web.BaseInfo
{
    public partial class Qualification : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                BindGrid();
                btnNew.OnClientClick = Window1.GetShowReference("QualificationEdit.aspx") + "return false;";
            }
        }
        /// 
        /// 绑定数据
        /// 
        public void BindGrid()
        {
            DataTable tb = BindData();
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        protected DataTable BindData()
        {
            string strSql = @"select QualificationId,QualificationCode,QualificationName,QualificationType,Remark 
            from [dbo].[Base_Qualification] 
            where 1=1  ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtQualificationCode.Text.Trim()))
            {
                strSql += " AND (QualificationCode like @QualificationCode OR QualificationName like @QualificationCode)";
                listStr.Add(new SqlParameter("@QualificationCode", "%" + this.txtQualificationCode.Text.Trim() + "%"));
            }
            if (this.ddlQualificationType.SelectedValue != null && !string.IsNullOrEmpty(this.ddlQualificationType.SelectedValue.Trim()))
            {
                strSql += " AND QualificationType = @QualificationType";
                listStr.Add(new SqlParameter("@QualificationType", this.ddlQualificationType.SelectedValue.Trim()));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            return tb;
        }
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion 
        protected void btnMenuModify_Click(object sender, EventArgs e)
        {
            EditData();
        }
        protected void btnMenuDel_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    BLL.QualificationService.DeleteQualificationById(rowID);
                }
                BindGrid();
                ShowNotify("删除数据成功!", MessageBoxIcon.Success);
            }
        }
        /// 
        /// 编辑数据方法
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("QualificationEdit.aspx?QualificationId={0}", Grid1.SelectedRowID, "编辑 - ")));
        }
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.QualificationMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuModify.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDel.Hidden = false;
                }
            }
        }
        #endregion
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            EditData();
        }
        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 Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
    }
}