using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.Environmental
{
    public partial class ProjectOperationReportEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string ProjectOperationReportId
        {
            get
            {
                return (string)ViewState["ProjectOperationReportId"];
            }
            set
            {
                ViewState["ProjectOperationReportId"] = value;
            }
        }
        #endregion
        #region 项目主键
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        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"];
                }
                BLL.ConstValue.InitConstValueDropDownList(this.drpYear, BLL.ConstValue.Group_0008, true);
                BLL.ConstValue.InitConstValueDropDownList(this.ddlQuarter, BLL.ConstValue.Group_0011, true);
                this.ProjectOperationReportId = Request.Params["ProjectOperationReportId"];
                if (!string.IsNullOrEmpty(this.ProjectOperationReportId))
                {
                    var projectOperationReport = Funs.DB.Environmental_ProjectOperationReport.FirstOrDefault(s => s.BusinessReportId == this.ProjectOperationReportId);
                    if (projectOperationReport != null)
                    {
                        this.ProjectId = projectOperationReport.ProjectId;
                        #region 赋值
                        if (projectOperationReport.Year != null)
                        {
                            this.drpYear.SelectedValue = projectOperationReport.Year.ToString();
                        }
                        if (projectOperationReport.Quarters != null)
                        {
                            this.ddlQuarter.SelectedValue = projectOperationReport.Quarters.ToString();
                        }
                        if (!string.IsNullOrWhiteSpace(projectOperationReport.FillingMan))
                        {
                            this.txtFillingMan.Text = projectOperationReport.FillingMan.ToString();
                        }
                        if (projectOperationReport.FillingDate != null)
                        {
                            this.txtFillingDate.Text = projectOperationReport.FillingDate.Value.ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrWhiteSpace(projectOperationReport.Code))
                        {
                            this.txtCode.Text = projectOperationReport.Code;
                        }
                        if (projectOperationReport.PersonNum != null)
                        {
                            this.txtPersonNum.Text = projectOperationReport.PersonNum.ToString();
                        }
                        if (projectOperationReport.TotalAssets != null)
                        {
                            this.txtTotalAssets.Text = projectOperationReport.TotalAssets.ToString();
                        }
                        if (projectOperationReport.TotalValue != null)
                        {
                            this.txtTotalValue.Text = projectOperationReport.TotalValue.ToString();
                        }
                        if (projectOperationReport.NewInvestment != null)
                        {
                            this.txtNewInvestment.Text = projectOperationReport.NewInvestment.ToString();
                        }
                        #endregion
                    }
                }
                else
                {
                    DateTime showDate = DateTime.Now.AddMonths(-3);
                    this.ddlQuarter.SelectedValue = Funs.GetNowQuarterlyByTime(showDate).ToString();
                    this.drpYear.SelectedValue = showDate.Year.ToString();
                    this.txtFillingMan.Text = this.CurrUser.UserName;
                    this.txtFillingDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    DateTime startTime = Funs.GetQuarterlyMonths(this.drpYear.SelectedValue, this.ddlQuarter.SelectedValue);
                    DateTime endTime = startTime.AddMonths(3);
                    GetData(startTime, endTime);
                }
            }
        }
        #endregion
        #region 保存、提交
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpYear.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择年度", MessageBoxIcon.Warning);
                return;
            }
            if (this.ddlQuarter.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择季度", MessageBoxIcon.Warning);
                return;
            }
            this.SaveData(BLL.Const.BtnSave);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(string type)
        {
            Model.Environmental_ProjectOperationReport operationReport = new Model.Environmental_ProjectOperationReport
            {
                ProjectId = this.ProjectId
            };
            if (this.drpYear.SelectedValue != BLL.Const._Null)
            {
                operationReport.Year = Funs.GetNewInt(this.drpYear.SelectedValue);
            }
            if (this.ddlQuarter.SelectedValue != BLL.Const._Null)
            {
                operationReport.Quarters = Funs.GetNewInt(this.ddlQuarter.SelectedValue);
            }
            operationReport.FillingMan = this.txtFillingMan.Text;
            operationReport.FillingDate = Funs.GetNewDateTime(this.txtFillingDate.Text);
            operationReport.Code = this.txtCode.Text.Trim();
            operationReport.PersonNum = Funs.GetNewInt(this.txtPersonNum.Text.Trim());
            operationReport.TotalAssets = Funs.GetNewDecimalOrZero(this.txtTotalAssets.Text.Trim());
            operationReport.TotalValue = Funs.GetNewDecimalOrZero(this.txtTotalValue.Text.Trim());
            operationReport.NewInvestment = Funs.GetNewDecimalOrZero(this.txtNewInvestment.Text.Trim());
            if (!string.IsNullOrEmpty(this.ProjectOperationReportId))
            {
                var model = Funs.DB.Environmental_ProjectOperationReport.FirstOrDefault(s => s.BusinessReportId == this.ProjectOperationReportId);
                if (model != null)
                {
                    model.Code = operationReport.Code;
                    model.PersonNum = operationReport.PersonNum;
                    model.TotalAssets = operationReport.TotalAssets;
                    model.TotalValue = operationReport.TotalValue;
                    model.NewInvestment = operationReport.NewInvestment;
                    model.Quarters = operationReport.Quarters;
                    model.Year = operationReport.Year;
                    model.FillingMan = operationReport.FillingMan;
                    model.FillingDate = operationReport.FillingDate;
                    BLL.LogService.AddSys_Log(this.CurrUser, operationReport.Year.ToString() + "-" + operationReport.Quarters.ToString(), operationReport.BusinessReportId, BLL.Const.ProjectOperationReportMenuId, BLL.Const.BtnModify);
                    Funs.DB.SubmitChanges();
                }
            }
            else
            {
                var model = (from x in Funs.DB.Environmental_ProjectOperationReport
                             where x.ProjectId == operationReport.ProjectId && x.Year == operationReport.Year && x.Quarters == operationReport.Quarters
                             select x).FirstOrDefault();
                if (model == null)
                {
                    this.ProjectOperationReportId = SQLHelper.GetNewID(typeof(Model.Environmental_ProjectOperationReport));
                    operationReport.BusinessReportId = this.ProjectOperationReportId;
                    Funs.DB.Environmental_ProjectOperationReport.InsertOnSubmit(operationReport);
                    BLL.LogService.AddSys_Log(this.CurrUser, operationReport.Year.ToString() + "-" + operationReport.Quarters.ToString(), operationReport.BusinessReportId,
                        BLL.Const.ProjectOperationReportMenuId, BLL.Const.BtnAdd);
                    Funs.DB.SubmitChanges();
                }
                else
                {
                    Alert.ShowInTop("该季度记录已存在", MessageBoxIcon.Warning);
                    return;
                }
            }
        }
        #endregion
        #region 年季度变化事件
        /// 
        /// 年季度变化事件
        /// 
        /// 
        /// 
        protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpYear.SelectedValue != BLL.Const._Null && this.ddlQuarter.SelectedValue != BLL.Const._Null)
            {
                DateTime startTime = Funs.GetQuarterlyMonths(this.drpYear.SelectedValue, this.ddlQuarter.SelectedValue);
                DateTime endTime = startTime.AddMonths(3);
                GetData(startTime, endTime);
            }
        }
        #endregion
        #region 获取数据
        /// 
        /// 获取数据
        /// 
        /// 
        /// 
        private void GetData(DateTime startTime, DateTime endTime)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
            }
        }
        #endregion
    }
}