using BLL;
using Microsoft.Office.Interop.Word;
using Model;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.JDGL.Check
{
    public partial class WeekItemEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string WeekId
        {
            get
            {
                return (string)ViewState["WeekId"];
            }
            set
            {
                ViewState["WeekId"] = 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.WeekId = Request.Params["WeekId"];
                this.ProjectId = this.CurrUser.LoginProjectId;
                //txtWeekNo.Readonly = true;
                if (!string.IsNullOrEmpty(WeekId))
                {
                    var model = Funs.DB.JDGL_WeekItem.FirstOrDefault(x => x.WeekId == WeekId);
                    if (model != null)
                    {
                        txtWeekNo.Text = model.WeekNo.ToString();
                        if (model.StartDate != null)
                            this.txtStartDate.Text = Convert.ToDateTime(model.StartDate).ToString("yyyy-MM-dd");
                        if (model.EndDate != null)
                            this.txtEndDate.Text = Convert.ToDateTime(model.EndDate).ToString("yyyy-MM-dd");
                    }
                }
                else
                {
                    var lastWeekItem = Funs.DB.JDGL_WeekItem.Where(p => p.ProjectId == this.ProjectId).OrderByDescending(x => x.WeekNo).FirstOrDefault();
                    if (lastWeekItem != null)
                    {
                        this.txtWeekNo.Text = (lastWeekItem.WeekNo + 1).ToString();
                        this.txtStartDate.Text = lastWeekItem.EndDate.AddDays(1).ToString("yyyy-MM-dd");
                        this.txtEndDate.Text = lastWeekItem.EndDate.AddDays(7).ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        this.txtWeekNo.Text = "1";
                        //this.txtStartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                        //this.txtEndDate.Text = DateTime.Now.AddDays(6).ToString("yyyy-MM-dd");
                    }
                }
            }
        }
        /// 
        /// 开始时间选择后触发事件
        /// 
        /// 
        /// 
        protected void StartDate_TextChanged(object sender, EventArgs e)
        {
            var txtStartDate = this.txtStartDate.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtStartDate))
            {
                this.txtEndDate.Text = Funs.GetNewDateTimeOrNow(txtStartDate).AddDays(6).ToString("yyyy-MM-dd");
            }
        }
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int weekNo = !string.IsNullOrWhiteSpace(txtWeekNo.Text.Trim()) ? int.Parse(txtWeekNo.Text.Trim()) : 0;
            if (string.IsNullOrEmpty(WeekId))
            {
                //判断周号是否存在
                var item = BLL.WeekItemService.GetWeekItemByProjectIdAndWeekNo(this.ProjectId, weekNo);
                if (item != null)
                {
                    Alert.ShowInParent($"已存在相同周号({weekNo})的周!", MessageBoxIcon.Warning);
                    return;
                }
            }
            var startDate = Funs.GetNewDateTimeOrNow(this.txtStartDate.Text.Trim());
            var endDate = Funs.GetNewDateTimeOrNow(this.txtEndDate.Text.Trim());
            if (startDate >= endDate)
            {
                Alert.ShowInParent("周开始日期必须大于结束日期!", MessageBoxIcon.Warning);
                return;
            }
            var model = new Model.JDGL_WeekItem
            {
                WeekNo = weekNo,
                StartDate = startDate,
                EndDate = endDate,
                CompileMan = this.CurrUser.UserId,
                CompileDate = DateTime.Now,
            };
            if (!string.IsNullOrEmpty(WeekId))
            {
                model.WeekId = WeekId;
                BLL.WeekItemService.UpdateWeekItem(model);
            }
            else
            {
                model.WeekId = SQLHelper.GetNewID(typeof(Model.JDGL_WeekItem));
                model.ProjectId = this.ProjectId;
                BLL.WeekItemService.AddWeekItem(model);
            }
            ShowNotify("保存成功", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}