using BLL;
using System;
using System.Data;
using System.Linq;
namespace FineUIPro.Web.Door
{
    public partial class InOutList : PageBase
    {
        /// 
        /// 项目id
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        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.ProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                Funs.DropDownPageSize(this.ddlPageSize);
                UnitService.InitUnitDropDownList(this.drpUnit, this.ProjectId, true);
                this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd HH:mm}", DateTime.Now.AddDays(-1));
                this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd HH:mm}", DateTime.Now);
                if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
                {
                    Grid1.PageSize = this.CurrUser.PageSize.Value;
                }
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
              //  GetPersonStatistic();
            }
        }
        /// 
        /// 获取数据,合并相同行
        /// 
        private void GetPersonStatistic()
        {
            if (!string.IsNullOrEmpty(this.ProjectId))
            {
                string unitId = this.drpUnit.SelectedValue != Const._Null ? this.drpUnit.SelectedValue : null;
                string name = !string.IsNullOrEmpty(this.txtName.Text.Trim()) ? this.txtName.Text.Trim() : null;
                var getData = InOutService.getListData(this.ProjectId, this.rbCheckType.SelectedValue, unitId, name, this.txtStartDate.Text, this.txtEndDate.Text, Grid1.PageIndex , Grid1.PageSize);//Funs.DB.SP_InOutList(this.ProjectId, unitId, name, this.rbCheckType.SelectedValue, startDate, endDate, Grid1.PageIndex + 1, Grid1.PageSize);
                Grid1.RecordCount = InOutService.count;
                Grid1.DataSource = getData;
                Grid1.DataBind();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetPersonStatistic();
        }
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("现场考勤记录" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = this.Grid1.RecordCount;
            GetPersonStatistic();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        #endregion
        #region 分页 排序
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GetPersonStatistic();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);
            GetPersonStatistic();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            GetPersonStatistic();
        }
        #endregion
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BLL.GetDataService.CorrectingPersonInOutNumberD(this.CurrUser.LoginProjectId);
            Alert.ShowInParent("操作完成", MessageBoxIcon.Success);
        }
    }
}