using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.ManHours
{
    public partial class AddManHours : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();//权限设置
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                BLL.Sys_UserService.InitCTEUserDropDownList(this.drpEngineer, true);
                BLL.DisciplinesWBSService.InitDisciplinesDropDownList(drpDiscipline, true);
                BLL.EProjectService.InitEprojectJobNoDropDownList(drpJobNo, true);

                string eProjectId = Request.Params["eprojectId"];
                if (!string.IsNullOrEmpty(eProjectId))
                {
                    this.txtJobNo.Hidden = false;
                    this.drpJobNo.Hidden = true;
                    var p = BLL.EProjectService.GeteProjectById(eProjectId);
                    if (p != null)
                    {
                        this.txtJobNo.Text = p.ProjectControl_JobNo;
                        if (!string.IsNullOrEmpty(p.ProjectControl_NetworkNo))
                        {
                            this.txtAccount.Text = p.ProjectControl_NetworkNo;
                        }
                        else if (!string.IsNullOrEmpty(p.ProjectControl_Account))
                        {
                            this.txtAccount.Text = p.ProjectControl_Account;
                        }
                        
                    }
                }
                else
                {
                    this.drpJobNo.Hidden = false;
                    this.txtJobNo.Hidden = true;
                }
            }
        }

        protected void drpJobNo_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            var p = BLL.EProjectService.GeteProjectById(drpJobNo.SelectedValue);
            if (p != null)
            {
                if (!string.IsNullOrEmpty(p.ProjectControl_NetworkNo))
                {
                    this.txtAccount.Text = p.ProjectControl_NetworkNo;
                }
                else if (!string.IsNullOrEmpty(p.ProjectControl_Account))
                {
                    this.txtAccount.Text = p.ProjectControl_Account;
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string eProjectId = Request.Params["eprojectId"];
            if (string.IsNullOrEmpty(eProjectId))
            {
                if (drpJobNo.SelectedValue != BLL.Const._Null)
                {
                    eProjectId = drpJobNo.SelectedValue;
                }
            }

            if (string.IsNullOrEmpty(eProjectId))
            {
                Alert.ShowInTop("Please select Job No!", MessageBoxIcon.Warning);
                return;
            }
            if (drpDiscipline.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("Please select Job Discipline!", MessageBoxIcon.Warning);
                return;
            }
            if (drpEngineer.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("Please select Job Engineer!", MessageBoxIcon.Warning);
                return;
            }


            Model.ManHours_Plan pm = new Model.ManHours_Plan();

            pm.ManHoursPlanId= BLL.SQLHelper.GetNewID(typeof(Model.ManHours_Plan));
            if (this.drpJobNo.SelectedValue != BLL.Const._Null)
            {
                pm.EProjectId = this.drpJobNo.SelectedValue;
            }
            else
            {
                pm.EProjectId = eProjectId;
            }

            string discId = drpDiscipline.SelectedValue;
            
            pm.DisciplineId = discId.Split('_')[0];

            string disc = drpDiscipline.SelectedText;
            string[] d = disc.Split('_');
            pm.Discipline = d[1];
            if (d.Count() == 2)
            {
                pm.Roles = d[0];
            }
            if (d.Count() == 3)
            {
                pm.Roles = d[0] + "_" + d[2];
            }
            pm.Account = txtAccount.Text;
            pm.EngineerId = drpEngineer.SelectedValue;
            pm.EngineerName = drpEngineer.SelectedText;

            if (BLL.Sys_UserService.IsCTEAndCalculated(drpEngineer.SelectedValue))
            {
                if (!string.IsNullOrEmpty(txtManHours.Text))
                {
                    pm.ManHours = Convert.ToInt32(txtManHours.Text);
                }
            }
            else
            {
                pm.ManHours = 0;
            }

            // pm.ManHours = Convert.ToInt32(txtManHours.Text);
            if (cbNotApplicable1.Checked)
            {
                pm.AccountDisabled = 1;
                pm.IsClose = 1;
            }
            else
            {
                pm.AccountDisabled =0;
                pm.IsClose = 0;
            }
            BLL.PlanService.AddPlan(pm);

            // 把设计专业回写到PM_Editor
            var wbs = BLL.DisciplinesWBSService.GetDisciplinesWBSById(discId.Split('_')[0]);
            var pmWbs = from x in BLL.Funs.DB.Editor_PM where x.EProjectId == eProjectId && x.DisciplinesWBSName == wbs.DisciplinesWBSName select x;
            if (wbs.Type == "1" && pmWbs.Count() == 0)
            {
                Model.Editor_PM pmEdit = new Model.Editor_PM();
                pmEdit.PMId = Guid.NewGuid().ToString();
                pmEdit.EProjectId = eProjectId;
                pmEdit.DisciplinesWBSCode = wbs.DisciplinesWBSCode;
                pmEdit.DisciplinesWBSName = wbs.DisciplinesWBSName;
                pmEdit.WBS =wbs.WBS;
                BLL.Funs.DB.Editor_PM.InsertOnSubmit(pmEdit);
                BLL.Funs.DB.SubmitChanges();
            }

            BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add ManHours_Plan");
            ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

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