using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.IO;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.SitePerson
{
    public partial class DayReportView : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string DayReportId
        {
            get
            {
                return (string)ViewState["DayReportId"];
            }
            set
            {
                ViewState["DayReportId"] = value;
            }
        }
        public string nowData
        {
            get
            {
                return (string)ViewState["nowData"];
            }
            set
            {
                ViewState["nowData"] = value;
            }
        }
        /// 
        /// 主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.ProjectId = this.CurrUser.LoginProjectId;
                this.DayReportId = Request.Params["DayReportId"];
                this.nowData = Request.Params["nowData"];
                DateTime cDate = DateTime.Now;
                if (this.nowData == "-1")
                {
                   var  dayReport = Funs.DB.SitePerson_DayReport.FirstOrDefault(x => x.DayReportId == this.DayReportId);
                    if (dayReport != null)
                    {
                        this.ProjectId = dayReport.ProjectId;
                        this.txtDayReportCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.DayReportId);
                        var user = BLL.UserService.GetUserByUserId(dayReport.CompileMan);
                        if (user != null)
                        {
                            this.txtCompileMan.Text = user.UserName;
                        }
                        if (dayReport.CompileDate.HasValue)
                        {
                            cDate = dayReport.CompileDate.Value;
                            this.txtCompileDate.Text = String.Format("{0:yyyy-MM-dd}", dayReport.CompileDate);                           
                        }
                    }
                }
                else
                {
                   var  dayReport = BLL.SitePerson_DayReportService.GetDayReportByDayReportId(this.DayReportId);
                    if (dayReport != null)
                    {
                        this.ProjectId = dayReport.ProjectId;
                        this.txtDayReportCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.DayReportId);
                        var user = BLL.UserService.GetUserByUserId(dayReport.CompileMan);
                        if (user != null)
                        {
                            this.txtCompileMan.Text = user.UserName;
                        }
                        if (dayReport.CompileDate.HasValue)
                        {
                            cDate = dayReport.CompileDate.Value;
                            this.txtCompileDate.Text = String.Format("{0:yyyy-MM-dd}", dayReport.CompileDate);
                        }
                    }
                }
                getDayReportDetails = (from x in Funs.DB.SitePerson_DayReportDetail
                                       join y in Funs.DB.SitePerson_DayReport on x.DayReportId equals y.DayReportId
                                       where y.ProjectId == this.CurrUser.LoginProjectId && y.CompileDate <=cDate
                                       select x).ToList();
                getYearDayReportDetails =( from x in getDayReportDetails
                                          join y in Funs.DB.SitePerson_DayReport on x.DayReportId equals y.DayReportId
                                          where y.CompileDate.Value.Year == cDate.Year && y.DayReportId != null
                                          select x).ToList();
                BindGrid();
                ///初始化审核菜单
               // this.ctlAuditFlow.MenuId = BLL.Const.DayReportMenuId;
               // this.ctlAuditFlow.DataId = this.DayReportId;
            }
        }
        #endregion
        private static List getDayReportDetails;
        private static List getYearDayReportDetails;
        #region 绑定明细数据
        /// 
        /// 绑定明细数据
        /// 
        private void BindGrid()
        {
            Model.SGGLDB db = Funs.DB;
            if (this.nowData == "-1")
            {
                var q = (from x in db.SitePerson_DayReportDetail
                         join a in db.SitePerson_DayReport on x.DayReportId equals a.DayReportId
                         join b in db.Base_Unit on x.UnitId equals b.UnitId
                         where x.DayReportId == this.DayReportId
                         orderby b.UnitCode
                         select new
                         {
                             x.DayReportDetailId,
                             x.DayReportId,
                             x.UnitId,
                             x.StaffData,
                             x.DayNum,
                             x.WorkTime,
                             x.CheckPersonNum,
                             x.RealPersonNum,
                             x.PersonWorkTime,
                             YearPersonWorkTime = getYears(x.UnitId),
                             TotalPersonWorkTime = getTotals(x.UnitId),
                             x.Remark,
                             b.UnitName,
                         }).ToList();
                Grid1.DataSource = q;
                Grid1.DataBind();
            }
            else
            {
                var q = (from x in db.SitePerson_DayReportDetail
                         join a in db.SitePerson_DayReport on x.DayReportId equals a.DayReportId
                         join b in db.Base_Unit on x.UnitId equals b.UnitId
                         where x.DayReportId == this.DayReportId
                         orderby b.UnitCode
                         select new
                         {
                             x.DayReportDetailId,
                             x.DayReportId,
                             x.UnitId,
                             x.StaffData,
                             x.DayNum,
                             x.WorkTime,
                             x.CheckPersonNum,
                             x.RealPersonNum,
                             x.PersonWorkTime,
                             YearPersonWorkTime = getYears(x.UnitId),
                             TotalPersonWorkTime = getTotals(x.UnitId),
                             x.Remark,
                             b.UnitName,
                         }).ToList();
                Grid1.DataSource = q;
                Grid1.DataBind();
            }
        }
        private decimal getYears(string UnitId)
        {
            return getYearDayReportDetails.Where(z => z.UnitId == UnitId).Sum(z => z.PersonWorkTime ?? 0);
        }
        private decimal getTotals(string UnitId)
        {
            return getDayReportDetails.Where(z => z.UnitId == UnitId).Sum(z => z.PersonWorkTime ?? 0);
        }
        #endregion
        #region 关闭弹出窗
        /// 
        /// 关闭弹出窗
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region Grid双击事件
        /// 
        /// Grid行双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            if (this.nowData != "-1")
            {
                btnMenuEdit_Click(null, null);
            }
        }
        #endregion
        #region 编辑
        /// 
        /// 编辑按钮
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            string dayReportDetailId = Grid1.SelectedRowID;
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("DayReportDetailEdit.aspx?DayReportDetailId={0}&type=view", dayReportDetailId, "查看 - ")));
        }
        #endregion
    }
}