using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace FineUIPro.Web.Report
{
    public partial class MonthReport : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();//权限设置              
                //项目类型
                this.drpJobType.DataTextField = "ConstText";
                this.drpJobType.DataValueField = "ConstValue";
                this.drpJobType.DataSource = BLL.ConstService.GetConstListByGroupId(BLL.Const.ProjectPlanner_JobType);
                this.drpJobType.DataBind();
                Funs.FineUIPleaseSelectJobType(this.drpJobType);
                //项目状态
                this.drpJobStatus.DataTextField = "ConstText";
                this.drpJobStatus.DataValueField = "ConstValue";
                this.drpJobStatus.DataSource = BLL.ConstService.GetConstListByGroupId(BLL.Const.ProjectPlanner_JobStatus);
                this.drpJobStatus.DataBind();
                Funs.FineUIPleaseSelectJobStatus(this.drpJobStatus);
                //this.btnPrint.OnClientClick = Window1.GetShowReference("MonthPrint.aspx") + "return false;";
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = "SELECT EProjectId," +
                "ProjectControl_JobNo," +
                "ProjectControl_JobType," +
                "ProjectControl_JobStatus," +
                "ProjectControl_JobTitle," +
                "ProjectControl_BUCode," +
                "PM_General_Priority," +
                "PM_General_Category " +
                "FROM dbo.Editor_EProject WHERE 1=1 ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtJobNo.Text.Trim()))
            {
                strSql += " AND ProjectControl_JobNo LIKE @jobNo ";
                listStr.Add(new SqlParameter("@jobNo", this.txtJobNo.Text.Trim() + "%"));
            }
            if (this.drpJobType.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND ProjectControl_JobType=@JobType ";
                listStr.Add(new SqlParameter("@JobType", this.drpJobType.SelectedItem.Text));
            }
            if (this.drpJobStatus.SelectedValue != BLL.Const._Null && this.drpJobStatus.SelectedValue != null)
            {
                strSql += " AND ProjectControl_JobStatus=@Status ";
                listStr.Add(new SqlParameter("@Status", this.drpJobStatus.SelectedItem.Text));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            // 2.获取当前分页数据
            //var table = this.GetPagedDataTable(Grid1, tb1);
            Grid1.RecordCount = tb.Rows.Count;
            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_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion
        #region 关闭弹出窗口
        /// 
        /// 关闭窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 权限设置
        /// 
        /// 菜单按钮权限
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.MonthReportMenuId);
            if (buttonList.Count > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnPrint))
                {
                    this.btnPrint.Hidden = false;
                }
            }
        }
        #endregion
        #region 查询
        protected void drpJobType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpJobStatus.Items.Clear();
            if (drpJobType.SelectedText == "Other")
            {
                BLL.ConstService.InitConstValueProjectStatus(this.drpJobStatus, BLL.Const.ProjectPlanner_JobStatus, "3", true);
            }
            if (drpJobType.SelectedText != "Other")
            {
                BLL.ConstService.InitConstValueProjectStatus(this.drpJobStatus, BLL.Const.ProjectPlanner_JobStatus, "2", true);
            }
        }
        /// 
        /// 下拉框选择事件
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            string eprojectId = this.Grid1.SelectedRowID;
            if (string.IsNullOrEmpty(eprojectId))
            {
                Alert.ShowInParent("Please select at least one record!");
                return;
            }
            else
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ReportPrint.aspx?report=2&eProjectId={0}&parm={1}", eprojectId, "编辑 - ")));
            }
        }
    }
}