using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.DataShow
{
    public partial class JDMonthDetail : PageBase
    {
        #region 项目主键
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        public string Month
        {
            get
            {
                return (string)ViewState["Month"];
            }
            set
            {
                ViewState["Month"] = value;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                if (!string.IsNullOrEmpty(Request.Params["month"]))
                {
                    this.Month = Request.Params["month"];
                }
                else
                {
                    //统计月份信息
                    DateTime months = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-01");
                    if (DateTime.Now.Day < 26)
                    {
                        months = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.AddMonths(-1).Month.ToString() + "-01");
                    }
                    this.Month = months.ToString("yyyy-MM-dd");
                }
                this.txtMonths.Text = this.Month.Replace("-01", "");
                BindGrid();
            }
        }
        #region  月份选择事件
        /// 
        /// 月份选择事件
        /// 
        /// 
        /// 
        protected void txtMonths_TextChanged(object sender, EventArgs e)
        {
            this.Month = string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(this.txtMonths.Text.Trim() + "-01"));
            BindGrid();
        }
        #endregion
        /// 
        /// 加载Grid
        /// 
        private 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();
        }
        /// 
        /// 加载数据
        /// 
        /// 
        private DataTable BindData()
        {
            string strSql = @"select mp.*,u.UnitName
,DutyPersonName = STUFF((SELECT ',' + p.UserName FROM dbo.Sys_User as p where PATINDEX('%,' + RTRIM(p.UserId) + ',%', ',' + mp.DutyPerson + ',') > 0 FOR XML PATH('')), 1, 1,'') 
from [dbo].[JDGL_MonthPlan] mp
left join Base_Unit u on u.UnitId=mp.UnitId
where mp.ProjectId=@ProjectId and mp.Months=@month order by mp.SortIndex";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.ProjectId));
            listStr.Add(new SqlParameter("@month", this.Month));
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            return tb;
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #region 表排序、分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
    }
}