using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.Door
{
    public partial class InOutManHoursItem : PageBase
    {
        /// 
        /// 项目id
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        /// 
        /// 单位
        /// 
        public string UnitName
        {
            get
            {
                return (string)ViewState["UnitName"];
            }
            set
            {
                ViewState["UnitName"] = value;
            }
        }
        /// 
        /// 岗位
        /// 
        public string PostName
        {
            get
            {
                return (string)ViewState["PostName"];
            }
            set
            {
                ViewState["PostName"] = value;
            }
        }
        /// 
        /// 开始时间
        /// 
        public string StartDate
        {
            get
            {
                return (string)ViewState["StartDate"];
            }
            set
            {
                ViewState["StartDate"] = value;
            }
        }
        /// 
        /// 结束时间
        /// 
        public string EndDate
        {
            get
            {
                return (string)ViewState["EndDate"];
            }
            set
            {
                ViewState["EndDate"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
                {
                    Grid1.PageSize = this.CurrUser.PageSize.Value;
                }
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                string strValue = this.CurrUser.SessionString;
                this.CurrUser.SessionString = string.Empty;
                if (!string.IsNullOrEmpty(strValue))
                {
                    var getlist = Funs.GetStrListByStr(strValue, '#');
                    if (getlist.Count() > 4)
                    {
                        this.ProjectId = getlist[0];
                        this.UnitName = getlist[1];
                        this.PostName = getlist[2];
                        this.StartDate = getlist[3];
                        this.EndDate = getlist[4];
                        GetPersonStatistic();
                    }
                }
            }
        }
        /// 
        /// 获取数据,合并相同行
        /// 
        private void GetPersonStatistic()
        {
            string strSql = @"SELECT NewID,T.ProjectId,T.InstallationName,T.UnitId,T.UnitName,T.EmployNO,T.EmployName,T.RecordDate
                                ,T.IDCardNo,T.Nation,T.PostName,T.WorkIn1,T.OffDuty1,T.WorkIn2,T.OffDuty2,T.WorkIn3,T.OffDuty3,T.ManHours
                                ,T.DepartmentID,T.DepartName,T.NationName,T.PostId,T.ManOverHours
                                FROM dbo.t_d_EmployInOutRecord T
                                LEFT JOIN dbo.SitePerson_Person AS P ON T.ProjectId=P.ProjectId AND T.IDCardNo=P.IdentityCard
                                WHERE T.ProjectId = @ProjectId ";
            List listStr = new List
            {
                new SqlParameter("@ProjectId", this.ProjectId),
            };
            if (this.UnitName == "未知")
            {
                strSql += " AND P.UnitId IS NULL";
            }
            else
            {
                strSql += " AND UnitName = @UnitName ";
                listStr.Add(new SqlParameter("@UnitName", this.UnitName));
            }
            if (this.PostName == "未知")
            {
                strSql += " AND P.WorkPostId IS NULL ";
            }
            else
            {
                strSql += " AND PostName = @PostName ";
                listStr.Add(new SqlParameter("@PostName", this.PostName));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                strSql += " AND RecordDate >= @StartDate";
                listStr.Add(new SqlParameter("@StartDate", Funs.GetNewDateTime(this.StartDate).Value));
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                strSql += " AND RecordDate <= @EndDate";
                listStr.Add(new SqlParameter("@EndDate", Funs.GetNewDateTime(this.EndDate).Value));
            }
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                strSql += " AND EmployName LIKE @EmployName";
                listStr.Add(new SqlParameter("@EmployName", "%" + this.txtName.Text.Trim() + "%"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #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
        #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(UnitName + PostName + "考勤记录" + filename, 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
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetPersonStatistic();
        }
    }
}