using BLL;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace FineUIPro.Web.EditorManage
{
    public partial class PunchEditorEdit : PageBase
    {
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();
                }
                BLL.ConstService.InitConstIdDropDownList(this.drpDiscipline, BLL.Const.PunchDetails_Discipline, true);
                BLL.ConstService.InitConstIdDropDownList(this.drpClass, BLL.Const.Resourses_Class, true);
                //BLL.ConstService.InitConstValueDropDownList(this.drpActionType, BLL.Const.Punch_ActionType, true);

                //BLL.Sys_UserService.InitUserDropDownList(this.drpBYCPerson, true);
                //BLL.Sys_UserService.InitUserDropDownList(this.drpInitiatedBy, true);
                //BLL.Sys_UserService.InitUserDropDownList(this.drpResiedBy, true);
                BLL.Sys_UserService.InitUserDropDownList(this.drpCTEChecker, true);
                //BLL.Sys_UserService.InitUserDropDownList(this.drpOwnerChecker, true);

                string PunchId = Request.Params["PunchId"];
                if (!string.IsNullOrEmpty(PunchId))
                {
                    Model.Editor_Punch punch = BLL.PunchService.GetPunchById(PunchId);
                    if (punch != null)
                    {
                        if (!string.IsNullOrEmpty(punch.DisciplineId))
                        {
                            this.drpDiscipline.SelectedValue = punch.DisciplineId;
                        }
                        if (!string.IsNullOrEmpty(punch.ClassId))
                        {
                            this.drpClass.SelectedValue = punch.ClassId;
                        }
                        this.txtTagNo.Text = punch.TagNo;
                        this.txtRespUnitPerson.Text = punch.RespUnitPerson;
                        //if (!string.IsNullOrEmpty(punch.ResiedById))
                        //{
                        //    this.drpResiedBy.SelectedValue = punch.ResiedById;
                        //}
                        if (punch.PlanedFinishDate.HasValue)
                        {
                            this.txtPlanedFinishDate.Text = string.Format("{0:yyyy-MM-dd}", punch.PlanedFinishDate);
                        }
                        if (!string.IsNullOrEmpty(punch.CTECheckerId))
                        {
                            this.drpCTEChecker.SelectedValue = punch.CTECheckerId;
                        }
                        if (punch.CTECheckDate.HasValue)
                        {
                            this.txtCTECheckDate.Text = string.Format("{0:yyyy-MM-dd}", punch.CTECheckDate);
                        }
                        //if (!string.IsNullOrEmpty(punch.OwnerCheckerId))
                        //{
                        //    this.drpOwnerChecker.SelectedValue = punch.OwnerCheckerId;
                        //}

                        this.txtArea.Text = punch.Area;
                        txtActionType.Text = punch.ActionType;
                        txtBYCPerson.Text = punch.BYC_Person;
                        txtInitiatedBy.Text = punch.InitiatedByName;

                        //if (!string.IsNullOrEmpty(punch.InitiatedByName))
                        //{
                        //    this.drpInitiatedBy.SelectedValue = punch.InitiatedByName;
                        //}
                        //if (!string.IsNullOrEmpty(punch.BYC_Person))
                        //{
                        //    this.drpBYCPerson.SelectedValue = punch.BYC_Person;
                        //}
                        if (punch.InitiatedByDate.HasValue)
                        {
                            this.txtInitiatedByDate.Text = string.Format("{0:yyyy-MM-dd}", punch.InitiatedByDate);
                        }

                        //if (punch.OwnerCheckDate.HasValue)
                        //{
                        //    this.txtOwnerCheckDate.Text = string.Format("{0:yyyy-MM-dd}", punch.OwnerCheckDate);
                        //}
                        this.txtDescription.Text = punch.Description;
                        this.txtRemark.Text = punch.Remark;

                        if (this.CurrUser.UserId == punch.ResiedById || this.CurrUser.UserId == BLL.Const.GlyId)
                        {
                            this.btnSave.Hidden = false;
                        }
                        else
                        {
                            this.btnSave.Hidden = true;
                        }

                        if (!punch.CTECheckDate.HasValue && punch.CTECheckerId == this.CurrUser.UserId)
                        {
                            EnableText();
                            this.txtCTECheckDate.Enabled = true;
                            this.txtCTECheckDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                            //this.txtOwnerCheckDate.Enabled = false;
                            this.btnSave.Hidden = true;
                            //this.btnOwnerChecked.Hidden = true;
                            this.btnCTEChecked.Hidden = false;
                        }
                        //else if (punch.CTECheckDate.HasValue && !punch.OwnerCheckDate.HasValue && punch.OwnerCheckerId == this.CurrUser.UserId)
                        //{
                        //    EnableText();
                        //    this.txtCTECheckDate.Enabled = false;
                        //    //this.txtOwnerCheckDate.Enabled = true;
                        //    //this.txtOwnerCheckDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                        //    this.btnSave.Hidden = true;
                        //    this.btnCTEChecked.Hidden = true;
                        //    //this.btnOwnerChecked.Hidden = false;
                        //}
                        else
                        {
                            this.txtCTECheckDate.Enabled = false;
                            //this.txtOwnerCheckDate.Enabled = false;
                        }
                    }
                   
                    //尾项信息确认后不得修改
                    if (punch.CTECheckDate.HasValue)
                    {
                        EnableText();
                    }
                }
                else
                {
                    string eprojectId = Request.Params["eProjectId"];
                    if (!string.IsNullOrEmpty(eprojectId))
                    {
                        var eproject = BLL.EProjectService.GeteProjectById(eprojectId);
                        if (eproject != null)
                        {
                            this.txtTagNo.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCodeByJobNo3", "dbo.Editor_Punch", "TagNo", eproject.ProjectControl_JobNo + "-");
                            //this.drpResiedBy.SelectedValue = this.CurrUser.UserId;
                            if (!string.IsNullOrEmpty(eproject.ProjectControl_ConstManagerId))
                            {
                                drpCTEChecker.SelectedValue = eproject.ProjectControl_ConstManagerId;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        private void EnableText()
        {
            drpDiscipline.Enabled = false;
            txtRespUnitPerson.Enabled = false;
            drpClass.Enabled = false;
            //drpResiedBy.Enabled = false;
            txtTagNo.Enabled = false;
            txtPlanedFinishDate.Enabled = false;
            txtDescription.Enabled = false;
            txtRemark.Enabled = false;
            this.drpCTEChecker.Enabled = false;
            //this.drpOwnerChecker.Enabled = false;
            this.btnSave.Hidden = true;
        }

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpClass.SelectedValue == BLL.Const._Null)
            {
                ShowNotify("Please select Priority", MessageBoxIcon.Warning);
                return;
            }
           
            if (this.drpCTEChecker.SelectedValue == BLL.Const._Null)
            {
                ShowNotify("Please select CTEChecker!", MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtPlanedFinishDate.Text))
            {
                ShowNotify("Expected Finish Date Cannot be empty!", MessageBoxIcon.Warning);
                return;
            }

            string PunchId = Request.Params["PunchId"];
            Model.Editor_Punch newPunch = new Model.Editor_Punch();
            if (this.drpDiscipline.SelectedValue != BLL.Const._Null)
            {
                newPunch.DisciplineId = this.drpDiscipline.SelectedValue;
                newPunch.DisciplineName = this.drpDiscipline.SelectedItem.Text;
            }
            if (this.drpClass.SelectedValue != BLL.Const._Null)
            {
                newPunch.ClassId = this.drpClass.SelectedValue;
                newPunch.ClassName = this.drpClass.SelectedItem.Text;
            }
            newPunch.TagNo = this.txtTagNo.Text.Trim();
            newPunch.RespUnitPerson = this.txtRespUnitPerson.Text.Trim();

            newPunch.ResiedById = CurrUser.UserId;
            newPunch.ResiedByName = CurrUser.UserName;

            //if (this.drpResiedBy.SelectedValue != BLL.Const._Null)
            //{
            //    newPunch.ResiedById = this.drpResiedBy.SelectedValue;
            //    newPunch.ResiedByName = this.drpResiedBy.SelectedItem.Text;
            //}
            newPunch.PlanedFinishDate = Funs.GetNewDateTime(this.txtPlanedFinishDate.Text.Trim());
            if (this.drpCTEChecker.SelectedValue != BLL.Const._Null)
            {
                newPunch.CTECheckerId = this.drpCTEChecker.SelectedValue;
                newPunch.CTECheckerName = this.drpCTEChecker.SelectedItem.Text;
            }
            newPunch.CTECheckDate = Funs.GetNewDateTime(this.txtCTECheckDate.Text.Trim());
            //if (this.drpOwnerChecker.SelectedValue != BLL.Const._Null)
            //{
            //    newPunch.OwnerCheckerId = this.drpOwnerChecker.SelectedValue;
            //    newPunch.OwnerCheckerName = this.drpOwnerChecker.SelectedItem.Text;
            //}
            //newPunch.OwnerCheckDate = Funs.GetNewDateTime(this.txtOwnerCheckDate.Text.Trim());
            newPunch.Description = this.txtDescription.Text.Trim();
            newPunch.Remark = this.txtRemark.Text.Trim();

            newPunch.InitiatedByDate = Funs.GetNewDateTime(this.txtInitiatedByDate.Text.Trim());
            newPunch.Area = txtArea.Text.Trim();
            newPunch.ActionType = txtActionType.Text.Trim();
            newPunch.BYC_Person = txtBYCPerson.Text.Trim();
            newPunch.InitiatedByName = txtInitiatedBy.Text.Trim();
            //if (drpBYCPerson.SelectedValue != Const._Null)
            //{
            //    newPunch.BYC_Person = drpBYCPerson.SelectedValue;
            //}
            //if (drpInitiatedBy.SelectedValue != Const._Null)
            //{
            //    newPunch.InitiatedByName = drpInitiatedBy.SelectedValue;
            //}

            if (!string.IsNullOrEmpty(PunchId))
            {
                newPunch.PunchId = PunchId;
                BLL.PunchService.UpdatePunch(newPunch);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Punch Editor information!");
            }
            else
            {
                newPunch.EProjectId = Request.Params["eProjectId"];
                PunchId = SQLHelper.GetNewID(typeof(Model.Editor_Punch));
                newPunch.PunchId = PunchId;
                BLL.PunchService.AddPunch(newPunch);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Punch Editor information!");
            }

            #region 邮件发送,不用了改为Global里自动发送
            //if (this.drpCTEChecker.SelectedValue != BLL.Const._Null)
            //{
            //    string EPId = Request.Params["eProjectId"];
            //    var eproject = BLL.EProjectService.GeteProjectById(EPId);
            //    if (eproject != null)
            //    {
            //        NameValueCollection nameValue = new NameValueCollection();
            //        nameValue.Add("projectNo", eproject.ProjectControl_JobNo.ToString().Trim());
            //        nameValue.Add("projectName", eproject.ProjectControl_JobTitle.ToString().Trim());
            //        nameValue.Add("CheckName", "CTE Check");
            //        Model.Sys_User sys_User = new Model.Sys_User();
            //        sys_User = BLL.Sys_UserService.GetUsersByUserId(this.drpCTEChecker.SelectedValue);
            //        List<Model.Sys_User> list = new List<Model.Sys_User>();
            //        list.Add(sys_User);
            //        EmailSendMessage(eproject, BLL.Const.CCOCString, nameValue, BLL.Const.CustomString, list, "CTE Check");
            //    }
            //}
            #endregion
            ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.PunchMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion

        #region CTE、业主确认
        /// <summary>
        /// CTE确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCTEChecked_Click(object sender, EventArgs e)
        {
            string PunchId = Request.Params["PunchId"];
            if (!string.IsNullOrEmpty(PunchId))
            {
                var punch = BLL.PunchService.GetPunchById(PunchId);
                if (punch != null)
                {
                    punch.CTECheckDate = Funs.GetNewDateTime(this.txtCTECheckDate.Text.Trim());
                    BLL.PunchService.UpdatePunch(punch);
                }
            }
            #region 邮件发送,不用了改为Global里自动发送           
            //if (this.drpOwnerChecker.SelectedValue != BLL.Const._Null)
            //{
            //    string EPId = Request.Params["eProjectId"];
            //    var eproject = BLL.EProjectService.GeteProjectById(EPId);
            //    if (eproject != null)
            //    {
            //        NameValueCollection nameValue = new NameValueCollection();
            //        nameValue.Add("projectNo", eproject.ProjectControl_JobNo.ToString().Trim());
            //        nameValue.Add("projectName", eproject.ProjectControl_JobTitle.ToString().Trim());
            //        nameValue.Add("CheckName", "Owner Check");
            //        Model.Sys_User sys_User = new Model.Sys_User();
            //        sys_User = BLL.Sys_UserService.GetUsersByUserId(this.drpOwnerChecker.SelectedValue);
            //        List<Model.Sys_User> list = new List<Model.Sys_User>();
            //        list.Add(sys_User);
            //        EmailSendMessage(eproject, BLL.Const.CCOCString, nameValue, BLL.Const.CustomString, list, "Owner Check");
            //    }
            //}
            #endregion
            ShowNotify("CTE Checked Successfully!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 业主确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void btnOwnerChecked_Click(object sender, EventArgs e)
        //{
        //    string PunchId = Request.Params["PunchId"];
        //    if (!string.IsNullOrEmpty(PunchId))
        //    {
        //        var punch = BLL.PunchService.GetPunchById(PunchId);
        //        if (punch != null)
        //        {
        //            punch.OwnerCheckDate = Funs.GetNewDateTime(this.txtOwnerCheckDate.Text.Trim());
        //            BLL.PunchService.UpdatePunch(punch);
        //        }
        //    }
        //    ShowNotify("Owner Checked Successfully!", MessageBoxIcon.Success);
        //    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        //}
        #endregion
    }
}