using BLL;
using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.JDGL.Check
{
    public partial class MonthPlanEditNew : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        /// 
        /// 默认月
        /// 
        private string Months
        {
            get
            {
                return (string)ViewState["Months"];
            }
            set
            {
                ViewState["Months"] = value;
            }
        }
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Id = Request.Params["Id"];
                this.ProjectId = this.CurrUser.LoginProjectId;
                BLL.ProjectUnitService.InitUnitDropDownList(drpUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, false);
                //五环责任人
                UserService.Init2(drpDutyPerson, this.CurrUser.LoginProjectId, false);
                if (!string.IsNullOrEmpty(Id))
                {
                    var model = Funs.DB.JDGL_MonthPlan.FirstOrDefault(x => x.MonthPlanId == Id);
                    if (model != null)
                    {
                        txtMonths.Text = string.Format("{0:yyyy-MM}", model.Months);
                        txtMonths.Readonly = true;
                        drpUnit.SelectedValue = model.UnitId;
                        if (model.IsOK == true)
                        {
                            this.drpIsOK.SelectedValue = "已完成";
                        }
                        else
                        {
                            this.drpIsOK.SelectedValue = "未完成";
                        }
                        if (!string.IsNullOrEmpty(model.DutyPerson))
                        {
                            List listPersonId = model.DutyPerson.Split(',').ToList();
                            drpDutyPerson.SelectedValueArray = listPersonId.ToArray();
                        }
                        if (model.PlanDate != null)
                            this.txtPlanDate.Text = Convert.ToDateTime(model.PlanDate).ToString("yyyy-MM-dd");
                        if (model.RealDate != null)
                            this.txtRealDate.Text = Convert.ToDateTime(model.RealDate).ToString("yyyy-MM-dd");
                        txtNodeContent.Text = model.NodeContent;
                        txtRemark.Text = model.Remark;
                    }
                }
                else
                {
                    this.Months = Request.Params["Months"];
                    this.txtMonths.Text = !string.IsNullOrWhiteSpace(this.Months) ? this.Months : DateTime.Now.ToString("yyyy-MM");
                }
            }
        }
        /// 
        /// 完成时间选择后触发事件
        /// 
        /// 
        /// 
        protected void RealDate_TextChanged(object sender, EventArgs e)
        {
            var txtRealDate = this.txtRealDate.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtRealDate))
            {
                this.drpIsOK.SelectedValue = "已完成";
            }
        }
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var Months = Convert.ToDateTime(this.txtMonths.Text + "-01");
            var model = new Model.JDGL_MonthPlan
            {
                Months = Months,
                UnitId = drpUnit.SelectedValue,
                NodeContent = txtNodeContent.Text,
                Remark = txtRemark.Text,
                PlanDate = Funs.GetNewDateTime(this.txtPlanDate.Text),
                CompileMan = this.CurrUser.UserId,
                CompileDate = DateTime.Now,
            };
            model.IsOK = this.drpIsOK.SelectedValue == "已完成";
            if (!string.IsNullOrWhiteSpace(this.txtRealDate.Text))
            {
                model.RealDate = Funs.GetNewDateTime(this.txtRealDate.Text);
            }
            model.DutyPerson = string.Join(",", drpDutyPerson.SelectedValueArray);
            //foreach (var item in this.drpDutyPerson.SelectedValueArray)
            //{
            //    if (item != BLL.Const._Null)
            //    {
            //        if (string.IsNullOrEmpty(model.DutyPerson))
            //        {
            //            model.DutyPerson = item;
            //        }
            //        else
            //        {
            //            model.DutyPerson += "," + item;
            //        }
            //    }
            //}
            if (!string.IsNullOrEmpty(Id))
            {
                model.MonthPlanId = Id;
                BLL.MonthPlanService.UpdateMonthPlan(model);
            }
            else
            {
                var lastMonthPlan = Funs.DB.JDGL_MonthPlan.Where(p => p.ProjectId == this.ProjectId && p.Months == Months).OrderByDescending(x => x.SortIndex).FirstOrDefault();
                model.MonthPlanId = SQLHelper.GetNewID(typeof(Model.JDGL_MonthPlan));
                model.ProjectId = this.ProjectId;
                model.SortIndex = lastMonthPlan != null ? lastMonthPlan.SortIndex + 1 : 1;
                BLL.MonthPlanService.AddMonthPlan(model);
            }
            Funs.DB.SubmitChanges();
            ShowNotify("保存成功", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}