using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace FineUIPro.Web.Door
{
    public partial class AbsenceDuty : 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);           
                WorkPostService.InitWorkPostDropDownList(this.drpWorkPost, true);
                this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                GetPersonStatistic();
            }
        }
        /// 
        /// 获取数据
        /// 
        private void GetPersonStatistic()
        {
            if (!string.IsNullOrEmpty(this.txtStartDate.Text))
            {
                string unitId = null;
                string WorkPost = null;
                if (this.drpUnit.SelectedValue != Const._Null && !string.IsNullOrEmpty(this.drpUnit.SelectedValue))
                {
                    unitId = Funs.GetStringByArray(this.drpUnit.SelectedValueArray);
                }
                if (this.drpWorkPost.SelectedValue != Const._Null && !string.IsNullOrEmpty(this.drpWorkPost.SelectedValue))
                {
                    WorkPost = Funs.GetStringByArray(this.drpWorkPost.SelectedValueArray);
                }
                var getData = Funs.DB.spAbsenceDutyReport(this.ProjectId, unitId, WorkPost, Funs.GetNewDateTimeOrNow(this.txtStartDate.Text));
                DataTable tb = this.LINQToDataTable(getData.ToList());
                Grid1.RecordCount = tb.Rows.Count;
                var table = this.GetPagedDataTable(Grid1, tb);
                Grid1.DataSource = table;
                Grid1.DataBind();
            }
            else
            {
                Alert.ShowInParent("请选择日期!");
                return;                
            }
        }
        
        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 drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpUnit.SelectedValueArray = Funs.RemoveDropDownListNull(this.drpUnit.SelectedValueArray);
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void drpWorkPost_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkPost.SelectedValueArray = Funs.RemoveDropDownListNull(this.drpWorkPost.SelectedValueArray);
        }
    }
}