using BLL;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.EditorManage
{
    public partial class KeyQuantityEditorEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string KeyQuantityId
        {
            get
            {
                return (string)ViewState["KeyQuantityId"];
            }
            set
            {
                ViewState["KeyQuantityId"] = value;
            }
        }
        /// 
        /// 项目Id
        /// 
        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.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                //BindGrid();
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();
                }
                BLL.QuantityDesctiptionService.InitIdentifierDropDownList(this.drpIdentifier, true);
                Funs.FineUIPleaseSelect(this.drpDescipline);
                Funs.FineUIPleaseSelect(this.drpQuantityDesctiption);
                Funs.FineUIPleaseSelect(this.drpPlanMHRsUnit);
                this.ProjectId = Request.Params["eProjectId"];
                this.KeyQuantityId = Request.Params["keyQuantityId"];
                if (!string.IsNullOrEmpty(this.KeyQuantityId))
                {
                    Model.Editor_KeyQuantity keyQuantity = BLL.KeyQuantityService.GetKeyQuantityById(this.KeyQuantityId);
                    if (keyQuantity != null)
                    {
                        this.ProjectId = keyQuantity.EProjectId;
                        //this.drpKeyId.Value = keyQuantity.KeyId;
                        //var key = BLL.QuantityDesctiptionService.GetQuantityDesctiptionById(this.drpKeyId.Value);
                        //if (key != null)
                        //{
                        //    if (!string.IsNullOrEmpty(key.DisciplinesWBSId))
                        //    {
                        //        var wbs = BLL.DisciplinesWBSService.GetDisciplinesWBSById(key.DisciplinesWBSId);
                        //        if (wbs != null)
                        //        {
                        //            this.txtIdentifier.Text = wbs.DisciplinesWBSCode;
                        //            this.txtDescipline.Text = wbs.DisciplinesWBSName;
                        //        }
                        //        this.txtPlanMHRsSummary.Text = BLL.KeyQuantityService.GetSumPlanMHRsByKeyId(this.drpKeyId.Value).ToString();
                        //        double? actualHoursSum = (from x in Funs.DB.ManHours_Actual
                        //                                  where x.EProjectId == this.ProjectId
                        //                                  && x.Discipline == wbs.DisciplinesWBSName
                        //                                  select x.Hours).Sum();
                        //        this.txtActualMHRs.Text = actualHoursSum.ToString();
                        //    }
                        //    txtQuantityDesctiption.Text = key.QuantityDesctiption;
                        //    txtPlanMHRsUnit.Text = key.PlanMHRsUnit.HasValue ? key.PlanMHRsUnit.ToString() : "";
                        //}
                        if (!string.IsNullOrEmpty(keyQuantity.Identifier))
                        {
                            this.drpIdentifier.Items.Clear();
                            BLL.QuantityDesctiptionService.InitIdentifierDropDownList(this.drpIdentifier, true);
                            this.drpIdentifier.SelectedValue = keyQuantity.Identifier;
                            if (!string.IsNullOrEmpty(keyQuantity.Descipline))
                            {
                                this.drpDescipline.Items.Clear();
                                BLL.QuantityDesctiptionService.InitDesciplineDropDownList(this.drpDescipline, true, keyQuantity.Identifier);
                                this.drpDescipline.SelectedValue = keyQuantity.Descipline;
                                if (!string.IsNullOrEmpty(keyQuantity.QuantityDesctiption))
                                {
                                    this.drpQuantityDesctiption.Items.Clear();
                                    BLL.QuantityDesctiptionService.InitQuantityDesctiptionDropDownList(this.drpQuantityDesctiption, true, keyQuantity.Identifier, keyQuantity.Descipline);
                                    this.drpQuantityDesctiption.SelectedValue = keyQuantity.QuantityDesctiption;
                                    if (keyQuantity.PlanMHRsUnit.HasValue)
                                    {
                                        this.drpPlanMHRsUnit.Items.Clear();
                                        BLL.QuantityDesctiptionService.InitPlanMHRsUnitDropDownList(this.drpPlanMHRsUnit, true, keyQuantity.Identifier, keyQuantity.Descipline, keyQuantity.QuantityDesctiption);
                                        this.drpPlanMHRsUnit.SelectedValue = keyQuantity.PlanMHRsUnit.ToString();
                                    }
                                }
                            }
                        }
                        this.txtPlanMHRsSummary.Text = BLL.KeyQuantityService.GetSumPlanMHRsByKeyId(keyQuantity.EProjectId,keyQuantity.Identifier, keyQuantity.Descipline).ToString();
                        this.txtInputQuantity.Text = keyQuantity.InputQuantity.HasValue ? keyQuantity.InputQuantity.ToString() : "";
                        this.txtPlanMHRs.Text = keyQuantity.PlanMHRs.HasValue ? keyQuantity.PlanMHRs.ToString() : "";
                        double? actualHoursSum = (from x in Funs.DB.ManHours_Actual
                                                  where x.EProjectId == this.ProjectId
                                                  && x.Discipline == keyQuantity.Descipline
                                                  select x.Hours).Sum();
                        this.txtActualMHRs.Text = actualHoursSum.ToString();
                    }
                }
            }
        }
        #endregion
        #region 绑定数据
        /// 
        /// 数据绑定
        /// 
        //private void BindGrid()
        //{
        //    string strSql = @"SELECT qd.KeyId,
        //                            qd.DepartId,
        //                            qd.DisciplinesWBSId,
        //                            qd.QuantityDesctiption,
        //                            qd.PlanMHRsUnit,
        //                            d.DepartName,
        //                            wbs.DisciplinesWBSCode,
        //                            wbs.DisciplinesWBSName
        //                            FROM Base_QuantityDesctiption qd
        //                            left join Base_Depart as d on qd.DepartId = d.DepartId
        //                    left join Base_DisciplinesWBS as wbs on wbs.DisciplinesWBSId = qd.DisciplinesWBSId WHERE 1=1";
        //    List listStr = new List();
        //    if (!string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
        //    {
        //        strSql += " AND d.DepartName like @DepartName ";
        //        listStr.Add(new SqlParameter("@DepartName", "%" + this.txtSearch.Text.Trim() + "%"));
        //    }
        //    SqlParameter[] parameter = listStr.ToArray();
        //    DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
        //    // 2.获取当前分页数据
        //    //var table = this.GetPagedDataTable(Grid1, tb1);
        //    Grid1.RecordCount = tb.Rows.Count;
        //    var table = this.GetPagedDataTable(Grid1, tb);
        //    Grid1.DataSource = table;
        //    Grid1.DataBind();
        //}
        #endregion
        #region 分页、排序
        /// 
        /// 分页
        /// 
        /// 
        /// 
        //protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        //{
        //    Grid1.PageIndex = e.NewPageIndex;
        //    BindGrid();
        //}
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        //protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        //    BindGrid();
        //}
        /// 
        /// 排序
        /// 
        /// 
        /// 
        //protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        //{
        //    Grid1.SortDirection = e.SortDirection;
        //    Grid1.SortField = e.SortField;
        //    BindGrid();
        //}
        /// 
        /// 筛选
        /// 
        /// 
        /// 
        //protected void txtSearch_TextChanged(object sender, EventArgs e)
        //{
        //    BindGrid();
        //}
        #endregion
        #region 保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpIdentifier.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpIdentifier.SelectedValue))
            {
                ShowAlert("Please select Identifier!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpDescipline.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpDescipline.SelectedValue))
            {
                ShowAlert("Please select Descipline!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpQuantityDesctiption.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpQuantityDesctiption.SelectedValue))
            {
                ShowAlert("Please select Quantity Desctiption!", MessageBoxIcon.Warning);
                return;
            }
            Model.Editor_KeyQuantity newKeyQuantity = new Model.Editor_KeyQuantity();
            //newKeyQuantity.KeyId = this.drpKeyId.Value;
            newKeyQuantity.InputQuantity = Funs.GetNewDecimal(this.txtInputQuantity.Text);
            newKeyQuantity.PlanMHRs = Funs.GetNewDecimal(this.txtPlanMHRs.Text);
            newKeyQuantity.Identifier = this.drpIdentifier.SelectedText;
            newKeyQuantity.Descipline = this.drpDescipline.SelectedText;
            newKeyQuantity.QuantityDesctiption = this.drpQuantityDesctiption.SelectedText;
            newKeyQuantity.PlanMHRsUnit = Funs.GetNewDecimal(this.drpPlanMHRsUnit.SelectedText);
            if (!string.IsNullOrEmpty(this.KeyQuantityId))
            {
                newKeyQuantity.KeyQuantityId = this.KeyQuantityId;
                BLL.KeyQuantityService.UpdateKeyQuantity(newKeyQuantity);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Key Quantity Editor information!");
                ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            }
            else
            {
                newKeyQuantity.EProjectId = ProjectId;
                this.KeyQuantityId = SQLHelper.GetNewID(typeof(Model.Editor_KeyQuantity));
                newKeyQuantity.KeyQuantityId = this.KeyQuantityId;
                BLL.KeyQuantityService.AddKeyQuantity(newKeyQuantity);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Key Quantity Editor information!");
                ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 部门下拉选择事件
        /// 
        /// 
        /// 
        //protected void drpKeyId_TextChanged(object sender, EventArgs e)
        //{
        //    this.txtIdentifier.Text = string.Empty;
        //    this.txtDescipline.Text = string.Empty;
        //    this.txtQuantityDesctiption.Text = string.Empty;
        //    this.txtPlanMHRsUnit.Text = string.Empty;
        //    this.txtPlanMHRsSummary.Text = string.Empty;
        //    if (!string.IsNullOrEmpty(this.drpKeyId.Value))
        //    {
        //        var key = BLL.QuantityDesctiptionService.GetQuantityDesctiptionById(this.drpKeyId.Value);
        //        if (key != null)
        //        {
        //            if (!string.IsNullOrEmpty(key.DisciplinesWBSId))
        //            {
        //                var wbs = BLL.DisciplinesWBSService.GetDisciplinesWBSById(key.DisciplinesWBSId);
        //                if (wbs != null)
        //                {
        //                    this.txtIdentifier.Text = wbs.DisciplinesWBSCode;
        //                    this.txtDescipline.Text = wbs.DisciplinesWBSName;
        //                }
        //                decimal? s = BLL.KeyQuantityService.GetSumPlanMHRsByKeyId(this.drpKeyId.Value);
        //                this.txtPlanMHRsSummary.Text = (s + Funs.GetNewDecimal(this.txtPlanMHRs.Text)).ToString();
        //                double? actualHoursSum = (from x in Funs.DB.ManHours_Actual
        //                                          where x.EProjectId == this.ProjectId
        //                                          && x.Discipline == wbs.DisciplinesWBSName
        //                                          select x.Hours).Sum();
        //                this.txtActualMHRs.Text = actualHoursSum.ToString();
        //            }
        //            txtQuantityDesctiption.Text = key.QuantityDesctiption;
        //            txtPlanMHRsUnit.Text = key.PlanMHRsUnit.HasValue ? key.PlanMHRsUnit.ToString() : "";
        //        }
        //    }
        //}
        /// 
        /// Input Quantity输入事件
        /// 
        /// 
        /// 
        protected void txtInputQuantity_TextChanged(object sender, EventArgs e)
        {
            this.txtPlanMHRs.Text = string.Empty;
            this.txtPlanMHRsSummary.Text = string.Empty;
            if (!string.IsNullOrEmpty(this.txtInputQuantity.Text.Trim()))
            {
                this.txtPlanMHRs.Text = (Funs.GetNewDecimal(this.txtInputQuantity.Text) * Funs.GetNewDecimal(this.drpPlanMHRsUnit.SelectedText)).ToString();
                decimal? s = BLL.KeyQuantityService.GetSumPlanMHRsByKeyId(this.ProjectId ,this.drpIdentifier.SelectedValue, this.drpDescipline.SelectedValue);
                if (s > 0)
                {
                    this.txtPlanMHRsSummary.Text = (s + Funs.GetNewDecimal(this.txtPlanMHRs.Text)).ToString();
                }
                else
                {
                    this.txtPlanMHRsSummary.Text = this.txtPlanMHRs.Text;
                }
            }
        }
        /// 
        /// Identifier 下拉选择
        /// 
        /// 
        /// 
        protected void drpIdentifier_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpDescipline.Items.Clear();
            if (this.drpIdentifier.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpIdentifier.SelectedValue))
            {
                BLL.QuantityDesctiptionService.InitDesciplineDropDownList(this.drpDescipline, true, this.drpIdentifier.SelectedValue);
            }
            else
            {
                Funs.FineUIPleaseSelect(this.drpDescipline);
            }
            this.drpDescipline.SelectedIndex = 0;
        }
        /// 
        /// Descipline下拉选择
        /// 
        /// 
        /// 
        protected void drpDescipline_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtActualMHRs.Text = string.Empty;
            drpQuantityDesctiption.Items.Clear();
            if (this.drpDescipline.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpDescipline.SelectedValue))
            {
                BLL.QuantityDesctiptionService.InitQuantityDesctiptionDropDownList(this.drpQuantityDesctiption, true, this.drpIdentifier.SelectedValue, this.drpDescipline.SelectedValue);
                double? actualHoursSum = (from x in Funs.DB.ManHours_Actual
                                          where x.EProjectId == this.ProjectId
                                          && x.Discipline == this.drpDescipline.SelectedValue
                                          select x.Hours).Sum();
                this.txtActualMHRs.Text = actualHoursSum.ToString();
            }
            else
            {
                Funs.FineUIPleaseSelect(this.drpQuantityDesctiption);
            }
            this.drpQuantityDesctiption.SelectedIndex = 0;
        }
        /// 
        /// Quantity Desctiption下拉选择
        /// 
        /// 
        /// 
        protected void drpQuantityDesctiption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpPlanMHRsUnit.Items.Clear();
            if (this.drpQuantityDesctiption.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpQuantityDesctiption.SelectedValue))
            {
                BLL.QuantityDesctiptionService.InitPlanMHRsUnitDropDownList(this.drpPlanMHRsUnit, true, this.drpIdentifier.SelectedValue, this.drpDescipline.SelectedValue, this.drpQuantityDesctiption.SelectedValue);
            }
            else
            {
                Funs.FineUIPleaseSelect(this.drpPlanMHRsUnit);
            }
            this.drpPlanMHRsUnit.SelectedIndex = 0;
        }
        #endregion
        #region 权限设置
        /// 
        /// 菜单按钮权限
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.KeyQuantityMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion        
    }
}